aboutsummaryrefslogtreecommitdiff
path: root/src/database.ts
diff options
context:
space:
mode:
authoryipstanley <stanley_yip@brown.edu>2019-02-11 17:29:30 -0500
committeryipstanley <stanley_yip@brown.edu>2019-02-11 17:29:30 -0500
commit448f8880b50dc50090a596cafaf68d2f444ce3db (patch)
tree025ad6877131f51a9c419bc8c5316254e31521b5 /src/database.ts
parenta33f178285505db54549ad3cec313daa914dc6e8 (diff)
parent1eb3d60457d4fc0ae957832c3ab751b54a24cc21 (diff)
Merge branch 'database' of https://github.com/browngraphicslab/Dash-Web into server_database_merge
Diffstat (limited to 'src/database.ts')
-rw-r--r--src/database.ts41
1 files changed, 41 insertions, 0 deletions
diff --git a/src/database.ts b/src/database.ts
new file mode 100644
index 000000000..a822b15bf
--- /dev/null
+++ b/src/database.ts
@@ -0,0 +1,41 @@
+import { action, configure } from 'mobx';
+import * as mongodb from 'mongodb';
+
+export class database {
+ private MongoClient = mongodb.MongoClient;
+ private url = 'mongodb://localhost:27017/website';
+
+ public update(id: string, field: string, value: string) {
+ this.MongoClient.connect(this.url, (err, db) => {
+ let collection = db.db().collection('documents');
+ collection.update({ "id": id }, { $set: { field: value } });
+ db.close();
+ });
+ }
+
+ public delete(id: string) {
+ this.MongoClient.connect(this.url, (err, db) => {
+ let collection = db.db().collection('documents');
+ collection.remove({ "id": id });
+ db.close();
+ });
+ }
+
+ public insert(kvpairs: JSON) {
+ this.MongoClient.connect(this.url, (err, db) => {
+ let collection = db.db().collection('documents');
+ collection.insert(kvpairs, () => { });
+ db.close();
+ });
+ }
+
+ public getDocument(id: string) {
+ var result: Array<JSON>;
+ this.MongoClient.connect(this.url, (err, db) => {
+ let collection = db.db().collection('documents');
+ collection.find({ "id": id }).toArray((err, db) => { result = db });
+ db.close();
+ return result[0];
+ });
+ }
+}