diff options
author | madelinegr <laura_wilson@brown.edu> | 2019-02-18 19:38:08 -0500 |
---|---|---|
committer | madelinegr <laura_wilson@brown.edu> | 2019-02-18 19:38:08 -0500 |
commit | 6fd2cec91efd6672a70e15a786954f92c1d23416 (patch) | |
tree | 0cbf4c1dd399bd041e05eb4c911a642547f673f9 /src/server/database.ts | |
parent | 41ba832136aef2b7e6a5034486757aa4b3047cf9 (diff) | |
parent | 70a8b4ab8075af4d06efb04c083b3bf11636373e (diff) |
Merge remote-tracking branch 'origin/server_database_merge' into authentication
Diffstat (limited to 'src/server/database.ts')
-rw-r--r-- | src/server/database.ts | 87 |
1 files changed, 54 insertions, 33 deletions
diff --git a/src/server/database.ts b/src/server/database.ts index 16211a2f6..07c5819ab 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -8,50 +8,71 @@ export class Database { public static Instance = new Database() private MongoClient = mongodb.MongoClient; private url = 'mongodb://localhost:27017/Dash'; + private db?: mongodb.Db; - public update(id: String, value: any) { - this.MongoClient.connect(this.url, (err, db) => { - let collection = db.db().collection('documents'); - collection.update({ _id: id }, { $set: value }); - db.close(); - }); + constructor() { + this.MongoClient.connect(this.url, (err, client) => { + this.db = client.db() + }) + } + + public update(id: string, value: any) { + if (this.db) { + let collection = this.db.collection('documents'); + collection.update({ _id: id }, { $set: value }, { + upsert: true + }); + } } public delete(id: string) { - this.MongoClient.connect(this.url, (err, db) => { - let collection = db.db().collection('documents'); + if (this.db) { + let collection = this.db.collection('documents'); collection.remove({ _id: id }); - db.close(); - }); + } + } + + public deleteAll() { + if (this.db) { + let collection = this.db.collection('documents'); + collection.deleteMany({}); + } } public insert(kvpairs: any) { - this.MongoClient.connect(this.url, (err, db) => { - let collection = db.db().collection('documents'); - collection.insertOne(kvpairs, () => { }); - db.close(); - }); + if (this.db) { + let collection = this.db.collection('documents'); + collection.insertOne(kvpairs, (err: any, res: any) => { + if (err) { + // console.log(err) + return + } + }); + } } - public getDocument(id: String): string | undefined { + public getDocument(id: string, fn: (res: any) => void) { var result: JSON; - this.MongoClient.connect(this.url, (err, db) => { - if (err) { - console.log(err) - return undefined - } - let collection = db.db().collection('documents'); - collection.findOne({ _id: id }, (err: any, res: any) => result = res) - console.log(result) - db.close(); - if (!result) { - console.log("not found") - return undefined - } - console.log("found") - return result; - }); - return undefined + if (this.db) { + let collection = this.db.collection('documents'); + collection.findOne({ _id: id }, (err: any, res: any) => { + result = res + if (!result) { + fn(undefined) + } + fn(result) + }) + }; + } + + public getDocuments(ids: string[], fn: (res: any) => void) { + if (this.db) { + let collection = this.db.collection('documents'); + let cursor = collection.find({ _id: { "$in": ids } }) + cursor.toArray((err, docs) => { + fn(docs); + }) + }; } public print() { |