@ts-awesome/orm 1.3.0-rc3 → 1.5.0-alpha.12
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.
- package/.gitlab-ci.yml +6 -2
- package/dist/base.d.ts +10 -5
- package/dist/base.js +27 -19
- package/dist/base.js.map +1 -1
- package/dist/builder.d.ts +1 -1
- package/dist/builder.js +71 -41
- package/dist/builder.js.map +1 -1
- package/dist/decorators.js +16 -18
- package/dist/decorators.js.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/interfaces.d.ts +35 -27
- package/dist/intermediate.d.ts +10 -2
- package/dist/operators.d.ts +1 -0
- package/dist/operators.js +7 -3
- package/dist/operators.js.map +1 -1
- package/dist/reader.js +2 -2
- package/dist/reader.js.map +1 -1
- package/dist/symbols.d.ts +5 -1
- package/dist/symbols.js +6 -2
- package/dist/symbols.js.map +1 -1
- package/dist/test-driver/compiler.js +1 -1
- package/dist/test-driver/compiler.js.map +1 -1
- package/dist/test-driver/driver.js +3 -14
- package/dist/test-driver/driver.js.map +1 -1
- package/dist/test-driver/executor.js +3 -3
- package/dist/test-driver/executor.js.map +1 -1
- package/dist/test-driver/index.js +5 -1
- package/dist/test-driver/index.js.map +1 -1
- package/dist/test-driver/interfaces.d.ts +3 -3
- package/dist/test-driver/transaction.js +10 -25
- package/dist/test-driver/transaction.js.map +1 -1
- package/dist/wrappers.d.ts +95 -1
- package/dist/wrappers.js +56 -4
- package/dist/wrappers.js.map +1 -1
- package/package.json +11 -11
- package/tests/builder.spec.js +596 -0
- package/tests/builder.spec.ts +56 -60
- package/tests/models.js +112 -0
- package/tests/reader.spec.js +128 -0
package/tests/builder.spec.ts
CHANGED
|
@@ -28,18 +28,18 @@ describe('Select', () => {
|
|
|
28
28
|
|
|
29
29
|
const expectation = {
|
|
30
30
|
default: [
|
|
31
|
-
{_column: {table: tableName, name: 'name'
|
|
32
|
-
{_column: {table: tableName, name: 'age'
|
|
31
|
+
{_column: {table: tableName, name: 'name'}},
|
|
32
|
+
{_column: {table: tableName, name: 'age'}}
|
|
33
33
|
],
|
|
34
34
|
alias: [
|
|
35
|
-
{_alias: nameAlias, _operands: [{_column: {table: tableName, name: 'name'
|
|
35
|
+
{_alias: nameAlias, _operands: [{_column: {table: tableName, name: 'name'}}]}
|
|
36
36
|
],
|
|
37
37
|
of: [
|
|
38
38
|
{_column: {table: readModelMeta(Employee).tableName, name: 'company'}}
|
|
39
39
|
],
|
|
40
40
|
expression: [
|
|
41
|
-
{_operator: '*', _operands: [{_column: {table: tableName, name: 'age'
|
|
42
|
-
{_func: 'MAX', _args: [{_column: {table: tableName, name: 'age'
|
|
41
|
+
{_operator: '*', _operands: [{_column: {table: tableName, name: 'age'}}, coefficient]},
|
|
42
|
+
{_func: 'MAX', _args: [{_column: {table: tableName, name: 'age'}}]}
|
|
43
43
|
]
|
|
44
44
|
};
|
|
45
45
|
|
|
@@ -65,7 +65,7 @@ describe('Select', () => {
|
|
|
65
65
|
_alias: undefined,
|
|
66
66
|
_type: joinTypes.inner,
|
|
67
67
|
_condition: {
|
|
68
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
68
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, {_column: {table: employeeTableInfo.tableName, name: `personId`}}],
|
|
69
69
|
_operator: '='
|
|
70
70
|
}
|
|
71
71
|
}];
|
|
@@ -74,7 +74,7 @@ describe('Select', () => {
|
|
|
74
74
|
_alias: undefined,
|
|
75
75
|
_type: joinTypes.left,
|
|
76
76
|
_condition: {
|
|
77
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
77
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, {_column: {table: employeeTableInfo.tableName, name: `personId`}}],
|
|
78
78
|
_operator: '='
|
|
79
79
|
}
|
|
80
80
|
}];
|
|
@@ -83,7 +83,7 @@ describe('Select', () => {
|
|
|
83
83
|
_alias: undefined,
|
|
84
84
|
_type: joinTypes.right,
|
|
85
85
|
_condition: {
|
|
86
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
86
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, {_column: {table: employeeTableInfo.tableName, name: `personId`}}],
|
|
87
87
|
_operator: '='
|
|
88
88
|
}
|
|
89
89
|
}];
|
|
@@ -92,7 +92,7 @@ describe('Select', () => {
|
|
|
92
92
|
_alias: undefined,
|
|
93
93
|
_type: joinTypes.full,
|
|
94
94
|
_condition: {
|
|
95
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
95
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, {_column: {table: employeeTableInfo.tableName, name: `personId`}}],
|
|
96
96
|
_operator: '='
|
|
97
97
|
}
|
|
98
98
|
}];
|
|
@@ -119,7 +119,7 @@ describe('Select', () => {
|
|
|
119
119
|
_alias: tableRef.tableName,
|
|
120
120
|
_type: joinTypes.inner,
|
|
121
121
|
_condition: {
|
|
122
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
122
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, {_column: {table: tableRef.tableName, name: `personId`}}],
|
|
123
123
|
_operator: '='
|
|
124
124
|
}
|
|
125
125
|
}];
|
|
@@ -128,7 +128,7 @@ describe('Select', () => {
|
|
|
128
128
|
_alias: tableRef.tableName,
|
|
129
129
|
_type: joinTypes.left,
|
|
130
130
|
_condition: {
|
|
131
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
131
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, {_column: {table: tableRef.tableName, name: `personId`}}],
|
|
132
132
|
_operator: '='
|
|
133
133
|
}
|
|
134
134
|
}];
|
|
@@ -137,7 +137,7 @@ describe('Select', () => {
|
|
|
137
137
|
_alias: tableRef.tableName,
|
|
138
138
|
_type: joinTypes.right,
|
|
139
139
|
_condition: {
|
|
140
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
140
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, {_column: {table: tableRef.tableName, name: `personId`}}],
|
|
141
141
|
_operator: '='
|
|
142
142
|
}
|
|
143
143
|
}];
|
|
@@ -146,7 +146,7 @@ describe('Select', () => {
|
|
|
146
146
|
_alias: tableRef.tableName,
|
|
147
147
|
_type: joinTypes.full,
|
|
148
148
|
_condition: {
|
|
149
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
149
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, {_column: {table: tableRef.tableName, name: `personId`}}],
|
|
150
150
|
_operator: '='
|
|
151
151
|
}
|
|
152
152
|
}];
|
|
@@ -162,8 +162,8 @@ describe('Select', () => {
|
|
|
162
162
|
const expectation = [{
|
|
163
163
|
_operator: 'AND',
|
|
164
164
|
_operands: [
|
|
165
|
-
{_operator: '=', _operands: [{_column: {table: tableName, name: 'age'
|
|
166
|
-
{_operator: 'LIKE', _operands: [{_column: {table: tableName, name: 'name'
|
|
165
|
+
{_operator: '=', _operands: [{_column: {table: tableName, name: 'age'}}, person.age]},
|
|
166
|
+
{_operator: 'LIKE', _operands: [{_column: {table: tableName, name: 'name'}}, person.name]},
|
|
167
167
|
]
|
|
168
168
|
}];
|
|
169
169
|
expect(query._where).toStrictEqual(expectation);
|
|
@@ -208,7 +208,7 @@ describe('Select', () => {
|
|
|
208
208
|
_operator: 'SUBQUERY',
|
|
209
209
|
_operands: [
|
|
210
210
|
{
|
|
211
|
-
_columns: [{_column: { table: "Tag", name: "name"
|
|
211
|
+
_columns: [{_column: { table: "Tag", name: "name"}}],
|
|
212
212
|
_table: readModelMeta(Tag),
|
|
213
213
|
_alias: null,
|
|
214
214
|
_type: "SELECT",
|
|
@@ -226,14 +226,12 @@ describe('Select', () => {
|
|
|
226
226
|
_column: {
|
|
227
227
|
name: 'id',
|
|
228
228
|
table: "Tag",
|
|
229
|
-
wrapper: undefined
|
|
230
229
|
}
|
|
231
230
|
},
|
|
232
231
|
{
|
|
233
232
|
_column: {
|
|
234
233
|
name: 'tag',
|
|
235
234
|
table: "TagPerson",
|
|
236
|
-
wrapper: undefined
|
|
237
235
|
}
|
|
238
236
|
}
|
|
239
237
|
],
|
|
@@ -245,7 +243,6 @@ describe('Select', () => {
|
|
|
245
243
|
_column: {
|
|
246
244
|
name: 'person',
|
|
247
245
|
table: "TagPerson",
|
|
248
|
-
wrapper: undefined
|
|
249
246
|
}
|
|
250
247
|
},
|
|
251
248
|
{
|
|
@@ -280,13 +277,13 @@ describe('Select', () => {
|
|
|
280
277
|
.having(({salary}) => sum(salary).gt(salaryRate));
|
|
281
278
|
|
|
282
279
|
const expectation = {
|
|
283
|
-
_columns: [{_func: 'SUM', _args: [{_column: {table: employeeTableName, name: 'salary'
|
|
284
|
-
_groupBy: [{_column: {table: employeeTableName, name: 'company'
|
|
280
|
+
_columns: [{_func: 'SUM', _args: [{_column: {table: employeeTableName, name: 'salary'}}]}],
|
|
281
|
+
_groupBy: [{_column: {table: employeeTableName, name: 'company'}}],
|
|
285
282
|
_having: [{
|
|
286
283
|
_operator: '>',
|
|
287
284
|
_operands: [{
|
|
288
285
|
_func: 'SUM',
|
|
289
|
-
_args: [{_column: {table: employeeTableName, name: 'salary'
|
|
286
|
+
_args: [{_column: {table: employeeTableName, name: 'salary'}}]
|
|
290
287
|
}, salaryRate]
|
|
291
288
|
}]
|
|
292
289
|
};
|
|
@@ -299,7 +296,7 @@ describe('Select', () => {
|
|
|
299
296
|
it('Group by', () => {
|
|
300
297
|
const queryThroughList = Select(Person).groupBy(['city']);
|
|
301
298
|
const queryThroughBuilder = Select(Person).groupBy(({city}) => [city]);
|
|
302
|
-
const expectation = [{_column: {table: tableName, name: 'city'
|
|
299
|
+
const expectation = [{_column: {table: tableName, name: 'city'}}];
|
|
303
300
|
|
|
304
301
|
expect(queryThroughList._groupBy).toStrictEqual(expectation);
|
|
305
302
|
expect(queryThroughBuilder._groupBy).toStrictEqual(expectation);
|
|
@@ -312,9 +309,9 @@ describe('Select', () => {
|
|
|
312
309
|
const descOrder = Select(Person).orderBy(({city}) => [desc(city)]);
|
|
313
310
|
|
|
314
311
|
const expectation = {
|
|
315
|
-
default: [{_column: {table: tableName, name: 'city'
|
|
316
|
-
asc: [{_column: {table: tableName, name: 'city'
|
|
317
|
-
desc: [{_column: {table: tableName, name: 'city'
|
|
312
|
+
default: [{_column: {table: tableName, name: 'city'}}],
|
|
313
|
+
asc: [{_column: {table: tableName, name: 'city'}, _order: 'ASC'}],
|
|
314
|
+
desc: [{_column: {table: tableName, name: 'city'}, _order: 'DESC'}]
|
|
318
315
|
};
|
|
319
316
|
|
|
320
317
|
expect(orderByThroughList._orderBy).toStrictEqual(expectation.default);
|
|
@@ -342,22 +339,21 @@ describe('Select', () => {
|
|
|
342
339
|
_where: [{
|
|
343
340
|
"_operands": [{
|
|
344
341
|
"_alias": "person_filter",
|
|
342
|
+
"_columns": [
|
|
343
|
+
{ "_column": { name: 'id', table: 'person_filter' }},
|
|
344
|
+
{ "_column": { name: 'uid', table: 'person_filter' }},
|
|
345
|
+
{ "_column": { name: 'name', table: 'person_filter' }},
|
|
346
|
+
{ "_column": { name: 'age', table: 'person_filter' }},
|
|
347
|
+
{ "_column": { name: 'city', table: 'person_filter' }},
|
|
348
|
+
],
|
|
345
349
|
"_distinct": false,
|
|
346
350
|
"_table": Person[TableMetadataSymbol],
|
|
347
351
|
"_type": "SELECT",
|
|
348
352
|
"_where": [{
|
|
349
353
|
"_operands": [{
|
|
350
|
-
"_column": {
|
|
351
|
-
"name": "id",
|
|
352
|
-
"table": "person_filter",
|
|
353
|
-
"wrapper": undefined,
|
|
354
|
-
},
|
|
354
|
+
"_column": { "name": "id", "table": "person_filter", },
|
|
355
355
|
}, {
|
|
356
|
-
"_column": {
|
|
357
|
-
"name": "id",
|
|
358
|
-
"table": "Person",
|
|
359
|
-
"wrapper": undefined,
|
|
360
|
-
},
|
|
356
|
+
"_column": { "name": "id", "table": "Person", },
|
|
361
357
|
}],
|
|
362
358
|
"_operator": "=",
|
|
363
359
|
}]
|
|
@@ -414,7 +410,7 @@ describe('Select', () => {
|
|
|
414
410
|
)
|
|
415
411
|
]).orderBy(() => [desc(of(null, 'score'))])
|
|
416
412
|
|
|
417
|
-
const expected = [{"_column": {"table": "Person", "name": "uid"
|
|
413
|
+
const expected = [{"_column": {"table": "Person", "name": "uid"}}, {
|
|
418
414
|
"_alias": "score", "_operands": [{
|
|
419
415
|
"_operator": "+", "_operands": [{
|
|
420
416
|
"_operator": "+",
|
|
@@ -432,7 +428,7 @@ describe('Select', () => {
|
|
|
432
428
|
"_operator": "AND",
|
|
433
429
|
"_operands": [{
|
|
434
430
|
"_operator": "=",
|
|
435
|
-
"_operands": [{"_column": {"table": "actions", "name": "personId"
|
|
431
|
+
"_operands": [{"_column": {"table": "actions", "name": "personId"}}, {
|
|
436
432
|
"_column": {
|
|
437
433
|
"table": "Person",
|
|
438
434
|
"name": "id"
|
|
@@ -440,10 +436,10 @@ describe('Select', () => {
|
|
|
440
436
|
}]
|
|
441
437
|
}, {
|
|
442
438
|
"_operator": "=",
|
|
443
|
-
"_operands": [{"_column": {"table": "actions", "name": "action"
|
|
439
|
+
"_operands": [{"_column": {"table": "actions", "name": "action"}}, "a"]
|
|
444
440
|
}, {
|
|
445
441
|
"_operator": ">=",
|
|
446
|
-
"_operands": [{"_column": {"table": "actions", "name": "created"
|
|
442
|
+
"_operands": [{"_column": {"table": "actions", "name": "created"}}, ts]
|
|
447
443
|
}]
|
|
448
444
|
}]
|
|
449
445
|
}]
|
|
@@ -462,7 +458,7 @@ describe('Select', () => {
|
|
|
462
458
|
"_operator": "AND",
|
|
463
459
|
"_operands": [{
|
|
464
460
|
"_operator": "=",
|
|
465
|
-
"_operands": [{"_column": {"table": "actions", "name": "personId"
|
|
461
|
+
"_operands": [{"_column": {"table": "actions", "name": "personId"}}, {
|
|
466
462
|
"_column": {
|
|
467
463
|
"table": "Person",
|
|
468
464
|
"name": "id"
|
|
@@ -470,10 +466,10 @@ describe('Select', () => {
|
|
|
470
466
|
}]
|
|
471
467
|
}, {
|
|
472
468
|
"_operator": "=",
|
|
473
|
-
"_operands": [{"_column": {"table": "actions", "name": "action"
|
|
469
|
+
"_operands": [{"_column": {"table": "actions", "name": "action"}}, "b"]
|
|
474
470
|
}, {
|
|
475
471
|
"_operator": ">=",
|
|
476
|
-
"_operands": [{"_column": {"table": "actions", "name": "created"
|
|
472
|
+
"_operands": [{"_column": {"table": "actions", "name": "created"}}, ts]
|
|
477
473
|
}]
|
|
478
474
|
}]
|
|
479
475
|
}]
|
|
@@ -493,7 +489,7 @@ describe('Select', () => {
|
|
|
493
489
|
"_operator": "AND",
|
|
494
490
|
"_operands": [{
|
|
495
491
|
"_operator": "=",
|
|
496
|
-
"_operands": [{"_column": {"table": "actions", "name": "personId"
|
|
492
|
+
"_operands": [{"_column": {"table": "actions", "name": "personId"}}, {
|
|
497
493
|
"_column": {
|
|
498
494
|
"table": "Person",
|
|
499
495
|
"name": "id"
|
|
@@ -501,10 +497,10 @@ describe('Select', () => {
|
|
|
501
497
|
}]
|
|
502
498
|
}, {
|
|
503
499
|
"_operator": "=",
|
|
504
|
-
"_operands": [{"_column": {"table": "actions", "name": "action"
|
|
500
|
+
"_operands": [{"_column": {"table": "actions", "name": "action"}}, "c"]
|
|
505
501
|
}, {
|
|
506
502
|
"_operator": ">=",
|
|
507
|
-
"_operands": [{"_column": {"table": "actions", "name": "created"
|
|
503
|
+
"_operands": [{"_column": {"table": "actions", "name": "created"}}, ts]
|
|
508
504
|
}]
|
|
509
505
|
}]
|
|
510
506
|
}]
|
|
@@ -544,10 +540,10 @@ describe('Insert', () => {
|
|
|
544
540
|
it('Insert record', () => {
|
|
545
541
|
const query = Insert(Person).values(person);
|
|
546
542
|
const expectation = {
|
|
547
|
-
id:
|
|
548
|
-
name:
|
|
549
|
-
age:
|
|
550
|
-
city:
|
|
543
|
+
id: person.id,
|
|
544
|
+
name: person.name,
|
|
545
|
+
age: person.age,
|
|
546
|
+
city: person.city,
|
|
551
547
|
};
|
|
552
548
|
expect(query._values).toStrictEqual(expectation);
|
|
553
549
|
Insert(Person).values({...person, uid: 'a80ec30e-791c-4499-a243-70af8b2bf7ba'});
|
|
@@ -571,10 +567,10 @@ describe('Upsert', () => {
|
|
|
571
567
|
const withConflict = Upsert(Person).values(person).conflict('idx');
|
|
572
568
|
const expectation = {
|
|
573
569
|
values: {
|
|
574
|
-
id:
|
|
575
|
-
name:
|
|
576
|
-
age:
|
|
577
|
-
city:
|
|
570
|
+
id: person.id,
|
|
571
|
+
name: person.name,
|
|
572
|
+
age: person.age,
|
|
573
|
+
city: person.city,
|
|
578
574
|
},
|
|
579
575
|
conflictExp: {_columns: ['id'], _where: undefined}
|
|
580
576
|
};
|
|
@@ -617,14 +613,14 @@ describe('Update', () => {
|
|
|
617
613
|
const query = Update(Person).values(person).where(({id}) => id.eq(person.id)).limit(limit);
|
|
618
614
|
const expectation = {
|
|
619
615
|
values: {
|
|
620
|
-
id:
|
|
621
|
-
name:
|
|
622
|
-
age:
|
|
623
|
-
city:
|
|
616
|
+
id: person.id,
|
|
617
|
+
name: person.name,
|
|
618
|
+
age: person.age,
|
|
619
|
+
city: person.city,
|
|
624
620
|
},
|
|
625
621
|
where: [{
|
|
626
622
|
_operator: '=',
|
|
627
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
623
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, person.id]
|
|
628
624
|
}]
|
|
629
625
|
};
|
|
630
626
|
expect(query._values).toStrictEqual(expectation.values);
|
|
@@ -646,7 +642,7 @@ describe('Delete', () => {
|
|
|
646
642
|
const query = Delete(Person).where(({id}) => id.eq(person.id)).limit(limit);
|
|
647
643
|
const expectation = [{
|
|
648
644
|
_operator: '=',
|
|
649
|
-
_operands: [{_column: {table: tableName, name: 'id'
|
|
645
|
+
_operands: [{_column: {table: tableName, name: 'id'}}, person.id]
|
|
650
646
|
}];
|
|
651
647
|
expect(query._where).toStrictEqual(expectation);
|
|
652
648
|
expect(query._limit).toBe(limit);
|
package/tests/models.js
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
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
|
+
exports.Employee = exports.TagPerson = exports.Tag = exports.Person = void 0;
|
|
13
|
+
const src_1 = require("../src");
|
|
14
|
+
const UUID = {};
|
|
15
|
+
let Person = class Person {
|
|
16
|
+
};
|
|
17
|
+
__decorate([
|
|
18
|
+
(0, src_1.dbField)({
|
|
19
|
+
primaryKey: true,
|
|
20
|
+
autoIncrement: true
|
|
21
|
+
}),
|
|
22
|
+
__metadata("design:type", Number)
|
|
23
|
+
], Person.prototype, "id", void 0);
|
|
24
|
+
__decorate([
|
|
25
|
+
(0, src_1.dbField)({
|
|
26
|
+
kind: UUID
|
|
27
|
+
}),
|
|
28
|
+
__metadata("design:type", String)
|
|
29
|
+
], Person.prototype, "uid", void 0);
|
|
30
|
+
__decorate([
|
|
31
|
+
src_1.dbField,
|
|
32
|
+
__metadata("design:type", String)
|
|
33
|
+
], Person.prototype, "name", void 0);
|
|
34
|
+
__decorate([
|
|
35
|
+
src_1.dbField,
|
|
36
|
+
__metadata("design:type", Number)
|
|
37
|
+
], Person.prototype, "age", void 0);
|
|
38
|
+
__decorate([
|
|
39
|
+
src_1.dbField,
|
|
40
|
+
__metadata("design:type", String)
|
|
41
|
+
], Person.prototype, "city", void 0);
|
|
42
|
+
__decorate([
|
|
43
|
+
(0, src_1.dbFilterField)({
|
|
44
|
+
table: 'employee',
|
|
45
|
+
keyField: 'person',
|
|
46
|
+
valueField: 'title',
|
|
47
|
+
}),
|
|
48
|
+
__metadata("design:type", Array)
|
|
49
|
+
], Person.prototype, "profiles", void 0);
|
|
50
|
+
__decorate([
|
|
51
|
+
(0, src_1.dbFilterField)(primary => (0, src_1.Select)(Tag)
|
|
52
|
+
.columns((x) => [x.name])
|
|
53
|
+
.join(TagPerson, (a, b) => (0, src_1.and)(a.id.eq(b.tag), b.person.eq(primary)))),
|
|
54
|
+
__metadata("design:type", Array)
|
|
55
|
+
], Person.prototype, "tags", void 0);
|
|
56
|
+
Person = __decorate([
|
|
57
|
+
(0, src_1.dbTable)('Person', [{ name: 'idx', fields: ['id'] }])
|
|
58
|
+
], Person);
|
|
59
|
+
exports.Person = Person;
|
|
60
|
+
let Tag = class Tag {
|
|
61
|
+
};
|
|
62
|
+
__decorate([
|
|
63
|
+
(0, src_1.dbField)({
|
|
64
|
+
primaryKey: true,
|
|
65
|
+
autoIncrement: true
|
|
66
|
+
}),
|
|
67
|
+
__metadata("design:type", Number)
|
|
68
|
+
], Tag.prototype, "id", void 0);
|
|
69
|
+
__decorate([
|
|
70
|
+
src_1.dbField,
|
|
71
|
+
__metadata("design:type", String)
|
|
72
|
+
], Tag.prototype, "name", void 0);
|
|
73
|
+
Tag = __decorate([
|
|
74
|
+
(0, src_1.dbTable)('Tag')
|
|
75
|
+
], Tag);
|
|
76
|
+
exports.Tag = Tag;
|
|
77
|
+
let TagPerson = class TagPerson {
|
|
78
|
+
};
|
|
79
|
+
__decorate([
|
|
80
|
+
(0, src_1.dbField)({
|
|
81
|
+
primaryKey: true,
|
|
82
|
+
}),
|
|
83
|
+
__metadata("design:type", Number)
|
|
84
|
+
], TagPerson.prototype, "person", void 0);
|
|
85
|
+
__decorate([
|
|
86
|
+
(0, src_1.dbField)({
|
|
87
|
+
primaryKey: true,
|
|
88
|
+
}),
|
|
89
|
+
__metadata("design:type", Number)
|
|
90
|
+
], TagPerson.prototype, "tag", void 0);
|
|
91
|
+
TagPerson = __decorate([
|
|
92
|
+
(0, src_1.dbTable)('TagPerson')
|
|
93
|
+
], TagPerson);
|
|
94
|
+
exports.TagPerson = TagPerson;
|
|
95
|
+
let Employee = class Employee {
|
|
96
|
+
};
|
|
97
|
+
__decorate([
|
|
98
|
+
src_1.dbField,
|
|
99
|
+
__metadata("design:type", Number)
|
|
100
|
+
], Employee.prototype, "personId", void 0);
|
|
101
|
+
__decorate([
|
|
102
|
+
src_1.dbField,
|
|
103
|
+
__metadata("design:type", String)
|
|
104
|
+
], Employee.prototype, "company", void 0);
|
|
105
|
+
__decorate([
|
|
106
|
+
src_1.dbField,
|
|
107
|
+
__metadata("design:type", Number)
|
|
108
|
+
], Employee.prototype, "salary", void 0);
|
|
109
|
+
Employee = __decorate([
|
|
110
|
+
(0, src_1.dbTable)('Employee')
|
|
111
|
+
], Employee);
|
|
112
|
+
exports.Employee = Employee;
|
|
@@ -0,0 +1,128 @@
|
|
|
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 DB_JSON = {
|
|
15
|
+
reader(raw) {
|
|
16
|
+
return typeof raw === 'string' ? JSON.parse(raw) : raw;
|
|
17
|
+
},
|
|
18
|
+
writer(value) {
|
|
19
|
+
return JSON.stringify(value);
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
function generatePersons(quantity) {
|
|
23
|
+
const res = [];
|
|
24
|
+
for (let i = 1; i <= quantity; i++) {
|
|
25
|
+
const person = new models_1.Person();
|
|
26
|
+
person['id'] = i;
|
|
27
|
+
person['city'] = `TestCity${i}`;
|
|
28
|
+
person['age'] = 18;
|
|
29
|
+
person['name'] = `TestName${i}`;
|
|
30
|
+
person['uid'] = `uid${i}`;
|
|
31
|
+
// person['profiles'] = ['profile-a'];
|
|
32
|
+
res.push(person);
|
|
33
|
+
}
|
|
34
|
+
return res;
|
|
35
|
+
}
|
|
36
|
+
describe('DbReader', () => {
|
|
37
|
+
const persons = generatePersons(5);
|
|
38
|
+
it('read raw', () => {
|
|
39
|
+
const result = (0, dist_1.reader)(persons);
|
|
40
|
+
expect(result).toStrictEqual(persons);
|
|
41
|
+
result.map(person => {
|
|
42
|
+
expect(person).toBeInstanceOf(models_1.Person);
|
|
43
|
+
expect(person).toStrictEqual(persons.find(p => p.id === person.id));
|
|
44
|
+
});
|
|
45
|
+
});
|
|
46
|
+
it('read Model', () => {
|
|
47
|
+
let Model = class Model {
|
|
48
|
+
constructor(id, raw, personal) {
|
|
49
|
+
this.id = id;
|
|
50
|
+
this.value = raw;
|
|
51
|
+
this.personal = personal;
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
__decorate([
|
|
55
|
+
dist_1.dbField,
|
|
56
|
+
__metadata("design:type", Number)
|
|
57
|
+
], Model.prototype, "id", void 0);
|
|
58
|
+
__decorate([
|
|
59
|
+
(0, dist_1.dbField)('raw'),
|
|
60
|
+
__metadata("design:type", String)
|
|
61
|
+
], Model.prototype, "value", void 0);
|
|
62
|
+
__decorate([
|
|
63
|
+
(0, dist_1.dbField)({ kind: DB_JSON, model: [models_1.Person] }),
|
|
64
|
+
__metadata("design:type", Array)
|
|
65
|
+
], Model.prototype, "personal", void 0);
|
|
66
|
+
Model = __decorate([
|
|
67
|
+
dist_1.dbTable,
|
|
68
|
+
__metadata("design:paramtypes", [Object, Object, Object])
|
|
69
|
+
], Model);
|
|
70
|
+
const model = {
|
|
71
|
+
id: "5",
|
|
72
|
+
raw: false,
|
|
73
|
+
personal: JSON.stringify(persons)
|
|
74
|
+
};
|
|
75
|
+
const expected = new Model(5, 'false', persons);
|
|
76
|
+
const results = (0, dist_1.reader)([model], Model);
|
|
77
|
+
expect(results.length).toBe(1);
|
|
78
|
+
const [result] = results;
|
|
79
|
+
expect(result).toBeInstanceOf(Model);
|
|
80
|
+
expect(result).toEqual(expected);
|
|
81
|
+
});
|
|
82
|
+
it('read Model from no data', () => {
|
|
83
|
+
let Model = class Model {
|
|
84
|
+
constructor(id, raw, personal) {
|
|
85
|
+
this.id = id;
|
|
86
|
+
this.value = raw;
|
|
87
|
+
this.personal = personal;
|
|
88
|
+
}
|
|
89
|
+
};
|
|
90
|
+
__decorate([
|
|
91
|
+
dist_1.dbField,
|
|
92
|
+
__metadata("design:type", Number)
|
|
93
|
+
], Model.prototype, "id", void 0);
|
|
94
|
+
__decorate([
|
|
95
|
+
(0, dist_1.dbField)('raw'),
|
|
96
|
+
__metadata("design:type", String)
|
|
97
|
+
], Model.prototype, "value", void 0);
|
|
98
|
+
__decorate([
|
|
99
|
+
(0, dist_1.dbField)({ kind: DB_JSON, model: [models_1.Person] }),
|
|
100
|
+
__metadata("design:type", Array)
|
|
101
|
+
], Model.prototype, "personal", void 0);
|
|
102
|
+
Model = __decorate([
|
|
103
|
+
dist_1.dbTable,
|
|
104
|
+
__metadata("design:paramtypes", [Object, Object, Object])
|
|
105
|
+
], Model);
|
|
106
|
+
const results = (0, dist_1.reader)([], Model);
|
|
107
|
+
expect(results.length).toBe(0);
|
|
108
|
+
});
|
|
109
|
+
it('read scalar', () => {
|
|
110
|
+
const count = 10;
|
|
111
|
+
const correctResult = (0, dist_1.reader)([{ field: count }], true);
|
|
112
|
+
expect(correctResult).toBe(count);
|
|
113
|
+
});
|
|
114
|
+
it('read scalar from no data', () => {
|
|
115
|
+
const fieldValue = 'string';
|
|
116
|
+
const emptyData = (0, dist_1.reader)([], true);
|
|
117
|
+
expect(emptyData).toBe(0);
|
|
118
|
+
expect(() => {
|
|
119
|
+
(0, dist_1.reader)([{ field: fieldValue }], true);
|
|
120
|
+
}).toThrowError(`Invalid scalar "${fieldValue}", number expected.`);
|
|
121
|
+
});
|
|
122
|
+
it('read scalar from invalid data', () => {
|
|
123
|
+
const fieldValue = 'string';
|
|
124
|
+
expect(() => {
|
|
125
|
+
(0, dist_1.reader)([{ field: fieldValue }], true);
|
|
126
|
+
}).toThrowError(`Invalid scalar "${fieldValue}", number expected.`);
|
|
127
|
+
});
|
|
128
|
+
});
|