@pantherx.dev/casbin-rule-migration
TypeScript icon, indicating that this package has built-in type declarations

0.8.0 • Public • Published

Casbin Route Rules Migration

npm version

This library contains shared classes, interfaces and other non-platform specific functions. Even though the library is public, we do not provide support, or assurance for this to work now, or continue to work in the future.

So: This likely won't be useful to you unless you are working with us, or with / on one of our products.


This will do a couple of things:

  1. Read latest rules from code
  2. Load last migration and related entities from the database
  3. Run a comparison
  4. Apply changes and create new migration

Here's an example

$ node dist/src/casbin-migration
=> Get latest migration
Found migration from Fri Aug 20 2021 15:24:09 GMT+0000 (Coordinated Universal Time) with ID fa3f5530-3651-4429-9f97-c501e4ce0dc1
Comparison finished. Results:
# Remain: 81
# Update: 0
# Create: 2
# Delete: 0
Applied changes to the database. Results:
# Create: 2
# Delete: 0
FINAL LIST OF MANAGED ID's [
  1160, 1161, 1162, 1163, 1164, 1165, 1166, 1167, 1168,
  1169, 1170, 1171, 1172, 1173, 1174, 1176, 1175, 1177,
  1178, 1179, 1180, 1181, 1182, 1183, 1184, 1185, 1186,
  1187, 1188, 1189, 1190, 1191, 1192, 1193, 1194, 1195,
  1196, 1197, 1198, 1199, 1200, 1201, 1202, 1203, 1204,
  1205, 1206, 1207, 1208, 1209, 1210, 1211, 1212, 1213,
  1214, 1215, 1216, 1217, 1218, 1219, 1220, 1221, 1222,
  1223, 1224, 1225, 1226, 1227, 1228, 1229, 1230, 1231,
  1232, 1233, 1234, 1235, 1236, 1237, 1238, 1239, 1240,
  1241, 1242, 1243, 1244, 1245, 1246, 1247, 1248, 1249,
  1250, 1251, 1252, 1253, 1254, 1255
]
=> Saving new migration with 96 entries ...

With no migrations pending:

$ node dist/src/casbin-migration
=> Get latest migration
Found migration from Mon Sep 06 2021 04:01:34 GMT+0700 (Indochina Time) with ID 93ef9980-b4c2-4157-8b3c-dd5900967822
Comparison finished. Results:
# Remain: 95
# Update: 0
# Create: 0
# Delete: 0
No changes found. Nothing to do.

Setup

Install with

npm i --save @pantherx.dev/casbin-rule-migration

Create a casbin-migration.ts in src:

import { migrate } from 'packages/casbin-rule-migration/src'
import { allRouteRules, roleResourceAccessRules } from './permissions/identity-authorization-policies/rules'

migrate(allRouteRules, roleResourceAccessRules, false)

For this to work properly:

  1. Start with an empty casbin_rule and casbin_rule_migrations table.
  2. Start the server
  3. Casbin rules will be created and initial migration saved.

From now on you can run this script, whenever there are any changes.

Notes

  • We do not "update" database entries but delete the old one, and create a new one

Readme

Keywords

none

Package Sidebar

Install

npm i @pantherx.dev/casbin-rule-migration

Weekly Downloads

0

Version

0.8.0

License

MIT

Unpacked Size

15 kB

Total Files

7

Last publish

Collaborators

  • franzos