@ts-awesome/orm 1.5.11 → 1.6.0-rc1

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.
@@ -1,770 +0,0 @@
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('CASE operator', () => {
530
- const query = (0, dist_1.Select)(models_1.Person)
531
- .columns(x => [(0, dist_1.alias)((0, dist_2.case_)({ when: x.age.gte(2), then: 'yes' }, { else: 'no' }), 'dynamic')]);
532
- expect(query._columns).toStrictEqual([{
533
- "_alias": "dynamic",
534
- "_operands": [{
535
- "_operator": "CASE",
536
- "_operands": [
537
- {
538
- "when": {
539
- "_operator": ">=",
540
- "_operands": [
541
- { "_column": { "name": "age", "table": "Person" } },
542
- 2
543
- ]
544
- },
545
- "then": "yes",
546
- },
547
- { "else": "no" }
548
- ]
549
- }]
550
- }]);
551
- });
552
- it('intersect operator', () => {
553
- const query = (0, dist_1.Select)(models_1.Person)
554
- .columns(['name'])
555
- .intersect((0, dist_1.Select)(models_1.Person)
556
- .columns(['name'])
557
- .where(x => x.age.lt(18)))
558
- .where(x => x.age.gte(18))
559
- .orderBy(['name']);
560
- expect(query._operators).toStrictEqual([{
561
- _operator: 'INTERSECT',
562
- _distinct: false,
563
- _operand: {
564
- "_type": "SELECT",
565
- "_table": models_1.Person[dist_1.TableMetadataSymbol],
566
- "_alias": null,
567
- "_distinct": false,
568
- "_for": undefined,
569
- "_columns": [{ "_column": { "table": "Person", "name": "name" } }],
570
- "_where": [{
571
- "_operator": "<",
572
- "_operands": [
573
- { "_column": { "table": "Person", "name": "age" } },
574
- 18
575
- ]
576
- }]
577
- }
578
- }]);
579
- });
580
- it('except operator', () => {
581
- const query = (0, dist_1.Select)(models_1.Person)
582
- .columns(['name'])
583
- .except((0, dist_1.Select)(models_1.Person)
584
- .columns(['name'])
585
- .where(x => x.age.lt(18)))
586
- .where(x => x.age.gte(18))
587
- .orderBy(['name']);
588
- expect(query._operators).toStrictEqual([{
589
- _operator: 'EXCEPT',
590
- _distinct: false,
591
- _operand: {
592
- "_type": "SELECT",
593
- "_table": models_1.Person[dist_1.TableMetadataSymbol],
594
- "_alias": null,
595
- "_distinct": false,
596
- "_for": undefined,
597
- "_columns": [{ "_column": { "table": "Person", "name": "name" } }],
598
- "_where": [{
599
- "_operator": "<",
600
- "_operands": [
601
- { "_column": { "table": "Person", "name": "age" } },
602
- 18
603
- ]
604
- }]
605
- }
606
- }]);
607
- });
608
- it('COUNT unique', () => {
609
- const query = (0, dist_1.Select)(models_1.Person)
610
- .columns(x => [(0, dist_2.count)(x.id, true)]);
611
- expect(query._columns).toStrictEqual([{
612
- _func: "COUNT",
613
- _args: [
614
- "DISTINCT",
615
- {
616
- _column: {
617
- name: 'id',
618
- table: 'Person'
619
- }
620
- }
621
- ]
622
- }]);
623
- });
624
- it('SELECT over subquery', () => {
625
- const query = (0, dist_1.Select)((0, dist_1.Select)(models_1.Person)
626
- .columns(x => [(0, dist_2.count)(x.id)])
627
- .groupBy(x => [x.age]))
628
- .columns(() => [(0, dist_2.count)(undefined, true)]);
629
- expect(query).toStrictEqual({
630
- "_alias": "Person_SUBQUERY",
631
- _columns: [{
632
- _func: "COUNT",
633
- _args: [
634
- "DISTINCT",
635
- '*'
636
- ]
637
- }],
638
- "_distinct": false,
639
- "_for": undefined,
640
- "_table": {
641
- "_alias": null,
642
- "_columns": [{
643
- "_func": "COUNT",
644
- "_args": [{
645
- "_column": {
646
- "name": "id",
647
- "table": "Person",
648
- }
649
- }]
650
- }],
651
- "_distinct": false,
652
- "_for": undefined,
653
- "_groupBy": [{
654
- "_column": {
655
- "name": "age",
656
- "table": "Person",
657
- }
658
- }],
659
- "_table": models_1.Person[dist_1.TableMetadataSymbol],
660
- "_type": "SELECT",
661
- "fields": new Map,
662
- "tableName": "Person_SUBQUERY",
663
- },
664
- "_type": "SELECT",
665
- });
666
- });
667
- });
668
- describe('Insert', () => {
669
- it('Check query info', () => {
670
- const query = (0, dist_1.Insert)(models_1.Person);
671
- expect(query._type).toBe('INSERT');
672
- expect(query._table).toStrictEqual(tableInfo);
673
- });
674
- it('Insert record', () => {
675
- const query = (0, dist_1.Insert)(models_1.Person).values(person);
676
- const expectation = {
677
- id: person.id,
678
- name: person.name,
679
- age: person.age,
680
- city: person.city,
681
- };
682
- expect(query._values).toStrictEqual(expectation);
683
- (0, dist_1.Insert)(models_1.Person).values(Object.assign(Object.assign({}, person), { uid: 'a80ec30e-791c-4499-a243-70af8b2bf7ba' }));
684
- });
685
- });
686
- describe('Upsert', () => {
687
- const infoMock = {
688
- [dist_1.TableMetadataSymbol]: {}
689
- };
690
- it('Check query info', () => {
691
- const query = (0, dist_1.Upsert)(models_1.Person);
692
- expect(query._type).toBe('UPSERT');
693
- expect(query._table).toStrictEqual(tableInfo);
694
- });
695
- it('Upsert record', () => {
696
- const defaultUpsert = (0, dist_1.Upsert)(models_1.Person).values(person);
697
- const withConflict = (0, dist_1.Upsert)(models_1.Person).values(person).conflict('idx');
698
- const expectation = {
699
- values: {
700
- id: person.id,
701
- name: person.name,
702
- age: person.age,
703
- city: person.city,
704
- },
705
- conflictExp: { _columns: ['id'], _where: undefined }
706
- };
707
- expect(defaultUpsert._values).toStrictEqual(expectation.values);
708
- expect(defaultUpsert._conflictExp).toBeUndefined();
709
- expect(withConflict._values).toStrictEqual(expectation.values);
710
- expect(withConflict._conflictExp).toStrictEqual(expectation.conflictExp);
711
- });
712
- it('Should fail if table has no primary key', () => {
713
- expect(() => {
714
- (0, dist_1.Upsert)(infoMock).conflict();
715
- }).toThrowError('Current table has no primary key. Please provide unique index for upsert');
716
- });
717
- it('Should fail if table indexes meta is empty', () => {
718
- expect(() => {
719
- (0, dist_1.Upsert)(infoMock).conflict('id');
720
- }).toThrowError('Table indexes meta is empty');
721
- });
722
- it('Should fail if index is not declared', () => {
723
- expect(() => {
724
- (0, dist_1.Upsert)(models_1.Person).conflict('name');
725
- }).toThrowError(`Index name is not declared for table ${tableName}`);
726
- });
727
- });
728
- describe('Update', () => {
729
- it('Check query info', () => {
730
- const query = (0, dist_1.Update)(models_1.Person);
731
- expect(query._type).toBe('UPDATE');
732
- expect(query._table).toStrictEqual(tableInfo);
733
- });
734
- it('Update record', () => {
735
- const limit = 10;
736
- const query = (0, dist_1.Update)(models_1.Person).values(person).where(({ id }) => id.eq(person.id)).limit(limit);
737
- const expectation = {
738
- values: {
739
- id: person.id,
740
- name: person.name,
741
- age: person.age,
742
- city: person.city,
743
- },
744
- where: [{
745
- _operator: '=',
746
- _operands: [{ _column: { table: tableName, name: 'id' } }, person.id]
747
- }]
748
- };
749
- expect(query._values).toStrictEqual(expectation.values);
750
- expect(query._where).toStrictEqual(expectation.where);
751
- expect(query._limit).toBe(limit);
752
- });
753
- });
754
- describe('Delete', () => {
755
- it('Check query info', () => {
756
- const query = (0, dist_1.Delete)(models_1.Person);
757
- expect(query._type).toBe('DELETE');
758
- expect(query._table).toStrictEqual(tableInfo);
759
- });
760
- it('Delete record', () => {
761
- const limit = 10;
762
- const query = (0, dist_1.Delete)(models_1.Person).where(({ id }) => id.eq(person.id)).limit(limit);
763
- const expectation = [{
764
- _operator: '=',
765
- _operands: [{ _column: { table: tableName, name: 'id' } }, person.id]
766
- }];
767
- expect(query._where).toStrictEqual(expectation);
768
- expect(query._limit).toBe(limit);
769
- });
770
- });