aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
diff options
context:
space:
mode:
authorTyler Schicke <tyler_schicke@brown.edu>2019-04-17 19:45:59 -0400
committerTyler Schicke <tyler_schicke@brown.edu>2019-04-17 19:45:59 -0400
commit9712a046868ee51a565a425d3216a2bb297c4eee (patch)
treed2adc9a47fb41ab9386dcdc0dc7e6404ad51df23 /src/server/database.ts
parent6afdd5e5136394c9dc739b5de390aa1b55c6360f (diff)
Got saving to database working
Diffstat (limited to 'src/server/database.ts')
-rw-r--r--src/server/database.ts16
1 files changed, 9 insertions, 7 deletions
diff --git a/src/server/database.ts b/src/server/database.ts
index 5457e4dd5..a61b4d823 100644
--- a/src/server/database.ts
+++ b/src/server/database.ts
@@ -13,14 +13,14 @@ export class Database {
this.MongoClient.connect(this.url, (err, client) => this.db = client.db());
}
- public update(id: string, value: any, callback: () => void) {
+ public update(id: string, value: any, callback: () => void, upsert = true, collectionName = Database.DocumentsCollection) {
if (this.db) {
- let collection = this.db.collection('documents');
+ let collection = this.db.collection(collectionName);
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 }
+ collection.updateOne({ _id: id }, { $set: value }, { upsert }
, (err, res) => {
if (err) {
console.log(err.message);
@@ -51,10 +51,12 @@ export class Database {
this.db && this.db.collection(collectionName).deleteMany({}, res));
}
- public insert(kvpairs: any, collectionName = Database.DocumentsCollection) {
- this.db && this.db.collection(collectionName).insertOne(kvpairs, (err, res) =>
- err // && console.log(err)
- );
+ public insert(value: any, collectionName = Database.DocumentsCollection) {
+ if ("id" in value) {
+ value._id = value.id;
+ delete value.id;
+ }
+ this.db && this.db.collection(collectionName).insertOne(value);
}
public getDocument(id: string, fn: (result?: Transferable) => void, collectionName = Database.DocumentsCollection) {