@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
|
@@ -0,0 +1,596 @@
|
|
|
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
|
+
_joins: [
|
|
206
|
+
{
|
|
207
|
+
_alias: undefined,
|
|
208
|
+
_tableName: "TagPerson",
|
|
209
|
+
_type: "INNER",
|
|
210
|
+
_condition: {
|
|
211
|
+
_operands: [
|
|
212
|
+
{
|
|
213
|
+
_operands: [
|
|
214
|
+
{
|
|
215
|
+
_column: {
|
|
216
|
+
name: 'id',
|
|
217
|
+
table: "Tag",
|
|
218
|
+
}
|
|
219
|
+
},
|
|
220
|
+
{
|
|
221
|
+
_column: {
|
|
222
|
+
name: 'tag',
|
|
223
|
+
table: "TagPerson",
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
],
|
|
227
|
+
_operator: '='
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
_operands: [
|
|
231
|
+
{
|
|
232
|
+
_column: {
|
|
233
|
+
name: 'person',
|
|
234
|
+
table: "TagPerson",
|
|
235
|
+
}
|
|
236
|
+
},
|
|
237
|
+
{
|
|
238
|
+
_column: {
|
|
239
|
+
name: 'id',
|
|
240
|
+
table: "Person",
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
],
|
|
244
|
+
_operator: '='
|
|
245
|
+
},
|
|
246
|
+
],
|
|
247
|
+
_operator: 'AND'
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
]
|
|
251
|
+
}
|
|
252
|
+
]
|
|
253
|
+
},
|
|
254
|
+
],
|
|
255
|
+
_operator: "IN",
|
|
256
|
+
}];
|
|
257
|
+
expect(query._where).toStrictEqual(expectation);
|
|
258
|
+
});
|
|
259
|
+
});
|
|
260
|
+
it('Having', () => {
|
|
261
|
+
const employeeTableName = (0, builder_1.readModelMeta)(models_1.Employee).tableName;
|
|
262
|
+
const salaryRate = 2000;
|
|
263
|
+
const query = (0, dist_1.Select)(models_1.Employee)
|
|
264
|
+
.columns(({ salary }) => [(0, dist_1.sum)(salary)])
|
|
265
|
+
.groupBy(['company'])
|
|
266
|
+
.having(({ salary }) => (0, dist_1.sum)(salary).gt(salaryRate));
|
|
267
|
+
const expectation = {
|
|
268
|
+
_columns: [{ _func: 'SUM', _args: [{ _column: { table: employeeTableName, name: 'salary' } }] }],
|
|
269
|
+
_groupBy: [{ _column: { table: employeeTableName, name: 'company' } }],
|
|
270
|
+
_having: [{
|
|
271
|
+
_operator: '>',
|
|
272
|
+
_operands: [{
|
|
273
|
+
_func: 'SUM',
|
|
274
|
+
_args: [{ _column: { table: employeeTableName, name: 'salary' } }]
|
|
275
|
+
}, salaryRate]
|
|
276
|
+
}]
|
|
277
|
+
};
|
|
278
|
+
expect(query._columns).toStrictEqual(expectation._columns);
|
|
279
|
+
expect(query._groupBy).toStrictEqual(expectation._groupBy);
|
|
280
|
+
expect(query._having).toStrictEqual(expectation._having);
|
|
281
|
+
});
|
|
282
|
+
it('Group by', () => {
|
|
283
|
+
const queryThroughList = (0, dist_1.Select)(models_1.Person).groupBy(['city']);
|
|
284
|
+
const queryThroughBuilder = (0, dist_1.Select)(models_1.Person).groupBy(({ city }) => [city]);
|
|
285
|
+
const expectation = [{ _column: { table: tableName, name: 'city' } }];
|
|
286
|
+
expect(queryThroughList._groupBy).toStrictEqual(expectation);
|
|
287
|
+
expect(queryThroughBuilder._groupBy).toStrictEqual(expectation);
|
|
288
|
+
});
|
|
289
|
+
it('Order By', () => {
|
|
290
|
+
const orderByThroughList = (0, dist_1.Select)(models_1.Person).orderBy(['city']);
|
|
291
|
+
const defaultOrder = (0, dist_1.Select)(models_1.Person).orderBy(({ city }) => [city]);
|
|
292
|
+
const ascOrder = (0, dist_1.Select)(models_1.Person).orderBy(({ city }) => [(0, dist_1.asc)(city)]);
|
|
293
|
+
const descOrder = (0, dist_1.Select)(models_1.Person).orderBy(({ city }) => [(0, dist_1.desc)(city)]);
|
|
294
|
+
const expectation = {
|
|
295
|
+
default: [{ _column: { table: tableName, name: 'city' } }],
|
|
296
|
+
asc: [{ _column: { table: tableName, name: 'city' }, _order: 'ASC' }],
|
|
297
|
+
desc: [{ _column: { table: tableName, name: 'city' }, _order: 'DESC' }]
|
|
298
|
+
};
|
|
299
|
+
expect(orderByThroughList._orderBy).toStrictEqual(expectation.default);
|
|
300
|
+
expect(defaultOrder._orderBy).toStrictEqual(expectation.default);
|
|
301
|
+
expect(ascOrder._orderBy).toStrictEqual(expectation.asc);
|
|
302
|
+
expect(descOrder._orderBy).toStrictEqual(expectation.desc);
|
|
303
|
+
});
|
|
304
|
+
it('Limit', () => {
|
|
305
|
+
const limit = 10;
|
|
306
|
+
const query = (0, dist_1.Select)(models_1.Person).limit(limit);
|
|
307
|
+
expect(query._limit).toBe(limit);
|
|
308
|
+
});
|
|
309
|
+
it('Offset', () => {
|
|
310
|
+
const offset = 3;
|
|
311
|
+
const query = (0, dist_1.Select)(models_1.Person).offset(offset);
|
|
312
|
+
expect(query._offset).toBe(offset);
|
|
313
|
+
});
|
|
314
|
+
it('subquery on self', () => {
|
|
315
|
+
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))));
|
|
316
|
+
const expectation = {
|
|
317
|
+
_where: [{
|
|
318
|
+
"_operands": [{
|
|
319
|
+
"_alias": "person_filter",
|
|
320
|
+
"_columns": [
|
|
321
|
+
{ "_column": { name: 'id', table: 'person_filter' } },
|
|
322
|
+
{ "_column": { name: 'uid', table: 'person_filter' } },
|
|
323
|
+
{ "_column": { name: 'name', table: 'person_filter' } },
|
|
324
|
+
{ "_column": { name: 'age', table: 'person_filter' } },
|
|
325
|
+
{ "_column": { name: 'city', table: 'person_filter' } },
|
|
326
|
+
],
|
|
327
|
+
"_distinct": false,
|
|
328
|
+
"_table": models_1.Person[dist_1.TableMetadataSymbol],
|
|
329
|
+
"_type": "SELECT",
|
|
330
|
+
"_where": [{
|
|
331
|
+
"_operands": [{
|
|
332
|
+
"_column": { "name": "id", "table": "person_filter", },
|
|
333
|
+
}, {
|
|
334
|
+
"_column": { "name": "id", "table": "Person", },
|
|
335
|
+
}],
|
|
336
|
+
"_operator": "=",
|
|
337
|
+
}]
|
|
338
|
+
}],
|
|
339
|
+
"_operator": "EXISTS",
|
|
340
|
+
}]
|
|
341
|
+
};
|
|
342
|
+
expect(query._where).toStrictEqual(expectation._where);
|
|
343
|
+
});
|
|
344
|
+
it('sum of subqueries', () => {
|
|
345
|
+
let PersonAction = class PersonAction {
|
|
346
|
+
};
|
|
347
|
+
__decorate([
|
|
348
|
+
dist_2.dbField,
|
|
349
|
+
__metadata("design:type", Number)
|
|
350
|
+
], PersonAction.prototype, "personId", void 0);
|
|
351
|
+
__decorate([
|
|
352
|
+
dist_2.dbField,
|
|
353
|
+
__metadata("design:type", String)
|
|
354
|
+
], PersonAction.prototype, "action", void 0);
|
|
355
|
+
__decorate([
|
|
356
|
+
dist_2.dbField,
|
|
357
|
+
__metadata("design:type", Date)
|
|
358
|
+
], PersonAction.prototype, "created", void 0);
|
|
359
|
+
PersonAction = __decorate([
|
|
360
|
+
(0, dist_2.dbTable)('actions')
|
|
361
|
+
], PersonAction);
|
|
362
|
+
const ts = new Date(Date.now() - 3600);
|
|
363
|
+
const query = (0, dist_1.Select)(models_1.Person)
|
|
364
|
+
.columns(({ uid }) => [
|
|
365
|
+
uid,
|
|
366
|
+
(0, dist_1.alias)((0, dist_1.Select)(PersonAction)
|
|
367
|
+
.columns(() => [(0, dist_2.count)()])
|
|
368
|
+
.where(({ personId, action, created }) => (0, dist_1.and)(personId.eq((0, dist_1.of)(models_1.Person, 'id')), action.eq('a'), created.gte(ts)))
|
|
369
|
+
.asScalar()
|
|
370
|
+
.mul(1).add((0, dist_1.Select)(PersonAction)
|
|
371
|
+
.columns(() => [(0, dist_2.count)()])
|
|
372
|
+
.where(({ personId, action, created }) => (0, dist_1.and)(personId.eq((0, dist_1.of)(models_1.Person, 'id')), action.eq('b'), created.gte(ts)))
|
|
373
|
+
.asScalar().mul(100)).add((0, dist_1.Select)(PersonAction)
|
|
374
|
+
.columns(() => [(0, dist_2.count)()])
|
|
375
|
+
.where(({ personId, action, created }) => (0, dist_1.and)(personId.eq((0, dist_1.of)(models_1.Person, 'id')), action.eq('c'), created.gte(ts)))
|
|
376
|
+
.asScalar().mul(100)), 'score')
|
|
377
|
+
]).orderBy(() => [(0, dist_1.desc)((0, dist_1.of)(null, 'score'))]);
|
|
378
|
+
const expected = [{ "_column": { "table": "Person", "name": "uid" } }, {
|
|
379
|
+
"_alias": "score", "_operands": [{
|
|
380
|
+
"_operator": "+", "_operands": [{
|
|
381
|
+
"_operator": "+",
|
|
382
|
+
"_operands": [{
|
|
383
|
+
"_operator": "*",
|
|
384
|
+
"_operands": [{
|
|
385
|
+
"_operator": "SUBQUERY",
|
|
386
|
+
"_operands": [{
|
|
387
|
+
"_type": "SELECT",
|
|
388
|
+
"_table": PersonAction[dist_1.TableMetadataSymbol],
|
|
389
|
+
"_alias": null,
|
|
390
|
+
"_distinct": false,
|
|
391
|
+
"_columns": [{ "_func": "COUNT", "_args": ["*"] }],
|
|
392
|
+
"_where": [{
|
|
393
|
+
"_operator": "AND",
|
|
394
|
+
"_operands": [{
|
|
395
|
+
"_operator": "=",
|
|
396
|
+
"_operands": [{ "_column": { "table": "actions", "name": "personId" } }, {
|
|
397
|
+
"_column": {
|
|
398
|
+
"table": "Person",
|
|
399
|
+
"name": "id"
|
|
400
|
+
}
|
|
401
|
+
}]
|
|
402
|
+
}, {
|
|
403
|
+
"_operator": "=",
|
|
404
|
+
"_operands": [{ "_column": { "table": "actions", "name": "action" } }, "a"]
|
|
405
|
+
}, {
|
|
406
|
+
"_operator": ">=",
|
|
407
|
+
"_operands": [{ "_column": { "table": "actions", "name": "created" } }, ts]
|
|
408
|
+
}]
|
|
409
|
+
}]
|
|
410
|
+
}]
|
|
411
|
+
}, 1]
|
|
412
|
+
}, {
|
|
413
|
+
"_operator": "*",
|
|
414
|
+
"_operands": [{
|
|
415
|
+
"_operator": "SUBQUERY",
|
|
416
|
+
"_operands": [{
|
|
417
|
+
"_type": "SELECT",
|
|
418
|
+
"_table": PersonAction[dist_1.TableMetadataSymbol],
|
|
419
|
+
"_alias": null,
|
|
420
|
+
"_distinct": false,
|
|
421
|
+
"_columns": [{ "_func": "COUNT", "_args": ["*"] }],
|
|
422
|
+
"_where": [{
|
|
423
|
+
"_operator": "AND",
|
|
424
|
+
"_operands": [{
|
|
425
|
+
"_operator": "=",
|
|
426
|
+
"_operands": [{ "_column": { "table": "actions", "name": "personId" } }, {
|
|
427
|
+
"_column": {
|
|
428
|
+
"table": "Person",
|
|
429
|
+
"name": "id"
|
|
430
|
+
}
|
|
431
|
+
}]
|
|
432
|
+
}, {
|
|
433
|
+
"_operator": "=",
|
|
434
|
+
"_operands": [{ "_column": { "table": "actions", "name": "action" } }, "b"]
|
|
435
|
+
}, {
|
|
436
|
+
"_operator": ">=",
|
|
437
|
+
"_operands": [{ "_column": { "table": "actions", "name": "created" } }, ts]
|
|
438
|
+
}]
|
|
439
|
+
}]
|
|
440
|
+
}]
|
|
441
|
+
}, 100]
|
|
442
|
+
}]
|
|
443
|
+
}, {
|
|
444
|
+
"_operator": "*",
|
|
445
|
+
"_operands": [{
|
|
446
|
+
"_operator": "SUBQUERY",
|
|
447
|
+
"_operands": [{
|
|
448
|
+
"_type": "SELECT",
|
|
449
|
+
"_table": PersonAction[dist_1.TableMetadataSymbol],
|
|
450
|
+
"_alias": null,
|
|
451
|
+
"_distinct": false,
|
|
452
|
+
"_columns": [{ "_func": "COUNT", "_args": ["*"] }],
|
|
453
|
+
"_where": [{
|
|
454
|
+
"_operator": "AND",
|
|
455
|
+
"_operands": [{
|
|
456
|
+
"_operator": "=",
|
|
457
|
+
"_operands": [{ "_column": { "table": "actions", "name": "personId" } }, {
|
|
458
|
+
"_column": {
|
|
459
|
+
"table": "Person",
|
|
460
|
+
"name": "id"
|
|
461
|
+
}
|
|
462
|
+
}]
|
|
463
|
+
}, {
|
|
464
|
+
"_operator": "=",
|
|
465
|
+
"_operands": [{ "_column": { "table": "actions", "name": "action" } }, "c"]
|
|
466
|
+
}, {
|
|
467
|
+
"_operator": ">=",
|
|
468
|
+
"_operands": [{ "_column": { "table": "actions", "name": "created" } }, ts]
|
|
469
|
+
}]
|
|
470
|
+
}]
|
|
471
|
+
}]
|
|
472
|
+
}, 100]
|
|
473
|
+
}]
|
|
474
|
+
}]
|
|
475
|
+
}];
|
|
476
|
+
expect(query._columns).toStrictEqual(expected);
|
|
477
|
+
expect(query._orderBy).toStrictEqual([{
|
|
478
|
+
_column: {
|
|
479
|
+
name: 'score',
|
|
480
|
+
},
|
|
481
|
+
_order: "DESC"
|
|
482
|
+
}]);
|
|
483
|
+
});
|
|
484
|
+
it('invalid sub queries', () => {
|
|
485
|
+
try {
|
|
486
|
+
(0, dist_1.Select)(models_1.Person).columns(['age', 'uid']).asScalar();
|
|
487
|
+
fail('expected to throw');
|
|
488
|
+
}
|
|
489
|
+
catch (e) {
|
|
490
|
+
// ignore
|
|
491
|
+
}
|
|
492
|
+
});
|
|
493
|
+
});
|
|
494
|
+
describe('Insert', () => {
|
|
495
|
+
it('Check query info', () => {
|
|
496
|
+
const query = (0, dist_1.Insert)(models_1.Person);
|
|
497
|
+
expect(query._type).toBe('INSERT');
|
|
498
|
+
expect(query._table).toStrictEqual(tableInfo);
|
|
499
|
+
});
|
|
500
|
+
it('Insert record', () => {
|
|
501
|
+
const query = (0, dist_1.Insert)(models_1.Person).values(person);
|
|
502
|
+
const expectation = {
|
|
503
|
+
id: person.id,
|
|
504
|
+
name: person.name,
|
|
505
|
+
age: person.age,
|
|
506
|
+
city: person.city,
|
|
507
|
+
};
|
|
508
|
+
expect(query._values).toStrictEqual(expectation);
|
|
509
|
+
(0, dist_1.Insert)(models_1.Person).values(Object.assign(Object.assign({}, person), { uid: 'a80ec30e-791c-4499-a243-70af8b2bf7ba' }));
|
|
510
|
+
});
|
|
511
|
+
});
|
|
512
|
+
describe('Upsert', () => {
|
|
513
|
+
const infoMock = {
|
|
514
|
+
[dist_1.TableMetadataSymbol]: {}
|
|
515
|
+
};
|
|
516
|
+
it('Check query info', () => {
|
|
517
|
+
const query = (0, dist_1.Upsert)(models_1.Person);
|
|
518
|
+
expect(query._type).toBe('UPSERT');
|
|
519
|
+
expect(query._table).toStrictEqual(tableInfo);
|
|
520
|
+
});
|
|
521
|
+
it('Upsert record', () => {
|
|
522
|
+
const defaultUpsert = (0, dist_1.Upsert)(models_1.Person).values(person);
|
|
523
|
+
const withConflict = (0, dist_1.Upsert)(models_1.Person).values(person).conflict('idx');
|
|
524
|
+
const expectation = {
|
|
525
|
+
values: {
|
|
526
|
+
id: person.id,
|
|
527
|
+
name: person.name,
|
|
528
|
+
age: person.age,
|
|
529
|
+
city: person.city,
|
|
530
|
+
},
|
|
531
|
+
conflictExp: { _columns: ['id'], _where: undefined }
|
|
532
|
+
};
|
|
533
|
+
expect(defaultUpsert._values).toStrictEqual(expectation.values);
|
|
534
|
+
expect(defaultUpsert._conflictExp).toBeUndefined();
|
|
535
|
+
expect(withConflict._values).toStrictEqual(expectation.values);
|
|
536
|
+
expect(withConflict._conflictExp).toStrictEqual(expectation.conflictExp);
|
|
537
|
+
});
|
|
538
|
+
it('Should fail if table has no primary key', () => {
|
|
539
|
+
expect(() => {
|
|
540
|
+
(0, dist_1.Upsert)(infoMock).conflict();
|
|
541
|
+
}).toThrowError('Current table has no primary key. Please provide unique index for upsert');
|
|
542
|
+
});
|
|
543
|
+
it('Should fail if table indexes meta is empty', () => {
|
|
544
|
+
expect(() => {
|
|
545
|
+
(0, dist_1.Upsert)(infoMock).conflict('id');
|
|
546
|
+
}).toThrowError('Table indexes meta is empty');
|
|
547
|
+
});
|
|
548
|
+
it('Should fail if index is not declared', () => {
|
|
549
|
+
expect(() => {
|
|
550
|
+
(0, dist_1.Upsert)(models_1.Person).conflict('name');
|
|
551
|
+
}).toThrowError(`Index name is not declared for table ${tableName}`);
|
|
552
|
+
});
|
|
553
|
+
});
|
|
554
|
+
describe('Update', () => {
|
|
555
|
+
it('Check query info', () => {
|
|
556
|
+
const query = (0, dist_1.Update)(models_1.Person);
|
|
557
|
+
expect(query._type).toBe('UPDATE');
|
|
558
|
+
expect(query._table).toStrictEqual(tableInfo);
|
|
559
|
+
});
|
|
560
|
+
it('Update record', () => {
|
|
561
|
+
const limit = 10;
|
|
562
|
+
const query = (0, dist_1.Update)(models_1.Person).values(person).where(({ id }) => id.eq(person.id)).limit(limit);
|
|
563
|
+
const expectation = {
|
|
564
|
+
values: {
|
|
565
|
+
id: person.id,
|
|
566
|
+
name: person.name,
|
|
567
|
+
age: person.age,
|
|
568
|
+
city: person.city,
|
|
569
|
+
},
|
|
570
|
+
where: [{
|
|
571
|
+
_operator: '=',
|
|
572
|
+
_operands: [{ _column: { table: tableName, name: 'id' } }, person.id]
|
|
573
|
+
}]
|
|
574
|
+
};
|
|
575
|
+
expect(query._values).toStrictEqual(expectation.values);
|
|
576
|
+
expect(query._where).toStrictEqual(expectation.where);
|
|
577
|
+
expect(query._limit).toBe(limit);
|
|
578
|
+
});
|
|
579
|
+
});
|
|
580
|
+
describe('Delete', () => {
|
|
581
|
+
it('Check query info', () => {
|
|
582
|
+
const query = (0, dist_1.Delete)(models_1.Person);
|
|
583
|
+
expect(query._type).toBe('DELETE');
|
|
584
|
+
expect(query._table).toStrictEqual(tableInfo);
|
|
585
|
+
});
|
|
586
|
+
it('Delete record', () => {
|
|
587
|
+
const limit = 10;
|
|
588
|
+
const query = (0, dist_1.Delete)(models_1.Person).where(({ id }) => id.eq(person.id)).limit(limit);
|
|
589
|
+
const expectation = [{
|
|
590
|
+
_operator: '=',
|
|
591
|
+
_operands: [{ _column: { table: tableName, name: 'id' } }, person.id]
|
|
592
|
+
}];
|
|
593
|
+
expect(query._where).toStrictEqual(expectation);
|
|
594
|
+
expect(query._limit).toBe(limit);
|
|
595
|
+
});
|
|
596
|
+
});
|