-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb.ts
More file actions
276 lines (221 loc) · 7.12 KB
/
db.ts
File metadata and controls
276 lines (221 loc) · 7.12 KB
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
// arken/node/db.ts
//
// Compile-first version to avoid TS getting stuck in huge Mongoose generic type relations.
// Runtime behavior is preserved; types are intentionally loosened at Mongoose/Query boundaries.
import { ReplaySubject } from 'rxjs';
import Loki from 'lokijs';
// import * as jetpack from 'fs-jetpack';
import mongoose, { ConnectOptions, Schema, Document, Types, Connection } from 'mongoose';
import { Model } from './mongo';
import safeStringify from 'fast-safe-stringify';
import fsPath from 'path';
import { v4 as uuidv4 } from 'uuid';
import _ from 'lodash';
export function isPostgresError(error: unknown): boolean {
if (!error) return false;
return _.every(['severity', 'code', 'detail', 'internalQuery', 'routine'], (attr) => _.has(error as object, attr));
}
export function isUniqueConstraintViolation(error: any) {
return isPostgresError(error) && (error as any).code === '23505';
}
export function generateLongId(): string {
return uuidv4().toUpperCase();
}
export function generateShortId() {
const timestamp = ((new Date().getTime() / 1000) | 0).toString(16);
return timestamp + 'xxxxxxxxxxxxxxxx'.replace(/[x]/g, () => ((Math.random() * 16) | 0).toString(16)).toLowerCase();
}
export const uuidFormat = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx';
export function generateLongId2(): string {
return uuidFormat
.replace(/[xy]/g, (c) => {
const r = (Math.random() * 16) | 0;
const v = c === 'x' ? r : (r & 0x3) | 0x8;
return v.toString(16);
})
.toUpperCase();
}
export function decodeRequest(data: any): any {
if (
data === undefined ||
data === null ||
typeof data === 'string' ||
typeof data === 'number' ||
typeof data === 'boolean'
) {
return data;
}
if (Array.isArray(data)) return data.map(decodeRequest);
if (typeof data !== 'object') return data;
const res: any = {};
for (const key in data) {
if (key === 'set') {
return data[key];
} else if (['create', 'connectOrCreate', 'upsert'].includes(key)) {
return decodeRequest(data[key]);
} else {
res[key] = decodeRequest(data[key]);
}
}
return Object.keys(res).length === 0 ? null : res;
}
export function escapeStringRegexp(string: string): string {
if (typeof string !== 'string') throw new TypeError('Expected a string');
return string.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
}
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 as any)[prop]) && !((target as any)[prop] instanceof Promise)) {
return createNestedProxy((target as any)[prop]);
}
return (target as any)[prop];
} else {
(target as any)[prop] = {};
return createNestedProxy((target as any)[prop]);
}
},
});
class Database {
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<any>>,
};
constructor() {}
log(...msgs: any[]) {
this.channel.log.next(msgs);
}
Schema = mongoose.Schema;
schema: Record<string, Schema> = {};
async initLoki() {
this.loki = new Loki(null as any, {
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,
ssl: false,
tls: false,
serverSelectionTimeoutMS: 5000,
directConnection: true,
} as any as ConnectOptions)
.then((conn) => conn.connection);
process.on('SIGINT', async () => {
try {
// @ts-ignore
if ((this.mongoose as any)?.connection) {
// @ts-ignore
await (this.mongoose as any).connection.close();
}
} catch {}
console.log('Mongoose connection disconnected through app termination');
process.exit(0);
});
}
model<T extends Document = Document>(key: string, schema?: Schema): Model<T> | undefined {
if (schema) {
this.data.model[key] = new Model<any>(mongoose.model(key, schema, key) as any);
}
if (!this.data.model[key]) {
log?.(`DB Model not found: ${key}`);
}
return this.data.model[key] as Model<T>;
}
initCollection(name: string, key: string, data: Record<string, any>) {
if (!this.collections[name]) (this.collections as any)[name] = {};
const colGroup = (this.collections as any)[name];
if (!colGroup[key]) {
colGroup[key] = this.loki!.addCollection(`${name}.${key}`);
}
if (key === 'config') {
delete (data as any).meta;
delete (data as any).$loki;
if (!colGroup[key].length) {
colGroup[key].insert(data);
}
for (const k in data) {
if (colGroup[key].data[0][k] === undefined && (data as any)[k] !== undefined) {
colGroup[key].data[0][k] = (data as any)[k];
}
}
for (const k in colGroup[key].data[0]) {
if (Object.prototype.hasOwnProperty.call(colGroup[key].data[0], k)) {
if (colGroup[key][k] === undefined) {
Object.defineProperty(colGroup[key], k, {
get() {
return colGroup[key].data[0][k];
},
set(x: any) {
colGroup[key].data[0][k] = x;
},
});
}
}
}
} else {
for (const i in data as any) {
const item = (data as any)[i];
delete item.meta;
delete item.$loki;
if (!colGroup[key].data.length) {
colGroup[key].insert(item);
}
for (const k in item) {
if (typeof colGroup[key].data[i] === 'undefined') colGroup[key].data[i] = {};
if (colGroup[key].data[i][k] === undefined && item[k] !== undefined) {
colGroup[key].data[i][k] = item[k];
}
}
}
}
colGroup[key].ensureId();
colGroup[key].ensureAllIndexes(true);
}
initCollections(name: string, data: Record<string, any>) {
log?.(`Adding collection: ${name}`);
for (const key in data) {
this.initCollection(name, key, (data as any)[key]);
}
return (this.collections as any)[name];
}
getCollections(name: string) {
return (this.collections as any)[name];
}
saveData() {
log?.('Saving data', ['p1']);
const data: Record<string, any> = {};
void data;
}
restoreData() {
log?.('Restoring data', ['p1']);
}
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;
}