基于 MongoDB 变化流的节点- Casbin 监视器
关于MongoDB变化流的更多信息,请查看 这里。 。 。 .
更多关于卡斯宾观察者的信息,看 这里。 。 。 .
安装
# NPM
npm install --save @casbin/mongo-changestream-watcher
# Yarn
yarn add @casbin/mongo-changestream-watcher
使用 Mongoose 适配器的简单示例
import { MongoChangeStreamWatcher } from '@casbin/mongo-changestream-watcher';
import { newEnforcer } from 'casbin';
// Initialize the watcher by connecting to a replica set.
const watcher = await MongoChangeStreamWatcher.newWatcher('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0', {collectionName: 'casbin_rule'});
const adapter = await MongooseAdapter.newAdapter('mongodb://localhost:27001,localhost:27002/casbin?replicaSet=rs0');
const enforcer = await newEnforcer('test/fixtures/basic_model.conf', adapter);
// Initialize the enforcer.
const enforcer = await newEnforcer('examples/authz_model.conf', 'examples/authz_policy.csv');
enforcer.setWatcher(watcher);
// By default, the watcher's callback is automatically set to the
// enforcer's loadPolicy() in the setWatcher() call.
// We can change it by explicitly setting a callback.
watcher.setUpdateCallback(() => console.log('Casbin need update'));
页:1
这个监视器没有操作 update-其他监视者一般都有电话 Mongo Change Stream对数据库收藏中的变化作出直接反应,因此,听到同一流的所有其他观察者在确实发生变化时都会被自动通知. 然而,这意味着观察者也会通过自己的变化得到通知.