-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.ts
executable file
·413 lines (365 loc) · 10.2 KB
/
model.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
import Debug from 'debug';
import { Builder } from './builder';
import { Dict, FindOpts, InsertOpts, ModelOpts } from './interface';
import { isEmpty, pick, has } from 'ramda';
import { Database, ISqlite } from 'sqlite';
import { TimestampSchema, ColumnSchema } from './schema';
const debug = Debug('lite-orm:model');
export class Model {
private _db: Database;
public _table: string;
private _pk: string[];
readonly _options: any;
private _attributes: Dict;
[key: string]: any;
constructor(options?: ModelOpts) {
this._attributes = {};
this._pk = [];
this._options = options || {};
this.initialize();
}
get schema() {
return Reflect.getMetadata('model:schema', this);
}
get indices() {
return Reflect.getMetadata('model:indices', this);
}
get options() {
return this._options;
}
get table(): string {
return this._table;
}
get db() {
return this._db;
}
initialize(): Model {
const schema = this.schema;
this._pk = Object.entries(schema)
.filter((item) => {
const v = item[1] as ColumnSchema;
return !!v.pk;
})
.map((item) => {
const [k] = item;
return k;
});
if (this.options?.timestamp) {
Reflect.defineMetadata(
'model:schema',
{ ...schema, ...TimestampSchema },
this,
);
}
if (this.options?.db) {
this._db = this.options.db;
}
return this;
}
async connect() {
if (typeof this.db === 'function') {
this._db = await Promise.resolve((this.db as Function)());
} else if (this.db instanceof Promise) {
this._db = await Promise.resolve(this.db);
}
return this.db;
}
getColumn(field: string): ColumnSchema | undefined {
return this.schema[field];
}
encode<T>(filed: string, value: any): T {
if (value === undefined) {
return this.schema[filed]?.default;
}
const column = this.getColumn(filed);
if (column?.encode) {
return column.encode(value);
}
return value;
}
decode<T>(filed: string, value: any): T {
if (value === undefined) {
return value;
}
const column = this.getColumn(filed);
if (column?.decode) {
return column.decode(value);
}
return value;
}
getAttrs(): Dict {
const schema = this.schema;
const data: Record<string, any> = {};
Object.keys(schema).forEach((k: string) => {
if (has(k, this)) {
data[k] = this[k];
}
});
return { ...this._attributes, ...data };
}
getAttr(name: string): any {
const data = this.getAttrs();
return this.decode(name, data[name]);
}
setAttr(name: string, value: any): void {
const schema = this.schema;
if (name in schema) {
this[name] = value;
this._attributes[name] = value;
}
}
purify(data: Dict): Dict {
return Object.entries(data).reduce((acc: Dict, [key, val]) => {
return { ...acc, [key]: this.encode(key, val) };
}, {});
}
toRowData(props: Dict) {
const schema = this.schema;
return Object.entries(props).reduce((acc, cur) => {
const [k, v]: [string, any] = cur;
const col: ColumnSchema = schema[k];
if (col && k in props) {
acc[col.name || k] = v;
} else {
acc[k] = v;
}
return acc;
}, {} as Record<string, any>);
}
toProps(row: Dict) {
const fieldPairs = Object.entries(this.schema);
return Object.entries(row).reduce((acc, cur) => {
const [k, v]: [string, any] = cur;
const item = fieldPairs.find(([key, col]: [string, any]) => {
return col.name === k || key === k;
});
if (item) {
acc[item[0]] = v;
} else {
acc[k] = v;
}
return acc;
}, {} as Record<string, any>);
}
clone(): Model {
return new (this.constructor as new () => this)();
}
instance(row: Dict): Model {
const instance = this.clone();
const data = this.toProps(row);
for (const key in data) {
const column = this.schema[key];
const val = column?.decode ? column.decode(data[key]) : data[key];
instance.setAttr(key, val);
}
return instance;
}
toObject(): Dict {
return this.getAttrs();
}
toJSON(): Dict {
return this.toObject();
}
async exec(sql: string): Promise<void> {
await this.connect();
return this.db?.exec(sql);
}
async call(
method: string,
sql: string,
params: Record<string, any>,
): Promise<any> {
await this.connect();
if (this.options?.debug) {
debug('[sql]: %s, [params]: %j', sql, params);
}
const stmt: any = await this.db.prepare(sql);
return stmt[method](params);
}
async insert(payload: Dict, options?: InsertOpts): Promise<Model | number> {
const builder = new Builder({});
const data = this.toRowData(this.purify({ ...payload }));
const defaultData = this.toRowData(this.defaultData());
const { sql, params } = builder
.table(this.table)
.insert({ ...defaultData, ...data });
const res = await this.call('run', sql, params);
const { lastID } = res;
if (this.options?.onInsert) {
await Promise.resolve(this.options.onInsert);
}
if (options?.lastId) {
return lastID;
}
return (await this.findById(lastID)) as Model;
}
async create(data: Dict, options?: InsertOpts): Promise<Model | number> {
return this.insert(data, options);
}
async update(where: Dict, payload: Dict): Promise<ISqlite.RunResult> {
const builder = new Builder({});
const data = this.purify(payload);
console.log('data', data);
const witTimestamp = this.attachTimestamp(data);
console.log(witTimestamp);
const row = this.toRowData(witTimestamp);
console.log('123123', row, where);
const { sql, params } = builder
.table(this.table)
.where(where)
.update({ ...row });
console.log(sql, params);
const res = await this.call('run', sql, params);
await this.onChange(row);
return res;
}
updateAttributes(payload: Dict): Promise<Model> {
if (!this._pk) {
throw new Error('updateAttributes must be called on instance');
}
const current = this._attributes;
Object.entries(payload).map((item) => {
const [key, value] = item;
if (has(key, current)) {
this[key] = value;
}
});
return this.save();
}
async upsert(data: Dict): Promise<Model> {
if (data.id) {
const record = await this.findById(data.id);
if (record) {
return record.updateAttributes(data);
}
}
return (await this.insert(data)) as Model;
}
async find(where: Dict = {}, options: FindOpts = {}): Promise<Model[]> {
const { limit, offset, order, fields, group } = options;
const builder = new Builder({});
const { sql, params } = builder
.table(this.table)
.where(this.toRowData(where))
.fields(fields)
.order(order)
.group(group)
.limit(limit)
.offset(offset)
.select();
const res = await this.call('all', sql, params);
if (options.rows) {
return res.map((row: Dict) => this.toProps(row));
}
return res.map((item: Dict) => {
return this.instance(item);
});
}
async count(where: Dict): Promise<number> {
const res = (await this.findOne(where, {
fields: ['count(*) as count'],
rows: true,
})) as Model;
return Number(res.count);
}
async findOne(where: Dict, options: FindOpts = {}): Promise<Model | null> {
const res = await this.find(where, { ...options, limit: 1 });
if (res.length) {
return res[0];
}
return null;
}
findAll(options?: FindOpts): Promise<Model[]> {
return this.find({}, options);
}
findById(id: number | string, options?: FindOpts): Promise<Model | null> {
return this.findOne({ id }, options);
}
findByIds(ids: number[], options?: FindOpts): Promise<Model[]> {
return this.find({ id: { $in: ids } }, options);
}
defaultData(): Dict {
const payload: Dict = {};
Object.entries(this.schema).map(([k, col]: [string, any]) => {
const def = col.default;
if (typeof def === 'undefined') {
return;
}
payload[k] = typeof def === 'function' ? def() : def;
});
return payload;
}
async onChange(changed: Dict): Promise<void> {
for (const k in changed) {
const col: ColumnSchema = this.schema[k];
if (!col) {
continue;
}
if (!col.onChange) {
continue;
}
await Promise.resolve(col.onChange.call(this, changed[k]));
}
}
getChange() {
const res = Object.entries(this._attributes).reduce(
(acc: Dict, cur: any[]) => {
const [k, v] = cur;
if (this[k] !== v) {
acc[k] = this[k];
}
return acc;
},
{},
);
if (this.options?.timestamp && Object.keys(res).length) {
return this.attachTimestamp(res);
}
return res;
}
attachTimestamp(data: Dict) {
if (!this.options?.timestamp) {
return data;
}
return { ...data, updatedAt: new Date().toISOString() };
}
async save(): Promise<Model> {
const pk = pick(this._pk, this.getAttrs());
if (!this._pk || isEmpty(pk)) {
throw new Error('save must be called on instance');
}
const changed = this.getChange();
if (!Object.keys(changed).length) {
return this;
}
console.log(this._pk, changed);
await this.update(pk, changed);
return (await this.findOne(pk)) as Model;
}
async remove(): Promise<ISqlite.RunResult> {
const pk = pick(this._pk, this._attributes);
if (!this._pk || isEmpty(pk)) {
throw new Error('save must be called on instance');
}
const res = await this.delete(pk);
if (this.options?.onRemove) {
await Promise.resolve(this.options.onRemove);
}
return res;
}
async deleteById(id: number): Promise<boolean> {
const record = this.findById(id);
if (!record) {
return false;
}
const builder = new Builder({});
const { sql, params } = builder.table(this.table).where({ id }).delete();
await this.call('run', sql, params);
return true;
}
async delete(where: Dict): Promise<ISqlite.RunResult> {
const builder = new Builder({});
const { sql, params } = builder.table(this.table).where(where).delete();
return await this.call('run', sql, params);
}
}