Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Add Mongodb-watcher #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
174 changes: 174 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "mongodb-watcher",
"version": "1.0.0",
"description": "moggodb-watcher for node casbin",
"main": "index.js",
Copy link

@nodece nodece Jun 6, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be index.ts.

"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add Apache license to this project, and update ISC to Apache.

"dependencies": {
"@types/jest": "^26.0.23",
"@types/mongodb": "^3.6.17",
"casbin": "^5.7.1",
"mongodb": "^3.6.9"
},
"devDependencies": {
"jest": "^27.0.4",
"ts-jest": "^27.0.2",
"typescript": "^4.3.2"
}
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add files field to this.

29 changes: 29 additions & 0 deletions src/mongodb.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import {MongoClient} from "mongodb";

export class MongodbConnection{
private readonly uri: string;
private readonly dbName: string;
private readonly collectionName: string;
public client: MongoClient;


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be only one blank line.

constructor(uri :string, dbName: string, collectionName: string) {
this.uri = uri;
this.dbName = dbName;
this.collectionName = collectionName;
}


Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be only one blank line.

open(){
this.client = new MongoClient(this.uri);
}

async getCollection(){
await this.client.connect();
return this.client.db(this.dbName).collection(this.collectionName);
}

public close(){
this.client.close();
}
}
53 changes: 53 additions & 0 deletions src/watcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import {MongodbConnection} from './mongodb'
import {Watcher} from "casbin";

export default class MongodbWatcher implements Watcher{

private readonly keyName : string;
private mongodbConnection : MongodbConnection;
private callback : () => void;

public static async newWatcher(uri : string,dbName: string,collectionName: string,keyName?: string):Promise<MongodbWatcher>{
return new MongodbWatcher(uri,dbName,collectionName,keyName);
}

private constructor(uri: string, dbName: string,collectionName: string, keyName?: string) {
this.keyName = keyName || "/casbin";
this.mongodbConnection = new MongodbConnection(uri,dbName,collectionName);
this.mongodbConnection.open();
this.mongodbConnection.getCollection().then(collection=>{
let that = this;
let latest = collection.find({}).limit(1);
latest.next(function (err,doc){
if (err) throw err;
let query = {_id: {$gt: doc._id}};
let options = {tailable:true,awaitData: true, numberOfRetries:-1};
let cursor = collection.find(query,options);
(function next(){
cursor.next(function (err,message){
if (err) throw err;
if (message.message === that.keyName){
if (that.callback)
that.callback();
}
next();
});
})();
})
})
}
public setUpdateCallback(callback: () => void): void {
this.callback = callback;
}

public async update(): Promise<boolean> {
let collection = await this.mongodbConnection.getCollection();
await collection.insertOne({message:this.keyName,time: Date.now()})
return true
}

public async close(): Promise<void>{
this.mongodbConnection.close();
}

}
16 changes: 16 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"moduleResolution": "node",
"strict": true,
"strictPropertyInitialization": false,
"declaration": true,
"declarationDir": "lib",
"outDir": "lib",
"esModuleInterop": true
},
"include": [
"src/**/*"
]
}
Loading