node-casbin / mongoose-adapter

Mongoose adapter for Casbin
https://github.com/casbin/node-casbin
Apache License 2.0
33 stars 32 forks source link

Need help in creating multiple adapter #67

Closed shubhanshusingh closed 1 year ago

shubhanshusingh commented 1 year ago

I have multiple databases in single mongo instance. I wish to create multiple adapter and enforcer for each database in order to direct Casbin rules CRUD operations to databases. How can I do it?

   let config = Object.assign({}, vaultConfig.mongo);
  let host = "mongodb://" + config.host;
  delete config.host;
  config.dbName = "baymax-prod";
  const adapter = await MongooseAdapter.newAdapter(host, {
    ...config
  });
  const enforcer = await casbin.newEnforcer(
    "src/domain_role_model.conf",
    adapter
  );
  let config1 = Object.assign({}, config);
  config1.dbName = "baymax-stage";
  const adapter1 = await MongooseAdapter.newAdapter(host, {
    ...config1
  });
  const enforcer1 = await casbin.newEnforcer(
    "src/domain_role_model.conf",
    adapter1
  );

  let b2 = await enforcer.addPolicy("admin", "tenant1", "experiment", "W");

  let b22 = await enforcer1.addPolicy("admin", "tenant1", "experiment", "W");

Above code is updating policies to same database which is initialised first i.e. baymax-prod. Adapter1 and Enforcer1 are saving policies to baymax-prod database instead of baymax-stage database.

can someone help?

casbin-bot commented 1 year ago

@nodece @Zxilly @Shivansh-yadav13

hsluoyz commented 1 year ago

@shubhanshusingh don't understand your question. What are you doing, what to expect and what you see?

shubhanshusingh commented 1 year ago

I have one mongo db instance where I have multiple databases.[]

Screenshot 2023-04-18 at 9 12 03 PM

(url)

Now I wish to connect to each db via mongoose casbin adapter? how can I do it? For each db the enforcer would be different.

@hsluoyz

nodece commented 1 year ago

Let me fix this issue.

shubhanshusingh commented 1 year ago

@nodece appreciate it! it's a much need requirement for my application.

nodece commented 1 year ago

@shubhanshusingh See #68

shubhanshusingh commented 1 year ago

I tested that build @nodece working perfectly, waiting for PR to be merged. Life saver!