diff options
author | Sam Wilkins <abdullah_ahmed@brown.edu> | 2019-04-09 18:05:57 -0400 |
---|---|---|
committer | Sam Wilkins <abdullah_ahmed@brown.edu> | 2019-04-09 18:05:57 -0400 |
commit | 79a0d8d66204868158849afda6518f6c641c826b (patch) | |
tree | c36b6064cb8577f375107c6df4885c7a3e0dc9e0 /src/server/database.ts | |
parent | b7d02ec188ecf043300ed858fdb68148b3e52a71 (diff) | |
parent | 89fd4327db1536990b4a4dc218819a1077f82445 (diff) |
merged with master
Diffstat (limited to 'src/server/database.ts')
-rw-r--r-- | src/server/database.ts | 74 |
1 files changed, 44 insertions, 30 deletions
diff --git a/src/server/database.ts b/src/server/database.ts index 87a0b3c70..0bc806253 100644 --- a/src/server/database.ts +++ b/src/server/database.ts @@ -1,36 +1,50 @@ -import { action, configure } from 'mobx'; import * as mongodb from 'mongodb'; -import { ObjectID } from 'mongodb'; -import { Transferable } from './Message'; -import { Utils } from '../Utils'; export class Database { - public static Instance = new Database() + public static Instance = new Database(); private MongoClient = mongodb.MongoClient; private url = 'mongodb://localhost:27017/Dash'; private db?: mongodb.Db; constructor() { this.MongoClient.connect(this.url, (err, client) => { - this.db = client.db() - }) + this.db = client.db(); + }); } + private currentWrites: { [_id: string]: Promise<void> } = {}; + public update(id: string, value: any, callback: () => void) { if (this.db) { let collection = this.db.collection('documents'); - collection.updateOne({ _id: id }, { $set: value }, { - upsert: true - }, (err, res) => { - if (err) { - console.log(err.message); - console.log(err.errmsg); - } - if (res) { - // console.log(JSON.stringify(res.result)); - } - callback() - }); + const prom = this.currentWrites[id]; + const run = (promise: Promise<void>, resolve?: () => void) => { + collection.updateOne({ _id: id }, { $set: value }, { + upsert: true + }, (err, res) => { + if (err) { + console.log(err.message); + console.log(err.errmsg); + } + // if (res) { + // console.log(JSON.stringify(res.result)); + // } + if (this.currentWrites[id] === promise) { + delete this.currentWrites[id]; + } + if (resolve) { + resolve(); + } + callback(); + }); + }; + if (prom) { + const newProm: Promise<void> = prom.then(() => run(newProm)); + this.currentWrites[id] = newProm; + } else { + const newProm: Promise<void> = new Promise<void>(res => run(newProm, res)); + this.currentWrites[id] = newProm; + } } } @@ -47,7 +61,7 @@ export class Database { let collection = this.db.collection(collectionName); collection.deleteMany({}, res); } - }) + }); } public insert(kvpairs: any) { @@ -56,7 +70,7 @@ export class Database { collection.insertOne(kvpairs, (err: any, res: any) => { if (err) { // console.log(err) - return + return; } }); } @@ -67,30 +81,30 @@ export class Database { if (this.db) { let collection = this.db.collection('documents'); collection.findOne({ _id: id }, (err: any, res: any) => { - result = res + result = res; if (!result) { - fn(undefined) + fn(undefined); } - fn(result) - }) - }; + 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 } }) + let cursor = collection.find({ _id: { "$in": ids } }); cursor.toArray((err, docs) => { if (err) { console.log(err.message); console.log(err.errmsg); } fn(docs); - }) - }; + }); + } } public print() { - console.log("db says hi!") + console.log("db says hi!"); } } |