aboutsummaryrefslogtreecommitdiff
path: root/src/server/database.ts
blob: 37bc00a8583d4bfb88101ab4b7a7986442e5d380 (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import * as mongodb from 'mongodb';
import * as mongoose from 'mongoose';
import { Opt } from '../fields/Doc';
import { emptyFunction, Utils } from '../Utils';
import { GoogleApiServerUtils } from './apis/google/GoogleApiServerUtils';
import { DocumentsCollection, IDatabase } from './IDatabase';
import { MemoryDatabase } from './MemoryDatabase';
import { Transferable } from './Message';
import { Upload } from './SharedMediaTypes';
import { ObjectId } from 'mongodb';

export namespace Database {

    export let disconnect: Function;
    const schema = 'Dash';
    const port = 27017;
    export const url = `mongodb://localhost:${port}/${schema}`;

    enum ConnectionStates {
        disconnected = 0,
        connected = 1,
        connecting = 2,
        disconnecting = 3,
        uninitialized = 99,
    }

    export async function tryInitializeConnection() {
        try {
            const { connection } = mongoose;
            disconnect = async () => new Promise<any>(resolve => connection.close().then(resolve));
            if (connection.readyState === ConnectionStates.disconnected) {
                await new Promise<void>((resolve, reject) => {
                    connection.on('error', reject);
                    connection.on('connected', () => {
                        console.log(`mongoose established default connection at ${url}`);
                        resolve();
                    });
                    mongoose.connect(url, {
                        //useNewUrlParser: true, 
                        dbName: schema,
                        // reconnectTries: Number.MAX_VALUE,
                        // reconnectInterval: 1000,
                    }); 
                });
            }
        } catch (e) {
            console.error(`Mongoose FAILED to establish default connection at ${url} with the following error:`);
            console.error(e);
            console.log('Since a valid database connection is required to use Dash, the server process will now exit.\nPlease try again later.');
            process.exit(1);
        }
    }

    export class Database implements IDatabase {
        private MongoClient = mongodb.MongoClient;
        private currentWrites: { [id: string]: Promise<void> } = {};
        private db?: mongodb.Db;
        private onConnect: (() => void)[] = [];

        async doConnect() {
            console.error(`\nConnecting to Mongo with URL : ${url}\n`);
            return new Promise<void>(resolve => {
                this.MongoClient.connect(url, { connectTimeoutMS: 30000, socketTimeoutMS: 30000, }).then(client => {
                    console.error("mongo connect response\n");
                    if (!client) {
                        console.error("\nMongo connect failed with the error:\n");
                        process.exit(0);
                    }
                    this.db = client.db();
                    this.onConnect.forEach(fn => fn());
                    resolve();
                });
            });
        }

        public async update(id: string, value: any, callback: (err: mongodb.MongoError, res: mongodb.UpdateResult) => void, upsert = true, collectionName = DocumentsCollection) {
            if (this.db) {
                const 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: new ObjectId(id) }, value, { upsert }).then(res => {
                                if (this.currentWrites[id] === newProm) {
                                    delete this.currentWrites[id];
                                }
                                resolve();
                                callback(undefined as any, res);
                            });
                    });
                };
                newProm = prom ? prom.then(run) : run();
                this.currentWrites[id] = newProm;
                return newProm;
            } else {
                this.onConnect.push(() => this.update(id, value, callback, upsert, collectionName));
            }
        }

        public replace(id: string, value: any, callback: (err: mongodb.MongoError, res: mongodb.UpdateResult<mongodb.Document>) => void, upsert = true, collectionName = DocumentsCollection) {
            if (this.db) {
                const collection = this.db.collection(collectionName);
                const prom = this.currentWrites[id];
                let newProm: Promise<void>;
                const run = (): Promise<void> => {
                    return new Promise<void>(resolve => {
                        collection.replaceOne({ _id: new ObjectId(id)}, value, { upsert }).then( res => {
                                if (this.currentWrites[id] === newProm) {
                                    delete this.currentWrites[id];
                                }
                                resolve();
                                callback(undefined as any, res as any);
                            });
                    });
                };
                newProm = prom ? prom.then(run) : run();
                this.currentWrites[id] = newProm;
            } else {
                this.onConnect.push(() => this.replace(id, value, callback, upsert, collectionName));
            }
        }

        public async getCollectionNames() {
            const cursor = this.db?.listCollections();
            const collectionNames: string[] = [];
            if (cursor) {
                while (await cursor.hasNext()) {
                    const collection: any = await cursor.next();
                    collection && collectionNames.push(collection.name);
                }
            }
            return collectionNames;
        }

        public delete(query: any, collectionName?: string): Promise<mongodb.DeleteResult>;
        public delete(id: string, collectionName?: string): Promise<mongodb.DeleteResult>;
        public delete(id: any, collectionName = DocumentsCollection) {
            if (typeof id === "string") {
                id = { _id: new ObjectId(id) };
            }
            if (this.db) {
                const db = this.db;
                return new Promise(res => db.collection(collectionName).deleteMany(id).then(result => res(result)));
            } else {
                return new Promise(res => this.onConnect.push(() => res(this.delete(id, collectionName))));
            }
        }

        public async dropSchema(...targetSchemas: string[]): Promise<any> {
            const executor = async (database: mongodb.Db) => {
                const existing = await Instance.getCollectionNames();
                let valid: string[];
                if (targetSchemas.length) {
                    valid = targetSchemas.filter(collection => existing.includes(collection));
                } else {
                    valid = existing;
                }
                const pending = Promise.all(valid.map(schemaName => database.dropCollection(schemaName)));
                return (await pending).every(dropOutcome => dropOutcome);
            };
            if (this.db) {
                return executor(this.db);
            } else {
                this.onConnect.push(() => this.db && executor(this.db));
            }
        }

        public async insert(value: any, collectionName = DocumentsCollection) {
            if (this.db && value !== null) {
                if ("id" in value) {
                    value._id = value.id;
                    delete value.id;
                }
                const id = value._id;
                const collection = this.db.collection(collectionName);
                const prom = this.currentWrites[id];
                let newProm: Promise<void>;
                const run = (): Promise<void> => {
                    return new Promise<void>(resolve => {
                        collection.insertOne(value).then(res => {
                            if (this.currentWrites[id] === newProm) {
                                delete this.currentWrites[id];
                            }
                            resolve();
                        });
                    });
                };
                newProm = prom ? prom.then(run) : run();
                this.currentWrites[id] = newProm;
                return newProm;
            } else if (value !== null) {
                this.onConnect.push(() => this.insert(value, collectionName));
            }
        }

        public getDocument(id: string, fn: (result?: Transferable) => void, collectionName = DocumentsCollection) {
            if (this.db) {
                this.db.collection(collectionName).findOne({ _id: new ObjectId(id) }).then(result => {
                    if (result) {
                        result.id = result._id;
                        //delete result._id;
                        fn(result.id);
                    } else {
                        fn(undefined);
                    }
                });
            } else {
                this.onConnect.push(() => this.getDocument(id, fn, collectionName));
            }
        }

        public getDocuments(ids: string[], fn: (result: Transferable[]) => void, collectionName = DocumentsCollection) {
            if (this.db) {
                this.db.collection(collectionName).find({ _id: { "$in": ids.map(id => new ObjectId(id)) } }).map(docs => {
                    // if (err) {
                    //     console.log(err.message);
                    //     console.log(err.errmsg);
                    // }
                    fn(docs.map((doc:any) => {
                        doc.id = doc._id;
                        delete doc._id;
                        return doc;
                    }));
                });
            } else {
                this.onConnect.push(() => this.getDocuments(ids, fn, collectionName));
            }
        }

        public async visit(ids: string[], fn: (result: any) => string[] | Promise<string[]>, collectionName = DocumentsCollection): Promise<void> {
            if (this.db) {
                const visited = new Set<string>();
                while (ids.length) {
                    const count = Math.min(ids.length, 1000);
                    const index = ids.length - count;
                    const fetchIds = ids.splice(index, count).filter(id => !visited.has(id));
                    if (!fetchIds.length) {
                        continue;
                    }
                    const docs = await new Promise<{ [key: string]: any }[]>(res => this.getDocuments(fetchIds, res, collectionName));
                    for (const doc of docs) {
                        const id = doc.id;
                        visited.add(id);
                        ids.push(...(await fn(doc)));
                    }
                }
            } else {
                return new Promise(res => {
                    this.onConnect.push(() => {
                        this.visit(ids, fn, collectionName);
                        res();
                    });
                });
            }
        }

        public query(query: { [key: string]: any }, projection?: { [key: string]: 0 | 1 }, collectionName = DocumentsCollection): Promise<mongodb.FindCursor> {
            if (this.db) {
                let cursor = this.db.collection(collectionName).find(query);
                if (projection) {
                    cursor = cursor.project(projection);
                }
                return Promise.resolve<mongodb.FindCursor>(cursor);
            } else {
                return new Promise<mongodb.FindCursor>(res => {
                    this.onConnect.push(() => res(this.query(query, projection, collectionName)));
                });
            }
        }

        public updateMany(query: any, update: any, collectionName = DocumentsCollection) {
            if (this.db) {
                const db = this.db;
                return new Promise<mongodb.UpdateResult>(res => db.collection(collectionName).updateMany(query, update).then(result => res(result)));
            } else {
                return new Promise<mongodb.UpdateResult>(res => {
                    this.onConnect.push(() => this.updateMany(query, update, collectionName).then(res));
                });
            }
        }

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

    function getDatabase() {
        switch (process.env.DB) {
            case "MEM":
                return new MemoryDatabase();
            default:
                return new Database();
        }
    }

    export const Instance = getDatabase();

    /**
     * Provides definitions and apis for working with
     * portions of the database not dedicated to storing documents
     * or Dash-internal user data.
     */
    export namespace Auxiliary {

        /**
         * All the auxiliary MongoDB collections (schemas)
         */
        export enum AuxiliaryCollections {
            GooglePhotosUploadHistory = "uploadedFromGooglePhotos",
            GoogleAccess = "googleAuthentication",
        }

        /**
         * Searches for the @param query in the specified @param collection,
         * and returns at most the first @param cap results. If @param removeId is true,
         * as it is by default, each object will be stripped of its database id.
         */
        const SanitizedCappedQuery = async (query: { [key: string]: any }, collection: string, cap: number, removeId = true) => {
            const cursor = await Instance.query(query, undefined, collection);
            const results = await cursor.toArray();
            const slice = results.slice(0, Math.min(cap, results.length));
            return removeId ? slice.map((result:any) => {
                delete result._id;
                return result;
            }) : slice;
        };

        /**
         * Searches for the @param query in the specified @param collection,
         * and returns at most the first result. If @param removeId is true,
         * as it is by default, each object will be stripped of its database id. 
         * Worth the special case since it converts the Array return type to a single
         * object of the specified type.
         */
        const SanitizedSingletonQuery = async <T>(query: { [key: string]: any }, collection: string, removeId = true): Promise<Opt<T>> => {
            const results = await SanitizedCappedQuery(query, collection, 1, removeId);
            return results.length ? results[0] : undefined;
        };

        /**
         * Checks to see if an image with the given @param contentSize 
         * already exists in the aux database, i.e. has already been downloaded from Google Photos.
         */
        export const QueryUploadHistory = async (contentSize: number) => {
            return SanitizedSingletonQuery<Upload.ImageInformation>({ contentSize }, AuxiliaryCollections.GooglePhotosUploadHistory);
        };

        /**
         * Records the uploading of the image with the given @param information,
         * using the given content size as a seed for the database id.
         */
        export const LogUpload = async (information: Upload.ImageInformation) => {
            const bundle = {
                _id: Utils.GenerateDeterministicGuid(String(information.contentSize)),
                ...information
            };
            return Instance.insert(bundle, AuxiliaryCollections.GooglePhotosUploadHistory);
        };

        /**
         * Manages the storage, retrieval and updating of the access token that
         * facilitates interactions with all their APIs for a given account.
         */
        export namespace GoogleAccessToken {

            /**
             * Format stored in database.
             */
            type StoredCredentials = GoogleApiServerUtils.EnrichedCredentials & { _id: string };

            /**
             * Retrieves the credentials associaed with @param userId
             * and optionally removes their database id according to @param removeId. 
             */
            export const Fetch = async (userId: string, removeId = true): Promise<Opt<StoredCredentials>> => {
                return SanitizedSingletonQuery<StoredCredentials>({ userId }, AuxiliaryCollections.GoogleAccess, removeId);
            };

            /**
             * Writes the @param enrichedCredentials to the database, associated
             * with @param userId for later retrieval and updating. 
             */
            export const Write = async (userId: string, enrichedCredentials: GoogleApiServerUtils.EnrichedCredentials) => {
                return Instance.insert({ userId, canAccess: [], ...enrichedCredentials }, AuxiliaryCollections.GoogleAccess);
            };

            /**
             * Updates the @param access_token and @param expiry_date fields
             * in the stored credentials associated with @param userId.
             */
            export const Update = async (userId: string, access_token: string, expiry_date: number) => {
                const entry = await Fetch(userId, false);
                if (entry) {
                    const parameters = { $set: { access_token, expiry_date } };
                    return Instance.update(entry._id, parameters, emptyFunction, true, AuxiliaryCollections.GoogleAccess);
                }
            };

            /**
             * Revokes the credentials associated with @param userId. 
             */
            export const Revoke = async (userId: string) => {
                const entry = await Fetch(userId, false);
                if (entry) {
                    Instance.delete({ _id: entry._id }, AuxiliaryCollections.GoogleAccess);
                }
            };

        }

    }

}