-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb.ts
425 lines (345 loc) · 12.5 KB
/
db.ts
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
414
415
416
417
418
419
420
421
422
423
424
425
import { ReplaySubject } from 'rxjs';
import Loki from 'lokijs';
import * as jetpack from 'fs-jetpack';
import mongoose, {
ConnectOptions,
Model as MongooseModel,
Schema,
Document,
Types,
Connection,
Mongoose,
Query,
} from 'mongoose';
import { Model } from './util/mongo';
import safeStringify from 'fast-safe-stringify';
import fsPath from 'path';
let app: any;
let log: (...msgs: any[]) => void;
const isObject = (obj: any): obj is Record<string, any> => obj && typeof obj === 'object';
const createNestedProxy = <T extends Record<string, any>>(obj: T): T =>
new Proxy(obj, {
// @ts-ignore
get(target, prop: keyof T) {
if (prop in target) {
// @ts-ignore
if (isObject(target[prop]) && !(target[prop] instanceof Promise)) {
return createNestedProxy(target[prop]);
}
return target[prop];
} else {
(target as any)[prop] = {};
return createNestedProxy((target as any)[prop]);
}
},
});
// const applicationExcludedModels = ['Omniverse', 'Metaverse', 'Application'];
// export class Model<T extends Document> {
// protected model: MongooseModel<T>;
// protected schema: Schema;
// constructor(model: MongooseModel<T>) {
// this.model = model;
// this.schema = model.schema;
// }
// aggregate(...props: any[]): any {
// return this.model.aggregate(...props);
// }
// where(...props: [any, any]): any {
// return this.model.where(...props);
// }
// find(filter: Record<string, any> = {}, options: Record<string, any> = {}): Query<T[], T> {
// if (!applicationExcludedModels.includes(this.model.modelName)) {
// filter.applicationId = app.filters.applicationId;
// }
// if (filter.applicationId && typeof filter.applicationId === 'string') {
// filter.applicationId = filter.applicationId;
// }
// return this.model.find(filter, options);
// }
// findAll(): Query<T[], T> {
// return this.model.find();
// }
// async upsert(
// filter: Record<string, any> = {},
// create: Record<string, any> = {},
// update: Record<string, any> = {},
// options: Record<string, any> = {}
// ): Promise<any> {
// console.log('Trying to find model with filter', filter, options);
// const res = await this.findOne(filter, options).exec();
// console.log('Result of findOne', res);
// if (res) {
// await this.update(filter, update, options);
// return await this.findOne(filter, options).exec();
// } else {
// return await this.create(create);
// }
// }
// findOne(filter: Record<string, any> = {}, options: Record<string, any> = {}): Query<T | null, T> {
// console.log(this.model.modelName);
// if (!applicationExcludedModels.includes(this.model.modelName)) {
// filter.applicationId = app.filters.applicationId;
// }
// if (filter.applicationId && typeof filter.applicationId === 'string') {
// filter.applicationId = filter.applicationId;
// }
// return this.model.findOne(filter, options);
// }
// create(data: Record<string, any>): any {
// console.log(this.model.modelName);
// if (!applicationExcludedModels.includes(this.model.modelName)) {
// data.applicationId = app.filters.applicationId;
// }
// if (data.applicationId && typeof data.applicationId === 'string') {
// data.applicationId = data.applicationId;
// }
// const res = this.model.create(data);
// const createHandler = <U extends object>(path: string[] = []): ProxyHandler<U> => ({
// // @ts-ignore
// get: (target: U, key: keyof U): any => {
// if (key === 'isProxy') return true;
// if (typeof target[key] === 'object' && target[key] != null) {
// // @ts-ignore
// return new Proxy(target[key], createHandler([...path, key as string]));
// }
// return target[key];
// },
// // @ts-ignore
// set: (target: U, key: keyof U, value: any): boolean => {
// console.log(`Setting ${[...path, key]} to: `, value);
// target[key] = value;
// return true;
// },
// });
// if ((res as any).meta) (res as any).meta = new Proxy((res as any).meta, createHandler<any>());
// return res;
// }
// update(filter: Record<string, any>, update: Record<string, any>, options: Record<string, any> = {}): any {
// if (!applicationExcludedModels.includes(this.model.modelName)) {
// filter.applicationId = app.filters.applicationId;
// update.applicationId = app.filters.applicationId; // Ensure the update object includes applicationId
// }
// if (filter.applicationId && typeof filter.applicationId === 'string') {
// filter.applicationId = filter.applicationId;
// }
// if (update.applicationId && typeof update.applicationId === 'string') {
// update.applicationId = update.applicationId;
// }
// return this.model.updateOne(filter, update, options);
// }
// updateOne(...props: [any, any]): any {
// return this.update(...props);
// }
// countDocuments(): any {
// return this.model.countDocuments();
// }
// }
class Database {
// @ts-ignore
public loki: Loki | null = null;
public mongoose: Connection;
public collections: Record<string, any>[] = [];
public channel = {
log: new ReplaySubject<any[]>(10),
};
public data = {
model: {} as Record<string, Model<Document>>,
};
constructor() {}
log(...msgs: any[]) {
this.channel.log.next(msgs);
}
Schema = mongoose.Schema;
schema: Record<string, Schema> = {};
async initLoki() {
this.loki = new Loki(null, {
autoload: false,
autosave: false,
});
this.restoreData();
setInterval(this.saveData.bind(this), 30 * 1000);
}
async initMongoose() {
this.mongoose = await mongoose
.connect(process.env.MONGO_ENDPOINT!, {
retryWrites: false,
useNewUrlParser: true,
useUnifiedTopology: true,
} as ConnectOptions)
.then((conn) => conn.connection);
// const mongoose: any = await Mongoose.connect(config.db.endpoint, {
// useNewUrlParser: true,
// useUnifiedTopology: true,
// replicaSet: 'myReplicaSetName',
// } as ConnectOptions);
// mongoose.connection.on('connected', function () {
// console.log('Mongoose connection open to ' + config.db.endpoint);
// });
// // If the connection throws an error
// mongoose.connection.on('error', function (err) {
// console.log('Mongoose connection error: ' + err);
// });
// // When the connection is disconnected
// mongoose.connection.on('disconnected', function () {
// console.log('Mongoose connection disconnected');
// });
// // if the Node process ends, close the Mongoose connection
// process.on('SIGINT', function () {
// mongoose.connection.close(function () {
// console.log('Mongoose connection disconnected through app termination');
// });
// setTimeout(function () {
// process.exit(0);
// }, 50);
// });
// // if the Node process ends, close the Mongoose connection
// process.on('SIGTERM', function () {
// mongoose.connection.close(function () {
// console.log('Mongoose connection disconnected through app termination');
// });
// setTimeout(function () {
// process.exit(0);
// }, 50);
// });
// this.mongoose.connection.on('connected', () => {
// log('Mongoose connection open to ' + process.env.MONGO_ENDPOINT);
// });
// this.mongoose.connection.on('error', err => {
// log('Mongoose connection error: ' + err);
// });
// this.mongoose.connection.on('disconnected', () => {
// log('Mongoose connection disconnected');
// });
process.on('SIGINT', async () => {
// @ts-ignore
if (this.mongoose?.connection)
// @ts-ignore
await this.mongoose.connection.close();
console.log('Mongoose connection disconnected through app termination');
process.exit(0);
});
}
model<T extends Document>(key: string, schema?: Schema): Model<T> | undefined {
if (schema) {
// @ts-ignore
this.data.model[key] = new Model<T>(mongoose.model<T>(key, schema, key));
}
if (!this.data.model[key]) {
log(`DB Model not found: ${key}`);
}
// @ts-ignore
return this.data.model[key] as Model<T>;
}
initCollection(name: string, key: string, data: Record<string, any>) {
if (!this.collections[name]) this.collections[name] = {};
if (!this.collections[name][key]) {
this.collections[name][key] = this.loki!.addCollection(`${name}.${key}`);
}
if (key === 'config') {
delete data.meta;
delete data.$loki;
if (!this.collections[name][key].length) {
this.collections[name][key].insert(data);
}
for (const k in data) {
if (this.collections[name][key].data[0][k] === undefined && data[k] !== undefined) {
this.collections[name][key].data[0][k] = data[k];
}
}
for (const k in this.collections[name][key].data[0]) {
if (Object.prototype.hasOwnProperty.call(this.collections[name][key].data[0], k)) {
if (this.collections[name][key][k] === undefined) {
Object.defineProperty(this.collections[name][key], k, {
get() {
return this.collections[name][key].data[0][k];
},
set(x) {
this.collections[name][key].data[0][k] = x;
},
});
}
}
}
} else {
for (const i in data) {
const item = data[i];
delete item.meta;
delete item.$loki;
if (!this.collections[name][key].data.length) {
this.collections[name][key].insert(item);
}
for (const k in item) {
if (typeof this.collections[name][key].data[i] === 'undefined') this.collections[name][key].data[i] = {};
if (this.collections[name][key].data[i][k] === undefined && item[k] !== undefined) {
this.collections[name][key].data[i][k] = item[k];
}
}
}
}
this.collections[name][key].ensureId();
this.collections[name][key].ensureAllIndexes(true);
}
initCollections(name: string, data: Record<string, any>) {
log(`Adding collection: ${name}`);
for (const key in data) {
this.initCollection(name, key, data[key]);
}
return this.collections[name];
}
getCollections(name: string) {
return this.collections[name];
}
saveData() {
log('Saving data', ['p1']);
const data: Record<string, any> = {};
// for (const name in this.collections) {
// data[name] = {}
// for (const key in this.collections[name]) {
// if (key === 'config') {
// delete this.collections[name][key].data[0].meta
// delete this.collections[name][key].data[0].$loki
// data[name].config = this.collections[name][key].data[0]
// } else {
// data[name][key] = Object.keys(this.collections[name][key].data).map(k => {
// const item = this.collections[name][key].data[k]
// delete item.meta
// delete item.$loki
// return item
// })
// }
// jetpack.write(`data/db/${name}/${key}.json`, this.beautify(data[name][key]))
// jetpack.write(`data/db/${name}/${key}.json.backup`, this.beautify(data[name][key]))
// }
// }
}
restoreData() {
log('Restoring data', ['p1']);
const files = jetpack.find(fsPath.join(__dirname, 'data/db'), { matching: '**/*.json' });
for (const file of files) {
log(`Found file: ${file}`);
try {
const data = jetpack.read(file, 'json');
const [name, key] = file.replace('data/db/', '').replace('.json', '').split('/');
this.initCollection(name, key, data as Record<string, any>);
} catch (e) {
if (e.toString().indexOf('JSON parsing failed') !== -1) {
log(`File corrupt, loading backup: ${file}`);
const data = jetpack.read(`${file}.backup`, 'json');
const [name, key] = file.replace('data/db/', '').replace('.json', '').split('/');
this.initCollection(name, key, data as Record<string, any>);
}
}
}
}
beautify(data: any) {
return JSON.stringify(JSON.parse(safeStringify(data)), null, 4);
}
}
export const db = new Database();
export async function init(props: { app: any }) {
app = props.app;
log = db.log.bind(db);
await db.initLoki();
await db.initMongoose();
return db;
}