aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
diff options
context:
space:
mode:
authorAndrew Kim <andrewdkim@users.noreply.github.com>2019-04-20 18:42:01 -0400
committerAndrew Kim <andrewdkim@users.noreply.github.com>2019-04-20 18:42:01 -0400
commit840de58f003d0962ef7d3a0ad6ea284d1f4870db (patch)
tree32377ecaedceb24e54b6645ae76f3a5e0171fd54 /src/server/database.ts
parent604d20941837fa90c06a7da7e77a27c262cd4648 (diff)
parente47656cdc18aa1fd801a3853fa0f819140a68646 (diff)
update
Diffstat (limited to 'src/server/database.ts')
-rw-r--r--src/server/database.ts120
1 files changed, 44 insertions, 76 deletions
diff --git a/src/server/database.ts b/src/server/database.ts
index 0bc806253..5457e4dd5 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -1,107 +1,75 @@
import * as mongodb from 'mongodb';
+import { Transferable } from './Message';
export class 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());
}
- private currentWrites: { [_id: string]: Promise<void> } = {};
-
public update(id: string, value: any, callback: () => void) {
if (this.db) {
let collection = this.db.collection('documents');
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();
+ 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();
+ });
});
};
- 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;
- }
+ 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() {