-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.js
935 lines (804 loc) · 26.9 KB
/
table.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
// server/models/table.js
let knex;
const Utils = require('codemaster').utils;
const ChinchayError = require('./chinchayError');
class Table {
constructor(tableName, givenKnex) {
this.table_name = tableName;
this.knex = givenKnex || knex;
if (!this.knex) throw new Error('Knex param missing.');
this.INSERT_LIMIT_ARRAY = 1000;
}
toString() {
return this.table_name;
}
table() {
return this.knex(this.table_name);
}
setKnex(elem) {
this.knex = elem;
}
getKnex() {
return this.knex;
}
// ################################################
// COUNT
// ################################################
count(search, options) {
const f = async (query, groupBy) => {
const results = await Table.fetchQuery(query);
if (!groupBy) {
return Object.keys(results[0]).length === 1 ? results[0].count : results[0];
}
for (let i = 0; i < results.length; i++) {
if (results[i].count) {
results[i].count = parseInt(results[i].count, 10);
}
}
return results;
};
options = options || {};
const { groupBy } = options;
const query = this.countQuery(search, options);
if (Table.returnAsQuery(options)) return query;
return f(query, groupBy);
}
countQuery(search, options) {
const shouldCountAsDistinct = options && options.countDistinct && !options.groupBy;
if (shouldCountAsDistinct) return this.countDistinctQuery(search, options);
return this.buildQuery('count', search, 'all', options);
}
countDistinctQuery(search, options) {
if (!options || !options.countDistinct) throw new ChinchayError('options.countDistinct is not defined', 'missing_count_distinct');
options.distinct = options.countDistinct;
const subQuery = this.buildQuery('select', search, 'all', options).as('foo');
const query = this.knex.count('*').from(subQuery);
return query;
}
countGroupBy(groupBy, search, options) {
options = options || {};
options.groupBy = groupBy;
return this.count(search, options);
}
countIn(target, validOptions, query, options) {
query = query || {};
if (Array.isArray(validOptions)) query[target] = ['in', validOptions];
else {
query[target] = validOptions;
}
return this.count(query, options);
}
// ################################################
// AGGREGATION FUNCTIONS
// ################################################
sum(column, search, options) {
return this.aggregateQuery('sum', column, search, options);
}
min(column, search, options) {
return this.aggregateQuery('min', column, search, options);
}
max(column, search, options) {
return this.aggregateQuery('max', column, search, options);
}
static async parseAggregationResult(query) {
const result = await Table.fetchQuery(query);
const nonGroupedBy = result.length === 1 && Object.keys(result[0]).length === 1;
if (nonGroupedBy) {
const key = Object.keys(result[0])[0];
const asNumber = parseInt(result[0][key] || 0, 10);
return Number.isNaN(asNumber) ? result[0][key] : asNumber;
}
return result;
}
aggregateQuery(aggregation, column, search, options) {
options = options || {};
options.aggregation = aggregation;
const query = this.buildQuery('aggregate', search, column, options);
if (Table.returnAsQuery(options)) return query;
return Table.parseAggregationResult(query);
}
// ################################################
// DELETE
// ################################################
delete(input, options) {
if (typeof input !== 'object') return this.deleteById(input, options);
const search = input;
const query = this.deleteWhere(search, options);
if (Table.returnAsQuery(options)) return query;
return Table.fetchQuery(query);
}
deleteWhere(search, options) {
const query = this.deleteQuery(search, options);
return query;
}
deleteById(id, options) {
if (!id) {
const err = new ChinchayError(new Error('Provide an id to delete'), 'delete_by_id_no_id', 'Method delete by Id needs an id as input');
if (options && options.returnAsQuery) throw err;
return Promise.reject(err);
}
const search = { id };
const query = this.delete(search, options);
if (Table.returnAsQuery(options)) return query;
return new Promise((resolve, reject) => {
query.then((results) => { resolve(results[0]); }).catch((err) => { reject(err); });
});
}
deleteQuery(search, options) {
return this.buildQuery('delete', search, 'all', options);
}
// ################################################
// NEW
// ################################################
new() {
const f = async () => {
const columns = await this.columnsNames();
const noColumns = !columns || columns.length === 0;
if (noColumns) throw new Error('Hubo un error creando un nuevo Objeto');
return Table.buildEntryFromColumns(columns);
};
return f();
}
// ################################################
// SAVE
// ################################################
// save(originalEntry) {
// const errorString = 'Something went wrong';
// const f = async () => {
// const entry = Utils.cloneJSON(originalEntry);
// const isSave = true;
// const parsed = await this.parseAttributesForUpsert(entry, isSave);
// const query = this.saveQuery(parsed);
// const saved = await Table.fetchQuery(query);
// if (!saved || saved.length === 0) throw new Error(errorString);
// return saved[0];
// };
// return f();
// }
async save(input, options) {
const batchSize = this.extractBatchSize(options);
const copy = Utils.cloneJSON(input);
if (Array.isArray(copy)) return this.saveBunch(copy, batchSize, options);
const parsed = Table.parseForSave(copy);
const query = this.saveQuery(parsed, options);
const [saved] = await Table.fetchQuery(query);
return saved;
}
extractBatchSize(input) {
if (typeof input === 'number') return input;
if (input) return input.batchSize;
return input;
}
static parseForSaveArray(array) {
array = array || [];
const parsed = [];
for (let i = 0; i < array.length; i++) {
const element = array[i];
parsed.push(Table.parseForSave(element));
}
return parsed;
}
getSubArray(array, i, batchSize = this.INSERT_LIMIT_ARRAY) {
const initial = batchSize * i;
const final = Math.min(array.length, (i + 1) * batchSize);
const result = array.slice(initial, final);
return result;
}
async saveBunch(array, batchSize = this.INSERT_LIMIT_ARRAY, options) {
const parsed = Table.parseForSaveArray(array);
const promises = [];
const iterations = parsed.length / batchSize;
for (let i = 0; i < iterations; i++) {
const subArray = this.getSubArray(parsed, i, batchSize);
const query = this.saveQuery(subArray, options);
promises.push(Table.fetchQuery(query));
}
const results = await Promise.all(promises);
let concatenated = [];
for (let i = 0; i < results.length; i++) {
concatenated = concatenated.concat(results[i]);
}
return concatenated;
}
saveQuery(entry, options) {
options = options || {};
const query = this.table().insert(entry).returning('*');
const { onConflict } = options;
if (onConflict && !onConflict.columns) throw new ChinchayError(new Error('onConflict requires columns attribute'), 'onConflictColumns', 'options.onConflict must have a "columns" property.');
if (onConflict && onConflict.merge) {
query.onConflict(onConflict.columns).merge(onConflict.merge);
} else if (onConflict && onConflict.ignore) {
query.onConflict(onConflict.columns).ignore();
}
return query;
}
// ################################################
// UPDATE
// ################################################
update(input, newValues, options) {
if (typeof input !== 'object') return this.updateById(input, newValues, options);
const search = input;
const query = this.updateWhere(search, newValues, options);
if (Table.returnAsQuery(options)) return query;
return this.fetchUpdateQuery(query, newValues, search);
}
updateById(id, newValues, options) {
const search = { id };
const query = this.update(search, newValues, options);
if (Table.returnAsQuery(options)) return query;
return new Promise((resolve, reject) => {
query.then((results) => { resolve(results[0]); }).catch((err) => { reject(err); });
});
}
updateQuery(search, values, options) {
let query = this.table();
query = Table.addUpdate(query, values);
query = Table.addWhere(query, search, options);
query = Table.addAdvancedOptions(query, options);
return query;
}
updateWhere(search, newValues, options) {
const values = Utils.cloneJSON(newValues);
const query = this.updateQuery(search, values, options);
return query;
}
fetchUpdateQuery(query, newValues, search) {
return new Promise((resolve, reject) => {
const idsDefined = search.id && newValues && newValues.id;
const idsDiffer = idsDefined && search.id !== newValues.id;
if (idsDiffer) return reject(Table.IdDifferError());
const getUpdatables = this.find(search);
const filterColumns = this.parseAttributesForUpsert(newValues);
return Promise.all([getUpdatables, filterColumns]).then(() => {
return resolve(Table.fetchQuery(query));
}).catch((err) => {
err.code = 'empty_update';
err = Table.makeError(err);
reject(err);
});
});
}
static IdDifferError() {
return new ChinchayError(new Error('Given ID differ'), 'given_id_differ');
}
// ################################################
// Find (R from CRUD)
// ################################################
all(columns, options) {
return this.find({}, columns, options);
}
find(search, columns, options) {
if (!options && Utils.isJSON(columns)) {
options = columns;
columns = 'all';
}
const query = this.findQuery(search, columns, options);
if (Table.returnAsQuery(options)) return query;
return Table.fetchQuery(query);
}
findQuery(search, columns, options) {
return this.buildQuery('find', search, columns, options);
}
findById(id, columns, options) {
const search = {};
search.id = id;
return new Promise((resolve, reject) => {
this.find(search, columns, options).then((entries) => {
if (entries.length === 0) return reject(new ChinchayError('id solicitado no existe', 'no_entry_for_id'));
return resolve(entries[0]);
}).catch((err) => {
reject(err);
});
});
}
async arrayOfIds(search, options) {
const result = await this.find(search, ['id'], options);
const array = [];
for (let i = 0; i < result.length; i++) {
const element = result[i];
array.push(element.id);
}
return array;
}
findIdIn(ids, columns, query, options) {
return this.findIn('id', ids, query, columns, options);
}
findIn(target, validOptions, query, columns, options) {
query = query || {};
if (Array.isArray(validOptions)) query[target] = ['in', validOptions];
else {
query[target] = validOptions;
}
return this.find(query, columns, options);
}
// ################################################
// BUILD QUERY
// ################################################
static addAdvancedOptions(query, options) {
if ((typeof options) !== 'object') return query;
if (options.groupBy) {
query = this.addGroupBy(query, options.groupBy);
}
query = this.addOrderByArray(query, options.orderBy);
if (options.limit) {
query = this.addLimit(query, options.limit);
}
if (options.offset) {
query = this.addOffset(query, options.offset);
}
query = this.addTimeInterval(query, options);
return query;
}
static addSelect(type, query, columns, options) {
switch (type) {
case 'find':
return Table.addFindSelect(query, columns, options);
case 'count':
return Table.addCountSelect(query, options);
case 'aggregate':
return Table.addAggregateSelect(query, columns, options);
case 'delete':
return Table.addDelete(query, options);
default:
return Table.addFindSelect(query, columns, options);
}
}
static addFindSelect(query, columns, options) {
options = options || {};
if (options.rawSelect) query = Table.addRawSelect(query, options.rawSelect);
if (options.distinct) {
query.distinct(options.distinct);
options.clearSelect = true;
}
if (options.clearSelect || (!Array.isArray(columns) && columns !== 'all')) return query;
if (!Array.isArray(columns)) columns = '*';
return query.select(columns);
}
static addCountSelect(query, options) {
options = options || {};
if (options.rawSelect) {
query = Table.addRawSelect(query, options.rawSelect);
}
if (options.countDistinct) {
return query.countDistinct(options.countDistinct);
}
return query.count();
}
static addAggregateSelect(query, column, options) {
options = options || {};
if (options.rawSelect) {
query = Table.addRawSelect(query, options.rawSelect);
}
if (!column) throw new Error('A column should be given to aggregate over');
if (!options.aggregation) throw new Error('Aggregation option missing');
return query[options.aggregation](column);
}
static addDelete(query) {
return query.del().returning('*');
}
static addWhere(query, search, options) {
options = options || {};
if ((typeof search) !== 'object' || search === null) search = {};
const keys = Object.keys(search);
for (let i = 0; i < keys.length; i++) {
const key = keys[i];
const value = search[key];
Table.insertWhere(query, key, value);
}
if (options.rawWhere) {
query = Table.addRawWhere(query, options.rawWhere);
}
return query;
}
buildQuery(selectType, search, columns, options) {
let query = this.table();
query = this.constructor.makeQuery(query, selectType, search, columns, options);
return query;
}
columnsNamesQuery() {
const query = { table_name: this.table_name };
return this.knex('information_schema.columns').select('column_name').where(query);
}
static insertWhere(query, key, value, operator) {
if (Table.isComposed(value)) Table.insertComposed(query, key, value);
else {
if (value === null) value = ['is', null];
else if (!Array.isArray(value)) value = ['=', value];
if (Table.isSpecial(value)) Table.insertSpecial(query, key, value, operator);
}
}
static isComposed(value) {
const composingOptions = ['and', 'or'];
return Array.isArray(value) && value.length === 3 && composingOptions.includes(value[1]);
}
static isSpecial(value) {
return Array.isArray(value) && value.length >= 2;
}
static insertSpecial(query, key, value, operator) {
if (operator === 'or') query.orWhere(key, value[0], value[1]);
else query.andWhere(key, value[0], value[1]);
}
static insertComposed(query, key, value) {
const [first, operator, second] = value;
const firstSearch = {};
firstSearch[key] = first;
const secondSearch = {};
secondSearch[key] = second;
query.andWhere(function addComposeSearch() {
Table.insertWhere(this, key, first);
Table.insertWhere(this, key, second, operator);
});
}
static makeQuery(query, selectType, search, columns, options) {
this.addSelect(selectType, query, columns, options);
query = this.addWhere(query, search, options);
query = this.addAdvancedOptions(query, options);
return query;
}
// ################################################
// MISCELLANEOUS
// ################################################
columnsNames() {
const f = async () => {
const query = this.columnsNamesQuery();
const results = await Table.fetchQuery(query);
if (!results || results.length === 0) throw new Error(`Hubo un error creando un nuevo objeto: ${this.table_name}`);
return Table.columnsNamesToArray(results);
};
return f();
}
getAttributesNames() {
console.log('WARNING: The method getAttributesNames is deprecated. prefer columnsNames'); // eslint-disable-line no-console
return this.columnsNames();
}
// // TODO: USE FIND TO MAKE THIS QUERY
async getFirstDate(search, options) {
options = options || {};
options.limit = 1;
options.orderBy = ['created_at', 'asc'];
options.returnAsQuery = true;
const result = await this.find(search, ['created_at'], options);
return result[0] ? result[0].created_at : undefined;
}
filterAttributes(attributes) {
const f = async () => {
if (!Utils.isJSON(attributes)) throw new Error('Parameter should be a json');
const columnsNames = await this.columnsNames();
if (Table.containsUnexistingProperties(columnsNames, attributes)) throw new Error('Intentando agregar columna inexistente');
return attributes;
};
return f();
}
filterColumns(columns) {
const f = async () => {
if (!Array.isArray(columns)) return [];
const validColumns = await this.columnsNames();
return Table.removeUnexistingColumns(validColumns, columns);
};
return f();
}
parseToSend(entry) { // eslint-disable-line
return new Promise((resolve) => {
resolve(entry);
});
}
// ################################################
// 'Private' methods (static)
// ################################################
static addColumn(query, column) {
if (!Table.hasColumnInSelect(query, column)) {
query.select(column);
}
}
static addGroupByToSelect(query, groupby) {
if (Array.isArray(groupby)) {
for (let i = 0; i < groupby.length; i++) {
Table.addColumn(query, groupby[i]);
}
} else {
Table.addColumn(query, groupby);
}
}
static addGroupBy(query, groupby) {
if (groupby) {
Table.addGroupByToSelect(query, groupby);
query.groupBy(groupby);
}
return query;
}
static addRawSelect(query, select) {
if (select) {
if (Array.isArray(select)) {
query.select(knex.raw(select[0], select[1]));
} else {
query.select(knex.raw(select));
}
}
return query;
}
static addTimestamps(attr, isNew) {
if (!attr) return;
if (isNew) {
attr.created_at = new Date();
}
attr.updated_at = new Date();
}
static addUpdate(query, values) {
if (!query || !values || !query.update) return query;
const isNew = false;
Table.removeUnSetableAttributes(values);
Table.addTimestamps(values, isNew);
query.update(values, '*');
return query;
}
static addRawWhere(query, where) {
if (where) {
if (Array.isArray(where)) {
query.where(knex.raw(where[0], where[1]));
} else {
query.where(knex.raw(where));
}
}
return query;
}
static buildEntryFromColumns(columns) {
if (!columns) return {};
const entry = {};
for (let i = 0; i < columns.length; i++) {
entry[columns[i]] = null;
}
return entry;
}
static columnsNamesToArray(input) {
input = input || [];
const array = [];
for (let i = 0; i < input.length; i++) {
array.push(input[i].column_name);
}
return array;
}
static containsUnexistingProperties(validProperties, obj) {
if (!Array.isArray(validProperties) || !Utils.isJSON(obj)) return false;
const keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
if (validProperties.indexOf(keys[i]) === -1) return true;
}
return false;
}
static extractColumns(query) {
if (query.columns) {
let col = query.columns;
if (Table.isStringArray(col)) {
col = JSON.parse(col);
} else if (typeof col === 'string') {
col = [col];
}
delete query.columns;
return col;
}
return 'all';
}
static extractQuery(query) {
console.log('warning:', 'prefer extractSeach. extractQuery is deprecated and will be removed in future versions.');
return Table.extractSearch(query);
}
static extractSearch(query) {
const keys = Object.keys(query);
for (let i = 0; i < keys.length; i++) {
const k = keys[i];
const elem = query[k];
if (typeof elem === 'string' && (elem.startsWith('[') || elem.startsWith('{'))) {
query[k] = JSON.parse(elem);
}
}
return query;
}
static extractOptions(query, configuration) {
const options = {};
const securityMode = Table.getSecurityMode(configuration);
const queryKeys = Object.keys(query);
for (let i = 0; i < OPTIONS_KEYS.length; i++) {
const key = OPTIONS_KEYS[i];
if (queryKeys.indexOf(key) > -1) {
let elem = query[key];
if (Table.isStringArray(elem)) {
elem = JSON.parse(elem);
}
options[key] = elem;
delete query[key];
}
}
if (securityMode) Table.removeRawOptions(options);
return options;
}
static getSecurityMode(configuration = {}) {
if (configuration.securityMode === false) return false;
return true;
}
static getDefaultKnex() {
return knex;
}
static getSelectString(query) {
const str = query.toString();
let withinParentesis = false;
for (let i = 0; i < str.length; i++) {
const s = str.substring(i);
if (s[0] === '(') withinParentesis = true;
if (s[0] === ')') withinParentesis = false;
if (!withinParentesis && s.substring(0, 4) === 'from') {
return str.substring(6, i);
}
}
return 'Query badly parsed';
}
static hasColumnInSelect(query, column) {
const selectStr = Table.getSelectString(query);
return selectStr.indexOf(` ${column} `) > -1 || selectStr.indexOf(` ${column},`) > -1 || selectStr.indexOf(`,${column} `) > -1 || selectStr.indexOf(`"${column}"`) > -1;
}
static setDefaultKnex(elem) {
knex = elem;
}
static addOrderByArray(query, array) {
if (!array) return query;
if (!Array.isArray(array)) { return Table.addOrderBy(query, array); }
for (let i = 0; i < array.length; i++) {
const column = array[i];
if (Array.isArray(column)) Table.addOrderByArray(query, column);
else {
const isFinal = i === array.length;
const hasOrderBy = !isFinal && !Array.isArray(array[i + 1]);
const order = hasOrderBy ? array[i + 1] : null;
if (hasOrderBy) i += 1;
Table.addOrderBy(query, column, order);
}
}
return query;
}
static addOrderBy(query, column, order) {
if (column) {
query.orderBy(column, order);
}
return query;
}
static addLimit(query, limit) {
if (limit) {
query.limit(limit);
}
return query;
}
static addOffset(query, offset) {
if (offset) {
query.offset(offset);
}
return query;
}
static addTimeInterval(query, options, endDate) {
options = options || {};
const startDate = typeof options === 'object' && !(options instanceof Date) ? options.startDate : options;
endDate = options && options.endDate ? options.endDate : endDate;
if (endDate) {
query.andWhere('created_at', '<', endDate);
}
if (startDate) {
query.andWhere('created_at', '>', startDate);
}
return query;
}
static fetchQuery(query) {
return new Promise((resolve, reject) => {
query.then((results) => {
return resolve(results);
}).catch((err) => {
reject(Table.makeError(err));
});
});
}
static returnAsQuery(options) {
options = options || {};
return options.returnAsQuery;
}
static removeUnSetableAttributes(attributes) {
if (!attributes) return;
delete attributes.id;
delete attributes.created_at;
delete attributes.updated_at;
}
parseAttributesForUpsert(attributes, isNew) {
const f = async () => {
Table.removeUnSetableAttributes(attributes);
attributes = await this.filterAttributes(attributes);
if (Utils.isEmptyJSON(attributes)) throw new Error('Parameter should not be empty');
Table.addTimestamps(attributes, isNew);
return attributes;
};
return f();
}
static parseForSave(entry) {
Table.removeUnSetableAttributes(entry);
Table.addTimestamps(entry, true);
return entry;
}
static makeError(err) { // Support for previous versions
err = err || new Error('Unknown error');
if (err.chinchayCode) return err;
const message = Table.getMessage(err);
err = message ? Table.error400(err, message) : Table.error500(err);
return err;
}
static error400(err, message) {
Table.setCode(err, 400);
err.message = message;
err.fullMessage = Table.cloneError(err);
return err;
}
static error500(err) {
Table.setCode(err, 500);
err.message = 'Internal Error';
err.fullMessage = Table.cloneError(err);
return err;
}
static cloneError(error) {
let { stack } = error;
stack = stack.replace('error: error:', 'error:');
let clone = new Error(stack);
clone = Object.assign(clone, error);
return clone;
}
// this method redefine de error code in several ways to support previous versions
static setCode(err, code) {
err.postgresCode = err.code;
err.chinchayCode = err.code;
err.code = code;
err.suggestedHTTPCode = code;
}
static getMessage(err) {
if (!err) return null;
return ERROR_400_BY_CODE[err.code] || ERROR_400[err.routine];
}
static isStringArray(elem) {
return typeof elem === 'string' && (elem.startsWith('[') || elem.startsWith('{'));
}
static mergeRawSelect(rawSelect, input) {
const q = knex.queryBuilder();
Table.addRawSelect(q, rawSelect);
let string = q.toString();
Table.addRawSelect(q, input);
string = q.toString();
string = string.substring(7, string.length);
return string;
}
static removeRawOptions(options) {
options = options || {};
for (let i = 0; i < OPTIONS_KEYS.length; i++) {
if (OPTIONS_KEYS[i].includes('raw')) {
delete options[OPTIONS_KEYS[i]];
}
}
return options;
}
static removeUnexistingColumns(validColumns, array) {
if (!Array.isArray(validColumns) || !Array.isArray(array)) return [];
const filtered = [];
for (let i = 0; i < array.length; i++) {
const column = array[i];
if (validColumns.indexOf(column) > -1) {
filtered.push(column);
}
}
return filtered;
}
}
const ERROR_400 = {
errorMissingColumn: 'Consulta invalida. Columna solicitada no existe',
check_ungrouped_columns_walker: 'Group by invalido.',
recompute_limits: 'Limite es invalido',
scanner_yyerror: 'Select invalido',
DateTimeParseError: 'Fecha ingresada no es válida',
unexistantID: 'Id solicitado no existe',
pg_atoi: 'Problema en tipo de variable',
};
const ERROR_400_BY_CODE = {
42703: 'Intentando de agregar columna inexistent',
};
const OPTIONS_KEYS = ['startDate', 'endDate', 'groupBy', 'orderBy', 'limit', 'offset', 'rawSelect', 'clearSelect', 'rawWhere', 'countDistinct'];
module.exports = Table;