aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
diff options
context:
space:
mode:
authorFawn <fangrui_tong@brown.edu>2019-04-15 15:32:46 -0400
committerFawn <fangrui_tong@brown.edu>2019-04-15 15:32:46 -0400
commit63ad49ff966d3c3f29bbe2c4d9758527f405bb6a (patch)
tree5f3f5b48b423b602ddee74d48a3ceaa487f3aad3 /src/server/database.ts
parente81c43baadcaf31314c07505fa7cde70e709706d (diff)
parent6c0b421db6aa3204bbc6e42139d240f503000b5d (diff)
fixed merge
Diffstat (limited to 'src/server/database.ts')
-rw-r--r--src/server/database.ts114
1 files changed, 48 insertions, 66 deletions
diff --git a/src/server/database.ts b/src/server/database.ts
index a42d29aac..7914febf8 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -1,96 +1,78 @@
-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 DocumentsCollection = 'documents';
+ public static Instance = new Database();
private MongoClient = mongodb.MongoClient;
private url = 'mongodb://localhost:27017/Dash';
+ private currentWrites: { [id: string]: Promise<void> } = {};
private db?: mongodb.Db;
constructor() {
- this.MongoClient.connect(this.url, (err, client) => {
- this.db = client.db()
- })
+ this.MongoClient.connect(this.url, (err, client) => this.db = client.db());
}
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];
+ let newProm: Promise<void>;
+ const run = (): Promise<void> => {
+ return new Promise<void>(resolve => {
+ 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] === newProm) {
+ delete this.currentWrites[id];
+ }
+ resolve();
+ callback();
+ });
+ });
+ };
+ newProm = prom ? prom.then(run) : run();
+ this.currentWrites[id] = newProm;
}
}
- public delete(id: string) {
- if (this.db) {
- let collection = this.db.collection('documents');
- collection.remove({ _id: id });
- }
+ public delete(id: string, collectionName = Database.DocumentsCollection) {
+ this.db && this.db.collection(collectionName).remove({ id: id });
}
- public deleteAll(collectionName: string = 'documents'): Promise<any> {
- return new Promise(res => {
- if (this.db) {
- let collection = this.db.collection(collectionName);
- collection.deleteMany({}, res);
- }
- })
+ public deleteAll(collectionName = Database.DocumentsCollection): Promise<any> {
+ return new Promise(res =>
+ this.db && this.db.collection(collectionName).deleteMany({}, res));
}
- public insert(kvpairs: any) {
- if (this.db) {
- let collection = this.db.collection('documents');
- collection.insertOne(kvpairs, (err: any, res: any) => {
- if (err) {
- // console.log(err)
- return
- }
- });
- }
+ public insert(kvpairs: any, collectionName = Database.DocumentsCollection) {
+ this.db && this.db.collection(collectionName).insertOne(kvpairs, (err, res) =>
+ err // && console.log(err)
+ );
}
- public getDocument(id: string, fn: (res: any) => void) {
- var result: JSON;
- 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 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))
}
- 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) => {
- if (err) {
- console.log(err.message);
- console.log(err.errmsg);
- }
- fn(docs);
- })
- };
+ public getDocuments(ids: string[], fn: (result: Transferable[]) => void, collectionName = Database.DocumentsCollection) {
+ this.db && this.db.collection(collectionName).find({ id: { "$in": ids } }).toArray((err, docs) => {
+ if (err) {
+ console.log(err.message);
+ console.log(err.errmsg);
+ }
+ fn(docs.map(doc => ({ id: doc._id, type: doc.type, data: doc.data })));
+ });
}
public print() {
- console.log("db says hi!")
+ console.log("db says hi!");
}
}