-
Notifications
You must be signed in to change notification settings - Fork 26
/
base-model.js
80 lines (69 loc) · 2.13 KB
/
base-model.js
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
import { Model, transaction } from 'objection';
export class BaseModel extends Model {
$beforeInsert() {
const ts = Date.now();
this.createdAt = ts;
this.updatedAt = ts;
}
$beforeUpdate() {
this.updatedAt = Date.now();
}
isPropertyType(key, type) {
let properties = this.constructor.jsonSchema ? this.constructor.jsonSchema.properties : {};
if (!properties[key]) return false;
let propType = properties[key].type || '';
if (Array.isArray(propType)) return propType.includes(type);
return type === propType;
}
$parseJson(json, opt) {
json = { ...json };
let properties = this.constructor.jsonSchema ? this.constructor.jsonSchema.properties : {};
let relations = this.constructor.relationMappings || {};
Object.keys(json).forEach(key => {
// delete any field not in schema or relations
if (!(key in properties) && !(key in relations)) {
delete json[key];
}
// convert numeric fields to boolean if defined as boolean in schema
if (key in json && this.isPropertyType(key, 'boolean')) {
json[key] = !!json[key];
}
});
return super.$parseJson(json, opt);
}
$parseDatabaseJson(db) {
let json = super.$parseDatabaseJson(db);
for (let prop in json) {
// convert numeric fields in database to boolean if defined as boolean
// in schema
if (this.isPropertyType(prop, 'boolean')) {
json[prop] = !!json[prop];
}
}
return json;
}
static relationMappings() {
return {};
}
static insertMany(records, tx) {
const insertFn = (record, tx) => this.query(tx).insertAndFetch(record);
return this.queryMany(records, insertFn, tx);
}
static updateMany(records, tx) {
const updateFn = (record, tx) =>
this.query(tx).patchAndFetchById(record[this.idColumn], record);
return this.queryMany(records, updateFn, tx);
}
static async queryMany(records, queryFn, externalTx) {
const tx = externalTx || (await transaction.start(this.knex()));
try {
let results = await Promise.all(records.map(r => queryFn(r, tx)));
if (!externalTx) await tx.commit();
return results;
} catch (error) {
if (!externalTx) await tx.rollback();
throw error;
}
}
}
export default BaseModel;