diff options
author | ab <abdullah_ahmed@brown.edu> | 2019-04-19 17:06:56 -0400 |
---|---|---|
committer | ab <abdullah_ahmed@brown.edu> | 2019-04-19 17:06:56 -0400 |
commit | d77951f2f29b5b7b4869128116c3627d77e2c73b (patch) | |
tree | 81a0a02dc59af308ec322920a2060ac1b53d15f2 /src/server/database.ts | |
parent | be1976fb0ba33064978ee973993b3a2316cdf43c (diff) | |
parent | ecae4ae106be3e07471208cb93ec0965548d2d12 (diff) |
Merge commit 'ecae4ae' into new_search
Diffstat (limited to 'src/server/database.ts')
-rw-r--r-- | src/server/database.ts | 18 |
1 files changed, 10 insertions, 8 deletions
diff --git a/src/server/database.ts b/src/server/database.ts index 5c70da931..1e8004328 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -13,14 +13,14 @@ export class Database { this.MongoClient.connect(this.url, (err, client) => this.db = client.db()); } - public update(id: string, value: any, callback: () => void) { + public update(id: string, value: any, callback: () => void, upsert = true, collectionName = Database.DocumentsCollection) { if (this.db) { - let collection = this.db.collection('documents'); + let collection = this.db.collection(collectionName); const prom = this.currentWrites[id]; let newProm: Promise<void>; const run = (): Promise<void> => { return new Promise<void>(resolve => { - collection.updateOne({ _id: id }, { $set: value }, { upsert: true } + collection.updateOne({ _id: id }, { $set: value }, { upsert } , (err, res) => { if (err) { console.log(err.message); @@ -51,15 +51,17 @@ export class Database { this.db && this.db.collection(collectionName).deleteMany({}, res)); } - public insert(kvpairs: any, collectionName = Database.DocumentsCollection) { - this.db && this.db.collection(collectionName).insertOne(kvpairs, (err, res) => - err // && console.log(err) - ); + public insert(value: any, collectionName = Database.DocumentsCollection) { + if ("id" in value) { + value._id = value.id; + delete value.id; + } + this.db && this.db.collection(collectionName).insertOne(value); } public getDocument(id: string, fn: (result?: Transferable) => void, collectionName = Database.DocumentsCollection) { this.db && this.db.collection(collectionName).findOne({ id: id }, (err, result) => - fn(result ? ({ id: result._id, type: result.type, data: result.data }) : undefined)) + fn(result ? ({ id: result._id, type: result.type, data: result.data }) : undefined)); } public getDocuments(ids: string[], fn: (result: Transferable[]) => void, collectionName = Database.DocumentsCollection) { |