@ts-awesome/orm 1.5.0 → 1.5.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,688 @@
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __metadata = (this && this.__metadata) || function (k, v) {
9
+ if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const dist_1 = require("../dist");
13
+ const models_1 = require("./models");
14
+ const builder_1 = require("../dist/builder");
15
+ const dist_2 = require("../dist");
16
+ const tableInfo = (0, builder_1.readModelMeta)(models_1.Person);
17
+ const tableName = tableInfo.tableName;
18
+ const person = { id: 1, name: 'Name', age: 18, city: 'City', profiles: ["profile-a"] };
19
+ describe('Select', () => {
20
+ it('Check query info', () => {
21
+ const query = (0, dist_1.Select)(models_1.Person);
22
+ expect(query._type).toBe('SELECT');
23
+ expect(query._table).toStrictEqual(tableInfo);
24
+ });
25
+ it('Columns through array list', () => {
26
+ const nameAlias = 'PersonName';
27
+ const coefficient = 2;
28
+ const columnsThroughList = (0, dist_1.Select)(models_1.Person).columns(['name', 'age']);
29
+ const columnsThroughBuilder = (0, dist_1.Select)(models_1.Person).columns(({ name, age }) => [name, age]);
30
+ const columnsWithAlias = (0, dist_1.Select)(models_1.Person).columns(({ name }) => [(0, dist_1.alias)(name, nameAlias)]);
31
+ const columnsWithOf = (0, dist_1.Select)(models_1.Person).columns(() => [(0, dist_1.of)(models_1.Employee, 'company')]);
32
+ const columnsWithExpression = (0, dist_1.Select)(models_1.Person).columns(({ age }) => [age.mul(coefficient), (0, dist_1.max)(age)]);
33
+ const expectation = {
34
+ default: [
35
+ { _column: { table: tableName, name: 'name' } },
36
+ { _column: { table: tableName, name: 'age' } }
37
+ ],
38
+ alias: [
39
+ { _alias: nameAlias, _operands: [{ _column: { table: tableName, name: 'name' } }] }
40
+ ],
41
+ of: [
42
+ { _column: { table: (0, builder_1.readModelMeta)(models_1.Employee).tableName, name: 'company' } }
43
+ ],
44
+ expression: [
45
+ { _operator: '*', _operands: [{ _column: { table: tableName, name: 'age' } }, coefficient] },
46
+ { _func: 'MAX', _args: [{ _column: { table: tableName, name: 'age' } }] }
47
+ ]
48
+ };
49
+ expect(columnsThroughList._columns).toStrictEqual(expectation.default);
50
+ expect(columnsThroughBuilder._columns).toStrictEqual(expectation.default);
51
+ expect(columnsWithAlias._columns).toStrictEqual(expectation.alias);
52
+ expect(columnsWithOf._columns).toStrictEqual(expectation.of);
53
+ expect(columnsWithExpression._columns).toStrictEqual(expectation.expression);
54
+ });
55
+ it('Joins', () => {
56
+ const employeeTableInfo = (0, builder_1.readModelMeta)(models_1.Employee);
57
+ const innerJoin = (0, dist_1.Select)(models_1.Person).join(models_1.Employee, ({ id }, { personId }) => id.eq(personId));
58
+ const leftJoin = (0, dist_1.Select)(models_1.Person).joinLeft(models_1.Employee, ({ id }, { personId }) => id.eq(personId));
59
+ const rightJoin = (0, dist_1.Select)(models_1.Person).joinRight(models_1.Employee, ({ id }, { personId }) => id.eq(personId));
60
+ const fullJoin = (0, dist_1.Select)(models_1.Person).joinFull(models_1.Employee, ({ id }, { personId }) => id.eq(personId));
61
+ const innerJoinExpectation = [{
62
+ _tableName: employeeTableInfo.tableName,
63
+ _alias: undefined,
64
+ _type: "INNER" /* joinTypes.inner */,
65
+ _condition: {
66
+ _operands: [{ _column: { table: tableName, name: 'id' } }, { _column: { table: employeeTableInfo.tableName, name: `personId` } }],
67
+ _operator: '='
68
+ }
69
+ }];
70
+ const leftJoinExpectation = [{
71
+ _tableName: employeeTableInfo.tableName,
72
+ _alias: undefined,
73
+ _type: "LEFT" /* joinTypes.left */,
74
+ _condition: {
75
+ _operands: [{ _column: { table: tableName, name: 'id' } }, { _column: { table: employeeTableInfo.tableName, name: `personId` } }],
76
+ _operator: '='
77
+ }
78
+ }];
79
+ const rightJoinExpectation = [{
80
+ _tableName: employeeTableInfo.tableName,
81
+ _alias: undefined,
82
+ _type: "RIGHT" /* joinTypes.right */,
83
+ _condition: {
84
+ _operands: [{ _column: { table: tableName, name: 'id' } }, { _column: { table: employeeTableInfo.tableName, name: `personId` } }],
85
+ _operator: '='
86
+ }
87
+ }];
88
+ const fullJoinExpectation = [{
89
+ _tableName: employeeTableInfo.tableName,
90
+ _alias: undefined,
91
+ _type: "FULL OUTER" /* joinTypes.full */,
92
+ _condition: {
93
+ _operands: [{ _column: { table: tableName, name: 'id' } }, { _column: { table: employeeTableInfo.tableName, name: `personId` } }],
94
+ _operator: '='
95
+ }
96
+ }];
97
+ expect(innerJoin._joins).toStrictEqual(innerJoinExpectation);
98
+ expect(leftJoin._joins).toStrictEqual(leftJoinExpectation);
99
+ expect(rightJoin._joins).toStrictEqual(rightJoinExpectation);
100
+ expect(fullJoin._joins).toStrictEqual(fullJoinExpectation);
101
+ });
102
+ it('Joins with alias', () => {
103
+ const tableRef = new builder_1.TableRef(models_1.Employee);
104
+ const employeeTableInfo = (0, builder_1.readModelMeta)(models_1.Employee);
105
+ const innerJoin = (0, dist_1.Select)(models_1.Person).join(models_1.Employee, tableRef, ({ id }, { personId }) => id.eq(personId));
106
+ const leftJoin = (0, dist_1.Select)(models_1.Person).joinLeft(models_1.Employee, tableRef, ({ id }, { personId }) => id.eq(personId));
107
+ const rightJoin = (0, dist_1.Select)(models_1.Person).joinRight(models_1.Employee, tableRef, ({ id }, { personId }) => id.eq(personId));
108
+ const fullJoin = (0, dist_1.Select)(models_1.Person).joinFull(models_1.Employee, tableRef, ({ id }, { personId }) => id.eq(personId));
109
+ const innerJoinExpectation = [{
110
+ _tableName: employeeTableInfo.tableName,
111
+ _alias: tableRef.tableName,
112
+ _type: "INNER" /* joinTypes.inner */,
113
+ _condition: {
114
+ _operands: [{ _column: { table: tableName, name: 'id' } }, { _column: { table: tableRef.tableName, name: `personId` } }],
115
+ _operator: '='
116
+ }
117
+ }];
118
+ const leftJoinExpectation = [{
119
+ _tableName: employeeTableInfo.tableName,
120
+ _alias: tableRef.tableName,
121
+ _type: "LEFT" /* joinTypes.left */,
122
+ _condition: {
123
+ _operands: [{ _column: { table: tableName, name: 'id' } }, { _column: { table: tableRef.tableName, name: `personId` } }],
124
+ _operator: '='
125
+ }
126
+ }];
127
+ const rightJoinExpectation = [{
128
+ _tableName: employeeTableInfo.tableName,
129
+ _alias: tableRef.tableName,
130
+ _type: "RIGHT" /* joinTypes.right */,
131
+ _condition: {
132
+ _operands: [{ _column: { table: tableName, name: 'id' } }, { _column: { table: tableRef.tableName, name: `personId` } }],
133
+ _operator: '='
134
+ }
135
+ }];
136
+ const fullJoinExpectation = [{
137
+ _tableName: employeeTableInfo.tableName,
138
+ _alias: tableRef.tableName,
139
+ _type: "FULL OUTER" /* joinTypes.full */,
140
+ _condition: {
141
+ _operands: [{ _column: { table: tableName, name: 'id' } }, { _column: { table: tableRef.tableName, name: `personId` } }],
142
+ _operator: '='
143
+ }
144
+ }];
145
+ expect(innerJoin._joins).toStrictEqual(innerJoinExpectation);
146
+ expect(leftJoin._joins).toStrictEqual(leftJoinExpectation);
147
+ expect(rightJoin._joins).toStrictEqual(rightJoinExpectation);
148
+ expect(fullJoin._joins).toStrictEqual(fullJoinExpectation);
149
+ });
150
+ it('Where', () => {
151
+ const query = (0, dist_1.Select)(models_1.Person).where(({ age, name }) => (0, dist_1.and)(age.eq(person.age), name.like(person.name)));
152
+ const expectation = [{
153
+ _operator: 'AND',
154
+ _operands: [
155
+ { _operator: '=', _operands: [{ _column: { table: tableName, name: 'age' } }, person.age] },
156
+ { _operator: 'LIKE', _operands: [{ _column: { table: tableName, name: 'name' } }, person.name] },
157
+ ]
158
+ }];
159
+ expect(query._where).toStrictEqual(expectation);
160
+ });
161
+ describe('Where filter fields', () => {
162
+ it('simple many to many', () => {
163
+ const query = (0, dist_1.Select)(models_1.Person).where(({ profiles }) => profiles.has('test'));
164
+ const expectation = [{
165
+ _operands: [
166
+ "test",
167
+ {
168
+ _operator: 'SUBQUERY',
169
+ _operands: [
170
+ {
171
+ _columns: [{ _column: { table: "employee", name: "title" } }],
172
+ _table: { fields: null, tableName: "employee" },
173
+ _type: "SELECT",
174
+ _where: [
175
+ {
176
+ _operands: [
177
+ { _column: { table: "employee", name: "person" } },
178
+ { _column: { table: tableName, name: "id" } }
179
+ ],
180
+ _operator: "="
181
+ }
182
+ ],
183
+ }
184
+ ]
185
+ },
186
+ ],
187
+ _operator: "IN",
188
+ }];
189
+ expect(query._where).toStrictEqual(expectation);
190
+ });
191
+ it('with builder', () => {
192
+ const query = (0, dist_1.Select)(models_1.Person).where(({ tags }) => tags.has('tag'));
193
+ const expectation = [{
194
+ _operands: [
195
+ "tag",
196
+ {
197
+ _operator: 'SUBQUERY',
198
+ _operands: [
199
+ {
200
+ _columns: [{ _column: { table: "Tag", name: "name" } }],
201
+ _table: (0, builder_1.readModelMeta)(models_1.Tag),
202
+ _alias: null,
203
+ _type: "SELECT",
204
+ _distinct: false,
205
+ "_for": undefined,
206
+ _joins: [
207
+ {
208
+ _alias: undefined,
209
+ _tableName: "TagPerson",
210
+ _type: "INNER",
211
+ _condition: {
212
+ _operands: [
213
+ {
214
+ _operands: [
215
+ {
216
+ _column: {
217
+ name: 'id',
218
+ table: "Tag",
219
+ }
220
+ },
221
+ {
222
+ _column: {
223
+ name: 'tag',
224
+ table: "TagPerson",
225
+ }
226
+ }
227
+ ],
228
+ _operator: '='
229
+ },
230
+ {
231
+ _operands: [
232
+ {
233
+ _column: {
234
+ name: 'person',
235
+ table: "TagPerson",
236
+ }
237
+ },
238
+ {
239
+ _column: {
240
+ name: 'id',
241
+ table: "Person",
242
+ }
243
+ }
244
+ ],
245
+ _operator: '='
246
+ },
247
+ ],
248
+ _operator: 'AND'
249
+ }
250
+ }
251
+ ]
252
+ }
253
+ ]
254
+ },
255
+ ],
256
+ _operator: "IN",
257
+ }];
258
+ expect(query._where).toStrictEqual(expectation);
259
+ });
260
+ });
261
+ it('Having', () => {
262
+ const employeeTableName = (0, builder_1.readModelMeta)(models_1.Employee).tableName;
263
+ const salaryRate = 2000;
264
+ const query = (0, dist_1.Select)(models_1.Employee)
265
+ .columns(({ salary }) => [(0, dist_1.sum)(salary)])
266
+ .groupBy(['company'])
267
+ .having(({ salary }) => (0, dist_1.sum)(salary).gt(salaryRate));
268
+ const expectation = {
269
+ _columns: [{ _func: 'SUM', _args: [{ _column: { table: employeeTableName, name: 'salary' } }] }],
270
+ _groupBy: [{ _column: { table: employeeTableName, name: 'company' } }],
271
+ _having: [{
272
+ _operator: '>',
273
+ _operands: [{
274
+ _func: 'SUM',
275
+ _args: [{ _column: { table: employeeTableName, name: 'salary' } }]
276
+ }, salaryRate]
277
+ }]
278
+ };
279
+ expect(query._columns).toStrictEqual(expectation._columns);
280
+ expect(query._groupBy).toStrictEqual(expectation._groupBy);
281
+ expect(query._having).toStrictEqual(expectation._having);
282
+ });
283
+ it('Group by', () => {
284
+ const queryThroughList = (0, dist_1.Select)(models_1.Person).groupBy(['city']);
285
+ const queryThroughBuilder = (0, dist_1.Select)(models_1.Person).groupBy(({ city }) => [city]);
286
+ const expectation = [{ _column: { table: tableName, name: 'city' } }];
287
+ expect(queryThroughList._groupBy).toStrictEqual(expectation);
288
+ expect(queryThroughBuilder._groupBy).toStrictEqual(expectation);
289
+ });
290
+ it('Order By', () => {
291
+ const orderByThroughList = (0, dist_1.Select)(models_1.Person).orderBy(['city']);
292
+ const defaultOrder = (0, dist_1.Select)(models_1.Person).orderBy(({ city }) => [city]);
293
+ const ascOrder = (0, dist_1.Select)(models_1.Person).orderBy(({ city }) => [(0, dist_1.asc)(city)]);
294
+ const descOrder = (0, dist_1.Select)(models_1.Person).orderBy(({ city }) => [(0, dist_1.desc)(city)]);
295
+ const numberedOrder = (0, dist_1.Select)(models_1.Person).orderBy(() => [(0, dist_1.asc)(0), (0, dist_1.desc)(1)]);
296
+ const expectation = {
297
+ default: [{ _column: { table: tableName, name: 'city' } }],
298
+ asc: [{ _column: { table: tableName, name: 'city' }, _order: 'ASC' }],
299
+ desc: [{ _column: { table: tableName, name: 'city' }, _order: 'DESC' }],
300
+ numbered: [{ _column: 0, _order: 'ASC' }, { _column: 1, _order: 'DESC' }],
301
+ };
302
+ expect(orderByThroughList._orderBy).toStrictEqual(expectation.default);
303
+ expect(defaultOrder._orderBy).toStrictEqual(expectation.default);
304
+ expect(ascOrder._orderBy).toStrictEqual(expectation.asc);
305
+ expect(descOrder._orderBy).toStrictEqual(expectation.desc);
306
+ expect(numberedOrder._orderBy).toStrictEqual(expectation.numbered);
307
+ });
308
+ it('Limit', () => {
309
+ const limit = 10;
310
+ const query = (0, dist_1.Select)(models_1.Person).limit(limit);
311
+ expect(query._limit).toBe(limit);
312
+ });
313
+ it('Offset', () => {
314
+ const offset = 3;
315
+ const query = (0, dist_1.Select)(models_1.Person).offset(offset);
316
+ expect(query._offset).toBe(offset);
317
+ });
318
+ it('subquery on self', () => {
319
+ const query = (0, dist_1.Select)(models_1.Person).where(({ id }) => (0, dist_2.exists)((0, dist_1.Select)((0, dist_1.alias)(models_1.Person, 'person_filter')).where(({ id: _ }) => _.eq(id))));
320
+ const expectation = {
321
+ _where: [{
322
+ "_operands": [{
323
+ "_alias": "person_filter",
324
+ "_columns": [
325
+ { "_column": { name: 'id', table: 'person_filter' } },
326
+ { "_column": { name: 'uid', table: 'person_filter' } },
327
+ { "_column": { name: 'name', table: 'person_filter' } },
328
+ { "_column": { name: 'age', table: 'person_filter' } },
329
+ { "_column": { name: 'city', table: 'person_filter' } },
330
+ ],
331
+ "_distinct": false,
332
+ "_for": undefined,
333
+ "_table": models_1.Person[dist_1.TableMetadataSymbol],
334
+ "_type": "SELECT",
335
+ "_where": [{
336
+ "_operands": [{
337
+ "_column": { "name": "id", "table": "person_filter", },
338
+ }, {
339
+ "_column": { "name": "id", "table": "Person", },
340
+ }],
341
+ "_operator": "=",
342
+ }]
343
+ }],
344
+ "_operator": "EXISTS",
345
+ }]
346
+ };
347
+ expect(query._where).toStrictEqual(expectation._where);
348
+ });
349
+ it('sum of subqueries', () => {
350
+ let PersonAction = class PersonAction {
351
+ };
352
+ __decorate([
353
+ dist_2.dbField,
354
+ __metadata("design:type", Number)
355
+ ], PersonAction.prototype, "personId", void 0);
356
+ __decorate([
357
+ dist_2.dbField,
358
+ __metadata("design:type", String)
359
+ ], PersonAction.prototype, "action", void 0);
360
+ __decorate([
361
+ dist_2.dbField,
362
+ __metadata("design:type", Date)
363
+ ], PersonAction.prototype, "created", void 0);
364
+ PersonAction = __decorate([
365
+ (0, dist_2.dbTable)('actions')
366
+ ], PersonAction);
367
+ const ts = new Date(Date.now() - 3600);
368
+ const query = (0, dist_1.Select)(models_1.Person)
369
+ .columns(({ uid }) => [
370
+ uid,
371
+ (0, dist_1.alias)((0, dist_1.Select)(PersonAction)
372
+ .columns(() => [(0, dist_2.count)()])
373
+ .where(({ personId, action, created }) => (0, dist_1.and)(personId.eq((0, dist_1.of)(models_1.Person, 'id')), action.eq('a'), created.gte(ts)))
374
+ .asScalar()
375
+ .mul(1).add((0, dist_1.Select)(PersonAction)
376
+ .columns(() => [(0, dist_2.count)()])
377
+ .where(({ personId, action, created }) => (0, dist_1.and)(personId.eq((0, dist_1.of)(models_1.Person, 'id')), action.eq('b'), created.gte(ts)))
378
+ .asScalar().mul(100)).add((0, dist_1.Select)(PersonAction)
379
+ .columns(() => [(0, dist_2.count)()])
380
+ .where(({ personId, action, created }) => (0, dist_1.and)(personId.eq((0, dist_1.of)(models_1.Person, 'id')), action.eq('c'), created.gte(ts)))
381
+ .asScalar().mul(100)), 'score')
382
+ ]).orderBy(() => [(0, dist_1.desc)((0, dist_1.of)(null, 'score'))]);
383
+ const expected = [{ "_column": { "table": "Person", "name": "uid" } }, {
384
+ "_alias": "score", "_operands": [{
385
+ "_operator": "+", "_operands": [{
386
+ "_operator": "+",
387
+ "_operands": [{
388
+ "_operator": "*",
389
+ "_operands": [{
390
+ "_operator": "SUBQUERY",
391
+ "_operands": [{
392
+ "_type": "SELECT",
393
+ "_table": PersonAction[dist_1.TableMetadataSymbol],
394
+ "_alias": null,
395
+ "_distinct": false,
396
+ "_for": undefined,
397
+ "_columns": [{ "_func": "COUNT", "_args": ["*"] }],
398
+ "_where": [{
399
+ "_operator": "AND",
400
+ "_operands": [{
401
+ "_operator": "=",
402
+ "_operands": [{ "_column": { "table": "actions", "name": "personId" } }, {
403
+ "_column": {
404
+ "table": "Person",
405
+ "name": "id"
406
+ }
407
+ }]
408
+ }, {
409
+ "_operator": "=",
410
+ "_operands": [{ "_column": { "table": "actions", "name": "action" } }, "a"]
411
+ }, {
412
+ "_operator": ">=",
413
+ "_operands": [{ "_column": { "table": "actions", "name": "created" } }, ts]
414
+ }]
415
+ }]
416
+ }]
417
+ }, 1]
418
+ }, {
419
+ "_operator": "*",
420
+ "_operands": [{
421
+ "_operator": "SUBQUERY",
422
+ "_operands": [{
423
+ "_type": "SELECT",
424
+ "_table": PersonAction[dist_1.TableMetadataSymbol],
425
+ "_alias": null,
426
+ "_distinct": false,
427
+ "_for": undefined,
428
+ "_columns": [{ "_func": "COUNT", "_args": ["*"] }],
429
+ "_where": [{
430
+ "_operator": "AND",
431
+ "_operands": [{
432
+ "_operator": "=",
433
+ "_operands": [{ "_column": { "table": "actions", "name": "personId" } }, {
434
+ "_column": {
435
+ "table": "Person",
436
+ "name": "id"
437
+ }
438
+ }]
439
+ }, {
440
+ "_operator": "=",
441
+ "_operands": [{ "_column": { "table": "actions", "name": "action" } }, "b"]
442
+ }, {
443
+ "_operator": ">=",
444
+ "_operands": [{ "_column": { "table": "actions", "name": "created" } }, ts]
445
+ }]
446
+ }]
447
+ }]
448
+ }, 100]
449
+ }]
450
+ }, {
451
+ "_operator": "*",
452
+ "_operands": [{
453
+ "_operator": "SUBQUERY",
454
+ "_operands": [{
455
+ "_type": "SELECT",
456
+ "_table": PersonAction[dist_1.TableMetadataSymbol],
457
+ "_alias": null,
458
+ "_distinct": false,
459
+ "_for": undefined,
460
+ "_columns": [{ "_func": "COUNT", "_args": ["*"] }],
461
+ "_where": [{
462
+ "_operator": "AND",
463
+ "_operands": [{
464
+ "_operator": "=",
465
+ "_operands": [{ "_column": { "table": "actions", "name": "personId" } }, {
466
+ "_column": {
467
+ "table": "Person",
468
+ "name": "id"
469
+ }
470
+ }]
471
+ }, {
472
+ "_operator": "=",
473
+ "_operands": [{ "_column": { "table": "actions", "name": "action" } }, "c"]
474
+ }, {
475
+ "_operator": ">=",
476
+ "_operands": [{ "_column": { "table": "actions", "name": "created" } }, ts]
477
+ }]
478
+ }]
479
+ }]
480
+ }, 100]
481
+ }]
482
+ }]
483
+ }];
484
+ expect(query._columns).toStrictEqual(expected);
485
+ expect(query._orderBy).toStrictEqual([{
486
+ _column: {
487
+ name: 'score',
488
+ },
489
+ _order: "DESC"
490
+ }]);
491
+ });
492
+ it('invalid sub queries', () => {
493
+ try {
494
+ (0, dist_1.Select)(models_1.Person).columns(['age', 'uid']).asScalar();
495
+ fail('expected to throw');
496
+ }
497
+ catch (e) {
498
+ // ignore
499
+ }
500
+ });
501
+ it('union operator', () => {
502
+ const query = (0, dist_1.Select)(models_1.Person)
503
+ .columns(['name'])
504
+ .union(true, (0, dist_1.Select)(models_1.Person)
505
+ .columns(['name'])
506
+ .where(x => x.age.lt(18)))
507
+ .where(x => x.age.gte(18))
508
+ .orderBy(['name']);
509
+ expect(query._operators).toStrictEqual([{
510
+ _operator: 'UNION',
511
+ _distinct: true,
512
+ _operand: {
513
+ "_type": "SELECT",
514
+ "_table": models_1.Person[dist_1.TableMetadataSymbol],
515
+ "_alias": null,
516
+ "_distinct": false,
517
+ "_for": undefined,
518
+ "_columns": [{ "_column": { "table": "Person", "name": "name" } }],
519
+ "_where": [{
520
+ "_operator": "<",
521
+ "_operands": [
522
+ { "_column": { "table": "Person", "name": "age" } },
523
+ 18
524
+ ]
525
+ }]
526
+ }
527
+ }]);
528
+ });
529
+ it('intersect operator', () => {
530
+ const query = (0, dist_1.Select)(models_1.Person)
531
+ .columns(['name'])
532
+ .intersect((0, dist_1.Select)(models_1.Person)
533
+ .columns(['name'])
534
+ .where(x => x.age.lt(18)))
535
+ .where(x => x.age.gte(18))
536
+ .orderBy(['name']);
537
+ expect(query._operators).toStrictEqual([{
538
+ _operator: 'INTERSECT',
539
+ _distinct: false,
540
+ _operand: {
541
+ "_type": "SELECT",
542
+ "_table": models_1.Person[dist_1.TableMetadataSymbol],
543
+ "_alias": null,
544
+ "_distinct": false,
545
+ "_for": undefined,
546
+ "_columns": [{ "_column": { "table": "Person", "name": "name" } }],
547
+ "_where": [{
548
+ "_operator": "<",
549
+ "_operands": [
550
+ { "_column": { "table": "Person", "name": "age" } },
551
+ 18
552
+ ]
553
+ }]
554
+ }
555
+ }]);
556
+ });
557
+ it('except operator', () => {
558
+ const query = (0, dist_1.Select)(models_1.Person)
559
+ .columns(['name'])
560
+ .except((0, dist_1.Select)(models_1.Person)
561
+ .columns(['name'])
562
+ .where(x => x.age.lt(18)))
563
+ .where(x => x.age.gte(18))
564
+ .orderBy(['name']);
565
+ expect(query._operators).toStrictEqual([{
566
+ _operator: 'EXCEPT',
567
+ _distinct: false,
568
+ _operand: {
569
+ "_type": "SELECT",
570
+ "_table": models_1.Person[dist_1.TableMetadataSymbol],
571
+ "_alias": null,
572
+ "_distinct": false,
573
+ "_for": undefined,
574
+ "_columns": [{ "_column": { "table": "Person", "name": "name" } }],
575
+ "_where": [{
576
+ "_operator": "<",
577
+ "_operands": [
578
+ { "_column": { "table": "Person", "name": "age" } },
579
+ 18
580
+ ]
581
+ }]
582
+ }
583
+ }]);
584
+ });
585
+ });
586
+ describe('Insert', () => {
587
+ it('Check query info', () => {
588
+ const query = (0, dist_1.Insert)(models_1.Person);
589
+ expect(query._type).toBe('INSERT');
590
+ expect(query._table).toStrictEqual(tableInfo);
591
+ });
592
+ it('Insert record', () => {
593
+ const query = (0, dist_1.Insert)(models_1.Person).values(person);
594
+ const expectation = {
595
+ id: person.id,
596
+ name: person.name,
597
+ age: person.age,
598
+ city: person.city,
599
+ };
600
+ expect(query._values).toStrictEqual(expectation);
601
+ (0, dist_1.Insert)(models_1.Person).values(Object.assign(Object.assign({}, person), { uid: 'a80ec30e-791c-4499-a243-70af8b2bf7ba' }));
602
+ });
603
+ });
604
+ describe('Upsert', () => {
605
+ const infoMock = {
606
+ [dist_1.TableMetadataSymbol]: {}
607
+ };
608
+ it('Check query info', () => {
609
+ const query = (0, dist_1.Upsert)(models_1.Person);
610
+ expect(query._type).toBe('UPSERT');
611
+ expect(query._table).toStrictEqual(tableInfo);
612
+ });
613
+ it('Upsert record', () => {
614
+ const defaultUpsert = (0, dist_1.Upsert)(models_1.Person).values(person);
615
+ const withConflict = (0, dist_1.Upsert)(models_1.Person).values(person).conflict('idx');
616
+ const expectation = {
617
+ values: {
618
+ id: person.id,
619
+ name: person.name,
620
+ age: person.age,
621
+ city: person.city,
622
+ },
623
+ conflictExp: { _columns: ['id'], _where: undefined }
624
+ };
625
+ expect(defaultUpsert._values).toStrictEqual(expectation.values);
626
+ expect(defaultUpsert._conflictExp).toBeUndefined();
627
+ expect(withConflict._values).toStrictEqual(expectation.values);
628
+ expect(withConflict._conflictExp).toStrictEqual(expectation.conflictExp);
629
+ });
630
+ it('Should fail if table has no primary key', () => {
631
+ expect(() => {
632
+ (0, dist_1.Upsert)(infoMock).conflict();
633
+ }).toThrowError('Current table has no primary key. Please provide unique index for upsert');
634
+ });
635
+ it('Should fail if table indexes meta is empty', () => {
636
+ expect(() => {
637
+ (0, dist_1.Upsert)(infoMock).conflict('id');
638
+ }).toThrowError('Table indexes meta is empty');
639
+ });
640
+ it('Should fail if index is not declared', () => {
641
+ expect(() => {
642
+ (0, dist_1.Upsert)(models_1.Person).conflict('name');
643
+ }).toThrowError(`Index name is not declared for table ${tableName}`);
644
+ });
645
+ });
646
+ describe('Update', () => {
647
+ it('Check query info', () => {
648
+ const query = (0, dist_1.Update)(models_1.Person);
649
+ expect(query._type).toBe('UPDATE');
650
+ expect(query._table).toStrictEqual(tableInfo);
651
+ });
652
+ it('Update record', () => {
653
+ const limit = 10;
654
+ const query = (0, dist_1.Update)(models_1.Person).values(person).where(({ id }) => id.eq(person.id)).limit(limit);
655
+ const expectation = {
656
+ values: {
657
+ id: person.id,
658
+ name: person.name,
659
+ age: person.age,
660
+ city: person.city,
661
+ },
662
+ where: [{
663
+ _operator: '=',
664
+ _operands: [{ _column: { table: tableName, name: 'id' } }, person.id]
665
+ }]
666
+ };
667
+ expect(query._values).toStrictEqual(expectation.values);
668
+ expect(query._where).toStrictEqual(expectation.where);
669
+ expect(query._limit).toBe(limit);
670
+ });
671
+ });
672
+ describe('Delete', () => {
673
+ it('Check query info', () => {
674
+ const query = (0, dist_1.Delete)(models_1.Person);
675
+ expect(query._type).toBe('DELETE');
676
+ expect(query._table).toStrictEqual(tableInfo);
677
+ });
678
+ it('Delete record', () => {
679
+ const limit = 10;
680
+ const query = (0, dist_1.Delete)(models_1.Person).where(({ id }) => id.eq(person.id)).limit(limit);
681
+ const expectation = [{
682
+ _operator: '=',
683
+ _operands: [{ _column: { table: tableName, name: 'id' } }, person.id]
684
+ }];
685
+ expect(query._where).toStrictEqual(expectation);
686
+ expect(query._limit).toBe(limit);
687
+ });
688
+ });