https://github.com/node-casbin/mongo-changestream-watcher
Casbin Watcher based on MongoDB Change Streams
https://github.com/node-casbin/mongo-changestream-watcher
casbin casbin-watcher mongodb nodejs
Last synced: 8 months ago
JSON representation
Casbin Watcher based on MongoDB Change Streams
- Host: GitHub
- URL: https://github.com/node-casbin/mongo-changestream-watcher
- Owner: node-casbin
- License: apache-2.0
- Created: 2022-08-22T14:03:13.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-08-07T01:48:23.000Z (over 2 years ago)
- Last Synced: 2024-10-25T03:04:28.913Z (over 1 year ago)
- Topics: casbin, casbin-watcher, mongodb, nodejs
- Language: TypeScript
- Homepage: https://casbin.org/docs/watchers
- Size: 98.6 KB
- Stars: 1
- Watchers: 4
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Node-Casbin Watcher based on MongoDB Change Streams
[![NPM version][npm-image]][npm-url]
[![NPM download][download-image]][download-url]
[](https://codebeat.co/projects/github-com-node-casbin-mongo-changestream-watcher-master)
[](https://coveralls.io/github/node-casbin/mongo-changestream-watcher?branch=master)
[](https://discord.gg/S5UjpzGZjN)
[](https://github.com/node-casbin/mongo-changestream-watcher/actions/workflows/main.yml)
[npm-image]: https://img.shields.io/npm/v/@casbin/mongo-changestream-watcher.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@casbin/mongo-changestream-watcher
[download-image]: https://img.shields.io/npm/dm/@casbin/mongo-changestream-watcher.svg?style=flat-square
[download-url]: https://npmjs.org/package/@casbin/mongo-changestream-watcher
For more information about MongoDB Change Streams, look [here](https://www.mongodb.com/docs/manual/changeStreams/).
More information about Casbin Watchers, look [here](https://casbin.org/docs/watchers).
## Installation
```shell script
# NPM
npm install --save @casbin/mongo-changestream-watcher
# Yarn
yarn add @casbin/mongo-changestream-watcher
```
## Simple Example using Mongoose Adapter
```typescript
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'));
```
## Notes
This watcher does not operate with `update`-calls typically found in other watchers. Mongo Change Stream directly reacts to changes in the database collection, and therefore all other watchers listening to the same stream will be automatically notified when changes do occur. However, this means that watcher also gets notified by its own changes.