node-casbin/sequelize-adapter

savePolicy() taking 6 seconds to perform

Closed this issue · 4 comments

I'm using MySQL and having some troubles with the time to add a new user policy. This is taking around 6 seconds. Is this the expected time?

My express.js route

app.put('/role/user/add', async (req, res) => {
  const status = await addUserRole(req.body.user, req.body.role_name);
  send_response(res, status);
})

Function to insert a new user role.

    async addUserRole(user_name, role_name){
        const status = await this.enforcer.addNamedGroupingPolicy('g', user_name, role_name);
        await this.enforcer.savePolicy();
        return status;
    }

@lucasrolim-intuitive , how many policy rules do you have? Which adapter are you using?

I have around 20 rules type (p) and 250 of type (g). I´m using the sequelizer adapter.

    async createEnforcer(){
        const database = await SequelizeAdapter.newAdapter(`mysql://${db_credentials.user}:${db_credentials.password}@${db_credentials.host}:${db_credentials.port}/${db_credentials.name}`, true);
        this.enforcer = await Enforcer.newEnforcer('casbin/model.conf', database);
        await this.enforcer.loadPolicy();
    }

@lucasrolim-intuitive Sequelizer Adapter supports the AutoSave feature, you do not need to call await this.enforcer.savePolicy() manually, addNamedGroupingPolicy() will automatically store the new policy rule into DB.

The savePolicy() in this adapter doesn't use bulk operation, it saves all policy rules (20 + 250) one by one, that's why it is slow. You can also choose to improve this adapter with bulk-style savePolicy operation

Thank you!