-
Notifications
You must be signed in to change notification settings - Fork 1
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "ISC", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add Apache license to this project, and update |
||
"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" | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please add |
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; | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
} | ||
} |
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(); | ||
} | ||
|
||
} |
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/**/*" | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be
index.ts
.