aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
diff options
context:
space:
mode:
authorlaurawilsonri <laura_wilson@brown.edu>2019-04-15 20:20:46 -0400
committerlaurawilsonri <laura_wilson@brown.edu>2019-04-15 20:20:46 -0400
commitb7a766aa5d53686e032b34d7213ad543108f12c3 (patch)
treed71854e091fa24997a30856d9ac9c937a6cd45d2 /src/server/database.ts
parent8b1026d357dd40fc4a00010739d99ffc9db08641 (diff)
parentff6ba4107aa33e021803399180b0d2844fc19d84 (diff)
Merge branch 'master' of https://github.com/browngraphicslab/Dash-Web into richTextEditor
Diffstat (limited to 'src/server/database.ts')
-rw-r--r--src/server/database.ts21
1 files changed, 11 insertions, 10 deletions
diff --git a/src/server/database.ts b/src/server/database.ts
index 3290edde0..7914febf8 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -1,20 +1,21 @@
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> => {
@@ -42,7 +43,7 @@ export class Database {
}
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> {
@@ -56,18 +57,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 })));
});
}