node-casbin / prisma-adapter

Prisma adapter for Casbin
Apache License 2.0
31 stars 18 forks source link

Order of policies stored in DB after a.savePolicy is not same as source #56

Closed azmi-plavaga closed 1 week ago

azmi-plavaga commented 10 months ago

Order of policies in DB after savePolicy is not same as in source file: examples/rbac_policy.csv

Sample test: https://github.com/node-casbin/prisma-adapter/blob/master/test/adapter.test.ts Code snippet: line 40-50

 try {
      // Because the DB is empty at first,
      // so we need to load the policy from the file adapter (.CSV) first.
      let e = await newEnforcer(
        'examples/rbac_model.conf',
        'examples/rbac_policy.csv'
      );

      // This is a trick to save the current policy to the DB.
      // We can't call e.savePolicy() because the adapter in the enforcer is still the file adapter.
      // The current policy means the policy in the Node-Casbin enforcer (aka in memory).
      await a.savePolicy(e.getModel());
casbin-bot commented 10 months ago

@nodece @Shivansh-yadav13

hsluoyz commented 2 weeks ago

@azmi-plavaga this is because the code below uses promises to insert each rule, so it doesn't keep the order. But if we don't use async promises, it will be very slow to insert one by one in the sync way. So I suggest we change it to batch insert like how we did in other adapters. So just one batch insert to insert all rules, fast and keep order.

https://github.com/node-casbin/prisma-adapter/blob/27c2f7d2bd7fd719e1b0a65f3a8dcf02da96ec26/src/adapter.ts#L75-L100