aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
diff options
context:
space:
mode:
authorBob Zeleznik <zzzman@gmail.com>2019-04-13 15:27:57 -0400
committerBob Zeleznik <zzzman@gmail.com>2019-04-13 15:27:57 -0400
commita23b160d19beff9163f970f7ae678c2aed9ce14e (patch)
tree72f3186e7b784294c7d999a000ab0dabd461fcae /src/server/database.ts
parent1190854396a0ff20441b285b2a3959d4c5f9650a (diff)
some code tweaking
Diffstat (limited to 'src/server/database.ts')
-rw-r--r--src/server/database.ts26
1 files changed, 13 insertions, 13 deletions
diff --git a/src/server/database.ts b/src/server/database.ts
index 6cc9945b9..e08385d98 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -1,20 +1,20 @@
import * as mongodb from 'mongodb';
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());
}
- private currentWrites: { [_id: string]: Promise<void> } = {};
-
- public update(id: string, value: any, callback: () => void) {
+ public update(id: string, value: any, callback: () => void, collectionName = Database.DocumentsCollection) {
if (this.db) {
- let collection = this.db.collection('documents');
+ let collection = this.db.collection(collectionName);
const prom = this.currentWrites[id];
const run = (): Promise<void> => {
let newProm = new Promise<void>(resolve => {
@@ -40,28 +40,28 @@ export class Database {
}
}
- public delete(id: string) {
- this.db && this.db.collection('documents').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> {
+ public deleteAll(collectionName = Database.DocumentsCollection): Promise<any> {
return new Promise(res =>
this.db && this.db.collection(collectionName).deleteMany({}, res));
}
- public insert(kvpairs: any) {
- this.db && this.db.collection('documents').insertOne(kvpairs, (err: any, res: any) =>
+ 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) {
- this.db && this.db.collection('documents').findOne({ _id: id }, (err: any, result: any) =>
+ 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 getDocuments(ids: string[], fn: (res: any) => void) {
- this.db && this.db.collection('documents').find({ _id: { "$in": ids } }).toArray((err, docs) => {
+ public getDocuments(ids: string[], fn: (res: any) => 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);