aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
blob: 51103520e429481e753d7bc9240ebf19e06affaa (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { action, configure } from 'mobx';
import * as mongodb from 'mongodb';
import { ObjectID } from 'mongodb';
import { Transferable } from './Message';
import { Utils } from '../Utils';

export class Database {
    public static Instance = new Database()
    private MongoClient = mongodb.MongoClient;
    private url = 'mongodb://localhost:27017/Dash';

    public update(id: string, value: any) {
        this.MongoClient.connect(this.url, { bufferMaxEntries: 1 }, (err, db) => {
            let collection = db.db().collection('documents');
            collection.update({ _id: id }, { $set: value }, {
                upsert: true
            });
            db.close();
        });
    }

    public delete(id: string) {
        this.MongoClient.connect(this.url, { bufferMaxEntries: 1 }, (err, db) => {
            let collection = db.db().collection('documents');
            collection.remove({ _id: id });
            db.close();
        });
    }

    public deleteAll() {
        this.MongoClient.connect(this.url, (err, db) => {
            let collection = db.db().collection('documents');
            collection.deleteMany({});
        })
    }

    public insert(kvpairs: any) {
        this.MongoClient.connect(this.url, { bufferMaxEntries: 1 }, (err, db) => {
            let collection = db.db().collection('documents');
            collection.insertOne(kvpairs, (err: any, res: any) => {
                if (err) {
                    // console.log(err)
                    return
                }
            });
            db.close();
        });
    }

    public getDocument(id: string, fn: (res: any) => void) {
        var result: JSON;
        this.MongoClient.connect(this.url, {
            bufferMaxEntries: 1
        }, (err, db) => {
            if (err) {
                console.log(err)
                return undefined
            }
            let collection = db.db().collection('documents');
            collection.findOne({ _id: id }, (err: any, res: any) => {
                result = res
                if (!result) {
                    fn(undefined)
                }
                fn(result)
            })
            db.close();
        });
    }

    public getDocuments(ids: string[], fn: (res: any) => void) {
        var result: JSON;
        this.MongoClient.connect(this.url, {
            bufferMaxEntries: 1
        }, (err, db) => {
            if (err) {
                console.log(err)
                return undefined
            }
            let collection = db.db().collection('documents');
            let cursor = collection.find({ _id: { "$in": ids } })
            cursor.toArray((err, docs) => {
                fn(docs);
            })
            db.close();
        });
    }

    public print() {
        console.log("db says hi!")
    }
}