@ts-awesome/orm 1.5.0 → 1.5.1

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,604 @@
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
+ });
502
+ describe('Insert', () => {
503
+ it('Check query info', () => {
504
+ const query = (0, dist_1.Insert)(models_1.Person);
505
+ expect(query._type).toBe('INSERT');
506
+ expect(query._table).toStrictEqual(tableInfo);
507
+ });
508
+ it('Insert record', () => {
509
+ const query = (0, dist_1.Insert)(models_1.Person).values(person);
510
+ const expectation = {
511
+ id: person.id,
512
+ name: person.name,
513
+ age: person.age,
514
+ city: person.city,
515
+ };
516
+ expect(query._values).toStrictEqual(expectation);
517
+ (0, dist_1.Insert)(models_1.Person).values(Object.assign(Object.assign({}, person), { uid: 'a80ec30e-791c-4499-a243-70af8b2bf7ba' }));
518
+ });
519
+ });
520
+ describe('Upsert', () => {
521
+ const infoMock = {
522
+ [dist_1.TableMetadataSymbol]: {}
523
+ };
524
+ it('Check query info', () => {
525
+ const query = (0, dist_1.Upsert)(models_1.Person);
526
+ expect(query._type).toBe('UPSERT');
527
+ expect(query._table).toStrictEqual(tableInfo);
528
+ });
529
+ it('Upsert record', () => {
530
+ const defaultUpsert = (0, dist_1.Upsert)(models_1.Person).values(person);
531
+ const withConflict = (0, dist_1.Upsert)(models_1.Person).values(person).conflict('idx');
532
+ const expectation = {
533
+ values: {
534
+ id: person.id,
535
+ name: person.name,
536
+ age: person.age,
537
+ city: person.city,
538
+ },
539
+ conflictExp: { _columns: ['id'], _where: undefined }
540
+ };
541
+ expect(defaultUpsert._values).toStrictEqual(expectation.values);
542
+ expect(defaultUpsert._conflictExp).toBeUndefined();
543
+ expect(withConflict._values).toStrictEqual(expectation.values);
544
+ expect(withConflict._conflictExp).toStrictEqual(expectation.conflictExp);
545
+ });
546
+ it('Should fail if table has no primary key', () => {
547
+ expect(() => {
548
+ (0, dist_1.Upsert)(infoMock).conflict();
549
+ }).toThrowError('Current table has no primary key. Please provide unique index for upsert');
550
+ });
551
+ it('Should fail if table indexes meta is empty', () => {
552
+ expect(() => {
553
+ (0, dist_1.Upsert)(infoMock).conflict('id');
554
+ }).toThrowError('Table indexes meta is empty');
555
+ });
556
+ it('Should fail if index is not declared', () => {
557
+ expect(() => {
558
+ (0, dist_1.Upsert)(models_1.Person).conflict('name');
559
+ }).toThrowError(`Index name is not declared for table ${tableName}`);
560
+ });
561
+ });
562
+ describe('Update', () => {
563
+ it('Check query info', () => {
564
+ const query = (0, dist_1.Update)(models_1.Person);
565
+ expect(query._type).toBe('UPDATE');
566
+ expect(query._table).toStrictEqual(tableInfo);
567
+ });
568
+ it('Update record', () => {
569
+ const limit = 10;
570
+ const query = (0, dist_1.Update)(models_1.Person).values(person).where(({ id }) => id.eq(person.id)).limit(limit);
571
+ const expectation = {
572
+ values: {
573
+ id: person.id,
574
+ name: person.name,
575
+ age: person.age,
576
+ city: person.city,
577
+ },
578
+ where: [{
579
+ _operator: '=',
580
+ _operands: [{ _column: { table: tableName, name: 'id' } }, person.id]
581
+ }]
582
+ };
583
+ expect(query._values).toStrictEqual(expectation.values);
584
+ expect(query._where).toStrictEqual(expectation.where);
585
+ expect(query._limit).toBe(limit);
586
+ });
587
+ });
588
+ describe('Delete', () => {
589
+ it('Check query info', () => {
590
+ const query = (0, dist_1.Delete)(models_1.Person);
591
+ expect(query._type).toBe('DELETE');
592
+ expect(query._table).toStrictEqual(tableInfo);
593
+ });
594
+ it('Delete record', () => {
595
+ const limit = 10;
596
+ const query = (0, dist_1.Delete)(models_1.Person).where(({ id }) => id.eq(person.id)).limit(limit);
597
+ const expectation = [{
598
+ _operator: '=',
599
+ _operands: [{ _column: { table: tableName, name: 'id' } }, person.id]
600
+ }];
601
+ expect(query._where).toStrictEqual(expectation);
602
+ expect(query._limit).toBe(limit);
603
+ });
604
+ });
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const wrappers_1 = require("../src/wrappers");
13
+ const test_driver_1 = require("../src/test-driver");
14
+ describe('BaseExecutor', () => {
15
+ it('named params', () => __awaiter(void 0, void 0, void 0, function* () {
16
+ const sqlName = 'test';
17
+ const namedParam = new wrappers_1.NamedParameter(sqlName);
18
+ const executor = new test_driver_1.TestExecutor();
19
+ executor.mapper = (query) => {
20
+ expect(query).toBeDefined();
21
+ expect(query.params).toBeDefined();
22
+ expect(query.params[sqlName]).toBe(123);
23
+ return [];
24
+ };
25
+ executor.setNamedParameter(namedParam, 123);
26
+ yield executor.execute({});
27
+ }));
28
+ });