-
Notifications
You must be signed in to change notification settings - Fork 4
/
model.js
1179 lines (998 loc) · 30.9 KB
/
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
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
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable import/no-dynamic-require, global-require */
import path from 'path';
import _ from 'lodash';
import {Model, AjvValidator} from 'objection';
import DataLoader from 'dataloader';
import {Cache, RedisCache} from 'sm-utils';
import BaseQueryBuilder from './query_builder';
import {plural} from './utils';
import {customUserError} from './user_error';
let LocalRedisCache = RedisCache;
const httpUrlPattern = new RegExp(
'^(https?:\\/\\/)?' + // protocol
'((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.?)+[a-z]{2,}|' + // domain name
'((\\d{1,3}\\.){3}\\d{1,3}))' + // OR ip (v4) address
'(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*' + // port and path
'(\\?[;&a-z\\d%_.~+=-]*)?' + // query string
'(\\#[-a-z\\d_]*)?$', 'i' // fragment locator
);
function mapResults(results, keys, columnName) {
const resultHash = {};
const mappedResults = [];
if (Array.isArray(columnName)) {
if (columnName.length === 1) {
columnName = columnName[0];
}
else {
for (const key of keys) {
const found = results.find(result => (
// we need == here because result can either return a string or a number
// eslint-disable-next-line eqeqeq
columnName.every((k, i) => result[k] == key[i])
));
mappedResults.push(found || null);
}
return mappedResults;
}
}
for (const result of results) {
resultHash[result[columnName]] = result;
}
for (const key of keys) {
mappedResults.push(resultHash[key] || null);
}
return mappedResults;
}
function mapManyResults(results, keys, columnName) {
const resultHash = {};
const mappedResults = [];
for (const result of results) {
const column = result[columnName];
resultHash[column] = resultHash[column] || [];
resultHash[column].push(result);
}
for (const key of keys) {
mappedResults.push(resultHash[key] || []);
}
return mappedResults;
}
async function handleResult(obj, options) {
if (!obj) {
if (!options.default) {
return null;
}
options.isDefault = true;
if (typeof options.default === 'function') {
obj = await options.default(options.args);
}
else {
obj = await options.default;
}
}
if (Array.isArray(obj)) {
return Promise.all(obj.map(item => handleResult(item, options)));
}
if (obj.afterResolve) {
return obj.afterResolve(options);
}
return obj;
}
/**
* @template T
* @template X
* @param {T[]} values
* @param {Object} [options]
* @param {(vals: T[]) => Promise<X[]>} [options.fn]
* @param {number} [options.limit]
* @param {number} [options.offset] default is 0
* @param {boolean} [options.nonNull] default is false
* @returns {Promise<X[]>}
*/
async function limitFilter(values, options = {}) {
const {limit, offset = 0, nonNull = false, fn = v => v} = options;
if (limit === 0 || offset >= values.length) return [];
if (!limit || limit >= values.length) {
let results = (await fn(values)) || [];
if (nonNull) results = results.filter(val => val != null);
return results;
}
let results = (await fn(values.slice(offset, limit))) || [];
if (nonNull) results = results.filter(val => val != null);
if (results.length >= limit) return results;
const extraResults = await limitFilter(values, {
fn,
limit: limit - results.length,
offset: offset + limit,
nonNull,
});
return results.concat(extraResults);
}
let globalLoaderContext = {};
/**
* Base class that all of our models will extend
* This has few extra utilities over the Objection Model
* 1. Automatic table names
* 2. Automatic timestamps
* 3. Soft Deletes
* 4. scopes (define as static scopes = {default(builder) {}, something(builder) {}, ...})
*/
class BaseModel extends Model {
static useLimitInFirst = true;
/**
* timestamps can be true, false or an object of {createdAt, updatedAt}
* if true, createdAt and updatedAt columns will automatically be updated
* you can change column names using an object
* eg. `{createdAt: 'add_time', updatedAt: 'modify_time'}`
* if you omit one column in the object, that column won't be touched at all
* eg. if you don't want updatedAt => `timestamps = {createdAt: 'createdAt'}`
*/
static timestamps = true;
static softDelete = false;
static basePath = '';
static dataLoaders = {};
static get Error() {
if (!this._Error) this._Error = customUserError(this.name);
return this._Error;
}
/**
* this can be false or an object of {
* ttl (in ms or timestring),
* columns: [] (include only these columns while caching)
* excludeColumns: [] (exclude these columns while caching)
* maxLocalItems: max items in the local cache of redis (using an lru cache)
* }
* if this is an object, all items accessed with loadById are cached for ttl duration
*/
static cacheById = false;
static createValidator() {
return new AjvValidator({
onCreateAjv: (ajv) => {
// Here you can modify the `Ajv` instance.
ajv.addFormat('url', httpUrlPattern);
},
options: {
allErrors: true,
validateSchema: false,
ownProperties: true,
v5: true,
},
});
}
static get timestampColumns() {
if (!this.__timestampColumns) {
this.timestampColumns = [];
}
return this.__timestampColumns;
}
// set the RedisCache class used internally by xorm
static setRedisCacheClass(redisCacheClass) {
LocalRedisCache = redisCacheClass;
if (this._redisCachePrefix) {
this.setRedisCacheGlobalPrefix(this._redisCachePrefix);
}
}
// set global prefix for the rediscache used internally by xorm
static setRedisCacheGlobalPrefix(prefix = 'a') {
this._redisCachePrefix = prefix;
LocalRedisCache.globalPrefix = prefix;
}
// define which columns will be converted to Date object when parsing this model
static set timestampColumns(columns) {
if (this.timestamps) {
if (this.timestamps === true) {
columns.push('createdAt');
columns.push('updatedAt');
}
else {
if (this.timestamps.createdAt) {
columns.push(this.timestamps.createdAt);
}
if (this.timestamps.updatedAt) {
columns.push(this.timestamps.updatedAt);
}
}
}
if (this.softDelete) {
columns.push(this.softDeleteColumn);
}
this.__timestampColumns = _.uniq(columns);
}
$parseJson(json, opt) {
json = super.$parseJson(json, opt);
this.constructor.timestampColumns.forEach((column) => {
if (json[column] && !(json[column] instanceof Date)) {
json[column] = new Date(json[column]);
}
});
return json;
}
get $cache() {
if (!this.__cache) {
this.__cache = new Cache();
}
return this.__cache;
}
static get cache() {
if (!this.__cache) {
this.__cache = new Cache();
}
return this.__cache;
}
static get idRedisCache() {
if (!this.__idRedisCache) {
const maxLocalItems = this.cacheById && this.cacheById.maxLocalItems;
this.__idRedisCache = new LocalRedisCache(`xorm:${this.name}:id`, {
maxLocalItems,
});
}
return this.__idRedisCache;
}
static get redisCache() {
if (!this.__redisCache) {
this.__redisCache = new LocalRedisCache(`xorm:${this.name}`);
}
return this.__redisCache;
}
// set the default context for the dataloader used by loader functions
static setGlobalLoaderContext(ctx) {
globalLoaderContext = ctx;
}
/**
* @typedef {object} loaderOpts
* @property {boolean} [ignoreResults=false] ignore the results returned by the loader function
* [default false]
* @property {string} [mapBy='id'] map results returned by the loaderFn using this key
* [default 'id']
* @property {boolean} [cache=false] cache the results returned by loaderFn indefinitely
* [default false]
* @property {boolean|function} [filterKeys=true] filter the falsy keys before calling loaderFn
* can also be a function, [default true]
*/
/**
* make a loader so you can use it to batch queries which are not covered by existing loaders
* @param {string} loaderName
* @param {function} loaderFunc
* @param {loaderOpts} [options={}]
* @returns {DataLoader}
*/
static makeLoader(loaderName, loaderFunc, options = {}) {
const loaderKey = `${this.tableName}Custom${loaderName}DataLoader`;
if (globalLoaderContext[loaderKey]) return globalLoaderContext[loaderKey];
const opts = Object.assign({
ignoreResults: false,
filterKeys: true,
mapBy: 'id',
cache: false,
}, options);
globalLoaderContext[loaderKey] = new DataLoader(async (keys) => {
if (!keys.length) return [];
let uniqFunc;
if (Array.isArray(keys[0])) {
uniqFunc = item => item.join('-');
}
let filteredKeys;
if (typeof opts.filterKeys === 'function') {
// this is for avoiding un-necessary queries where the value is 0 or null
filteredKeys = _.uniqBy(keys.filter(opts.filterKeys), uniqFunc);
}
else if (opts.filterKeys) {
filteredKeys = _.uniqBy(keys.filter(key => (key && key !== '0')), uniqFunc);
}
else {
filteredKeys = keys;
}
let results = [];
if (filteredKeys.length) {
results = await loaderFunc(filteredKeys);
}
// since we don't need any results, we just return
// an array of null so that dataloader doesn't complain
if (opts.ignoreResults) {
return _.fill(Array(keys.length), null);
}
if (opts.mapBy) {
return mapResults(results, keys, opts.mapBy);
}
return results;
}, {cache: opts.cache});
return globalLoaderContext[loaderKey];
}
static _getLoaderCtx(options) {
const ctx = options.ctx;
const knex = options.knex;
let loaderCtx;
if (!ctx) {
if (knex) {
// we can't use global loader
// we need to incorporate knex
if (!knex._xorm_ctx) {
knex._xorm_ctx = {};
}
loaderCtx = knex._xorm_ctx;
}
else {
// we can use global ctx
loaderCtx = globalLoaderContext;
}
}
else if (knex) {
if (!ctx._xorm_ctx) {
ctx._xorm_ctx = new WeakMap();
}
loaderCtx = ctx._xorm_ctx.get(knex);
if (!loaderCtx) {
loaderCtx = {};
ctx._xorm_ctx.set(knex, loaderCtx);
}
}
else {
loaderCtx = ctx;
}
return loaderCtx;
}
/**
* get the loader for a specific column name
* @param {string|Array<string>} columnName
* @param {object} options
* @param {object|null} options.ctx context for the dataloader [optional]
* @param {object|null} options.knex knex instance for this dataloader [optional]
* @returns {DataLoader}
*/
static getLoader(columnName, options = {}) {
const knex = options.knex;
let loaderName;
if (Array.isArray(columnName)) {
if (columnName.length === 1) {
columnName = columnName[0];
}
else {
loaderName = `${this.tableName}${columnName.join('-')}DataLoader`;
}
}
if (!loaderName) {
loaderName = `${this.tableName}${columnName}DataLoader`;
}
const cache = !!options.ctx;
const ctx = this._getLoaderCtx(options);
if (ctx[loaderName]) return ctx[loaderName];
ctx[loaderName] = new DataLoader(async (keys) => {
let uniqFunc;
if (Array.isArray(columnName)) {
uniqFunc = item => item.join('-');
}
// this is for avoiding un-necessary queries where the value is 0 or null
const filteredKeys = _.uniqBy(keys.filter(key => (key && key !== '0')), uniqFunc);
let results = [];
if (filteredKeys.length) {
if (Array.isArray(columnName)) {
results = await this.query(knex).whereInComposite(columnName, filteredKeys);
}
else {
results = await this.query(knex).whereIn(columnName, filteredKeys);
}
}
return mapResults(results, keys, columnName);
}, {cache});
return ctx[loaderName];
}
/**
* get loader for one to many columns
* @param {string} columnName
* @param {object} options
* @param {any} options.modify
* @param {object|null} options.ctx
* @param {object|null} options.knex
* @returns {DataLoader}
*/
static getManyLoader(columnName, options = {}) {
const knex = options.knex;
const cache = !!options.ctx;
const ctx = this._getLoaderCtx(options);
let loaderName = `${this.tableName}${columnName}DataLoader`;
if (options.modify) {
if (_.isPlainObject(options.modify)) {
loaderName += JSON.stringify(options.modify);
}
else {
loaderName += String(options.modify);
}
}
if (ctx[loaderName]) return ctx[loaderName];
ctx[loaderName] = new DataLoader(async (keys) => {
// this is for avoiding un-necessary queries where the value is 0 or null
const filteredKeys = _.uniq(keys.filter(key => (key && key !== '0')));
let results = [];
if (filteredKeys.length) {
const query = this.query(knex).whereIn(columnName, filteredKeys);
if (options.modify) {
if (_.isPlainObject(options.modify)) {
query.where(options.modify);
}
else {
query.modify(options.modify);
}
}
results = await query;
}
return mapManyResults(results, keys, columnName);
}, {cache});
return ctx[loaderName];
}
/**
* @param {string} relationName
* @param {object} [options={}]
* @param {string} options.ownerCol
* @param {object|null} options.ctx
* @param {object|null} options.knex
* @returns {DataLoader}
*/
static getRelationLoader(relationName, options = {}) {
const knex = options.knex;
const cache = !!options.ctx;
const ctx = this._getLoaderCtx(options);
const loaderName = `${this.tableName}Rel${relationName}DataLoader`;
if (ctx[loaderName]) return ctx[loaderName];
ctx[loaderName] = new DataLoader(async (keys) => {
const objs = keys.map((key) => {
const obj = new this();
obj[options.ownerCol || this.idColumn] = key;
return obj;
});
const query = this.loadRelated(objs, relationName, undefined, knex);
const results = await query;
return results.map(result => result[relationName]);
}, {cache});
return ctx[loaderName];
}
static getIdLoader(options = {}) {
return this.getLoader(this.idColumn, options);
}
/**
* load result by column, using dataloader
* @param {string|array} columnName can be a string or an array of strings for composite loading
* @param {any} columnValue can be single or an array
* @param {object} options object of {
* ctx: context for the dataloader [optional / default null]
* nonNull: only return nonNull results [default false]
* limit: only return this many results [default null => return as many results as possible]
* offset: in conjunction with limit [default 0]
* knex: use this particular knex instance
* }
*/
static async _loadByColumn(columnName, columnValue, options = {}) {
if (!columnValue || columnValue === '0') return null;
if (Array.isArray(columnValue) && !columnValue.length) return [];
let manyLoader = false;
if (Array.isArray(columnName)) {
// many loader in case of composite columns, eg. [a, b] in [[1,2], [3,4]]
if (Array.isArray(columnValue[0])) {
manyLoader = true;
}
}
else if (Array.isArray(columnValue)) {
// many loader in case of normal columns, eg. a in [1, 2, 3, 4]
manyLoader = true;
}
if (!manyLoader) {
return this.getLoader(columnName, options).load(columnValue);
}
if (options.nonNull) {
columnValue = columnValue.filter(val => (val && val !== '0'));
}
else {
// change falsy values to false
// otherwise dataloader creates problems (does not accept null)
columnValue = columnValue.map(val => val || false);
}
const loader = this.getLoader(columnName, options);
return limitFilter(
columnValue,
{
...options,
fn: values => loader.loadMany(values),
},
);
}
/**
* load result by column, using dataloader
* @param {string|array} columnName can be a string or an array of strings for composite loading
* @param {any} columnValue can be single or an array
* @param {object} options object of {
* ctx: context for the dataloader [optional / default null]
* nonNull: only return nonNull results [default false]
* limit: only return this many results [default null => return as many results as possible]
* offset: in conjunction with limit [default 0]
* knex: use this particular knex instance
* }
*/
static loadByColumn(columnName, columnValue, options = {}) {
// separate loadById and loadByColumn, in case we override loadById
if (columnName === this.idColumn) {
return this.loadById(columnValue, options);
}
return this._loadByColumn(columnName, columnValue, options);
}
static fromJsonSimple(json) {
if (!json) return null;
return this.fromJson(json, {
skipValidation: true,
skipParseRelations: true,
});
}
/**
* load result by id, using dataloader
* @param {any} id can be single or an array
* @param {object} options object of {
* ctx: context for the dataloader [optional / default null]
* nonNull: only return nonNull results [default false]
* limit: only return this many results [default null => return as many results as possible]
* offset: in conjunction with limit [default 0]
* knex: use this particular knex instance
* }
*/
static loadById(id, options = {}) {
if (!this.cacheById || options.knex) {
return this._loadByColumn(this.idColumn, id, options);
}
const ttl = this.cacheById.ttl || '1d';
const parse = this.fromJsonSimple.bind(this);
const singleItem = async (idx) => {
let item = await this._loadByColumn(this.idColumn, idx, options);
if (!item) return null;
if (this.cacheById.columns) {
item = _.pick(item, this.cacheById.columns);
}
if (this.cacheById.excludeColumns) {
item = _.omit(item, this.cacheById.excludeColumns);
}
return item;
};
if (Array.isArray(id)) {
if (!id.length) return [];
return limitFilter(
id,
{
...options,
fn: ids => Promise.map(ids, idx => (
this.idRedisCache.getOrSet(
String(idx),
() => singleItem(idx),
{ttl, parse},
)
)),
}
);
}
if (!id || id === '0') return null;
return this.idRedisCache.getOrSet(
String(id),
() => singleItem(id),
{ttl, parse},
);
}
/**
* load many results for a single value
* @param {string} columnName
* @param {any} columnValue
* @param {object} options
* @param {any} options.modify
* @param {object|null} options.ctx
* @param {object|null} options.knex
* @returns {Array}
*/
static loadManyByColumn(columnName, columnValue, options = {}) {
if (!columnValue || columnValue === '0') return null;
if (Array.isArray(columnValue)) {
// change falsy values to false, otherwise dataloader creates problems (does not accept null)
columnValue = columnValue.map(val => val || false);
return this.getManyLoader(columnName, options).loadMany(columnValue);
}
return this.getManyLoader(columnName, options).load(columnValue);
}
static deleteCacheById(id) {
return this.idRedisCache.del(String(id));
}
// base path for requiring models in relations
static setBasePath(basePath) {
this.basePath = basePath;
}
static get softDeleteColumn() {
if (_.isString(this.softDelete)) {
return this.softDelete;
}
return 'deletedAt';
}
static get systemColumns() {
if (!this._systemColumns) {
const columns = [];
// timestamps (createdAt + updatedAt handling)
if (this.timestamps) {
if (this.timestamps === true) {
columns.push('createdAt');
columns.push('updatedAt');
}
else {
if (this.timestamps.createdAt) {
columns.push(this.timestamps.createdAt);
}
if (this.timestamps.updatedAt) {
columns.push(this.timestamps.updatedAt);
}
}
}
if (this.softDelete) {
columns.push(this.softDeleteColumn);
}
this._systemColumns = columns;
}
return this._systemColumns;
}
static set systemColumns(columns) {
this._systemColumns = columns;
}
static get tableName() {
this._tableName = this._tableName || this.name;
return this._tableName;
}
static set tableName(table) {
this._tableName = table;
}
static get relationMappings() {
if (this._relationMappings) return this._relationMappings;
// generate relation mappings
this._relationMappings = {};
this.$relations();
return this._relationMappings;
}
static set relationMappings(mappings) {
this._relationMappings = mappings;
}
static $relations() {}
static where(...args) {
return this.query().where(...args);
}
static find(...args) {
return this.query().find(...args);
}
static getFindOneResolver(options = {}) {
return (async (root, args) => {
if (args[this.idColumn]) {
return this.loadById(args[this.idColumn], options);
}
const keys = Object.keys(args);
if (!keys.length) return null;
if (keys.length === 1) {
return this.loadByColumn(keys[0], args[keys[0]], options);
}
const query = this.query();
keys.forEach((key) => {
query.where(key, args[key]);
});
return query.first();
});
}
/**
* beforeResolve can be used to return a modified item to use for resolving
* called before resolving (using graphql) a particular item
* @param {Object} options options is {args} args = arguments passed from graphql query
* @return {Object} a xorm model object
*/
beforeResolve(args) { // eslint-disable-line
return this;
}
/**
* afterResolve can be used to modify the item we got from the resolver
* called after resolving (using graphql) a particular item
* @param {Object} options options is {args, default, isDefault}
* args = graphql query args
* isDefault = true if default value is returned
* @return {Object} a xorm model object
*/
afterResolve(args) { // eslint-disable-line
return this;
}
async loadByRelation(relationName, options = {}) {
const relation = this.constructor.getRelation(relationName);
if (!relation) {
throw new Error(`relation ${relationName} is not defined in ${this.constructor.name}`);
}
const relatedCols = (relation.relatedProp && relation.relatedProp.cols) || [];
const ownerCols = (relation.ownerProp && relation.ownerProp.cols) || [];
const args = options.args || {};
let self = this;
if (this.beforeResolve) {
// eslint-disable-next-line consistent-this
self = await this.beforeResolve(options);
}
// Return the object if it is already fetched
if (self[relationName] !== undefined) {
return handleResult(self[relationName], options);
}
// Only pass single column relations through data loader
if (relatedCols.length !== 1 || ownerCols.length !== 1) {
await self.$loadRelated(relationName, undefined, options.knex);
return handleResult(self[relationName], options);
}
if (
relation instanceof Model.BelongsToOneRelation ||
relation instanceof Model.HasOneRelation
) {
self[relationName] = await relation.relatedModelClass
.loadByColumn(relatedCols[0], self[ownerCols[0]], options);
return handleResult(self[relationName], options);
}
if (relation instanceof Model.HasManyRelation) {
const modify = relation.modify;
if (String(modify).indexOf('noop') !== -1) {
self[relationName] = await relation.relatedModelClass
.loadManyByColumn(relatedCols[0], self[ownerCols[0]], options);
return handleResult(self[relationName], options);
}
self[relationName] = await relation.relatedModelClass
.loadManyByColumn(relatedCols[0], self[ownerCols[0]], {
modify,
ctx: options.ctx,
knex: options.knex,
});
return handleResult(self[relationName], options);
}
if (
relation instanceof Model.ManyToManyRelation ||
relation instanceof Model.HasOneThroughRelation
) {
return handleResult(
await this.constructor.getRelationLoader(
relationName,
{
ownerCol: ownerCols[0],
ctx: options.ctx,
knex: options.knex,
},
).load(self[this.constructor.idColumn]),
args
);
}
await self.$loadRelated(relationName, undefined, options.knex);
return handleResult(self[relationName], args);
}
static getRelationResolver(relationName, options = {}) {
const relation = this.getRelation(relationName);
if (!relation) {
throw new Error(`relation ${relationName} is not defined in ${this.name}`);
}
if (relation.relatedModelClass.selfRelationResolver) {
return relation.relatedModelClass.selfRelationResolver(relation, options);
}
return (async (obj, args) => {
options.args = args;
return obj.loadByRelation(relationName, options);
});
}
static getFindByIdSubResolver(propName, options = {}) {
if (!propName) propName = `${_.camelCase(this.name)}Id`;
return (async obj => this.loadById(obj[propName], options));
}
static getDeleteByIdResolver() {
return (async (root, obj) => this.query().deleteById(obj[this.idColumn])
.then(() => ({id: obj[this.idColumn]})));
}
$beforeInsert(context) {
super.$beforeInsert(context);
const timestamps = this.constructor.timestamps;
if (timestamps && !context.dontTouch) {
if (timestamps === true) {
this.createdAt = new Date().toISOString();
this.updatedAt = new Date().toISOString();
}
else {
if (timestamps.createdAt) {
this[timestamps.createdAt] = new Date().toISOString();
}
if (timestamps.updatedAt) {
this[timestamps.updatedAt] = new Date().toISOString();
}
}
}
}
$beforeUpdate(opt, context) {
super.$beforeUpdate(opt, context);
const timestamps = this.constructor.timestamps;
if (timestamps && !context.dontTouch) {
if (timestamps === true) {
this.updatedAt = new Date().toISOString();
}
else if (timestamps.updatedAt) {
this[timestamps.updatedAt] = new Date().toISOString();
}
}
}
$beforeDelete(context) {
super.$beforeDelete(context);
}
async $afterUpdate(opts, queryContext) {
await super.$afterUpdate(opts, queryContext);
if (!this.constructor.cacheById) return;
const id = queryContext.id || this.id;
if (id) {
await this.constructor.deleteCacheById(id);
}
}
async $afterDelete(queryContext) {
await super.$afterDelete(queryContext);
if (!this.constructor.cacheById) return;
const id = queryContext.id || this.id;
if (id) {
await this.constructor.deleteCacheById(id);
}
}
static getJsonSchema() {
// Memoize the jsonSchema but only for this class. The hasOwnProperty check
// will fail for subclasses and the value gets recreated.
// eslint-disable-next-line
if (!this.hasOwnProperty('$$jsonSchema')) {
// this.jsonSchema is often a getter that returns a new object each time. We need
// memoize it to make sure we get the same instance each time.
const jsonSchema = this.jsonSchema;
if (jsonSchema && jsonSchema.properties) {
const columns = this.systemColumns || [];
columns.forEach((column) => {
jsonSchema.properties[column] = {};
});
}
Object.defineProperty(this, '$$jsonSchema', {
enumerable: false,
writable: true,
configurable: true,
value: jsonSchema,
});
}