aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/server/database.ts')
-rw-r--r--src/server/database.ts64
1 files changed, 36 insertions, 28 deletions
diff --git a/src/server/database.ts b/src/server/database.ts
index e08385d98..c51d4a3f6 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -1,47 +1,55 @@
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 currentWrites: { [id: string]: Promise<void> } = {};
private db?: mongodb.Db;
constructor() {
this.MongoClient.connect(this.url, (err, client) => this.db = client.db());
}
- public update(id: string, value: any, callback: () => void, collectionName = Database.DocumentsCollection) {
+ public update(id: string, value: any, callback: () => void) {
if (this.db) {
- let collection = this.db.collection(collectionName);
+ let collection = this.db.collection('documents');
const prom = this.currentWrites[id];
+ let newProm: Promise<void>;
const run = (): Promise<void> => {
- let newProm = 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();
- });
+ 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();
+ });
});
- return newProm;
};
- this.currentWrites[id] = prom ? prom.then(run) : run();
+ if (prom) {
+ newProm = prom.then(run);
+ this.currentWrites[id] = newProm;
+ } else {
+ newProm = run();
+ this.currentWrites[id] = newProm;
+ }
}
}
public delete(id: string, collectionName = Database.DocumentsCollection) {
- this.db && this.db.collection(collectionName).remove({ _id: id });
+ this.db && this.db.collection(collectionName).remove({ id: id });
}
public deleteAll(collectionName = Database.DocumentsCollection): Promise<any> {
@@ -55,18 +63,18 @@ export class Database {
);
}
- public getDocument(id: string, fn: (res: any) => void, collectionName = Database.DocumentsCollection) {
- this.db && this.db.collection(collectionName).findOne({ _id: id }, (err, result) =>
- fn(result ? result : undefined));
+ 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, collectionName = Database.DocumentsCollection) {
- this.db && this.db.collection(collectionName).find({ _id: { "$in": ids } }).toArray((err, 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);
+ fn(docs.map(doc => ({ id: doc._id, type: doc.type, data: doc.data })));
});
}