@spinajs/orm 2.0.180 → 2.0.182
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/lib/cjs/builders.d.ts +643 -643
- package/lib/cjs/builders.js +1602 -1602
- package/lib/cjs/builders.js.map +1 -1
- package/lib/cjs/converters.d.ts +34 -34
- package/lib/cjs/converters.js +104 -104
- package/lib/cjs/decorators.d.ts +152 -152
- package/lib/cjs/decorators.js +449 -449
- package/lib/cjs/dehydrators.d.ts +10 -10
- package/lib/cjs/dehydrators.js +47 -47
- package/lib/cjs/driver.d.ts +82 -82
- package/lib/cjs/driver.js +102 -102
- package/lib/cjs/driver.js.map +1 -1
- package/lib/cjs/enums.d.ts +116 -116
- package/lib/cjs/enums.js +126 -126
- package/lib/cjs/enums.js.map +1 -1
- package/lib/cjs/exceptions.d.ts +6 -6
- package/lib/cjs/exceptions.js +10 -10
- package/lib/cjs/hydrators.d.ts +19 -19
- package/lib/cjs/hydrators.js +132 -132
- package/lib/cjs/hydrators.js.map +1 -1
- package/lib/cjs/index.d.ts +17 -17
- package/lib/cjs/index.js +33 -33
- package/lib/cjs/interfaces.d.ts +921 -919
- package/lib/cjs/interfaces.d.ts.map +1 -1
- package/lib/cjs/interfaces.js +279 -279
- package/lib/cjs/interfaces.js.map +1 -1
- package/lib/cjs/middlewares.d.ts +62 -62
- package/lib/cjs/middlewares.js +258 -258
- package/lib/cjs/model.d.ts +288 -284
- package/lib/cjs/model.d.ts.map +1 -1
- package/lib/cjs/model.js +826 -810
- package/lib/cjs/model.js.map +1 -1
- package/lib/cjs/orm.d.ts +61 -61
- package/lib/cjs/orm.js +333 -333
- package/lib/cjs/orm.js.map +1 -1
- package/lib/cjs/relation-objects.d.ts +108 -108
- package/lib/cjs/relation-objects.js +221 -221
- package/lib/cjs/relations.d.ts +61 -61
- package/lib/cjs/relations.js +194 -194
- package/lib/cjs/relations.js.map +1 -1
- package/lib/cjs/statements.d.ts +143 -143
- package/lib/cjs/statements.js +309 -309
- package/lib/cjs/statements.js.map +1 -1
- package/lib/cjs/types.d.ts +32 -32
- package/lib/cjs/types.js +2 -2
- package/lib/cjs/wrappers.d.ts +5 -5
- package/lib/cjs/wrappers.js +12 -12
- package/lib/mjs/builders.d.ts +643 -643
- package/lib/mjs/builders.js +1594 -1594
- package/lib/mjs/builders.js.map +1 -1
- package/lib/mjs/converters.d.ts +34 -34
- package/lib/mjs/converters.js +96 -96
- package/lib/mjs/decorators.d.ts +152 -152
- package/lib/mjs/decorators.js +422 -422
- package/lib/mjs/dehydrators.d.ts +10 -10
- package/lib/mjs/dehydrators.js +41 -41
- package/lib/mjs/driver.d.ts +82 -82
- package/lib/mjs/driver.js +98 -98
- package/lib/mjs/driver.js.map +1 -1
- package/lib/mjs/enums.d.ts +116 -116
- package/lib/mjs/enums.js +123 -123
- package/lib/mjs/enums.js.map +1 -1
- package/lib/mjs/exceptions.d.ts +6 -6
- package/lib/mjs/exceptions.js +6 -6
- package/lib/mjs/hydrators.d.ts +19 -19
- package/lib/mjs/hydrators.js +128 -128
- package/lib/mjs/hydrators.js.map +1 -1
- package/lib/mjs/index.d.ts +17 -17
- package/lib/mjs/index.js +17 -17
- package/lib/mjs/interfaces.d.ts +921 -919
- package/lib/mjs/interfaces.d.ts.map +1 -1
- package/lib/mjs/interfaces.js +267 -267
- package/lib/mjs/interfaces.js.map +1 -1
- package/lib/mjs/middlewares.d.ts +62 -62
- package/lib/mjs/middlewares.js +249 -249
- package/lib/mjs/model.d.ts +288 -284
- package/lib/mjs/model.d.ts.map +1 -1
- package/lib/mjs/model.js +816 -800
- package/lib/mjs/model.js.map +1 -1
- package/lib/mjs/orm.d.ts +61 -61
- package/lib/mjs/orm.js +326 -326
- package/lib/mjs/orm.js.map +1 -1
- package/lib/mjs/relation-objects.d.ts +108 -108
- package/lib/mjs/relation-objects.js +211 -211
- package/lib/mjs/relations.d.ts +61 -61
- package/lib/mjs/relations.js +191 -191
- package/lib/mjs/relations.js.map +1 -1
- package/lib/mjs/statements.d.ts +143 -143
- package/lib/mjs/statements.js +301 -301
- package/lib/mjs/statements.js.map +1 -1
- package/lib/mjs/types.d.ts +32 -32
- package/lib/mjs/types.js +1 -1
- package/lib/mjs/wrappers.d.ts +5 -5
- package/lib/mjs/wrappers.js +9 -9
- package/lib/tsconfig.cjs.tsbuildinfo +1 -1
- package/lib/tsconfig.mjs.tsbuildinfo +1 -1
- package/package.json +5 -5
package/lib/mjs/builders.js
CHANGED
|
@@ -1,1595 +1,1595 @@
|
|
|
1
|
-
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
-
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
-
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
-
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;
|
|
5
|
-
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
-
};
|
|
7
|
-
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
-
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
-
};
|
|
10
|
-
var RawQuery_1, WhereBuilder_1, SelectQueryBuilder_1;
|
|
11
|
-
/* eslint-disable prettier/prettier */
|
|
12
|
-
import { Container, Inject, NewInstance, DI, Injectable } from '@spinajs/di';
|
|
13
|
-
import { InvalidArgument, MethodNotImplemented, InvalidOperation } from '@spinajs/exceptions';
|
|
14
|
-
import { OrmException } from './exceptions.js';
|
|
15
|
-
import _ from 'lodash';
|
|
16
|
-
import { use } from 'typescript-mix';
|
|
17
|
-
import { ColumnMethods, ColumnType, QueryMethod, SortOrder, WhereBoolean, SqlOperator, JoinMethod } from './enums.js';
|
|
18
|
-
import { DeleteQueryCompiler, InsertQueryCompiler, SelectQueryCompiler, TruncateTableQueryCompiler, TableQueryCompiler, AlterTableQueryCompiler, UpdateQueryCompiler, QueryContext, IndexQueryCompiler, RelationType, ReferentialAction, DefaultValueBuilder, ColumnAlterationType, TableExistsCompiler, DropTableCompiler, TableCloneQueryCompiler, QueryMiddleware, DropEventQueryCompiler, EventQueryCompiler } from './interfaces.js';
|
|
19
|
-
import { BetweenStatement, ColumnMethodStatement, ColumnStatement, ExistsQueryStatement, InSetStatement, InStatement, RawQueryStatement, WhereQueryStatement, WhereStatement, ColumnRawStatement, JoinStatement, WithRecursiveStatement, GroupByStatement, Wrap } from './statements.js';
|
|
20
|
-
import { OrmDriver } from './driver.js';
|
|
21
|
-
import { extractModelDescriptor } from './model.js';
|
|
22
|
-
import { BelongsToRelation, OneToManyRelation, ManyToManyRelation, BelongsToRecursiveRelation } from './relations.js';
|
|
23
|
-
function isWhereOperator(val) {
|
|
24
|
-
return _.isString(val) && Object.values(SqlOperator).includes(val.toLowerCase());
|
|
25
|
-
}
|
|
26
|
-
let Builder = class Builder {
|
|
27
|
-
get Driver() {
|
|
28
|
-
return this._driver;
|
|
29
|
-
}
|
|
30
|
-
get Container() {
|
|
31
|
-
return this._container;
|
|
32
|
-
}
|
|
33
|
-
get Model() {
|
|
34
|
-
return this._model;
|
|
35
|
-
}
|
|
36
|
-
constructor(container, driver, model) {
|
|
37
|
-
this._middlewares = [];
|
|
38
|
-
this._queryMiddlewares = [];
|
|
39
|
-
this._driver = driver;
|
|
40
|
-
this._container = container;
|
|
41
|
-
this._model = model;
|
|
42
|
-
this._nonSelect = true;
|
|
43
|
-
this._asRaw = false;
|
|
44
|
-
this._queryMiddlewares = DI.resolve(Array.ofType(QueryMiddleware));
|
|
45
|
-
}
|
|
46
|
-
then(onfulfilled, onrejected) {
|
|
47
|
-
return this._driver
|
|
48
|
-
.execute(this)
|
|
49
|
-
.then((result) => {
|
|
50
|
-
try {
|
|
51
|
-
if (this._asRaw) {
|
|
52
|
-
onfulfilled(result);
|
|
53
|
-
return;
|
|
54
|
-
}
|
|
55
|
-
let transformedResult = result;
|
|
56
|
-
// if we have something to transform ...
|
|
57
|
-
if (transformedResult) {
|
|
58
|
-
this._middlewares.forEach((m) => {
|
|
59
|
-
Object.assign(transformedResult, m.afterQuery(transformedResult));
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
if (this._model && !this._nonSelect) {
|
|
63
|
-
// TODO: rething this casting
|
|
64
|
-
const models = transformedResult.map((r) => {
|
|
65
|
-
let model = null;
|
|
66
|
-
for (const middleware of this._middlewares.reverse()) {
|
|
67
|
-
model = middleware.modelCreation(r);
|
|
68
|
-
if (model !== null) {
|
|
69
|
-
break;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
if (model === null) {
|
|
73
|
-
model = new this._model();
|
|
74
|
-
model.hydrate(r);
|
|
75
|
-
}
|
|
76
|
-
return model;
|
|
77
|
-
});
|
|
78
|
-
const afterMiddlewarePromises = this._middlewares.reduce((prev, current) => {
|
|
79
|
-
return prev.concat([current.afterHydration(models)]);
|
|
80
|
-
}, []);
|
|
81
|
-
if (this._middlewares.length > 0) {
|
|
82
|
-
Promise.all(afterMiddlewarePromises).then(() => {
|
|
83
|
-
try {
|
|
84
|
-
onfulfilled(models);
|
|
85
|
-
}
|
|
86
|
-
catch (err) {
|
|
87
|
-
onrejected(err);
|
|
88
|
-
}
|
|
89
|
-
}, onrejected);
|
|
90
|
-
}
|
|
91
|
-
else {
|
|
92
|
-
onfulfilled(models);
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
else {
|
|
96
|
-
onfulfilled(transformedResult);
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
catch (err) {
|
|
100
|
-
onrejected(err);
|
|
101
|
-
}
|
|
102
|
-
})
|
|
103
|
-
.catch((err) => {
|
|
104
|
-
onrejected(err);
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
middleware(middleware) {
|
|
108
|
-
this._middlewares.push(middleware);
|
|
109
|
-
return this;
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Builds query that is ready to use in DB
|
|
113
|
-
*/
|
|
114
|
-
toDB() {
|
|
115
|
-
throw new MethodNotImplemented();
|
|
116
|
-
}
|
|
117
|
-
};
|
|
118
|
-
Builder = __decorate([
|
|
119
|
-
NewInstance(),
|
|
120
|
-
Inject(Container),
|
|
121
|
-
__metadata("design:paramtypes", [Object, OrmDriver, Object])
|
|
122
|
-
], Builder);
|
|
123
|
-
export { Builder };
|
|
124
|
-
/**
|
|
125
|
-
* Base class for queires. Implements basic query functionality
|
|
126
|
-
*
|
|
127
|
-
*/
|
|
128
|
-
let QueryBuilder = class QueryBuilder extends Builder {
|
|
129
|
-
constructor(container, driver, model) {
|
|
130
|
-
super(container, driver, model);
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* SQL table name that query is executed on
|
|
134
|
-
*
|
|
135
|
-
* @example
|
|
136
|
-
* SELECT * FROM `users`
|
|
137
|
-
*/
|
|
138
|
-
get Table() {
|
|
139
|
-
return this._table;
|
|
140
|
-
}
|
|
141
|
-
/**
|
|
142
|
-
* DB table alias
|
|
143
|
-
*/
|
|
144
|
-
get TableAlias() {
|
|
145
|
-
return this._tableAlias;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* SQL schema/database name that query is executed on.
|
|
149
|
-
*
|
|
150
|
-
* @example
|
|
151
|
-
* SELECT * FROM `spinejs`.`users` as u
|
|
152
|
-
*/
|
|
153
|
-
get Database() {
|
|
154
|
-
return this._database;
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Sets schema to this query.
|
|
158
|
-
*
|
|
159
|
-
* @param database - schema or database name in database
|
|
160
|
-
*/
|
|
161
|
-
database(database) {
|
|
162
|
-
if (!database) {
|
|
163
|
-
throw new InvalidArgument(`schema argument cannot be null or empty`);
|
|
164
|
-
}
|
|
165
|
-
this._database = database;
|
|
166
|
-
return this;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* Sets table that query is executed on
|
|
170
|
-
*
|
|
171
|
-
* @param table - sql table name
|
|
172
|
-
* @param alias - sql table alias
|
|
173
|
-
*
|
|
174
|
-
* @example
|
|
175
|
-
*
|
|
176
|
-
* this.setTable("user","u")
|
|
177
|
-
*
|
|
178
|
-
*/
|
|
179
|
-
setTable(table, alias) {
|
|
180
|
-
if (!table.trim()) {
|
|
181
|
-
throw new InvalidArgument('table name is empty');
|
|
182
|
-
}
|
|
183
|
-
this._table = table;
|
|
184
|
-
this.setAlias(alias);
|
|
185
|
-
return this;
|
|
186
|
-
}
|
|
187
|
-
/**
|
|
188
|
-
* Sets table alias for query
|
|
189
|
-
*
|
|
190
|
-
* @param alias - sql table alias
|
|
191
|
-
*/
|
|
192
|
-
setAlias(alias) {
|
|
193
|
-
this._tableAlias = alias;
|
|
194
|
-
return this;
|
|
195
|
-
}
|
|
196
|
-
from(table, alias) {
|
|
197
|
-
return this.setTable(table, alias);
|
|
198
|
-
}
|
|
199
|
-
};
|
|
200
|
-
QueryBuilder = __decorate([
|
|
201
|
-
NewInstance(),
|
|
202
|
-
Inject(Container),
|
|
203
|
-
__metadata("design:paramtypes", [Object, OrmDriver, Object])
|
|
204
|
-
], QueryBuilder);
|
|
205
|
-
export { QueryBuilder };
|
|
206
|
-
let LimitBuilder = class LimitBuilder {
|
|
207
|
-
constructor() {
|
|
208
|
-
this._first = false;
|
|
209
|
-
this._limit = {
|
|
210
|
-
limit: -1,
|
|
211
|
-
offset: -1,
|
|
212
|
-
};
|
|
213
|
-
}
|
|
214
|
-
take(count) {
|
|
215
|
-
if (count <= 0) {
|
|
216
|
-
throw new InvalidArgument(`take count cannot be negative number`);
|
|
217
|
-
}
|
|
218
|
-
this._limit.limit = count;
|
|
219
|
-
return this;
|
|
220
|
-
}
|
|
221
|
-
skip(count) {
|
|
222
|
-
if (count < 0) {
|
|
223
|
-
throw new InvalidArgument(`skip count cannot be negative number`);
|
|
224
|
-
}
|
|
225
|
-
this._limit.offset = count;
|
|
226
|
-
return this;
|
|
227
|
-
}
|
|
228
|
-
takeFirst() {
|
|
229
|
-
this._first = true;
|
|
230
|
-
this._limit.limit = 1;
|
|
231
|
-
return this;
|
|
232
|
-
}
|
|
233
|
-
async first() {
|
|
234
|
-
return (await this.takeFirst());
|
|
235
|
-
}
|
|
236
|
-
async firstOrFail() {
|
|
237
|
-
return this.firstOrThrow(new OrmException('not found'));
|
|
238
|
-
}
|
|
239
|
-
async orThrow(error) {
|
|
240
|
-
const result = (await this);
|
|
241
|
-
if (result === undefined || (Array.isArray(result) && result.length === 0)) {
|
|
242
|
-
throw error;
|
|
243
|
-
}
|
|
244
|
-
return result;
|
|
245
|
-
}
|
|
246
|
-
async firstOrThrow(error) {
|
|
247
|
-
const result = await this.first();
|
|
248
|
-
if (result === undefined) {
|
|
249
|
-
throw error;
|
|
250
|
-
}
|
|
251
|
-
return result;
|
|
252
|
-
}
|
|
253
|
-
getLimits() {
|
|
254
|
-
return this._limit;
|
|
255
|
-
}
|
|
256
|
-
};
|
|
257
|
-
LimitBuilder = __decorate([
|
|
258
|
-
NewInstance(),
|
|
259
|
-
__metadata("design:paramtypes", [])
|
|
260
|
-
], LimitBuilder);
|
|
261
|
-
export { LimitBuilder };
|
|
262
|
-
let OrderByBuilder = class OrderByBuilder {
|
|
263
|
-
constructor() {
|
|
264
|
-
this._sort = {
|
|
265
|
-
column: '',
|
|
266
|
-
order: SortOrder.ASC,
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
order(column, direction) {
|
|
270
|
-
if (!column) {
|
|
271
|
-
return this;
|
|
272
|
-
}
|
|
273
|
-
this._sort = {
|
|
274
|
-
column,
|
|
275
|
-
order: direction,
|
|
276
|
-
};
|
|
277
|
-
return this;
|
|
278
|
-
}
|
|
279
|
-
orderBy(column) {
|
|
280
|
-
this._sort = {
|
|
281
|
-
column,
|
|
282
|
-
order: SortOrder.ASC,
|
|
283
|
-
};
|
|
284
|
-
return this;
|
|
285
|
-
}
|
|
286
|
-
orderByDescending(column) {
|
|
287
|
-
this._sort = {
|
|
288
|
-
column,
|
|
289
|
-
order: SortOrder.DESC,
|
|
290
|
-
};
|
|
291
|
-
return this;
|
|
292
|
-
}
|
|
293
|
-
getSort() {
|
|
294
|
-
return this._sort.column.trim() !== '' ? this._sort : null;
|
|
295
|
-
}
|
|
296
|
-
};
|
|
297
|
-
OrderByBuilder = __decorate([
|
|
298
|
-
NewInstance(),
|
|
299
|
-
__metadata("design:paramtypes", [])
|
|
300
|
-
], OrderByBuilder);
|
|
301
|
-
export { OrderByBuilder };
|
|
302
|
-
let ColumnsBuilder = class ColumnsBuilder {
|
|
303
|
-
constructor() {
|
|
304
|
-
this._columns = [];
|
|
305
|
-
}
|
|
306
|
-
/**
|
|
307
|
-
* Clears all select clauses from the query.
|
|
308
|
-
*
|
|
309
|
-
* @example
|
|
310
|
-
*
|
|
311
|
-
* query.columns()
|
|
312
|
-
*
|
|
313
|
-
*/
|
|
314
|
-
clearColumns() {
|
|
315
|
-
this._columns = [];
|
|
316
|
-
return this;
|
|
317
|
-
}
|
|
318
|
-
columns(names) {
|
|
319
|
-
const descriptor = extractModelDescriptor(this._model);
|
|
320
|
-
this._columns = names.map((n) => {
|
|
321
|
-
return this._container.resolve(ColumnStatement, [n, null, this._tableAlias, descriptor?.Columns.find((c) => c.Name === n)]);
|
|
322
|
-
});
|
|
323
|
-
return this;
|
|
324
|
-
}
|
|
325
|
-
select(column, alias) {
|
|
326
|
-
const descriptor = extractModelDescriptor(this._model);
|
|
327
|
-
if (column instanceof Map) {
|
|
328
|
-
column.forEach((alias, colName) => {
|
|
329
|
-
this._columns.push(this._container.resolve(ColumnStatement, [colName, alias, this._tableAlias, descriptor?.Columns.find((c) => c.Name === colName)]));
|
|
330
|
-
});
|
|
331
|
-
}
|
|
332
|
-
if (column instanceof RawQuery) {
|
|
333
|
-
this._columns.push(this._container.resolve(ColumnRawStatement, [column, null, this._tableAlias]));
|
|
334
|
-
}
|
|
335
|
-
else {
|
|
336
|
-
this._columns.push(this._container.resolve(ColumnStatement, [column, alias, this._tableAlias, descriptor?.Columns.find((c) => c.Name === column)]));
|
|
337
|
-
}
|
|
338
|
-
return this;
|
|
339
|
-
}
|
|
340
|
-
getColumns() {
|
|
341
|
-
return this._columns;
|
|
342
|
-
}
|
|
343
|
-
};
|
|
344
|
-
ColumnsBuilder = __decorate([
|
|
345
|
-
NewInstance(),
|
|
346
|
-
__metadata("design:paramtypes", [])
|
|
347
|
-
], ColumnsBuilder);
|
|
348
|
-
export { ColumnsBuilder };
|
|
349
|
-
let RawQuery = RawQuery_1 = class RawQuery {
|
|
350
|
-
get Query() {
|
|
351
|
-
return this._query;
|
|
352
|
-
}
|
|
353
|
-
get Bindings() {
|
|
354
|
-
return this._bindings;
|
|
355
|
-
}
|
|
356
|
-
static create(query, bindings) {
|
|
357
|
-
return new RawQuery_1(query, bindings);
|
|
358
|
-
}
|
|
359
|
-
constructor(query, bindings) {
|
|
360
|
-
this._query = '';
|
|
361
|
-
this._bindings = [];
|
|
362
|
-
this._query = query;
|
|
363
|
-
this._bindings = bindings;
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
RawQuery = RawQuery_1 = __decorate([
|
|
367
|
-
NewInstance(),
|
|
368
|
-
__metadata("design:paramtypes", [String, Array])
|
|
369
|
-
], RawQuery);
|
|
370
|
-
export { RawQuery };
|
|
371
|
-
let GroupByBuilder = class GroupByBuilder {
|
|
372
|
-
constructor() {
|
|
373
|
-
this._groupStatements = [];
|
|
374
|
-
}
|
|
375
|
-
get GroupStatements() {
|
|
376
|
-
return this._groupStatements;
|
|
377
|
-
}
|
|
378
|
-
clearGroupBy() {
|
|
379
|
-
this._groupStatements = [];
|
|
380
|
-
return this;
|
|
381
|
-
}
|
|
382
|
-
groupBy(expression) {
|
|
383
|
-
this._groupStatements.push(this._container.resolve(GroupByStatement, [expression]));
|
|
384
|
-
return this;
|
|
385
|
-
}
|
|
386
|
-
};
|
|
387
|
-
GroupByBuilder = __decorate([
|
|
388
|
-
NewInstance()
|
|
389
|
-
], GroupByBuilder);
|
|
390
|
-
export { GroupByBuilder };
|
|
391
|
-
let JoinBuilder = class JoinBuilder {
|
|
392
|
-
get JoinStatements() {
|
|
393
|
-
return this._joinStatements;
|
|
394
|
-
}
|
|
395
|
-
constructor(container) {
|
|
396
|
-
this._joinStatements = [];
|
|
397
|
-
this._container = container;
|
|
398
|
-
this._joinStatements = [];
|
|
399
|
-
}
|
|
400
|
-
clearJoins() {
|
|
401
|
-
this._joinStatements = [];
|
|
402
|
-
return this;
|
|
403
|
-
}
|
|
404
|
-
innerJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey) {
|
|
405
|
-
this.addJoinStatement.call(this, JoinMethod.INNER, ...arguments);
|
|
406
|
-
return this;
|
|
407
|
-
}
|
|
408
|
-
leftJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
409
|
-
this.addJoinStatement.call(this, JoinMethod.LEFT, ...arguments);
|
|
410
|
-
return this;
|
|
411
|
-
}
|
|
412
|
-
leftOuterJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
413
|
-
this.addJoinStatement.call(this, JoinMethod.LEFT_OUTER, ...arguments);
|
|
414
|
-
return this;
|
|
415
|
-
}
|
|
416
|
-
rightJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
417
|
-
this.addJoinStatement.call(this, JoinMethod.RIGHT, ...arguments);
|
|
418
|
-
return this;
|
|
419
|
-
}
|
|
420
|
-
rightOuterJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
421
|
-
this.addJoinStatement.call(this, JoinMethod.RIGHT_OUTER, ...arguments);
|
|
422
|
-
return this;
|
|
423
|
-
}
|
|
424
|
-
fullOuterJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
425
|
-
this.addJoinStatement.call(this, JoinMethod.FULL_OUTER, ...arguments);
|
|
426
|
-
return this;
|
|
427
|
-
}
|
|
428
|
-
crossJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
429
|
-
this.addJoinStatement.call(this, JoinMethod.CROSS, ...arguments);
|
|
430
|
-
return this;
|
|
431
|
-
}
|
|
432
|
-
addJoinStatement(method, table, AliasOrForeignKey, fkOrPkKey, primaryKey, _database) {
|
|
433
|
-
let stmt = null;
|
|
434
|
-
if (arguments.length === 3) {
|
|
435
|
-
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method, AliasOrForeignKey]);
|
|
436
|
-
}
|
|
437
|
-
else if (arguments.length === 4) {
|
|
438
|
-
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method, AliasOrForeignKey, fkOrPkKey, null, this._tableAlias]);
|
|
439
|
-
}
|
|
440
|
-
else if (arguments.length === 5) {
|
|
441
|
-
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method, fkOrPkKey, primaryKey, AliasOrForeignKey, this._tableAlias]);
|
|
442
|
-
}
|
|
443
|
-
else if (arguments.length === 6) {
|
|
444
|
-
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method, fkOrPkKey, primaryKey, AliasOrForeignKey, this._tableAlias, _database]);
|
|
445
|
-
}
|
|
446
|
-
else {
|
|
447
|
-
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method]);
|
|
448
|
-
}
|
|
449
|
-
this.JoinStatements.push(stmt);
|
|
450
|
-
}
|
|
451
|
-
};
|
|
452
|
-
JoinBuilder = __decorate([
|
|
453
|
-
NewInstance(),
|
|
454
|
-
__metadata("design:paramtypes", [Container])
|
|
455
|
-
], JoinBuilder);
|
|
456
|
-
export { JoinBuilder };
|
|
457
|
-
let WithRecursiveBuilder = class WithRecursiveBuilder {
|
|
458
|
-
get CteRecursive() {
|
|
459
|
-
return this._cteStatement;
|
|
460
|
-
}
|
|
461
|
-
withRecursive(rcKeyName, pkName) {
|
|
462
|
-
this._cteStatement = this._container.resolve(WithRecursiveStatement, ['cte', this, rcKeyName, pkName]);
|
|
463
|
-
return this;
|
|
464
|
-
}
|
|
465
|
-
};
|
|
466
|
-
WithRecursiveBuilder = __decorate([
|
|
467
|
-
NewInstance()
|
|
468
|
-
], WithRecursiveBuilder);
|
|
469
|
-
export { WithRecursiveBuilder };
|
|
470
|
-
let WhereBuilder = WhereBuilder_1 = class WhereBuilder {
|
|
471
|
-
get Statements() {
|
|
472
|
-
return this._statements;
|
|
473
|
-
}
|
|
474
|
-
get Op() {
|
|
475
|
-
return this._boolean;
|
|
476
|
-
}
|
|
477
|
-
constructor(container, tableAlias) {
|
|
478
|
-
this._statements = [];
|
|
479
|
-
this._boolean = WhereBoolean.AND;
|
|
480
|
-
this._container = container;
|
|
481
|
-
this._boolean = WhereBoolean.AND;
|
|
482
|
-
this._statements = [];
|
|
483
|
-
this._tableAlias = tableAlias;
|
|
484
|
-
}
|
|
485
|
-
when(condition, callback, callbackElse) {
|
|
486
|
-
if (condition) {
|
|
487
|
-
if (callback)
|
|
488
|
-
callback.call(this);
|
|
489
|
-
}
|
|
490
|
-
else if (callbackElse) {
|
|
491
|
-
callbackElse.call(this);
|
|
492
|
-
}
|
|
493
|
-
return this;
|
|
494
|
-
}
|
|
495
|
-
where(column, operator, value) {
|
|
496
|
-
const self = this;
|
|
497
|
-
if (column === null || (undefined && arguments.length === 1)) {
|
|
498
|
-
return;
|
|
499
|
-
}
|
|
500
|
-
// Support "where true || where false"
|
|
501
|
-
if (_.isBoolean(column)) {
|
|
502
|
-
return this.where(RawQuery.create(column ? 'TRUE' : 'FALSE'));
|
|
503
|
-
}
|
|
504
|
-
if (column instanceof RawQuery) {
|
|
505
|
-
this.Statements.push(this._container.resolve(RawQueryStatement, [column.Query, column.Bindings, self._tableAlias]));
|
|
506
|
-
return this;
|
|
507
|
-
}
|
|
508
|
-
// handle nested where's
|
|
509
|
-
if (_.isFunction(column)) {
|
|
510
|
-
const builder = new WhereBuilder_1(this._container, this._tableAlias);
|
|
511
|
-
column.call(builder);
|
|
512
|
-
self.Statements.push(this._container.resolve(WhereQueryStatement, [builder, self._tableAlias]));
|
|
513
|
-
return this;
|
|
514
|
-
}
|
|
515
|
-
// handle simple key = object[key] AND ....
|
|
516
|
-
if (_.isObject(column) && !(column instanceof Wrap)) {
|
|
517
|
-
return this.whereObject(column);
|
|
518
|
-
}
|
|
519
|
-
if (typeof value === 'undefined') {
|
|
520
|
-
return _handleForTwo.call(this, column, operator);
|
|
521
|
-
}
|
|
522
|
-
return _handleForThree.call(this, column, operator, value);
|
|
523
|
-
/**
|
|
524
|
-
* handles for where("foo", 1).where(...) cases
|
|
525
|
-
* it produces WHERE foo = 1
|
|
526
|
-
*/
|
|
527
|
-
function _handleForTwo(c, v) {
|
|
528
|
-
let sVal = v;
|
|
529
|
-
if (sVal === undefined) {
|
|
530
|
-
throw new InvalidArgument(`value cannot be undefined`);
|
|
531
|
-
}
|
|
532
|
-
if (Number.isNaN(sVal)) {
|
|
533
|
-
throw new InvalidArgument(`value cannot be NaN`);
|
|
534
|
-
}
|
|
535
|
-
if (!_.isString(c) && !(c instanceof Wrap)) {
|
|
536
|
-
throw new InvalidArgument(`column is not of type string or wrapped.`);
|
|
537
|
-
}
|
|
538
|
-
if (sVal === null) {
|
|
539
|
-
return this.whereNull(c);
|
|
540
|
-
}
|
|
541
|
-
self._statements.push(self._container.resolve(WhereStatement, [c, SqlOperator.EQ, sVal, self._tableAlias, this._container, self._model]));
|
|
542
|
-
return self;
|
|
543
|
-
}
|
|
544
|
-
/**
|
|
545
|
-
* Handles for where("foo",'!=',1) etc
|
|
546
|
-
* it produces WHERE foo != 1
|
|
547
|
-
*/
|
|
548
|
-
function _handleForThree(c, o, v) {
|
|
549
|
-
let sVal = v;
|
|
550
|
-
if (!isWhereOperator(o)) {
|
|
551
|
-
throw new InvalidArgument(`operator ${o} is invalid`);
|
|
552
|
-
}
|
|
553
|
-
if (!_.isString(c) && !(c instanceof Wrap)) {
|
|
554
|
-
throw new InvalidArgument(`column is not of type string or wrapped.`);
|
|
555
|
-
}
|
|
556
|
-
if (Number.isNaN(sVal)) {
|
|
557
|
-
throw new InvalidArgument(`value cannot be NaN`);
|
|
558
|
-
}
|
|
559
|
-
if (sVal === null) {
|
|
560
|
-
return this.whereNull(c);
|
|
561
|
-
}
|
|
562
|
-
if (sVal === null) {
|
|
563
|
-
return o === SqlOperator.NOT_NULL ? this.whereNotNull(c) : this.whereNull(c);
|
|
564
|
-
}
|
|
565
|
-
self._statements.push(self._container.resolve(WhereStatement, [c, o, sVal, self._tableAlias, this._container, self._model]));
|
|
566
|
-
return this;
|
|
567
|
-
}
|
|
568
|
-
}
|
|
569
|
-
orWhere(column, ..._args) {
|
|
570
|
-
this._boolean = WhereBoolean.OR;
|
|
571
|
-
return this.where(column, ...Array.from(arguments).slice(1));
|
|
572
|
-
}
|
|
573
|
-
andWhere(column, ..._args) {
|
|
574
|
-
this._boolean = WhereBoolean.AND;
|
|
575
|
-
return this.where(column, ...Array.from(arguments).slice(1));
|
|
576
|
-
}
|
|
577
|
-
whereObject(obj) {
|
|
578
|
-
this._boolean = WhereBoolean.AND;
|
|
579
|
-
for (const key of Object.keys(obj).filter((x) => obj[x] !== undefined)) {
|
|
580
|
-
const val = obj[key];
|
|
581
|
-
if (Array.isArray(val)) {
|
|
582
|
-
if (val.length !== 0) {
|
|
583
|
-
this.whereIn(key, val);
|
|
584
|
-
}
|
|
585
|
-
}
|
|
586
|
-
else if (val === null) {
|
|
587
|
-
this.whereNull(key);
|
|
588
|
-
}
|
|
589
|
-
else
|
|
590
|
-
this.andWhere(key, SqlOperator.EQ, val);
|
|
591
|
-
}
|
|
592
|
-
return this;
|
|
593
|
-
}
|
|
594
|
-
whereNotNull(column) {
|
|
595
|
-
this._statements.push(this._container.resolve(WhereStatement, [column, SqlOperator.NOT_NULL, null, this._tableAlias, this._container]));
|
|
596
|
-
return this;
|
|
597
|
-
}
|
|
598
|
-
whereNull(column) {
|
|
599
|
-
this._statements.push(this._container.resolve(WhereStatement, [column, SqlOperator.NULL, null, this._tableAlias, this._container]));
|
|
600
|
-
return this;
|
|
601
|
-
}
|
|
602
|
-
whereNot(column, val) {
|
|
603
|
-
return this.where(column, SqlOperator.NOT, val);
|
|
604
|
-
}
|
|
605
|
-
whereIn(column, val) {
|
|
606
|
-
this._statements.push(this._container.resolve(InStatement, [column, val, false, this._tableAlias, this._container]));
|
|
607
|
-
return this;
|
|
608
|
-
}
|
|
609
|
-
whereNotIn(column, val) {
|
|
610
|
-
this._statements.push(this._container.resolve(InStatement, [column, val, true, this._tableAlias, this._container]));
|
|
611
|
-
return this;
|
|
612
|
-
}
|
|
613
|
-
whereExist(query) {
|
|
614
|
-
this._statements.push(this._container.resolve(ExistsQueryStatement, [query, false]));
|
|
615
|
-
return this;
|
|
616
|
-
}
|
|
617
|
-
whereNotExists(query) {
|
|
618
|
-
this._statements.push(this._container.resolve(ExistsQueryStatement, [query, true]));
|
|
619
|
-
return this;
|
|
620
|
-
}
|
|
621
|
-
whereBetween(column, val) {
|
|
622
|
-
this._statements.push(this._container.resolve(BetweenStatement, [column, val, false, this._tableAlias]));
|
|
623
|
-
return this;
|
|
624
|
-
}
|
|
625
|
-
whereNotBetween(column, val) {
|
|
626
|
-
this._statements.push(this._container.resolve(BetweenStatement, [column, val, true, this._tableAlias]));
|
|
627
|
-
return this;
|
|
628
|
-
}
|
|
629
|
-
whereInSet(column, val) {
|
|
630
|
-
this._statements.push(this._container.resolve(InSetStatement, [column, val, false, this._tableAlias]));
|
|
631
|
-
return this;
|
|
632
|
-
}
|
|
633
|
-
whereNotInSet(column, val) {
|
|
634
|
-
this._statements.push(this._container.resolve(InSetStatement, [column, val, true, this._tableAlias]));
|
|
635
|
-
return this;
|
|
636
|
-
}
|
|
637
|
-
clearWhere() {
|
|
638
|
-
this._statements = [];
|
|
639
|
-
return this;
|
|
640
|
-
}
|
|
641
|
-
};
|
|
642
|
-
WhereBuilder = WhereBuilder_1 = __decorate([
|
|
643
|
-
NewInstance(),
|
|
644
|
-
__metadata("design:paramtypes", [Container, String])
|
|
645
|
-
], WhereBuilder);
|
|
646
|
-
export { WhereBuilder };
|
|
647
|
-
let SelectQueryBuilder = SelectQueryBuilder_1 = class SelectQueryBuilder extends QueryBuilder {
|
|
648
|
-
get Owner() {
|
|
649
|
-
return this._owner;
|
|
650
|
-
}
|
|
651
|
-
get IsDistinct() {
|
|
652
|
-
return this._distinct;
|
|
653
|
-
}
|
|
654
|
-
get Relations() {
|
|
655
|
-
return this._relations;
|
|
656
|
-
}
|
|
657
|
-
constructor(container, driver, model, owner) {
|
|
658
|
-
super(container, driver, model);
|
|
659
|
-
this._columns = [];
|
|
660
|
-
/**
|
|
661
|
-
* where query props
|
|
662
|
-
*/
|
|
663
|
-
this._statements = [];
|
|
664
|
-
this._joinStatements = [];
|
|
665
|
-
this._groupStatements = [];
|
|
666
|
-
this._relations = [];
|
|
667
|
-
this._owner = owner;
|
|
668
|
-
this._distinct = false;
|
|
669
|
-
this._method = QueryMethod.SELECT;
|
|
670
|
-
this._boolean = WhereBoolean.AND;
|
|
671
|
-
this._sort = {
|
|
672
|
-
column: '',
|
|
673
|
-
order: SortOrder.ASC,
|
|
674
|
-
};
|
|
675
|
-
this._first = false;
|
|
676
|
-
this._limit = {
|
|
677
|
-
limit: -1,
|
|
678
|
-
offset: -1,
|
|
679
|
-
};
|
|
680
|
-
this._nonSelect = false;
|
|
681
|
-
this.QueryContext = QueryContext.Select;
|
|
682
|
-
this._queryMiddlewares.forEach((x) => x.afterQueryCreation(this));
|
|
683
|
-
}
|
|
684
|
-
async asRaw() {
|
|
685
|
-
this._asRaw = true;
|
|
686
|
-
return (await this);
|
|
687
|
-
}
|
|
688
|
-
setAlias(alias) {
|
|
689
|
-
this._tableAlias = alias;
|
|
690
|
-
this._columns.forEach((c) => (c.TableAlias = alias));
|
|
691
|
-
this._joinStatements.forEach((c) => (c.TableAlias = alias));
|
|
692
|
-
this._statements.forEach((c) => (c.TableAlias = alias));
|
|
693
|
-
return this;
|
|
694
|
-
}
|
|
695
|
-
clone() {
|
|
696
|
-
const builder = new SelectQueryBuilder_1(this._container, this._driver, this._model, this._owner);
|
|
697
|
-
builder._columns = this._columns.slice(0);
|
|
698
|
-
builder._joinStatements = this._joinStatements.slice(0);
|
|
699
|
-
builder._statements = this._statements.slice(0);
|
|
700
|
-
builder._limit = { ...this._limit };
|
|
701
|
-
builder._sort = { ...this._sort };
|
|
702
|
-
builder._boolean = this._boolean;
|
|
703
|
-
builder._distinct = this._distinct;
|
|
704
|
-
builder._table = this._table;
|
|
705
|
-
builder._tableAlias = this._tableAlias;
|
|
706
|
-
builder._queryMiddlewares = [...this._queryMiddlewares];
|
|
707
|
-
return builder;
|
|
708
|
-
}
|
|
709
|
-
populate(relation, callback) {
|
|
710
|
-
if (!relation) {
|
|
711
|
-
return this;
|
|
712
|
-
}
|
|
713
|
-
if (typeof relation === 'object') {
|
|
714
|
-
for (const i in relation) {
|
|
715
|
-
this.populate(i, () => {
|
|
716
|
-
if (relation)
|
|
717
|
-
this.populate(relation[i]);
|
|
718
|
-
});
|
|
719
|
-
}
|
|
720
|
-
return this;
|
|
721
|
-
}
|
|
722
|
-
// if relation was already populated, just call callback on it
|
|
723
|
-
const fRelation = this._relations.find((r) => r.Name === relation);
|
|
724
|
-
if (fRelation) {
|
|
725
|
-
fRelation.executeOnQuery(callback);
|
|
726
|
-
return this;
|
|
727
|
-
}
|
|
728
|
-
let relInstance = null;
|
|
729
|
-
const descriptor = extractModelDescriptor(this._model);
|
|
730
|
-
let rDescriptor = null;
|
|
731
|
-
for (const [key, value] of descriptor.Relations) {
|
|
732
|
-
if (key.toLowerCase() === relation.toLowerCase().trim()) {
|
|
733
|
-
rDescriptor = value;
|
|
734
|
-
break;
|
|
735
|
-
}
|
|
736
|
-
}
|
|
737
|
-
if (!rDescriptor) {
|
|
738
|
-
throw new InvalidArgument(`Relation ${relation} not exists in model ${this._model?.constructor.name}`);
|
|
739
|
-
}
|
|
740
|
-
if (rDescriptor.Recursive) {
|
|
741
|
-
relInstance = this._container.resolve(BelongsToRecursiveRelation, [this, rDescriptor, this._owner]);
|
|
742
|
-
}
|
|
743
|
-
else {
|
|
744
|
-
switch (rDescriptor.Type) {
|
|
745
|
-
case RelationType.One:
|
|
746
|
-
// if parent relation is one to many we dont set parent relation
|
|
747
|
-
// couse its new query to not mess with column aliases and hydrator
|
|
748
|
-
relInstance = this._container.resolve(BelongsToRelation, [this, rDescriptor, this._owner instanceof OneToManyRelation ? null : this._owner]);
|
|
749
|
-
break;
|
|
750
|
-
case RelationType.Many:
|
|
751
|
-
relInstance = this._container.resolve(OneToManyRelation, [this, rDescriptor, this._owner]);
|
|
752
|
-
break;
|
|
753
|
-
case RelationType.ManyToMany:
|
|
754
|
-
relInstance = this._container.resolve(ManyToManyRelation, [this, rDescriptor, null]);
|
|
755
|
-
break;
|
|
756
|
-
}
|
|
757
|
-
}
|
|
758
|
-
relInstance.execute(callback);
|
|
759
|
-
relInstance.Name = relation;
|
|
760
|
-
this._relations.push(relInstance);
|
|
761
|
-
return this;
|
|
762
|
-
}
|
|
763
|
-
mergeBuilder(builder) {
|
|
764
|
-
this._joinStatements = this._joinStatements.concat(builder._joinStatements);
|
|
765
|
-
this._columns = this._columns.concat(builder._columns);
|
|
766
|
-
this.mergeStatements(builder);
|
|
767
|
-
}
|
|
768
|
-
mergeRelations(builder) {
|
|
769
|
-
this._relations = this._relations.concat(builder._relations);
|
|
770
|
-
this._middlewares = this._middlewares.concat(builder._middlewares);
|
|
771
|
-
}
|
|
772
|
-
mergeStatements(builder, callback) {
|
|
773
|
-
const stms = callback ? builder._statements.filter(callback) : builder._statements;
|
|
774
|
-
this._statements = this._statements.concat(stms);
|
|
775
|
-
}
|
|
776
|
-
min(column, as) {
|
|
777
|
-
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.MIN, as, this._tableAlias]));
|
|
778
|
-
return this;
|
|
779
|
-
}
|
|
780
|
-
max(column, as) {
|
|
781
|
-
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.MAX, as, this._tableAlias]));
|
|
782
|
-
return this;
|
|
783
|
-
}
|
|
784
|
-
count(column, as) {
|
|
785
|
-
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.COUNT, as, this._tableAlias]));
|
|
786
|
-
return this;
|
|
787
|
-
}
|
|
788
|
-
sum(column, as) {
|
|
789
|
-
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.SUM, as, this._tableAlias]));
|
|
790
|
-
return this;
|
|
791
|
-
}
|
|
792
|
-
avg(column, as) {
|
|
793
|
-
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.AVG, as, this._tableAlias]));
|
|
794
|
-
return this;
|
|
795
|
-
}
|
|
796
|
-
distinct() {
|
|
797
|
-
if (this._columns.length === 0 || this._columns[0].IsWildcard) {
|
|
798
|
-
throw new InvalidOperation('Cannot force DISTINCT on unknown column');
|
|
799
|
-
}
|
|
800
|
-
this._distinct = true;
|
|
801
|
-
return this;
|
|
802
|
-
}
|
|
803
|
-
toDB() {
|
|
804
|
-
const compiler = this._container.resolve(SelectQueryCompiler, [this]);
|
|
805
|
-
return compiler.compile();
|
|
806
|
-
}
|
|
807
|
-
async all() {
|
|
808
|
-
return (await this);
|
|
809
|
-
}
|
|
810
|
-
then(onfulfilled, onrejected) {
|
|
811
|
-
this._queryMiddlewares.forEach((x) => x.beforeQueryExecution(this));
|
|
812
|
-
return super.then((result) => {
|
|
813
|
-
if (this._first) {
|
|
814
|
-
if (Array.isArray(result)) {
|
|
815
|
-
if (result.length !== 0) {
|
|
816
|
-
return onfulfilled(result ? result[0] : null);
|
|
817
|
-
}
|
|
818
|
-
else {
|
|
819
|
-
try {
|
|
820
|
-
return onfulfilled(undefined);
|
|
821
|
-
}
|
|
822
|
-
catch (err) {
|
|
823
|
-
onrejected(err);
|
|
824
|
-
}
|
|
825
|
-
}
|
|
826
|
-
}
|
|
827
|
-
else {
|
|
828
|
-
return onfulfilled(result);
|
|
829
|
-
}
|
|
830
|
-
}
|
|
831
|
-
else {
|
|
832
|
-
return onfulfilled(result);
|
|
833
|
-
}
|
|
834
|
-
}, onrejected);
|
|
835
|
-
}
|
|
836
|
-
async execute() {
|
|
837
|
-
return (await this);
|
|
838
|
-
}
|
|
839
|
-
};
|
|
840
|
-
__decorate([
|
|
841
|
-
use(WhereBuilder, LimitBuilder, OrderByBuilder, ColumnsBuilder, JoinBuilder, WithRecursiveBuilder, GroupByBuilder),
|
|
842
|
-
__metadata("design:type", Object)
|
|
843
|
-
], SelectQueryBuilder.prototype, "this", void 0);
|
|
844
|
-
SelectQueryBuilder = SelectQueryBuilder_1 = __decorate([
|
|
845
|
-
Injectable(
|
|
846
|
-
__metadata("design:paramtypes", [Object, OrmDriver, Object, Object])
|
|
847
|
-
], SelectQueryBuilder);
|
|
848
|
-
export { SelectQueryBuilder };
|
|
849
|
-
export class SelectQueryBuilderC extends SelectQueryBuilder {
|
|
850
|
-
}
|
|
851
|
-
let DeleteQueryBuilder = class DeleteQueryBuilder extends QueryBuilder {
|
|
852
|
-
constructor(container, driver, model) {
|
|
853
|
-
super(container, driver, model);
|
|
854
|
-
this._method = QueryMethod.DELETE;
|
|
855
|
-
this._statements = [];
|
|
856
|
-
this._boolean = WhereBoolean.AND;
|
|
857
|
-
this._limit = {
|
|
858
|
-
limit: -1,
|
|
859
|
-
offset: -1,
|
|
860
|
-
};
|
|
861
|
-
this.QueryContext = QueryContext.Delete;
|
|
862
|
-
}
|
|
863
|
-
toDB() {
|
|
864
|
-
return this._container.resolve(DeleteQueryCompiler, [this]).compile();
|
|
865
|
-
}
|
|
866
|
-
};
|
|
867
|
-
__decorate([
|
|
868
|
-
use(WhereBuilder, LimitBuilder)
|
|
869
|
-
/// @ts-ignore
|
|
870
|
-
,
|
|
871
|
-
__metadata("design:type", Object)
|
|
872
|
-
], DeleteQueryBuilder.prototype, "this", void 0);
|
|
873
|
-
DeleteQueryBuilder = __decorate([
|
|
874
|
-
NewInstance(),
|
|
875
|
-
__metadata("design:paramtypes", [Container, OrmDriver, Object])
|
|
876
|
-
], DeleteQueryBuilder);
|
|
877
|
-
export { DeleteQueryBuilder };
|
|
878
|
-
let OnDuplicateQueryBuilder = class OnDuplicateQueryBuilder {
|
|
879
|
-
constructor(container, insertQueryBuilder, column) {
|
|
880
|
-
this._parent = insertQueryBuilder;
|
|
881
|
-
this._container = container;
|
|
882
|
-
this._column = _.isArray(column) ? column : [column];
|
|
883
|
-
}
|
|
884
|
-
getColumn() {
|
|
885
|
-
return this._column;
|
|
886
|
-
}
|
|
887
|
-
getColumnsToUpdate() {
|
|
888
|
-
return this._columnsToUpdate;
|
|
889
|
-
}
|
|
890
|
-
getParent() {
|
|
891
|
-
return this._parent;
|
|
892
|
-
}
|
|
893
|
-
update(columns) {
|
|
894
|
-
this._columnsToUpdate = columns;
|
|
895
|
-
return this;
|
|
896
|
-
}
|
|
897
|
-
then(onfulfilled, onrejected) {
|
|
898
|
-
return this._parent.then(onfulfilled, onrejected);
|
|
899
|
-
}
|
|
900
|
-
toDB() {
|
|
901
|
-
return this._parent.toDB();
|
|
902
|
-
}
|
|
903
|
-
};
|
|
904
|
-
OnDuplicateQueryBuilder = __decorate([
|
|
905
|
-
NewInstance(),
|
|
906
|
-
__metadata("design:paramtypes", [Object, QueryBuilder, Object])
|
|
907
|
-
], OnDuplicateQueryBuilder);
|
|
908
|
-
export { OnDuplicateQueryBuilder };
|
|
909
|
-
let UpdateQueryBuilder = class UpdateQueryBuilder extends QueryBuilder {
|
|
910
|
-
get Value() {
|
|
911
|
-
return this._value;
|
|
912
|
-
}
|
|
913
|
-
constructor(container, driver, model) {
|
|
914
|
-
super(container, driver, model);
|
|
915
|
-
this._value = {};
|
|
916
|
-
this._method = QueryMethod.UPDATE;
|
|
917
|
-
this._boolean = WhereBoolean.AND;
|
|
918
|
-
this._statements = [];
|
|
919
|
-
this.QueryContext = QueryContext.Update;
|
|
920
|
-
}
|
|
921
|
-
in(name) {
|
|
922
|
-
this.setTable(name);
|
|
923
|
-
return this;
|
|
924
|
-
}
|
|
925
|
-
update(value) {
|
|
926
|
-
this._value = value;
|
|
927
|
-
return this;
|
|
928
|
-
}
|
|
929
|
-
toDB() {
|
|
930
|
-
return this._container.resolve(UpdateQueryCompiler, [this]).compile();
|
|
931
|
-
}
|
|
932
|
-
};
|
|
933
|
-
__decorate([
|
|
934
|
-
use(WhereBuilder),
|
|
935
|
-
__metadata("design:type", Object)
|
|
936
|
-
], UpdateQueryBuilder.prototype, "this", void 0);
|
|
937
|
-
UpdateQueryBuilder = __decorate([
|
|
938
|
-
NewInstance(),
|
|
939
|
-
__metadata("design:paramtypes", [Container, OrmDriver, Object])
|
|
940
|
-
], UpdateQueryBuilder);
|
|
941
|
-
export { UpdateQueryBuilder };
|
|
942
|
-
let InsertQueryBuilder = class InsertQueryBuilder extends QueryBuilder {
|
|
943
|
-
get Values() {
|
|
944
|
-
return this._values;
|
|
945
|
-
}
|
|
946
|
-
get Ignore() {
|
|
947
|
-
return this._ignore;
|
|
948
|
-
}
|
|
949
|
-
get Update() {
|
|
950
|
-
return this._update;
|
|
951
|
-
}
|
|
952
|
-
get Replace() {
|
|
953
|
-
return this._replace;
|
|
954
|
-
}
|
|
955
|
-
constructor(container, driver, model) {
|
|
956
|
-
super(container, driver, model);
|
|
957
|
-
this._method = QueryMethod.INSERT;
|
|
958
|
-
this._columns = [];
|
|
959
|
-
this._values = [];
|
|
960
|
-
this.QueryContext = QueryContext.Insert;
|
|
961
|
-
}
|
|
962
|
-
/**
|
|
963
|
-
* Sets insert to ignore on duplicate
|
|
964
|
-
*/
|
|
965
|
-
orIgnore() {
|
|
966
|
-
this._ignore = true;
|
|
967
|
-
return this;
|
|
968
|
-
}
|
|
969
|
-
orReplace() {
|
|
970
|
-
this._update = true;
|
|
971
|
-
return this;
|
|
972
|
-
}
|
|
973
|
-
values(data) {
|
|
974
|
-
const self = this;
|
|
975
|
-
if (Array.isArray(data)) {
|
|
976
|
-
this.columns(_.chain(data).map(_.keys).flatten().uniq().value());
|
|
977
|
-
data.forEach((d) => {
|
|
978
|
-
_addData(d);
|
|
979
|
-
});
|
|
980
|
-
}
|
|
981
|
-
else {
|
|
982
|
-
this.columns(_.keysIn(data));
|
|
983
|
-
_addData(data);
|
|
984
|
-
}
|
|
985
|
-
function _addData(d) {
|
|
986
|
-
const binding = [];
|
|
987
|
-
self._columns
|
|
988
|
-
.filter((c) => !(c.Column instanceof RawQuery))
|
|
989
|
-
.map((c) => {
|
|
990
|
-
return c.Column;
|
|
991
|
-
})
|
|
992
|
-
.forEach((c) => {
|
|
993
|
-
binding.push(d[c]);
|
|
994
|
-
});
|
|
995
|
-
self._values.push(binding);
|
|
996
|
-
}
|
|
997
|
-
return this;
|
|
998
|
-
}
|
|
999
|
-
into(table, schema) {
|
|
1000
|
-
this.setTable(table, schema);
|
|
1001
|
-
return this;
|
|
1002
|
-
}
|
|
1003
|
-
onDuplicate(column) {
|
|
1004
|
-
let columnToCheck = column;
|
|
1005
|
-
if (!columnToCheck && this._model) {
|
|
1006
|
-
const dsc = extractModelDescriptor(this._model);
|
|
1007
|
-
columnToCheck = dsc.Columns.filter((c) => c.Unique).map((c) => c.Name);
|
|
1008
|
-
}
|
|
1009
|
-
this._update = true;
|
|
1010
|
-
this.DuplicateQueryBuilder = new OnDuplicateQueryBuilder(this._container, this, columnToCheck);
|
|
1011
|
-
return this.DuplicateQueryBuilder;
|
|
1012
|
-
}
|
|
1013
|
-
toDB() {
|
|
1014
|
-
return this._container.resolve(InsertQueryCompiler, [this]).compile();
|
|
1015
|
-
}
|
|
1016
|
-
};
|
|
1017
|
-
__decorate([
|
|
1018
|
-
use(ColumnsBuilder),
|
|
1019
|
-
__metadata("design:type", Object)
|
|
1020
|
-
], InsertQueryBuilder.prototype, "this", void 0);
|
|
1021
|
-
InsertQueryBuilder = __decorate([
|
|
1022
|
-
NewInstance(),
|
|
1023
|
-
__metadata("design:paramtypes", [Container, OrmDriver, Object])
|
|
1024
|
-
], InsertQueryBuilder);
|
|
1025
|
-
export { InsertQueryBuilder };
|
|
1026
|
-
let IndexQueryBuilder = class IndexQueryBuilder extends Builder {
|
|
1027
|
-
constructor(container, driver) {
|
|
1028
|
-
super(container, driver);
|
|
1029
|
-
this.QueryContext = QueryContext.Schema;
|
|
1030
|
-
}
|
|
1031
|
-
name(name) {
|
|
1032
|
-
this.Name = name;
|
|
1033
|
-
return this;
|
|
1034
|
-
}
|
|
1035
|
-
unique() {
|
|
1036
|
-
this.Unique = true;
|
|
1037
|
-
return this;
|
|
1038
|
-
}
|
|
1039
|
-
table(name) {
|
|
1040
|
-
this.Table = name;
|
|
1041
|
-
return this;
|
|
1042
|
-
}
|
|
1043
|
-
columns(colNames) {
|
|
1044
|
-
this.Columns = colNames;
|
|
1045
|
-
return this;
|
|
1046
|
-
}
|
|
1047
|
-
toDB() {
|
|
1048
|
-
return this._container.resolve(IndexQueryCompiler, [this]).compile();
|
|
1049
|
-
}
|
|
1050
|
-
};
|
|
1051
|
-
IndexQueryBuilder = __decorate([
|
|
1052
|
-
NewInstance(),
|
|
1053
|
-
Inject(Container),
|
|
1054
|
-
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1055
|
-
], IndexQueryBuilder);
|
|
1056
|
-
export { IndexQueryBuilder };
|
|
1057
|
-
let ForeignKeyBuilder = class ForeignKeyBuilder {
|
|
1058
|
-
constructor() {
|
|
1059
|
-
this.OnDeleteAction = ReferentialAction.NoAction;
|
|
1060
|
-
this.OnUpdateAction = ReferentialAction.NoAction;
|
|
1061
|
-
}
|
|
1062
|
-
/**
|
|
1063
|
-
*
|
|
1064
|
-
* Referenced field in child table
|
|
1065
|
-
*
|
|
1066
|
-
* @param fkName - name of foreign field in child table
|
|
1067
|
-
*/
|
|
1068
|
-
foreignKey(fkName) {
|
|
1069
|
-
this.ForeignKeyField = fkName;
|
|
1070
|
-
return this;
|
|
1071
|
-
}
|
|
1072
|
-
/**
|
|
1073
|
-
*
|
|
1074
|
-
* Referenced parent table & key
|
|
1075
|
-
*
|
|
1076
|
-
* @param table - parent table
|
|
1077
|
-
* @param pKey - parant table key field
|
|
1078
|
-
*/
|
|
1079
|
-
references(table, pKey) {
|
|
1080
|
-
this.Table = table;
|
|
1081
|
-
this.PrimaryKey = pKey;
|
|
1082
|
-
return this;
|
|
1083
|
-
}
|
|
1084
|
-
/**
|
|
1085
|
-
*
|
|
1086
|
-
* On delete action
|
|
1087
|
-
*
|
|
1088
|
-
* @param action - action to take on delete
|
|
1089
|
-
*/
|
|
1090
|
-
onDelete(action) {
|
|
1091
|
-
this.OnDeleteAction = action;
|
|
1092
|
-
return this;
|
|
1093
|
-
}
|
|
1094
|
-
/**
|
|
1095
|
-
*
|
|
1096
|
-
* On update action
|
|
1097
|
-
*
|
|
1098
|
-
* @param action - action to take on update
|
|
1099
|
-
*/
|
|
1100
|
-
onUpdate(action) {
|
|
1101
|
-
this.OnUpdateAction = action;
|
|
1102
|
-
return this;
|
|
1103
|
-
}
|
|
1104
|
-
/**
|
|
1105
|
-
* Shorhand for on update and on delete cascade settings
|
|
1106
|
-
*/
|
|
1107
|
-
cascade() {
|
|
1108
|
-
this.OnUpdateAction = ReferentialAction.Cascade;
|
|
1109
|
-
this.OnDeleteAction = ReferentialAction.Cascade;
|
|
1110
|
-
return this;
|
|
1111
|
-
}
|
|
1112
|
-
};
|
|
1113
|
-
ForeignKeyBuilder = __decorate([
|
|
1114
|
-
NewInstance(),
|
|
1115
|
-
__metadata("design:paramtypes", [])
|
|
1116
|
-
], ForeignKeyBuilder);
|
|
1117
|
-
export { ForeignKeyBuilder };
|
|
1118
|
-
let ColumnQueryBuilder = class ColumnQueryBuilder {
|
|
1119
|
-
constructor(container, name, type, ...args) {
|
|
1120
|
-
this.container = container;
|
|
1121
|
-
this.Name = name;
|
|
1122
|
-
this.Type = type;
|
|
1123
|
-
this.Charset = '';
|
|
1124
|
-
this.Args = [];
|
|
1125
|
-
this.AutoIncrement = false;
|
|
1126
|
-
this.NotNull = false;
|
|
1127
|
-
this.Collation = '';
|
|
1128
|
-
this.Comment = '';
|
|
1129
|
-
this.Unique = false;
|
|
1130
|
-
this.Unsigned = false;
|
|
1131
|
-
this.Args.push(...args);
|
|
1132
|
-
}
|
|
1133
|
-
notNull() {
|
|
1134
|
-
this.NotNull = true;
|
|
1135
|
-
return this;
|
|
1136
|
-
}
|
|
1137
|
-
unique() {
|
|
1138
|
-
this.Unique = true;
|
|
1139
|
-
return this;
|
|
1140
|
-
}
|
|
1141
|
-
unsigned() {
|
|
1142
|
-
this.Unsigned = true;
|
|
1143
|
-
return this;
|
|
1144
|
-
}
|
|
1145
|
-
autoIncrement() {
|
|
1146
|
-
this.AutoIncrement = true;
|
|
1147
|
-
return this;
|
|
1148
|
-
}
|
|
1149
|
-
default() {
|
|
1150
|
-
this.Default = this.container.resolve((DefaultValueBuilder), [this]);
|
|
1151
|
-
return this.Default;
|
|
1152
|
-
}
|
|
1153
|
-
primaryKey() {
|
|
1154
|
-
this.PrimaryKey = true;
|
|
1155
|
-
return this;
|
|
1156
|
-
}
|
|
1157
|
-
comment(comment) {
|
|
1158
|
-
this.Comment = comment;
|
|
1159
|
-
return this;
|
|
1160
|
-
}
|
|
1161
|
-
charset(charset) {
|
|
1162
|
-
this.Charset = charset;
|
|
1163
|
-
return this;
|
|
1164
|
-
}
|
|
1165
|
-
collation(collation) {
|
|
1166
|
-
this.Collation = collation;
|
|
1167
|
-
return this;
|
|
1168
|
-
}
|
|
1169
|
-
};
|
|
1170
|
-
ColumnQueryBuilder = __decorate([
|
|
1171
|
-
NewInstance(),
|
|
1172
|
-
Inject(Container),
|
|
1173
|
-
__metadata("design:paramtypes", [Object, String, String, Object])
|
|
1174
|
-
], ColumnQueryBuilder);
|
|
1175
|
-
export { ColumnQueryBuilder };
|
|
1176
|
-
let AlterColumnQueryBuilder = class AlterColumnQueryBuilder extends ColumnQueryBuilder {
|
|
1177
|
-
constructor(container, name, type, ...args) {
|
|
1178
|
-
super(container, name, type, ...args);
|
|
1179
|
-
this.OldName = name;
|
|
1180
|
-
// we assume add by default
|
|
1181
|
-
this.AlterType = ColumnAlterationType.Add;
|
|
1182
|
-
}
|
|
1183
|
-
addColumn() {
|
|
1184
|
-
this.AlterType = ColumnAlterationType.Add;
|
|
1185
|
-
return this;
|
|
1186
|
-
}
|
|
1187
|
-
modify() {
|
|
1188
|
-
this.AlterType = ColumnAlterationType.Modify;
|
|
1189
|
-
return this;
|
|
1190
|
-
}
|
|
1191
|
-
rename(newName) {
|
|
1192
|
-
this.AlterType = ColumnAlterationType.Rename;
|
|
1193
|
-
this.Name = newName;
|
|
1194
|
-
return this;
|
|
1195
|
-
}
|
|
1196
|
-
after(columnName) {
|
|
1197
|
-
this.AfterColumn = columnName;
|
|
1198
|
-
return this;
|
|
1199
|
-
}
|
|
1200
|
-
};
|
|
1201
|
-
AlterColumnQueryBuilder = __decorate([
|
|
1202
|
-
Inject(Container),
|
|
1203
|
-
NewInstance(),
|
|
1204
|
-
__metadata("design:paramtypes", [Object, String, String, Object])
|
|
1205
|
-
], AlterColumnQueryBuilder);
|
|
1206
|
-
export { AlterColumnQueryBuilder };
|
|
1207
|
-
let TableExistsQueryBuilder = class TableExistsQueryBuilder extends QueryBuilder {
|
|
1208
|
-
constructor(container, driver, name) {
|
|
1209
|
-
super(container, driver, null);
|
|
1210
|
-
this.setTable(name);
|
|
1211
|
-
this.QueryContext = QueryContext.Select;
|
|
1212
|
-
}
|
|
1213
|
-
toDB() {
|
|
1214
|
-
return this._container.resolve(TableExistsCompiler, [this]).compile();
|
|
1215
|
-
}
|
|
1216
|
-
};
|
|
1217
|
-
TableExistsQueryBuilder = __decorate([
|
|
1218
|
-
NewInstance(),
|
|
1219
|
-
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1220
|
-
], TableExistsQueryBuilder);
|
|
1221
|
-
export { TableExistsQueryBuilder };
|
|
1222
|
-
let DropTableQueryBuilder = class DropTableQueryBuilder extends QueryBuilder {
|
|
1223
|
-
constructor(container, driver, name, database) {
|
|
1224
|
-
super(container, driver, null);
|
|
1225
|
-
this.setTable(name);
|
|
1226
|
-
if (database) {
|
|
1227
|
-
this.database(database);
|
|
1228
|
-
}
|
|
1229
|
-
this.Exists = false;
|
|
1230
|
-
this.QueryContext = QueryContext.Schema;
|
|
1231
|
-
}
|
|
1232
|
-
ifExists() {
|
|
1233
|
-
this.Exists = true;
|
|
1234
|
-
return this;
|
|
1235
|
-
}
|
|
1236
|
-
toDB() {
|
|
1237
|
-
return this._container.resolve(DropTableCompiler, [this]).compile();
|
|
1238
|
-
}
|
|
1239
|
-
};
|
|
1240
|
-
DropTableQueryBuilder = __decorate([
|
|
1241
|
-
NewInstance(),
|
|
1242
|
-
__metadata("design:paramtypes", [Container, OrmDriver, String, String])
|
|
1243
|
-
], DropTableQueryBuilder);
|
|
1244
|
-
export { DropTableQueryBuilder };
|
|
1245
|
-
let AlterTableQueryBuilder = class AlterTableQueryBuilder extends QueryBuilder {
|
|
1246
|
-
get Columns() {
|
|
1247
|
-
return this._columns;
|
|
1248
|
-
}
|
|
1249
|
-
constructor(container, driver, name) {
|
|
1250
|
-
super(container, driver, null);
|
|
1251
|
-
this.setTable(name);
|
|
1252
|
-
this.QueryContext = QueryContext.Schema;
|
|
1253
|
-
this._columns = [];
|
|
1254
|
-
this.DroppedColumns = [];
|
|
1255
|
-
}
|
|
1256
|
-
/**
|
|
1257
|
-
* Renames table
|
|
1258
|
-
*
|
|
1259
|
-
* @param newTableName - new table name
|
|
1260
|
-
*/
|
|
1261
|
-
rename(newTableName) {
|
|
1262
|
-
this.NewTableName = newTableName;
|
|
1263
|
-
}
|
|
1264
|
-
dropColumn(column) {
|
|
1265
|
-
this.DroppedColumns.push(column);
|
|
1266
|
-
}
|
|
1267
|
-
toDB() {
|
|
1268
|
-
return this._container.resolve(AlterTableQueryCompiler, [this]).compile();
|
|
1269
|
-
}
|
|
1270
|
-
};
|
|
1271
|
-
AlterTableQueryBuilder = __decorate([
|
|
1272
|
-
NewInstance(),
|
|
1273
|
-
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1274
|
-
], AlterTableQueryBuilder);
|
|
1275
|
-
export { AlterTableQueryBuilder };
|
|
1276
|
-
let TableQueryBuilder = class TableQueryBuilder extends QueryBuilder {
|
|
1277
|
-
/**
|
|
1278
|
-
* Alias for string(name, 36 )
|
|
1279
|
-
*/
|
|
1280
|
-
uuid(name) {
|
|
1281
|
-
return this.string(name, 36);
|
|
1282
|
-
}
|
|
1283
|
-
ifExists() {
|
|
1284
|
-
this._checkExists = true;
|
|
1285
|
-
return this;
|
|
1286
|
-
}
|
|
1287
|
-
/**
|
|
1288
|
-
* Mark table as temporary
|
|
1289
|
-
*/
|
|
1290
|
-
temporary() {
|
|
1291
|
-
this._temporary = true;
|
|
1292
|
-
return this;
|
|
1293
|
-
}
|
|
1294
|
-
/**
|
|
1295
|
-
* Turn on history trackign for this table
|
|
1296
|
-
* Each change & row will be tracked, and all history of changes can be accessed
|
|
1297
|
-
*/
|
|
1298
|
-
trackHistory() {
|
|
1299
|
-
this._trackHistory = true;
|
|
1300
|
-
return this;
|
|
1301
|
-
}
|
|
1302
|
-
get Columns() {
|
|
1303
|
-
return this._columns;
|
|
1304
|
-
}
|
|
1305
|
-
get ForeignKeys() {
|
|
1306
|
-
return this._foreignKeys;
|
|
1307
|
-
}
|
|
1308
|
-
get CheckExists() {
|
|
1309
|
-
return this._checkExists;
|
|
1310
|
-
}
|
|
1311
|
-
get Temporary() {
|
|
1312
|
-
return this._temporary;
|
|
1313
|
-
}
|
|
1314
|
-
get TrackHistory() {
|
|
1315
|
-
return this._trackHistory;
|
|
1316
|
-
}
|
|
1317
|
-
constructor(container, driver, name) {
|
|
1318
|
-
super(container, driver, null);
|
|
1319
|
-
this._charset = '';
|
|
1320
|
-
this._comment = '';
|
|
1321
|
-
this._columns = [];
|
|
1322
|
-
this._foreignKeys = [];
|
|
1323
|
-
this._temporary = false;
|
|
1324
|
-
this._trackHistory = false;
|
|
1325
|
-
this.setTable(name);
|
|
1326
|
-
this.QueryContext = QueryContext.Schema;
|
|
1327
|
-
}
|
|
1328
|
-
increments(name) {
|
|
1329
|
-
return this.int(name).autoIncrement().notNull().primaryKey();
|
|
1330
|
-
}
|
|
1331
|
-
comment(comment) {
|
|
1332
|
-
this._comment = comment;
|
|
1333
|
-
}
|
|
1334
|
-
charset(charset) {
|
|
1335
|
-
this._charset = charset;
|
|
1336
|
-
}
|
|
1337
|
-
foreignKey(foreignKey) {
|
|
1338
|
-
const builder = new ForeignKeyBuilder();
|
|
1339
|
-
builder.foreignKey(foreignKey);
|
|
1340
|
-
this._foreignKeys.push(builder);
|
|
1341
|
-
return builder;
|
|
1342
|
-
}
|
|
1343
|
-
toDB() {
|
|
1344
|
-
return this._container.resolve(TableQueryCompiler, [this]).compile();
|
|
1345
|
-
}
|
|
1346
|
-
};
|
|
1347
|
-
TableQueryBuilder = __decorate([
|
|
1348
|
-
NewInstance(),
|
|
1349
|
-
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1350
|
-
], TableQueryBuilder);
|
|
1351
|
-
export { TableQueryBuilder };
|
|
1352
|
-
let TruncateTableQueryBuilder = class TruncateTableQueryBuilder extends QueryBuilder {
|
|
1353
|
-
constructor(container, driver) {
|
|
1354
|
-
super(container, driver);
|
|
1355
|
-
this.container = container;
|
|
1356
|
-
this.driver = driver;
|
|
1357
|
-
}
|
|
1358
|
-
toDB() {
|
|
1359
|
-
return this._container.resolve(TruncateTableQueryCompiler, [this.Container, this]).compile();
|
|
1360
|
-
}
|
|
1361
|
-
};
|
|
1362
|
-
TruncateTableQueryBuilder = __decorate([
|
|
1363
|
-
NewInstance(),
|
|
1364
|
-
Inject(Container),
|
|
1365
|
-
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1366
|
-
], TruncateTableQueryBuilder);
|
|
1367
|
-
export { TruncateTableQueryBuilder };
|
|
1368
|
-
let CloneTableQueryBuilder = class CloneTableQueryBuilder extends QueryBuilder {
|
|
1369
|
-
get CloneSource() {
|
|
1370
|
-
return this._cloneSrc;
|
|
1371
|
-
}
|
|
1372
|
-
get Temporary() {
|
|
1373
|
-
return this._temporary;
|
|
1374
|
-
}
|
|
1375
|
-
get Shallow() {
|
|
1376
|
-
return this._shallow;
|
|
1377
|
-
}
|
|
1378
|
-
get Filter() {
|
|
1379
|
-
return this._filter;
|
|
1380
|
-
}
|
|
1381
|
-
constructor(container, driver) {
|
|
1382
|
-
super(container, driver);
|
|
1383
|
-
this.container = container;
|
|
1384
|
-
this.driver = driver;
|
|
1385
|
-
this._shallow = true;
|
|
1386
|
-
this._cloneSrc = '';
|
|
1387
|
-
this._temporary = false;
|
|
1388
|
-
}
|
|
1389
|
-
/**
|
|
1390
|
-
* Clones table structure without data
|
|
1391
|
-
* Shorthand for createTable(( table) => table.clone("new"));
|
|
1392
|
-
*
|
|
1393
|
-
* @param srcTable - source table name
|
|
1394
|
-
* @param newTable - target table name
|
|
1395
|
-
*/
|
|
1396
|
-
shallowClone(srcTable, newTable) {
|
|
1397
|
-
this.setTable(newTable);
|
|
1398
|
-
this._cloneSrc = srcTable;
|
|
1399
|
-
return this;
|
|
1400
|
-
}
|
|
1401
|
-
/**
|
|
1402
|
-
* Clones table with data
|
|
1403
|
-
*
|
|
1404
|
-
* @param srcTable - source table name
|
|
1405
|
-
* @param newTable - target table name
|
|
1406
|
-
* @param filter - data filter, set null if all data is to be cloned
|
|
1407
|
-
*/
|
|
1408
|
-
async deepClone(srcTable, newTable, filter) {
|
|
1409
|
-
this.setTable(newTable);
|
|
1410
|
-
this._cloneSrc = srcTable;
|
|
1411
|
-
this._shallow = false;
|
|
1412
|
-
if (filter) {
|
|
1413
|
-
this._filter = new SelectQueryBuilder(this._container, this._driver);
|
|
1414
|
-
this._filter.setTable(this._cloneSrc);
|
|
1415
|
-
filter(this._filter);
|
|
1416
|
-
}
|
|
1417
|
-
return this;
|
|
1418
|
-
}
|
|
1419
|
-
toDB() {
|
|
1420
|
-
return this._container.resolve(TableCloneQueryCompiler, [this]).compile();
|
|
1421
|
-
}
|
|
1422
|
-
};
|
|
1423
|
-
CloneTableQueryBuilder = __decorate([
|
|
1424
|
-
NewInstance(),
|
|
1425
|
-
Inject(Container),
|
|
1426
|
-
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1427
|
-
], CloneTableQueryBuilder);
|
|
1428
|
-
export { CloneTableQueryBuilder };
|
|
1429
|
-
export class EventIntervalDesc {
|
|
1430
|
-
}
|
|
1431
|
-
let EventQueryBuilder = class EventQueryBuilder extends QueryBuilder {
|
|
1432
|
-
constructor(container, driver, Name) {
|
|
1433
|
-
super(container, driver);
|
|
1434
|
-
this.container = container;
|
|
1435
|
-
this.driver = driver;
|
|
1436
|
-
this.Name = Name;
|
|
1437
|
-
}
|
|
1438
|
-
/**
|
|
1439
|
-
* execute every time with specified interval ( days, hours, seconds, minutes etc)
|
|
1440
|
-
*/
|
|
1441
|
-
every() {
|
|
1442
|
-
this.EveryInterval = new EventIntervalDesc();
|
|
1443
|
-
return this.EveryInterval;
|
|
1444
|
-
}
|
|
1445
|
-
/**
|
|
1446
|
-
*
|
|
1447
|
-
* Execute at specific time
|
|
1448
|
-
*
|
|
1449
|
-
* @param dateTime - specific time
|
|
1450
|
-
*/
|
|
1451
|
-
at(dateTime) {
|
|
1452
|
-
this.At = dateTime;
|
|
1453
|
-
}
|
|
1454
|
-
/**
|
|
1455
|
-
* execute once at specific interfal from now eg. now + 1 day
|
|
1456
|
-
*/
|
|
1457
|
-
fromNow() {
|
|
1458
|
-
this.FromNowInverval = new EventIntervalDesc();
|
|
1459
|
-
return this.FromNowInverval;
|
|
1460
|
-
}
|
|
1461
|
-
/**
|
|
1462
|
-
*
|
|
1463
|
-
* @param sql - code to execute, could be raw sql query, single builder, or multiple builders that will be executed on by one
|
|
1464
|
-
*/
|
|
1465
|
-
do(sql) {
|
|
1466
|
-
if (sql instanceof RawQueryStatement) {
|
|
1467
|
-
this.RawSql = sql;
|
|
1468
|
-
}
|
|
1469
|
-
else if (Array.isArray(sql)) {
|
|
1470
|
-
this.Queries = sql;
|
|
1471
|
-
}
|
|
1472
|
-
else {
|
|
1473
|
-
this.Queries = [sql];
|
|
1474
|
-
}
|
|
1475
|
-
}
|
|
1476
|
-
/**
|
|
1477
|
-
*
|
|
1478
|
-
* Add comment to schedule for documentation. It is passed to sql engine
|
|
1479
|
-
*
|
|
1480
|
-
* @param comment - comment text
|
|
1481
|
-
*/
|
|
1482
|
-
comment(comment) {
|
|
1483
|
-
this.Comment = comment;
|
|
1484
|
-
}
|
|
1485
|
-
toDB() {
|
|
1486
|
-
return this._container.resolve(EventQueryCompiler, [this]).compile();
|
|
1487
|
-
}
|
|
1488
|
-
};
|
|
1489
|
-
EventQueryBuilder = __decorate([
|
|
1490
|
-
NewInstance(),
|
|
1491
|
-
Inject(Container),
|
|
1492
|
-
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1493
|
-
], EventQueryBuilder);
|
|
1494
|
-
export { EventQueryBuilder };
|
|
1495
|
-
let DropEventQueryBuilder = class DropEventQueryBuilder extends QueryBuilder {
|
|
1496
|
-
constructor(container, driver, Name) {
|
|
1497
|
-
super(container, driver);
|
|
1498
|
-
this.container = container;
|
|
1499
|
-
this.driver = driver;
|
|
1500
|
-
this.Name = Name;
|
|
1501
|
-
}
|
|
1502
|
-
toDB() {
|
|
1503
|
-
return this._container.resolve(DropEventQueryCompiler, [this]).compile();
|
|
1504
|
-
}
|
|
1505
|
-
};
|
|
1506
|
-
DropEventQueryBuilder = __decorate([
|
|
1507
|
-
NewInstance(),
|
|
1508
|
-
Inject(Container),
|
|
1509
|
-
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1510
|
-
], DropEventQueryBuilder);
|
|
1511
|
-
export { DropEventQueryBuilder };
|
|
1512
|
-
/**
|
|
1513
|
-
* Creates schedule job in database engine.
|
|
1514
|
-
* Note, some engines does not support this, so it will implemented
|
|
1515
|
-
* as nodejs interval
|
|
1516
|
-
*/
|
|
1517
|
-
let ScheduleQueryBuilder = class ScheduleQueryBuilder {
|
|
1518
|
-
constructor(container, driver) {
|
|
1519
|
-
this.container = container;
|
|
1520
|
-
this.driver = driver;
|
|
1521
|
-
}
|
|
1522
|
-
create(name, callback) {
|
|
1523
|
-
const builder = new EventQueryBuilder(this.container, this.driver, name);
|
|
1524
|
-
callback.call(this, builder);
|
|
1525
|
-
return builder;
|
|
1526
|
-
}
|
|
1527
|
-
drop(name) {
|
|
1528
|
-
return new DropEventQueryBuilder(this.container, this.driver, name);
|
|
1529
|
-
}
|
|
1530
|
-
};
|
|
1531
|
-
ScheduleQueryBuilder = __decorate([
|
|
1532
|
-
NewInstance(),
|
|
1533
|
-
Inject(Container),
|
|
1534
|
-
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1535
|
-
], ScheduleQueryBuilder);
|
|
1536
|
-
export { ScheduleQueryBuilder };
|
|
1537
|
-
let SchemaQueryBuilder = class SchemaQueryBuilder {
|
|
1538
|
-
constructor(container, driver) {
|
|
1539
|
-
this.container = container;
|
|
1540
|
-
this.driver = driver;
|
|
1541
|
-
}
|
|
1542
|
-
createTable(name, callback) {
|
|
1543
|
-
const builder = new TableQueryBuilder(this.container, this.driver, name);
|
|
1544
|
-
callback.call(this, builder);
|
|
1545
|
-
return builder;
|
|
1546
|
-
}
|
|
1547
|
-
cloneTable(callback) {
|
|
1548
|
-
const builder = new CloneTableQueryBuilder(this.container, this.driver);
|
|
1549
|
-
callback(builder);
|
|
1550
|
-
return builder;
|
|
1551
|
-
}
|
|
1552
|
-
alterTable(name, callback) {
|
|
1553
|
-
const builder = new AlterTableQueryBuilder(this.container, this.driver, name);
|
|
1554
|
-
callback.call(this, builder);
|
|
1555
|
-
return builder;
|
|
1556
|
-
}
|
|
1557
|
-
dropTable(name, schema) {
|
|
1558
|
-
return new DropTableQueryBuilder(this.container, this.driver, name, schema);
|
|
1559
|
-
}
|
|
1560
|
-
async tableExists(name, schema) {
|
|
1561
|
-
const query = new TableExistsQueryBuilder(this.container, this.driver, name);
|
|
1562
|
-
if (schema) {
|
|
1563
|
-
query.database(schema);
|
|
1564
|
-
}
|
|
1565
|
-
const exists = await query;
|
|
1566
|
-
return exists !== null && exists.length === 1;
|
|
1567
|
-
}
|
|
1568
|
-
event(name) {
|
|
1569
|
-
return new EventQueryBuilder(this.container, this.driver, name);
|
|
1570
|
-
}
|
|
1571
|
-
dropEvent(name) {
|
|
1572
|
-
return new DropEventQueryBuilder(this.container, this.driver, name);
|
|
1573
|
-
}
|
|
1574
|
-
};
|
|
1575
|
-
SchemaQueryBuilder = __decorate([
|
|
1576
|
-
NewInstance(),
|
|
1577
|
-
Inject(Container),
|
|
1578
|
-
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1579
|
-
], SchemaQueryBuilder);
|
|
1580
|
-
export { SchemaQueryBuilder };
|
|
1581
|
-
Object.values(ColumnType).forEach((type) => {
|
|
1582
|
-
TableQueryBuilder.prototype[type] = function (name, ...args) {
|
|
1583
|
-
const _builder = new ColumnQueryBuilder(this.Container, name, type, ...args);
|
|
1584
|
-
this._columns.push(_builder);
|
|
1585
|
-
return _builder;
|
|
1586
|
-
};
|
|
1587
|
-
});
|
|
1588
|
-
Object.values(ColumnType).forEach((type) => {
|
|
1589
|
-
AlterTableQueryBuilder.prototype[type] = function (name, ...args) {
|
|
1590
|
-
const _builder = new AlterColumnQueryBuilder(this.Container, name, type, ...args);
|
|
1591
|
-
this._columns.push(_builder);
|
|
1592
|
-
return _builder;
|
|
1593
|
-
};
|
|
1594
|
-
});
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
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;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
var RawQuery_1, WhereBuilder_1, SelectQueryBuilder_1;
|
|
11
|
+
/* eslint-disable prettier/prettier */
|
|
12
|
+
import { Container, Inject, NewInstance, DI, Injectable } from '@spinajs/di';
|
|
13
|
+
import { InvalidArgument, MethodNotImplemented, InvalidOperation } from '@spinajs/exceptions';
|
|
14
|
+
import { OrmException } from './exceptions.js';
|
|
15
|
+
import _ from 'lodash';
|
|
16
|
+
import { use } from 'typescript-mix';
|
|
17
|
+
import { ColumnMethods, ColumnType, QueryMethod, SortOrder, WhereBoolean, SqlOperator, JoinMethod } from './enums.js';
|
|
18
|
+
import { DeleteQueryCompiler, InsertQueryCompiler, SelectQueryCompiler, TruncateTableQueryCompiler, TableQueryCompiler, AlterTableQueryCompiler, UpdateQueryCompiler, QueryContext, IndexQueryCompiler, RelationType, ReferentialAction, DefaultValueBuilder, ColumnAlterationType, TableExistsCompiler, DropTableCompiler, TableCloneQueryCompiler, QueryMiddleware, DropEventQueryCompiler, EventQueryCompiler } from './interfaces.js';
|
|
19
|
+
import { BetweenStatement, ColumnMethodStatement, ColumnStatement, ExistsQueryStatement, InSetStatement, InStatement, RawQueryStatement, WhereQueryStatement, WhereStatement, ColumnRawStatement, JoinStatement, WithRecursiveStatement, GroupByStatement, Wrap } from './statements.js';
|
|
20
|
+
import { OrmDriver } from './driver.js';
|
|
21
|
+
import { extractModelDescriptor } from './model.js';
|
|
22
|
+
import { BelongsToRelation, OneToManyRelation, ManyToManyRelation, BelongsToRecursiveRelation } from './relations.js';
|
|
23
|
+
function isWhereOperator(val) {
|
|
24
|
+
return _.isString(val) && Object.values(SqlOperator).includes(val.toLowerCase());
|
|
25
|
+
}
|
|
26
|
+
let Builder = class Builder {
|
|
27
|
+
get Driver() {
|
|
28
|
+
return this._driver;
|
|
29
|
+
}
|
|
30
|
+
get Container() {
|
|
31
|
+
return this._container;
|
|
32
|
+
}
|
|
33
|
+
get Model() {
|
|
34
|
+
return this._model;
|
|
35
|
+
}
|
|
36
|
+
constructor(container, driver, model) {
|
|
37
|
+
this._middlewares = [];
|
|
38
|
+
this._queryMiddlewares = [];
|
|
39
|
+
this._driver = driver;
|
|
40
|
+
this._container = container;
|
|
41
|
+
this._model = model;
|
|
42
|
+
this._nonSelect = true;
|
|
43
|
+
this._asRaw = false;
|
|
44
|
+
this._queryMiddlewares = DI.resolve(Array.ofType(QueryMiddleware));
|
|
45
|
+
}
|
|
46
|
+
then(onfulfilled, onrejected) {
|
|
47
|
+
return this._driver
|
|
48
|
+
.execute(this)
|
|
49
|
+
.then((result) => {
|
|
50
|
+
try {
|
|
51
|
+
if (this._asRaw) {
|
|
52
|
+
onfulfilled(result);
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
let transformedResult = result;
|
|
56
|
+
// if we have something to transform ...
|
|
57
|
+
if (transformedResult) {
|
|
58
|
+
this._middlewares.forEach((m) => {
|
|
59
|
+
Object.assign(transformedResult, m.afterQuery(transformedResult));
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
if (this._model && !this._nonSelect) {
|
|
63
|
+
// TODO: rething this casting
|
|
64
|
+
const models = transformedResult.map((r) => {
|
|
65
|
+
let model = null;
|
|
66
|
+
for (const middleware of this._middlewares.reverse()) {
|
|
67
|
+
model = middleware.modelCreation(r);
|
|
68
|
+
if (model !== null) {
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (model === null) {
|
|
73
|
+
model = new this._model();
|
|
74
|
+
model.hydrate(r);
|
|
75
|
+
}
|
|
76
|
+
return model;
|
|
77
|
+
});
|
|
78
|
+
const afterMiddlewarePromises = this._middlewares.reduce((prev, current) => {
|
|
79
|
+
return prev.concat([current.afterHydration(models)]);
|
|
80
|
+
}, []);
|
|
81
|
+
if (this._middlewares.length > 0) {
|
|
82
|
+
Promise.all(afterMiddlewarePromises).then(() => {
|
|
83
|
+
try {
|
|
84
|
+
onfulfilled(models);
|
|
85
|
+
}
|
|
86
|
+
catch (err) {
|
|
87
|
+
onrejected(err);
|
|
88
|
+
}
|
|
89
|
+
}, onrejected);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
onfulfilled(models);
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
else {
|
|
96
|
+
onfulfilled(transformedResult);
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
catch (err) {
|
|
100
|
+
onrejected(err);
|
|
101
|
+
}
|
|
102
|
+
})
|
|
103
|
+
.catch((err) => {
|
|
104
|
+
onrejected(err);
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
middleware(middleware) {
|
|
108
|
+
this._middlewares.push(middleware);
|
|
109
|
+
return this;
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Builds query that is ready to use in DB
|
|
113
|
+
*/
|
|
114
|
+
toDB() {
|
|
115
|
+
throw new MethodNotImplemented();
|
|
116
|
+
}
|
|
117
|
+
};
|
|
118
|
+
Builder = __decorate([
|
|
119
|
+
NewInstance(),
|
|
120
|
+
Inject(Container),
|
|
121
|
+
__metadata("design:paramtypes", [Object, OrmDriver, Object])
|
|
122
|
+
], Builder);
|
|
123
|
+
export { Builder };
|
|
124
|
+
/**
|
|
125
|
+
* Base class for queires. Implements basic query functionality
|
|
126
|
+
*
|
|
127
|
+
*/
|
|
128
|
+
let QueryBuilder = class QueryBuilder extends Builder {
|
|
129
|
+
constructor(container, driver, model) {
|
|
130
|
+
super(container, driver, model);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* SQL table name that query is executed on
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* SELECT * FROM `users`
|
|
137
|
+
*/
|
|
138
|
+
get Table() {
|
|
139
|
+
return this._table;
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* DB table alias
|
|
143
|
+
*/
|
|
144
|
+
get TableAlias() {
|
|
145
|
+
return this._tableAlias;
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* SQL schema/database name that query is executed on.
|
|
149
|
+
*
|
|
150
|
+
* @example
|
|
151
|
+
* SELECT * FROM `spinejs`.`users` as u
|
|
152
|
+
*/
|
|
153
|
+
get Database() {
|
|
154
|
+
return this._database;
|
|
155
|
+
}
|
|
156
|
+
/**
|
|
157
|
+
* Sets schema to this query.
|
|
158
|
+
*
|
|
159
|
+
* @param database - schema or database name in database
|
|
160
|
+
*/
|
|
161
|
+
database(database) {
|
|
162
|
+
if (!database) {
|
|
163
|
+
throw new InvalidArgument(`schema argument cannot be null or empty`);
|
|
164
|
+
}
|
|
165
|
+
this._database = database;
|
|
166
|
+
return this;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Sets table that query is executed on
|
|
170
|
+
*
|
|
171
|
+
* @param table - sql table name
|
|
172
|
+
* @param alias - sql table alias
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
*
|
|
176
|
+
* this.setTable("user","u")
|
|
177
|
+
*
|
|
178
|
+
*/
|
|
179
|
+
setTable(table, alias) {
|
|
180
|
+
if (!table.trim()) {
|
|
181
|
+
throw new InvalidArgument('table name is empty');
|
|
182
|
+
}
|
|
183
|
+
this._table = table;
|
|
184
|
+
this.setAlias(alias);
|
|
185
|
+
return this;
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Sets table alias for query
|
|
189
|
+
*
|
|
190
|
+
* @param alias - sql table alias
|
|
191
|
+
*/
|
|
192
|
+
setAlias(alias) {
|
|
193
|
+
this._tableAlias = alias;
|
|
194
|
+
return this;
|
|
195
|
+
}
|
|
196
|
+
from(table, alias) {
|
|
197
|
+
return this.setTable(table, alias);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
QueryBuilder = __decorate([
|
|
201
|
+
NewInstance(),
|
|
202
|
+
Inject(Container),
|
|
203
|
+
__metadata("design:paramtypes", [Object, OrmDriver, Object])
|
|
204
|
+
], QueryBuilder);
|
|
205
|
+
export { QueryBuilder };
|
|
206
|
+
let LimitBuilder = class LimitBuilder {
|
|
207
|
+
constructor() {
|
|
208
|
+
this._first = false;
|
|
209
|
+
this._limit = {
|
|
210
|
+
limit: -1,
|
|
211
|
+
offset: -1,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
take(count) {
|
|
215
|
+
if (count <= 0) {
|
|
216
|
+
throw new InvalidArgument(`take count cannot be negative number`);
|
|
217
|
+
}
|
|
218
|
+
this._limit.limit = count;
|
|
219
|
+
return this;
|
|
220
|
+
}
|
|
221
|
+
skip(count) {
|
|
222
|
+
if (count < 0) {
|
|
223
|
+
throw new InvalidArgument(`skip count cannot be negative number`);
|
|
224
|
+
}
|
|
225
|
+
this._limit.offset = count;
|
|
226
|
+
return this;
|
|
227
|
+
}
|
|
228
|
+
takeFirst() {
|
|
229
|
+
this._first = true;
|
|
230
|
+
this._limit.limit = 1;
|
|
231
|
+
return this;
|
|
232
|
+
}
|
|
233
|
+
async first() {
|
|
234
|
+
return (await this.takeFirst());
|
|
235
|
+
}
|
|
236
|
+
async firstOrFail() {
|
|
237
|
+
return this.firstOrThrow(new OrmException('not found'));
|
|
238
|
+
}
|
|
239
|
+
async orThrow(error) {
|
|
240
|
+
const result = (await this);
|
|
241
|
+
if (result === undefined || (Array.isArray(result) && result.length === 0)) {
|
|
242
|
+
throw error;
|
|
243
|
+
}
|
|
244
|
+
return result;
|
|
245
|
+
}
|
|
246
|
+
async firstOrThrow(error) {
|
|
247
|
+
const result = await this.first();
|
|
248
|
+
if (result === undefined) {
|
|
249
|
+
throw error;
|
|
250
|
+
}
|
|
251
|
+
return result;
|
|
252
|
+
}
|
|
253
|
+
getLimits() {
|
|
254
|
+
return this._limit;
|
|
255
|
+
}
|
|
256
|
+
};
|
|
257
|
+
LimitBuilder = __decorate([
|
|
258
|
+
NewInstance(),
|
|
259
|
+
__metadata("design:paramtypes", [])
|
|
260
|
+
], LimitBuilder);
|
|
261
|
+
export { LimitBuilder };
|
|
262
|
+
let OrderByBuilder = class OrderByBuilder {
|
|
263
|
+
constructor() {
|
|
264
|
+
this._sort = {
|
|
265
|
+
column: '',
|
|
266
|
+
order: SortOrder.ASC,
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
order(column, direction) {
|
|
270
|
+
if (!column) {
|
|
271
|
+
return this;
|
|
272
|
+
}
|
|
273
|
+
this._sort = {
|
|
274
|
+
column,
|
|
275
|
+
order: direction,
|
|
276
|
+
};
|
|
277
|
+
return this;
|
|
278
|
+
}
|
|
279
|
+
orderBy(column) {
|
|
280
|
+
this._sort = {
|
|
281
|
+
column,
|
|
282
|
+
order: SortOrder.ASC,
|
|
283
|
+
};
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
orderByDescending(column) {
|
|
287
|
+
this._sort = {
|
|
288
|
+
column,
|
|
289
|
+
order: SortOrder.DESC,
|
|
290
|
+
};
|
|
291
|
+
return this;
|
|
292
|
+
}
|
|
293
|
+
getSort() {
|
|
294
|
+
return this._sort.column.trim() !== '' ? this._sort : null;
|
|
295
|
+
}
|
|
296
|
+
};
|
|
297
|
+
OrderByBuilder = __decorate([
|
|
298
|
+
NewInstance(),
|
|
299
|
+
__metadata("design:paramtypes", [])
|
|
300
|
+
], OrderByBuilder);
|
|
301
|
+
export { OrderByBuilder };
|
|
302
|
+
let ColumnsBuilder = class ColumnsBuilder {
|
|
303
|
+
constructor() {
|
|
304
|
+
this._columns = [];
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* Clears all select clauses from the query.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
*
|
|
311
|
+
* query.columns()
|
|
312
|
+
*
|
|
313
|
+
*/
|
|
314
|
+
clearColumns() {
|
|
315
|
+
this._columns = [];
|
|
316
|
+
return this;
|
|
317
|
+
}
|
|
318
|
+
columns(names) {
|
|
319
|
+
const descriptor = extractModelDescriptor(this._model);
|
|
320
|
+
this._columns = names.map((n) => {
|
|
321
|
+
return this._container.resolve(ColumnStatement, [n, null, this._tableAlias, descriptor?.Columns.find((c) => c.Name === n)]);
|
|
322
|
+
});
|
|
323
|
+
return this;
|
|
324
|
+
}
|
|
325
|
+
select(column, alias) {
|
|
326
|
+
const descriptor = extractModelDescriptor(this._model);
|
|
327
|
+
if (column instanceof Map) {
|
|
328
|
+
column.forEach((alias, colName) => {
|
|
329
|
+
this._columns.push(this._container.resolve(ColumnStatement, [colName, alias, this._tableAlias, descriptor?.Columns.find((c) => c.Name === colName)]));
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
if (column instanceof RawQuery) {
|
|
333
|
+
this._columns.push(this._container.resolve(ColumnRawStatement, [column, null, this._tableAlias]));
|
|
334
|
+
}
|
|
335
|
+
else {
|
|
336
|
+
this._columns.push(this._container.resolve(ColumnStatement, [column, alias, this._tableAlias, descriptor?.Columns.find((c) => c.Name === column)]));
|
|
337
|
+
}
|
|
338
|
+
return this;
|
|
339
|
+
}
|
|
340
|
+
getColumns() {
|
|
341
|
+
return this._columns;
|
|
342
|
+
}
|
|
343
|
+
};
|
|
344
|
+
ColumnsBuilder = __decorate([
|
|
345
|
+
NewInstance(),
|
|
346
|
+
__metadata("design:paramtypes", [])
|
|
347
|
+
], ColumnsBuilder);
|
|
348
|
+
export { ColumnsBuilder };
|
|
349
|
+
let RawQuery = RawQuery_1 = class RawQuery {
|
|
350
|
+
get Query() {
|
|
351
|
+
return this._query;
|
|
352
|
+
}
|
|
353
|
+
get Bindings() {
|
|
354
|
+
return this._bindings;
|
|
355
|
+
}
|
|
356
|
+
static create(query, bindings) {
|
|
357
|
+
return new RawQuery_1(query, bindings);
|
|
358
|
+
}
|
|
359
|
+
constructor(query, bindings) {
|
|
360
|
+
this._query = '';
|
|
361
|
+
this._bindings = [];
|
|
362
|
+
this._query = query;
|
|
363
|
+
this._bindings = bindings;
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
RawQuery = RawQuery_1 = __decorate([
|
|
367
|
+
NewInstance(),
|
|
368
|
+
__metadata("design:paramtypes", [String, Array])
|
|
369
|
+
], RawQuery);
|
|
370
|
+
export { RawQuery };
|
|
371
|
+
let GroupByBuilder = class GroupByBuilder {
|
|
372
|
+
constructor() {
|
|
373
|
+
this._groupStatements = [];
|
|
374
|
+
}
|
|
375
|
+
get GroupStatements() {
|
|
376
|
+
return this._groupStatements;
|
|
377
|
+
}
|
|
378
|
+
clearGroupBy() {
|
|
379
|
+
this._groupStatements = [];
|
|
380
|
+
return this;
|
|
381
|
+
}
|
|
382
|
+
groupBy(expression) {
|
|
383
|
+
this._groupStatements.push(this._container.resolve(GroupByStatement, [expression]));
|
|
384
|
+
return this;
|
|
385
|
+
}
|
|
386
|
+
};
|
|
387
|
+
GroupByBuilder = __decorate([
|
|
388
|
+
NewInstance()
|
|
389
|
+
], GroupByBuilder);
|
|
390
|
+
export { GroupByBuilder };
|
|
391
|
+
let JoinBuilder = class JoinBuilder {
|
|
392
|
+
get JoinStatements() {
|
|
393
|
+
return this._joinStatements;
|
|
394
|
+
}
|
|
395
|
+
constructor(container) {
|
|
396
|
+
this._joinStatements = [];
|
|
397
|
+
this._container = container;
|
|
398
|
+
this._joinStatements = [];
|
|
399
|
+
}
|
|
400
|
+
clearJoins() {
|
|
401
|
+
this._joinStatements = [];
|
|
402
|
+
return this;
|
|
403
|
+
}
|
|
404
|
+
innerJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey) {
|
|
405
|
+
this.addJoinStatement.call(this, JoinMethod.INNER, ...arguments);
|
|
406
|
+
return this;
|
|
407
|
+
}
|
|
408
|
+
leftJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
409
|
+
this.addJoinStatement.call(this, JoinMethod.LEFT, ...arguments);
|
|
410
|
+
return this;
|
|
411
|
+
}
|
|
412
|
+
leftOuterJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
413
|
+
this.addJoinStatement.call(this, JoinMethod.LEFT_OUTER, ...arguments);
|
|
414
|
+
return this;
|
|
415
|
+
}
|
|
416
|
+
rightJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
417
|
+
this.addJoinStatement.call(this, JoinMethod.RIGHT, ...arguments);
|
|
418
|
+
return this;
|
|
419
|
+
}
|
|
420
|
+
rightOuterJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
421
|
+
this.addJoinStatement.call(this, JoinMethod.RIGHT_OUTER, ...arguments);
|
|
422
|
+
return this;
|
|
423
|
+
}
|
|
424
|
+
fullOuterJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
425
|
+
this.addJoinStatement.call(this, JoinMethod.FULL_OUTER, ...arguments);
|
|
426
|
+
return this;
|
|
427
|
+
}
|
|
428
|
+
crossJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey, _database) {
|
|
429
|
+
this.addJoinStatement.call(this, JoinMethod.CROSS, ...arguments);
|
|
430
|
+
return this;
|
|
431
|
+
}
|
|
432
|
+
addJoinStatement(method, table, AliasOrForeignKey, fkOrPkKey, primaryKey, _database) {
|
|
433
|
+
let stmt = null;
|
|
434
|
+
if (arguments.length === 3) {
|
|
435
|
+
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method, AliasOrForeignKey]);
|
|
436
|
+
}
|
|
437
|
+
else if (arguments.length === 4) {
|
|
438
|
+
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method, AliasOrForeignKey, fkOrPkKey, null, this._tableAlias]);
|
|
439
|
+
}
|
|
440
|
+
else if (arguments.length === 5) {
|
|
441
|
+
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method, fkOrPkKey, primaryKey, AliasOrForeignKey, this._tableAlias]);
|
|
442
|
+
}
|
|
443
|
+
else if (arguments.length === 6) {
|
|
444
|
+
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method, fkOrPkKey, primaryKey, AliasOrForeignKey, this._tableAlias, _database]);
|
|
445
|
+
}
|
|
446
|
+
else {
|
|
447
|
+
stmt = this._container.resolve(JoinStatement, [this, this._model, table, method]);
|
|
448
|
+
}
|
|
449
|
+
this.JoinStatements.push(stmt);
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
JoinBuilder = __decorate([
|
|
453
|
+
NewInstance(),
|
|
454
|
+
__metadata("design:paramtypes", [Container])
|
|
455
|
+
], JoinBuilder);
|
|
456
|
+
export { JoinBuilder };
|
|
457
|
+
let WithRecursiveBuilder = class WithRecursiveBuilder {
|
|
458
|
+
get CteRecursive() {
|
|
459
|
+
return this._cteStatement;
|
|
460
|
+
}
|
|
461
|
+
withRecursive(rcKeyName, pkName) {
|
|
462
|
+
this._cteStatement = this._container.resolve(WithRecursiveStatement, ['cte', this, rcKeyName, pkName]);
|
|
463
|
+
return this;
|
|
464
|
+
}
|
|
465
|
+
};
|
|
466
|
+
WithRecursiveBuilder = __decorate([
|
|
467
|
+
NewInstance()
|
|
468
|
+
], WithRecursiveBuilder);
|
|
469
|
+
export { WithRecursiveBuilder };
|
|
470
|
+
let WhereBuilder = WhereBuilder_1 = class WhereBuilder {
|
|
471
|
+
get Statements() {
|
|
472
|
+
return this._statements;
|
|
473
|
+
}
|
|
474
|
+
get Op() {
|
|
475
|
+
return this._boolean;
|
|
476
|
+
}
|
|
477
|
+
constructor(container, tableAlias) {
|
|
478
|
+
this._statements = [];
|
|
479
|
+
this._boolean = WhereBoolean.AND;
|
|
480
|
+
this._container = container;
|
|
481
|
+
this._boolean = WhereBoolean.AND;
|
|
482
|
+
this._statements = [];
|
|
483
|
+
this._tableAlias = tableAlias;
|
|
484
|
+
}
|
|
485
|
+
when(condition, callback, callbackElse) {
|
|
486
|
+
if (condition) {
|
|
487
|
+
if (callback)
|
|
488
|
+
callback.call(this);
|
|
489
|
+
}
|
|
490
|
+
else if (callbackElse) {
|
|
491
|
+
callbackElse.call(this);
|
|
492
|
+
}
|
|
493
|
+
return this;
|
|
494
|
+
}
|
|
495
|
+
where(column, operator, value) {
|
|
496
|
+
const self = this;
|
|
497
|
+
if (column === null || (undefined && arguments.length === 1)) {
|
|
498
|
+
return;
|
|
499
|
+
}
|
|
500
|
+
// Support "where true || where false"
|
|
501
|
+
if (_.isBoolean(column)) {
|
|
502
|
+
return this.where(RawQuery.create(column ? 'TRUE' : 'FALSE'));
|
|
503
|
+
}
|
|
504
|
+
if (column instanceof RawQuery) {
|
|
505
|
+
this.Statements.push(this._container.resolve(RawQueryStatement, [column.Query, column.Bindings, self._tableAlias]));
|
|
506
|
+
return this;
|
|
507
|
+
}
|
|
508
|
+
// handle nested where's
|
|
509
|
+
if (_.isFunction(column)) {
|
|
510
|
+
const builder = new WhereBuilder_1(this._container, this._tableAlias);
|
|
511
|
+
column.call(builder);
|
|
512
|
+
self.Statements.push(this._container.resolve(WhereQueryStatement, [builder, self._tableAlias]));
|
|
513
|
+
return this;
|
|
514
|
+
}
|
|
515
|
+
// handle simple key = object[key] AND ....
|
|
516
|
+
if (_.isObject(column) && !(column instanceof Wrap)) {
|
|
517
|
+
return this.whereObject(column);
|
|
518
|
+
}
|
|
519
|
+
if (typeof value === 'undefined') {
|
|
520
|
+
return _handleForTwo.call(this, column, operator);
|
|
521
|
+
}
|
|
522
|
+
return _handleForThree.call(this, column, operator, value);
|
|
523
|
+
/**
|
|
524
|
+
* handles for where("foo", 1).where(...) cases
|
|
525
|
+
* it produces WHERE foo = 1
|
|
526
|
+
*/
|
|
527
|
+
function _handleForTwo(c, v) {
|
|
528
|
+
let sVal = v;
|
|
529
|
+
if (sVal === undefined) {
|
|
530
|
+
throw new InvalidArgument(`value cannot be undefined`);
|
|
531
|
+
}
|
|
532
|
+
if (Number.isNaN(sVal)) {
|
|
533
|
+
throw new InvalidArgument(`value cannot be NaN`);
|
|
534
|
+
}
|
|
535
|
+
if (!_.isString(c) && !(c instanceof Wrap)) {
|
|
536
|
+
throw new InvalidArgument(`column is not of type string or wrapped.`);
|
|
537
|
+
}
|
|
538
|
+
if (sVal === null) {
|
|
539
|
+
return this.whereNull(c);
|
|
540
|
+
}
|
|
541
|
+
self._statements.push(self._container.resolve(WhereStatement, [c, SqlOperator.EQ, sVal, self._tableAlias, this._container, self._model]));
|
|
542
|
+
return self;
|
|
543
|
+
}
|
|
544
|
+
/**
|
|
545
|
+
* Handles for where("foo",'!=',1) etc
|
|
546
|
+
* it produces WHERE foo != 1
|
|
547
|
+
*/
|
|
548
|
+
function _handleForThree(c, o, v) {
|
|
549
|
+
let sVal = v;
|
|
550
|
+
if (!isWhereOperator(o)) {
|
|
551
|
+
throw new InvalidArgument(`operator ${o} is invalid`);
|
|
552
|
+
}
|
|
553
|
+
if (!_.isString(c) && !(c instanceof Wrap)) {
|
|
554
|
+
throw new InvalidArgument(`column is not of type string or wrapped.`);
|
|
555
|
+
}
|
|
556
|
+
if (Number.isNaN(sVal)) {
|
|
557
|
+
throw new InvalidArgument(`value cannot be NaN`);
|
|
558
|
+
}
|
|
559
|
+
if (sVal === null) {
|
|
560
|
+
return this.whereNull(c);
|
|
561
|
+
}
|
|
562
|
+
if (sVal === null) {
|
|
563
|
+
return o === SqlOperator.NOT_NULL ? this.whereNotNull(c) : this.whereNull(c);
|
|
564
|
+
}
|
|
565
|
+
self._statements.push(self._container.resolve(WhereStatement, [c, o, sVal, self._tableAlias, this._container, self._model]));
|
|
566
|
+
return this;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
orWhere(column, ..._args) {
|
|
570
|
+
this._boolean = WhereBoolean.OR;
|
|
571
|
+
return this.where(column, ...Array.from(arguments).slice(1));
|
|
572
|
+
}
|
|
573
|
+
andWhere(column, ..._args) {
|
|
574
|
+
this._boolean = WhereBoolean.AND;
|
|
575
|
+
return this.where(column, ...Array.from(arguments).slice(1));
|
|
576
|
+
}
|
|
577
|
+
whereObject(obj) {
|
|
578
|
+
this._boolean = WhereBoolean.AND;
|
|
579
|
+
for (const key of Object.keys(obj).filter((x) => obj[x] !== undefined)) {
|
|
580
|
+
const val = obj[key];
|
|
581
|
+
if (Array.isArray(val)) {
|
|
582
|
+
if (val.length !== 0) {
|
|
583
|
+
this.whereIn(key, val);
|
|
584
|
+
}
|
|
585
|
+
}
|
|
586
|
+
else if (val === null) {
|
|
587
|
+
this.whereNull(key);
|
|
588
|
+
}
|
|
589
|
+
else
|
|
590
|
+
this.andWhere(key, SqlOperator.EQ, val);
|
|
591
|
+
}
|
|
592
|
+
return this;
|
|
593
|
+
}
|
|
594
|
+
whereNotNull(column) {
|
|
595
|
+
this._statements.push(this._container.resolve(WhereStatement, [column, SqlOperator.NOT_NULL, null, this._tableAlias, this._container]));
|
|
596
|
+
return this;
|
|
597
|
+
}
|
|
598
|
+
whereNull(column) {
|
|
599
|
+
this._statements.push(this._container.resolve(WhereStatement, [column, SqlOperator.NULL, null, this._tableAlias, this._container]));
|
|
600
|
+
return this;
|
|
601
|
+
}
|
|
602
|
+
whereNot(column, val) {
|
|
603
|
+
return this.where(column, SqlOperator.NOT, val);
|
|
604
|
+
}
|
|
605
|
+
whereIn(column, val) {
|
|
606
|
+
this._statements.push(this._container.resolve(InStatement, [column, val, false, this._tableAlias, this._container]));
|
|
607
|
+
return this;
|
|
608
|
+
}
|
|
609
|
+
whereNotIn(column, val) {
|
|
610
|
+
this._statements.push(this._container.resolve(InStatement, [column, val, true, this._tableAlias, this._container]));
|
|
611
|
+
return this;
|
|
612
|
+
}
|
|
613
|
+
whereExist(query) {
|
|
614
|
+
this._statements.push(this._container.resolve(ExistsQueryStatement, [query, false]));
|
|
615
|
+
return this;
|
|
616
|
+
}
|
|
617
|
+
whereNotExists(query) {
|
|
618
|
+
this._statements.push(this._container.resolve(ExistsQueryStatement, [query, true]));
|
|
619
|
+
return this;
|
|
620
|
+
}
|
|
621
|
+
whereBetween(column, val) {
|
|
622
|
+
this._statements.push(this._container.resolve(BetweenStatement, [column, val, false, this._tableAlias]));
|
|
623
|
+
return this;
|
|
624
|
+
}
|
|
625
|
+
whereNotBetween(column, val) {
|
|
626
|
+
this._statements.push(this._container.resolve(BetweenStatement, [column, val, true, this._tableAlias]));
|
|
627
|
+
return this;
|
|
628
|
+
}
|
|
629
|
+
whereInSet(column, val) {
|
|
630
|
+
this._statements.push(this._container.resolve(InSetStatement, [column, val, false, this._tableAlias]));
|
|
631
|
+
return this;
|
|
632
|
+
}
|
|
633
|
+
whereNotInSet(column, val) {
|
|
634
|
+
this._statements.push(this._container.resolve(InSetStatement, [column, val, true, this._tableAlias]));
|
|
635
|
+
return this;
|
|
636
|
+
}
|
|
637
|
+
clearWhere() {
|
|
638
|
+
this._statements = [];
|
|
639
|
+
return this;
|
|
640
|
+
}
|
|
641
|
+
};
|
|
642
|
+
WhereBuilder = WhereBuilder_1 = __decorate([
|
|
643
|
+
NewInstance(),
|
|
644
|
+
__metadata("design:paramtypes", [Container, String])
|
|
645
|
+
], WhereBuilder);
|
|
646
|
+
export { WhereBuilder };
|
|
647
|
+
let SelectQueryBuilder = SelectQueryBuilder_1 = class SelectQueryBuilder extends QueryBuilder {
|
|
648
|
+
get Owner() {
|
|
649
|
+
return this._owner;
|
|
650
|
+
}
|
|
651
|
+
get IsDistinct() {
|
|
652
|
+
return this._distinct;
|
|
653
|
+
}
|
|
654
|
+
get Relations() {
|
|
655
|
+
return this._relations;
|
|
656
|
+
}
|
|
657
|
+
constructor(container, driver, model, owner) {
|
|
658
|
+
super(container, driver, model);
|
|
659
|
+
this._columns = [];
|
|
660
|
+
/**
|
|
661
|
+
* where query props
|
|
662
|
+
*/
|
|
663
|
+
this._statements = [];
|
|
664
|
+
this._joinStatements = [];
|
|
665
|
+
this._groupStatements = [];
|
|
666
|
+
this._relations = [];
|
|
667
|
+
this._owner = owner;
|
|
668
|
+
this._distinct = false;
|
|
669
|
+
this._method = QueryMethod.SELECT;
|
|
670
|
+
this._boolean = WhereBoolean.AND;
|
|
671
|
+
this._sort = {
|
|
672
|
+
column: '',
|
|
673
|
+
order: SortOrder.ASC,
|
|
674
|
+
};
|
|
675
|
+
this._first = false;
|
|
676
|
+
this._limit = {
|
|
677
|
+
limit: -1,
|
|
678
|
+
offset: -1,
|
|
679
|
+
};
|
|
680
|
+
this._nonSelect = false;
|
|
681
|
+
this.QueryContext = QueryContext.Select;
|
|
682
|
+
this._queryMiddlewares.forEach((x) => x.afterQueryCreation(this));
|
|
683
|
+
}
|
|
684
|
+
async asRaw() {
|
|
685
|
+
this._asRaw = true;
|
|
686
|
+
return (await this);
|
|
687
|
+
}
|
|
688
|
+
setAlias(alias) {
|
|
689
|
+
this._tableAlias = alias;
|
|
690
|
+
this._columns.forEach((c) => (c.TableAlias = alias));
|
|
691
|
+
this._joinStatements.forEach((c) => (c.TableAlias = alias));
|
|
692
|
+
this._statements.forEach((c) => (c.TableAlias = alias));
|
|
693
|
+
return this;
|
|
694
|
+
}
|
|
695
|
+
clone() {
|
|
696
|
+
const builder = new SelectQueryBuilder_1(this._container, this._driver, this._model, this._owner);
|
|
697
|
+
builder._columns = this._columns.slice(0);
|
|
698
|
+
builder._joinStatements = this._joinStatements.slice(0);
|
|
699
|
+
builder._statements = this._statements.slice(0);
|
|
700
|
+
builder._limit = { ...this._limit };
|
|
701
|
+
builder._sort = { ...this._sort };
|
|
702
|
+
builder._boolean = this._boolean;
|
|
703
|
+
builder._distinct = this._distinct;
|
|
704
|
+
builder._table = this._table;
|
|
705
|
+
builder._tableAlias = this._tableAlias;
|
|
706
|
+
builder._queryMiddlewares = [...this._queryMiddlewares];
|
|
707
|
+
return builder;
|
|
708
|
+
}
|
|
709
|
+
populate(relation, callback) {
|
|
710
|
+
if (!relation) {
|
|
711
|
+
return this;
|
|
712
|
+
}
|
|
713
|
+
if (typeof relation === 'object') {
|
|
714
|
+
for (const i in relation) {
|
|
715
|
+
this.populate(i, () => {
|
|
716
|
+
if (relation)
|
|
717
|
+
this.populate(relation[i]);
|
|
718
|
+
});
|
|
719
|
+
}
|
|
720
|
+
return this;
|
|
721
|
+
}
|
|
722
|
+
// if relation was already populated, just call callback on it
|
|
723
|
+
const fRelation = this._relations.find((r) => r.Name === relation);
|
|
724
|
+
if (fRelation) {
|
|
725
|
+
fRelation.executeOnQuery(callback);
|
|
726
|
+
return this;
|
|
727
|
+
}
|
|
728
|
+
let relInstance = null;
|
|
729
|
+
const descriptor = extractModelDescriptor(this._model);
|
|
730
|
+
let rDescriptor = null;
|
|
731
|
+
for (const [key, value] of descriptor.Relations) {
|
|
732
|
+
if (key.toLowerCase() === relation.toLowerCase().trim()) {
|
|
733
|
+
rDescriptor = value;
|
|
734
|
+
break;
|
|
735
|
+
}
|
|
736
|
+
}
|
|
737
|
+
if (!rDescriptor) {
|
|
738
|
+
throw new InvalidArgument(`Relation ${relation} not exists in model ${this._model?.constructor.name}`);
|
|
739
|
+
}
|
|
740
|
+
if (rDescriptor.Recursive) {
|
|
741
|
+
relInstance = this._container.resolve(BelongsToRecursiveRelation, [this, rDescriptor, this._owner]);
|
|
742
|
+
}
|
|
743
|
+
else {
|
|
744
|
+
switch (rDescriptor.Type) {
|
|
745
|
+
case RelationType.One:
|
|
746
|
+
// if parent relation is one to many we dont set parent relation
|
|
747
|
+
// couse its new query to not mess with column aliases and hydrator
|
|
748
|
+
relInstance = this._container.resolve(BelongsToRelation, [this, rDescriptor, this._owner instanceof OneToManyRelation ? null : this._owner]);
|
|
749
|
+
break;
|
|
750
|
+
case RelationType.Many:
|
|
751
|
+
relInstance = this._container.resolve(OneToManyRelation, [this, rDescriptor, this._owner]);
|
|
752
|
+
break;
|
|
753
|
+
case RelationType.ManyToMany:
|
|
754
|
+
relInstance = this._container.resolve(ManyToManyRelation, [this, rDescriptor, null]);
|
|
755
|
+
break;
|
|
756
|
+
}
|
|
757
|
+
}
|
|
758
|
+
relInstance.execute(callback);
|
|
759
|
+
relInstance.Name = relation;
|
|
760
|
+
this._relations.push(relInstance);
|
|
761
|
+
return this;
|
|
762
|
+
}
|
|
763
|
+
mergeBuilder(builder) {
|
|
764
|
+
this._joinStatements = this._joinStatements.concat(builder._joinStatements);
|
|
765
|
+
this._columns = this._columns.concat(builder._columns);
|
|
766
|
+
this.mergeStatements(builder);
|
|
767
|
+
}
|
|
768
|
+
mergeRelations(builder) {
|
|
769
|
+
this._relations = this._relations.concat(builder._relations);
|
|
770
|
+
this._middlewares = this._middlewares.concat(builder._middlewares);
|
|
771
|
+
}
|
|
772
|
+
mergeStatements(builder, callback) {
|
|
773
|
+
const stms = callback ? builder._statements.filter(callback) : builder._statements;
|
|
774
|
+
this._statements = this._statements.concat(stms);
|
|
775
|
+
}
|
|
776
|
+
min(column, as) {
|
|
777
|
+
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.MIN, as, this._tableAlias]));
|
|
778
|
+
return this;
|
|
779
|
+
}
|
|
780
|
+
max(column, as) {
|
|
781
|
+
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.MAX, as, this._tableAlias]));
|
|
782
|
+
return this;
|
|
783
|
+
}
|
|
784
|
+
count(column, as) {
|
|
785
|
+
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.COUNT, as, this._tableAlias]));
|
|
786
|
+
return this;
|
|
787
|
+
}
|
|
788
|
+
sum(column, as) {
|
|
789
|
+
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.SUM, as, this._tableAlias]));
|
|
790
|
+
return this;
|
|
791
|
+
}
|
|
792
|
+
avg(column, as) {
|
|
793
|
+
this._columns.push(this._container.resolve(ColumnMethodStatement, [column, ColumnMethods.AVG, as, this._tableAlias]));
|
|
794
|
+
return this;
|
|
795
|
+
}
|
|
796
|
+
distinct() {
|
|
797
|
+
if (this._columns.length === 0 || this._columns[0].IsWildcard) {
|
|
798
|
+
throw new InvalidOperation('Cannot force DISTINCT on unknown column');
|
|
799
|
+
}
|
|
800
|
+
this._distinct = true;
|
|
801
|
+
return this;
|
|
802
|
+
}
|
|
803
|
+
toDB() {
|
|
804
|
+
const compiler = this._container.resolve(SelectQueryCompiler, [this]);
|
|
805
|
+
return compiler.compile();
|
|
806
|
+
}
|
|
807
|
+
async all() {
|
|
808
|
+
return (await this);
|
|
809
|
+
}
|
|
810
|
+
then(onfulfilled, onrejected) {
|
|
811
|
+
this._queryMiddlewares.forEach((x) => x.beforeQueryExecution(this));
|
|
812
|
+
return super.then((result) => {
|
|
813
|
+
if (this._first) {
|
|
814
|
+
if (Array.isArray(result)) {
|
|
815
|
+
if (result.length !== 0) {
|
|
816
|
+
return onfulfilled(result ? result[0] : null);
|
|
817
|
+
}
|
|
818
|
+
else {
|
|
819
|
+
try {
|
|
820
|
+
return onfulfilled(undefined);
|
|
821
|
+
}
|
|
822
|
+
catch (err) {
|
|
823
|
+
onrejected(err);
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
else {
|
|
828
|
+
return onfulfilled(result);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
else {
|
|
832
|
+
return onfulfilled(result);
|
|
833
|
+
}
|
|
834
|
+
}, onrejected);
|
|
835
|
+
}
|
|
836
|
+
async execute() {
|
|
837
|
+
return (await this);
|
|
838
|
+
}
|
|
839
|
+
};
|
|
840
|
+
__decorate([
|
|
841
|
+
use(WhereBuilder, LimitBuilder, OrderByBuilder, ColumnsBuilder, JoinBuilder, WithRecursiveBuilder, GroupByBuilder),
|
|
842
|
+
__metadata("design:type", Object)
|
|
843
|
+
], SelectQueryBuilder.prototype, "this", void 0);
|
|
844
|
+
SelectQueryBuilder = SelectQueryBuilder_1 = __decorate([
|
|
845
|
+
Injectable(SelectQueryBuilder),
|
|
846
|
+
__metadata("design:paramtypes", [Object, OrmDriver, Object, Object])
|
|
847
|
+
], SelectQueryBuilder);
|
|
848
|
+
export { SelectQueryBuilder };
|
|
849
|
+
export class SelectQueryBuilderC extends SelectQueryBuilder {
|
|
850
|
+
}
|
|
851
|
+
let DeleteQueryBuilder = class DeleteQueryBuilder extends QueryBuilder {
|
|
852
|
+
constructor(container, driver, model) {
|
|
853
|
+
super(container, driver, model);
|
|
854
|
+
this._method = QueryMethod.DELETE;
|
|
855
|
+
this._statements = [];
|
|
856
|
+
this._boolean = WhereBoolean.AND;
|
|
857
|
+
this._limit = {
|
|
858
|
+
limit: -1,
|
|
859
|
+
offset: -1,
|
|
860
|
+
};
|
|
861
|
+
this.QueryContext = QueryContext.Delete;
|
|
862
|
+
}
|
|
863
|
+
toDB() {
|
|
864
|
+
return this._container.resolve(DeleteQueryCompiler, [this]).compile();
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
__decorate([
|
|
868
|
+
use(WhereBuilder, LimitBuilder)
|
|
869
|
+
/// @ts-ignore
|
|
870
|
+
,
|
|
871
|
+
__metadata("design:type", Object)
|
|
872
|
+
], DeleteQueryBuilder.prototype, "this", void 0);
|
|
873
|
+
DeleteQueryBuilder = __decorate([
|
|
874
|
+
NewInstance(),
|
|
875
|
+
__metadata("design:paramtypes", [Container, OrmDriver, Object])
|
|
876
|
+
], DeleteQueryBuilder);
|
|
877
|
+
export { DeleteQueryBuilder };
|
|
878
|
+
let OnDuplicateQueryBuilder = class OnDuplicateQueryBuilder {
|
|
879
|
+
constructor(container, insertQueryBuilder, column) {
|
|
880
|
+
this._parent = insertQueryBuilder;
|
|
881
|
+
this._container = container;
|
|
882
|
+
this._column = _.isArray(column) ? column : [column];
|
|
883
|
+
}
|
|
884
|
+
getColumn() {
|
|
885
|
+
return this._column;
|
|
886
|
+
}
|
|
887
|
+
getColumnsToUpdate() {
|
|
888
|
+
return this._columnsToUpdate;
|
|
889
|
+
}
|
|
890
|
+
getParent() {
|
|
891
|
+
return this._parent;
|
|
892
|
+
}
|
|
893
|
+
update(columns) {
|
|
894
|
+
this._columnsToUpdate = columns;
|
|
895
|
+
return this;
|
|
896
|
+
}
|
|
897
|
+
then(onfulfilled, onrejected) {
|
|
898
|
+
return this._parent.then(onfulfilled, onrejected);
|
|
899
|
+
}
|
|
900
|
+
toDB() {
|
|
901
|
+
return this._parent.toDB();
|
|
902
|
+
}
|
|
903
|
+
};
|
|
904
|
+
OnDuplicateQueryBuilder = __decorate([
|
|
905
|
+
NewInstance(),
|
|
906
|
+
__metadata("design:paramtypes", [Object, QueryBuilder, Object])
|
|
907
|
+
], OnDuplicateQueryBuilder);
|
|
908
|
+
export { OnDuplicateQueryBuilder };
|
|
909
|
+
let UpdateQueryBuilder = class UpdateQueryBuilder extends QueryBuilder {
|
|
910
|
+
get Value() {
|
|
911
|
+
return this._value;
|
|
912
|
+
}
|
|
913
|
+
constructor(container, driver, model) {
|
|
914
|
+
super(container, driver, model);
|
|
915
|
+
this._value = {};
|
|
916
|
+
this._method = QueryMethod.UPDATE;
|
|
917
|
+
this._boolean = WhereBoolean.AND;
|
|
918
|
+
this._statements = [];
|
|
919
|
+
this.QueryContext = QueryContext.Update;
|
|
920
|
+
}
|
|
921
|
+
in(name) {
|
|
922
|
+
this.setTable(name);
|
|
923
|
+
return this;
|
|
924
|
+
}
|
|
925
|
+
update(value) {
|
|
926
|
+
this._value = value;
|
|
927
|
+
return this;
|
|
928
|
+
}
|
|
929
|
+
toDB() {
|
|
930
|
+
return this._container.resolve(UpdateQueryCompiler, [this]).compile();
|
|
931
|
+
}
|
|
932
|
+
};
|
|
933
|
+
__decorate([
|
|
934
|
+
use(WhereBuilder),
|
|
935
|
+
__metadata("design:type", Object)
|
|
936
|
+
], UpdateQueryBuilder.prototype, "this", void 0);
|
|
937
|
+
UpdateQueryBuilder = __decorate([
|
|
938
|
+
NewInstance(),
|
|
939
|
+
__metadata("design:paramtypes", [Container, OrmDriver, Object])
|
|
940
|
+
], UpdateQueryBuilder);
|
|
941
|
+
export { UpdateQueryBuilder };
|
|
942
|
+
let InsertQueryBuilder = class InsertQueryBuilder extends QueryBuilder {
|
|
943
|
+
get Values() {
|
|
944
|
+
return this._values;
|
|
945
|
+
}
|
|
946
|
+
get Ignore() {
|
|
947
|
+
return this._ignore;
|
|
948
|
+
}
|
|
949
|
+
get Update() {
|
|
950
|
+
return this._update;
|
|
951
|
+
}
|
|
952
|
+
get Replace() {
|
|
953
|
+
return this._replace;
|
|
954
|
+
}
|
|
955
|
+
constructor(container, driver, model) {
|
|
956
|
+
super(container, driver, model);
|
|
957
|
+
this._method = QueryMethod.INSERT;
|
|
958
|
+
this._columns = [];
|
|
959
|
+
this._values = [];
|
|
960
|
+
this.QueryContext = QueryContext.Insert;
|
|
961
|
+
}
|
|
962
|
+
/**
|
|
963
|
+
* Sets insert to ignore on duplicate
|
|
964
|
+
*/
|
|
965
|
+
orIgnore() {
|
|
966
|
+
this._ignore = true;
|
|
967
|
+
return this;
|
|
968
|
+
}
|
|
969
|
+
orReplace() {
|
|
970
|
+
this._update = true;
|
|
971
|
+
return this;
|
|
972
|
+
}
|
|
973
|
+
values(data) {
|
|
974
|
+
const self = this;
|
|
975
|
+
if (Array.isArray(data)) {
|
|
976
|
+
this.columns(_.chain(data).map(_.keys).flatten().uniq().value());
|
|
977
|
+
data.forEach((d) => {
|
|
978
|
+
_addData(d);
|
|
979
|
+
});
|
|
980
|
+
}
|
|
981
|
+
else {
|
|
982
|
+
this.columns(_.keysIn(data));
|
|
983
|
+
_addData(data);
|
|
984
|
+
}
|
|
985
|
+
function _addData(d) {
|
|
986
|
+
const binding = [];
|
|
987
|
+
self._columns
|
|
988
|
+
.filter((c) => !(c.Column instanceof RawQuery))
|
|
989
|
+
.map((c) => {
|
|
990
|
+
return c.Column;
|
|
991
|
+
})
|
|
992
|
+
.forEach((c) => {
|
|
993
|
+
binding.push(d[c]);
|
|
994
|
+
});
|
|
995
|
+
self._values.push(binding);
|
|
996
|
+
}
|
|
997
|
+
return this;
|
|
998
|
+
}
|
|
999
|
+
into(table, schema) {
|
|
1000
|
+
this.setTable(table, schema);
|
|
1001
|
+
return this;
|
|
1002
|
+
}
|
|
1003
|
+
onDuplicate(column) {
|
|
1004
|
+
let columnToCheck = column;
|
|
1005
|
+
if (!columnToCheck && this._model) {
|
|
1006
|
+
const dsc = extractModelDescriptor(this._model);
|
|
1007
|
+
columnToCheck = dsc.Columns.filter((c) => c.Unique).map((c) => c.Name);
|
|
1008
|
+
}
|
|
1009
|
+
this._update = true;
|
|
1010
|
+
this.DuplicateQueryBuilder = new OnDuplicateQueryBuilder(this._container, this, columnToCheck);
|
|
1011
|
+
return this.DuplicateQueryBuilder;
|
|
1012
|
+
}
|
|
1013
|
+
toDB() {
|
|
1014
|
+
return this._container.resolve(InsertQueryCompiler, [this]).compile();
|
|
1015
|
+
}
|
|
1016
|
+
};
|
|
1017
|
+
__decorate([
|
|
1018
|
+
use(ColumnsBuilder),
|
|
1019
|
+
__metadata("design:type", Object)
|
|
1020
|
+
], InsertQueryBuilder.prototype, "this", void 0);
|
|
1021
|
+
InsertQueryBuilder = __decorate([
|
|
1022
|
+
NewInstance(),
|
|
1023
|
+
__metadata("design:paramtypes", [Container, OrmDriver, Object])
|
|
1024
|
+
], InsertQueryBuilder);
|
|
1025
|
+
export { InsertQueryBuilder };
|
|
1026
|
+
let IndexQueryBuilder = class IndexQueryBuilder extends Builder {
|
|
1027
|
+
constructor(container, driver) {
|
|
1028
|
+
super(container, driver);
|
|
1029
|
+
this.QueryContext = QueryContext.Schema;
|
|
1030
|
+
}
|
|
1031
|
+
name(name) {
|
|
1032
|
+
this.Name = name;
|
|
1033
|
+
return this;
|
|
1034
|
+
}
|
|
1035
|
+
unique() {
|
|
1036
|
+
this.Unique = true;
|
|
1037
|
+
return this;
|
|
1038
|
+
}
|
|
1039
|
+
table(name) {
|
|
1040
|
+
this.Table = name;
|
|
1041
|
+
return this;
|
|
1042
|
+
}
|
|
1043
|
+
columns(colNames) {
|
|
1044
|
+
this.Columns = colNames;
|
|
1045
|
+
return this;
|
|
1046
|
+
}
|
|
1047
|
+
toDB() {
|
|
1048
|
+
return this._container.resolve(IndexQueryCompiler, [this]).compile();
|
|
1049
|
+
}
|
|
1050
|
+
};
|
|
1051
|
+
IndexQueryBuilder = __decorate([
|
|
1052
|
+
NewInstance(),
|
|
1053
|
+
Inject(Container),
|
|
1054
|
+
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1055
|
+
], IndexQueryBuilder);
|
|
1056
|
+
export { IndexQueryBuilder };
|
|
1057
|
+
let ForeignKeyBuilder = class ForeignKeyBuilder {
|
|
1058
|
+
constructor() {
|
|
1059
|
+
this.OnDeleteAction = ReferentialAction.NoAction;
|
|
1060
|
+
this.OnUpdateAction = ReferentialAction.NoAction;
|
|
1061
|
+
}
|
|
1062
|
+
/**
|
|
1063
|
+
*
|
|
1064
|
+
* Referenced field in child table
|
|
1065
|
+
*
|
|
1066
|
+
* @param fkName - name of foreign field in child table
|
|
1067
|
+
*/
|
|
1068
|
+
foreignKey(fkName) {
|
|
1069
|
+
this.ForeignKeyField = fkName;
|
|
1070
|
+
return this;
|
|
1071
|
+
}
|
|
1072
|
+
/**
|
|
1073
|
+
*
|
|
1074
|
+
* Referenced parent table & key
|
|
1075
|
+
*
|
|
1076
|
+
* @param table - parent table
|
|
1077
|
+
* @param pKey - parant table key field
|
|
1078
|
+
*/
|
|
1079
|
+
references(table, pKey) {
|
|
1080
|
+
this.Table = table;
|
|
1081
|
+
this.PrimaryKey = pKey;
|
|
1082
|
+
return this;
|
|
1083
|
+
}
|
|
1084
|
+
/**
|
|
1085
|
+
*
|
|
1086
|
+
* On delete action
|
|
1087
|
+
*
|
|
1088
|
+
* @param action - action to take on delete
|
|
1089
|
+
*/
|
|
1090
|
+
onDelete(action) {
|
|
1091
|
+
this.OnDeleteAction = action;
|
|
1092
|
+
return this;
|
|
1093
|
+
}
|
|
1094
|
+
/**
|
|
1095
|
+
*
|
|
1096
|
+
* On update action
|
|
1097
|
+
*
|
|
1098
|
+
* @param action - action to take on update
|
|
1099
|
+
*/
|
|
1100
|
+
onUpdate(action) {
|
|
1101
|
+
this.OnUpdateAction = action;
|
|
1102
|
+
return this;
|
|
1103
|
+
}
|
|
1104
|
+
/**
|
|
1105
|
+
* Shorhand for on update and on delete cascade settings
|
|
1106
|
+
*/
|
|
1107
|
+
cascade() {
|
|
1108
|
+
this.OnUpdateAction = ReferentialAction.Cascade;
|
|
1109
|
+
this.OnDeleteAction = ReferentialAction.Cascade;
|
|
1110
|
+
return this;
|
|
1111
|
+
}
|
|
1112
|
+
};
|
|
1113
|
+
ForeignKeyBuilder = __decorate([
|
|
1114
|
+
NewInstance(),
|
|
1115
|
+
__metadata("design:paramtypes", [])
|
|
1116
|
+
], ForeignKeyBuilder);
|
|
1117
|
+
export { ForeignKeyBuilder };
|
|
1118
|
+
let ColumnQueryBuilder = class ColumnQueryBuilder {
|
|
1119
|
+
constructor(container, name, type, ...args) {
|
|
1120
|
+
this.container = container;
|
|
1121
|
+
this.Name = name;
|
|
1122
|
+
this.Type = type;
|
|
1123
|
+
this.Charset = '';
|
|
1124
|
+
this.Args = [];
|
|
1125
|
+
this.AutoIncrement = false;
|
|
1126
|
+
this.NotNull = false;
|
|
1127
|
+
this.Collation = '';
|
|
1128
|
+
this.Comment = '';
|
|
1129
|
+
this.Unique = false;
|
|
1130
|
+
this.Unsigned = false;
|
|
1131
|
+
this.Args.push(...args);
|
|
1132
|
+
}
|
|
1133
|
+
notNull() {
|
|
1134
|
+
this.NotNull = true;
|
|
1135
|
+
return this;
|
|
1136
|
+
}
|
|
1137
|
+
unique() {
|
|
1138
|
+
this.Unique = true;
|
|
1139
|
+
return this;
|
|
1140
|
+
}
|
|
1141
|
+
unsigned() {
|
|
1142
|
+
this.Unsigned = true;
|
|
1143
|
+
return this;
|
|
1144
|
+
}
|
|
1145
|
+
autoIncrement() {
|
|
1146
|
+
this.AutoIncrement = true;
|
|
1147
|
+
return this;
|
|
1148
|
+
}
|
|
1149
|
+
default() {
|
|
1150
|
+
this.Default = this.container.resolve((DefaultValueBuilder), [this]);
|
|
1151
|
+
return this.Default;
|
|
1152
|
+
}
|
|
1153
|
+
primaryKey() {
|
|
1154
|
+
this.PrimaryKey = true;
|
|
1155
|
+
return this;
|
|
1156
|
+
}
|
|
1157
|
+
comment(comment) {
|
|
1158
|
+
this.Comment = comment;
|
|
1159
|
+
return this;
|
|
1160
|
+
}
|
|
1161
|
+
charset(charset) {
|
|
1162
|
+
this.Charset = charset;
|
|
1163
|
+
return this;
|
|
1164
|
+
}
|
|
1165
|
+
collation(collation) {
|
|
1166
|
+
this.Collation = collation;
|
|
1167
|
+
return this;
|
|
1168
|
+
}
|
|
1169
|
+
};
|
|
1170
|
+
ColumnQueryBuilder = __decorate([
|
|
1171
|
+
NewInstance(),
|
|
1172
|
+
Inject(Container),
|
|
1173
|
+
__metadata("design:paramtypes", [Object, String, String, Object])
|
|
1174
|
+
], ColumnQueryBuilder);
|
|
1175
|
+
export { ColumnQueryBuilder };
|
|
1176
|
+
let AlterColumnQueryBuilder = class AlterColumnQueryBuilder extends ColumnQueryBuilder {
|
|
1177
|
+
constructor(container, name, type, ...args) {
|
|
1178
|
+
super(container, name, type, ...args);
|
|
1179
|
+
this.OldName = name;
|
|
1180
|
+
// we assume add by default
|
|
1181
|
+
this.AlterType = ColumnAlterationType.Add;
|
|
1182
|
+
}
|
|
1183
|
+
addColumn() {
|
|
1184
|
+
this.AlterType = ColumnAlterationType.Add;
|
|
1185
|
+
return this;
|
|
1186
|
+
}
|
|
1187
|
+
modify() {
|
|
1188
|
+
this.AlterType = ColumnAlterationType.Modify;
|
|
1189
|
+
return this;
|
|
1190
|
+
}
|
|
1191
|
+
rename(newName) {
|
|
1192
|
+
this.AlterType = ColumnAlterationType.Rename;
|
|
1193
|
+
this.Name = newName;
|
|
1194
|
+
return this;
|
|
1195
|
+
}
|
|
1196
|
+
after(columnName) {
|
|
1197
|
+
this.AfterColumn = columnName;
|
|
1198
|
+
return this;
|
|
1199
|
+
}
|
|
1200
|
+
};
|
|
1201
|
+
AlterColumnQueryBuilder = __decorate([
|
|
1202
|
+
Inject(Container),
|
|
1203
|
+
NewInstance(),
|
|
1204
|
+
__metadata("design:paramtypes", [Object, String, String, Object])
|
|
1205
|
+
], AlterColumnQueryBuilder);
|
|
1206
|
+
export { AlterColumnQueryBuilder };
|
|
1207
|
+
let TableExistsQueryBuilder = class TableExistsQueryBuilder extends QueryBuilder {
|
|
1208
|
+
constructor(container, driver, name) {
|
|
1209
|
+
super(container, driver, null);
|
|
1210
|
+
this.setTable(name);
|
|
1211
|
+
this.QueryContext = QueryContext.Select;
|
|
1212
|
+
}
|
|
1213
|
+
toDB() {
|
|
1214
|
+
return this._container.resolve(TableExistsCompiler, [this]).compile();
|
|
1215
|
+
}
|
|
1216
|
+
};
|
|
1217
|
+
TableExistsQueryBuilder = __decorate([
|
|
1218
|
+
NewInstance(),
|
|
1219
|
+
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1220
|
+
], TableExistsQueryBuilder);
|
|
1221
|
+
export { TableExistsQueryBuilder };
|
|
1222
|
+
let DropTableQueryBuilder = class DropTableQueryBuilder extends QueryBuilder {
|
|
1223
|
+
constructor(container, driver, name, database) {
|
|
1224
|
+
super(container, driver, null);
|
|
1225
|
+
this.setTable(name);
|
|
1226
|
+
if (database) {
|
|
1227
|
+
this.database(database);
|
|
1228
|
+
}
|
|
1229
|
+
this.Exists = false;
|
|
1230
|
+
this.QueryContext = QueryContext.Schema;
|
|
1231
|
+
}
|
|
1232
|
+
ifExists() {
|
|
1233
|
+
this.Exists = true;
|
|
1234
|
+
return this;
|
|
1235
|
+
}
|
|
1236
|
+
toDB() {
|
|
1237
|
+
return this._container.resolve(DropTableCompiler, [this]).compile();
|
|
1238
|
+
}
|
|
1239
|
+
};
|
|
1240
|
+
DropTableQueryBuilder = __decorate([
|
|
1241
|
+
NewInstance(),
|
|
1242
|
+
__metadata("design:paramtypes", [Container, OrmDriver, String, String])
|
|
1243
|
+
], DropTableQueryBuilder);
|
|
1244
|
+
export { DropTableQueryBuilder };
|
|
1245
|
+
let AlterTableQueryBuilder = class AlterTableQueryBuilder extends QueryBuilder {
|
|
1246
|
+
get Columns() {
|
|
1247
|
+
return this._columns;
|
|
1248
|
+
}
|
|
1249
|
+
constructor(container, driver, name) {
|
|
1250
|
+
super(container, driver, null);
|
|
1251
|
+
this.setTable(name);
|
|
1252
|
+
this.QueryContext = QueryContext.Schema;
|
|
1253
|
+
this._columns = [];
|
|
1254
|
+
this.DroppedColumns = [];
|
|
1255
|
+
}
|
|
1256
|
+
/**
|
|
1257
|
+
* Renames table
|
|
1258
|
+
*
|
|
1259
|
+
* @param newTableName - new table name
|
|
1260
|
+
*/
|
|
1261
|
+
rename(newTableName) {
|
|
1262
|
+
this.NewTableName = newTableName;
|
|
1263
|
+
}
|
|
1264
|
+
dropColumn(column) {
|
|
1265
|
+
this.DroppedColumns.push(column);
|
|
1266
|
+
}
|
|
1267
|
+
toDB() {
|
|
1268
|
+
return this._container.resolve(AlterTableQueryCompiler, [this]).compile();
|
|
1269
|
+
}
|
|
1270
|
+
};
|
|
1271
|
+
AlterTableQueryBuilder = __decorate([
|
|
1272
|
+
NewInstance(),
|
|
1273
|
+
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1274
|
+
], AlterTableQueryBuilder);
|
|
1275
|
+
export { AlterTableQueryBuilder };
|
|
1276
|
+
let TableQueryBuilder = class TableQueryBuilder extends QueryBuilder {
|
|
1277
|
+
/**
|
|
1278
|
+
* Alias for string(name, 36 )
|
|
1279
|
+
*/
|
|
1280
|
+
uuid(name) {
|
|
1281
|
+
return this.string(name, 36);
|
|
1282
|
+
}
|
|
1283
|
+
ifExists() {
|
|
1284
|
+
this._checkExists = true;
|
|
1285
|
+
return this;
|
|
1286
|
+
}
|
|
1287
|
+
/**
|
|
1288
|
+
* Mark table as temporary
|
|
1289
|
+
*/
|
|
1290
|
+
temporary() {
|
|
1291
|
+
this._temporary = true;
|
|
1292
|
+
return this;
|
|
1293
|
+
}
|
|
1294
|
+
/**
|
|
1295
|
+
* Turn on history trackign for this table
|
|
1296
|
+
* Each change & row will be tracked, and all history of changes can be accessed
|
|
1297
|
+
*/
|
|
1298
|
+
trackHistory() {
|
|
1299
|
+
this._trackHistory = true;
|
|
1300
|
+
return this;
|
|
1301
|
+
}
|
|
1302
|
+
get Columns() {
|
|
1303
|
+
return this._columns;
|
|
1304
|
+
}
|
|
1305
|
+
get ForeignKeys() {
|
|
1306
|
+
return this._foreignKeys;
|
|
1307
|
+
}
|
|
1308
|
+
get CheckExists() {
|
|
1309
|
+
return this._checkExists;
|
|
1310
|
+
}
|
|
1311
|
+
get Temporary() {
|
|
1312
|
+
return this._temporary;
|
|
1313
|
+
}
|
|
1314
|
+
get TrackHistory() {
|
|
1315
|
+
return this._trackHistory;
|
|
1316
|
+
}
|
|
1317
|
+
constructor(container, driver, name) {
|
|
1318
|
+
super(container, driver, null);
|
|
1319
|
+
this._charset = '';
|
|
1320
|
+
this._comment = '';
|
|
1321
|
+
this._columns = [];
|
|
1322
|
+
this._foreignKeys = [];
|
|
1323
|
+
this._temporary = false;
|
|
1324
|
+
this._trackHistory = false;
|
|
1325
|
+
this.setTable(name);
|
|
1326
|
+
this.QueryContext = QueryContext.Schema;
|
|
1327
|
+
}
|
|
1328
|
+
increments(name) {
|
|
1329
|
+
return this.int(name).autoIncrement().notNull().primaryKey();
|
|
1330
|
+
}
|
|
1331
|
+
comment(comment) {
|
|
1332
|
+
this._comment = comment;
|
|
1333
|
+
}
|
|
1334
|
+
charset(charset) {
|
|
1335
|
+
this._charset = charset;
|
|
1336
|
+
}
|
|
1337
|
+
foreignKey(foreignKey) {
|
|
1338
|
+
const builder = new ForeignKeyBuilder();
|
|
1339
|
+
builder.foreignKey(foreignKey);
|
|
1340
|
+
this._foreignKeys.push(builder);
|
|
1341
|
+
return builder;
|
|
1342
|
+
}
|
|
1343
|
+
toDB() {
|
|
1344
|
+
return this._container.resolve(TableQueryCompiler, [this]).compile();
|
|
1345
|
+
}
|
|
1346
|
+
};
|
|
1347
|
+
TableQueryBuilder = __decorate([
|
|
1348
|
+
NewInstance(),
|
|
1349
|
+
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1350
|
+
], TableQueryBuilder);
|
|
1351
|
+
export { TableQueryBuilder };
|
|
1352
|
+
let TruncateTableQueryBuilder = class TruncateTableQueryBuilder extends QueryBuilder {
|
|
1353
|
+
constructor(container, driver) {
|
|
1354
|
+
super(container, driver);
|
|
1355
|
+
this.container = container;
|
|
1356
|
+
this.driver = driver;
|
|
1357
|
+
}
|
|
1358
|
+
toDB() {
|
|
1359
|
+
return this._container.resolve(TruncateTableQueryCompiler, [this.Container, this]).compile();
|
|
1360
|
+
}
|
|
1361
|
+
};
|
|
1362
|
+
TruncateTableQueryBuilder = __decorate([
|
|
1363
|
+
NewInstance(),
|
|
1364
|
+
Inject(Container),
|
|
1365
|
+
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1366
|
+
], TruncateTableQueryBuilder);
|
|
1367
|
+
export { TruncateTableQueryBuilder };
|
|
1368
|
+
let CloneTableQueryBuilder = class CloneTableQueryBuilder extends QueryBuilder {
|
|
1369
|
+
get CloneSource() {
|
|
1370
|
+
return this._cloneSrc;
|
|
1371
|
+
}
|
|
1372
|
+
get Temporary() {
|
|
1373
|
+
return this._temporary;
|
|
1374
|
+
}
|
|
1375
|
+
get Shallow() {
|
|
1376
|
+
return this._shallow;
|
|
1377
|
+
}
|
|
1378
|
+
get Filter() {
|
|
1379
|
+
return this._filter;
|
|
1380
|
+
}
|
|
1381
|
+
constructor(container, driver) {
|
|
1382
|
+
super(container, driver);
|
|
1383
|
+
this.container = container;
|
|
1384
|
+
this.driver = driver;
|
|
1385
|
+
this._shallow = true;
|
|
1386
|
+
this._cloneSrc = '';
|
|
1387
|
+
this._temporary = false;
|
|
1388
|
+
}
|
|
1389
|
+
/**
|
|
1390
|
+
* Clones table structure without data
|
|
1391
|
+
* Shorthand for createTable(( table) => table.clone("new"));
|
|
1392
|
+
*
|
|
1393
|
+
* @param srcTable - source table name
|
|
1394
|
+
* @param newTable - target table name
|
|
1395
|
+
*/
|
|
1396
|
+
shallowClone(srcTable, newTable) {
|
|
1397
|
+
this.setTable(newTable);
|
|
1398
|
+
this._cloneSrc = srcTable;
|
|
1399
|
+
return this;
|
|
1400
|
+
}
|
|
1401
|
+
/**
|
|
1402
|
+
* Clones table with data
|
|
1403
|
+
*
|
|
1404
|
+
* @param srcTable - source table name
|
|
1405
|
+
* @param newTable - target table name
|
|
1406
|
+
* @param filter - data filter, set null if all data is to be cloned
|
|
1407
|
+
*/
|
|
1408
|
+
async deepClone(srcTable, newTable, filter) {
|
|
1409
|
+
this.setTable(newTable);
|
|
1410
|
+
this._cloneSrc = srcTable;
|
|
1411
|
+
this._shallow = false;
|
|
1412
|
+
if (filter) {
|
|
1413
|
+
this._filter = new SelectQueryBuilder(this._container, this._driver);
|
|
1414
|
+
this._filter.setTable(this._cloneSrc);
|
|
1415
|
+
filter(this._filter);
|
|
1416
|
+
}
|
|
1417
|
+
return this;
|
|
1418
|
+
}
|
|
1419
|
+
toDB() {
|
|
1420
|
+
return this._container.resolve(TableCloneQueryCompiler, [this]).compile();
|
|
1421
|
+
}
|
|
1422
|
+
};
|
|
1423
|
+
CloneTableQueryBuilder = __decorate([
|
|
1424
|
+
NewInstance(),
|
|
1425
|
+
Inject(Container),
|
|
1426
|
+
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1427
|
+
], CloneTableQueryBuilder);
|
|
1428
|
+
export { CloneTableQueryBuilder };
|
|
1429
|
+
export class EventIntervalDesc {
|
|
1430
|
+
}
|
|
1431
|
+
let EventQueryBuilder = class EventQueryBuilder extends QueryBuilder {
|
|
1432
|
+
constructor(container, driver, Name) {
|
|
1433
|
+
super(container, driver);
|
|
1434
|
+
this.container = container;
|
|
1435
|
+
this.driver = driver;
|
|
1436
|
+
this.Name = Name;
|
|
1437
|
+
}
|
|
1438
|
+
/**
|
|
1439
|
+
* execute every time with specified interval ( days, hours, seconds, minutes etc)
|
|
1440
|
+
*/
|
|
1441
|
+
every() {
|
|
1442
|
+
this.EveryInterval = new EventIntervalDesc();
|
|
1443
|
+
return this.EveryInterval;
|
|
1444
|
+
}
|
|
1445
|
+
/**
|
|
1446
|
+
*
|
|
1447
|
+
* Execute at specific time
|
|
1448
|
+
*
|
|
1449
|
+
* @param dateTime - specific time
|
|
1450
|
+
*/
|
|
1451
|
+
at(dateTime) {
|
|
1452
|
+
this.At = dateTime;
|
|
1453
|
+
}
|
|
1454
|
+
/**
|
|
1455
|
+
* execute once at specific interfal from now eg. now + 1 day
|
|
1456
|
+
*/
|
|
1457
|
+
fromNow() {
|
|
1458
|
+
this.FromNowInverval = new EventIntervalDesc();
|
|
1459
|
+
return this.FromNowInverval;
|
|
1460
|
+
}
|
|
1461
|
+
/**
|
|
1462
|
+
*
|
|
1463
|
+
* @param sql - code to execute, could be raw sql query, single builder, or multiple builders that will be executed on by one
|
|
1464
|
+
*/
|
|
1465
|
+
do(sql) {
|
|
1466
|
+
if (sql instanceof RawQueryStatement) {
|
|
1467
|
+
this.RawSql = sql;
|
|
1468
|
+
}
|
|
1469
|
+
else if (Array.isArray(sql)) {
|
|
1470
|
+
this.Queries = sql;
|
|
1471
|
+
}
|
|
1472
|
+
else {
|
|
1473
|
+
this.Queries = [sql];
|
|
1474
|
+
}
|
|
1475
|
+
}
|
|
1476
|
+
/**
|
|
1477
|
+
*
|
|
1478
|
+
* Add comment to schedule for documentation. It is passed to sql engine
|
|
1479
|
+
*
|
|
1480
|
+
* @param comment - comment text
|
|
1481
|
+
*/
|
|
1482
|
+
comment(comment) {
|
|
1483
|
+
this.Comment = comment;
|
|
1484
|
+
}
|
|
1485
|
+
toDB() {
|
|
1486
|
+
return this._container.resolve(EventQueryCompiler, [this]).compile();
|
|
1487
|
+
}
|
|
1488
|
+
};
|
|
1489
|
+
EventQueryBuilder = __decorate([
|
|
1490
|
+
NewInstance(),
|
|
1491
|
+
Inject(Container),
|
|
1492
|
+
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1493
|
+
], EventQueryBuilder);
|
|
1494
|
+
export { EventQueryBuilder };
|
|
1495
|
+
let DropEventQueryBuilder = class DropEventQueryBuilder extends QueryBuilder {
|
|
1496
|
+
constructor(container, driver, Name) {
|
|
1497
|
+
super(container, driver);
|
|
1498
|
+
this.container = container;
|
|
1499
|
+
this.driver = driver;
|
|
1500
|
+
this.Name = Name;
|
|
1501
|
+
}
|
|
1502
|
+
toDB() {
|
|
1503
|
+
return this._container.resolve(DropEventQueryCompiler, [this]).compile();
|
|
1504
|
+
}
|
|
1505
|
+
};
|
|
1506
|
+
DropEventQueryBuilder = __decorate([
|
|
1507
|
+
NewInstance(),
|
|
1508
|
+
Inject(Container),
|
|
1509
|
+
__metadata("design:paramtypes", [Container, OrmDriver, String])
|
|
1510
|
+
], DropEventQueryBuilder);
|
|
1511
|
+
export { DropEventQueryBuilder };
|
|
1512
|
+
/**
|
|
1513
|
+
* Creates schedule job in database engine.
|
|
1514
|
+
* Note, some engines does not support this, so it will implemented
|
|
1515
|
+
* as nodejs interval
|
|
1516
|
+
*/
|
|
1517
|
+
let ScheduleQueryBuilder = class ScheduleQueryBuilder {
|
|
1518
|
+
constructor(container, driver) {
|
|
1519
|
+
this.container = container;
|
|
1520
|
+
this.driver = driver;
|
|
1521
|
+
}
|
|
1522
|
+
create(name, callback) {
|
|
1523
|
+
const builder = new EventQueryBuilder(this.container, this.driver, name);
|
|
1524
|
+
callback.call(this, builder);
|
|
1525
|
+
return builder;
|
|
1526
|
+
}
|
|
1527
|
+
drop(name) {
|
|
1528
|
+
return new DropEventQueryBuilder(this.container, this.driver, name);
|
|
1529
|
+
}
|
|
1530
|
+
};
|
|
1531
|
+
ScheduleQueryBuilder = __decorate([
|
|
1532
|
+
NewInstance(),
|
|
1533
|
+
Inject(Container),
|
|
1534
|
+
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1535
|
+
], ScheduleQueryBuilder);
|
|
1536
|
+
export { ScheduleQueryBuilder };
|
|
1537
|
+
let SchemaQueryBuilder = class SchemaQueryBuilder {
|
|
1538
|
+
constructor(container, driver) {
|
|
1539
|
+
this.container = container;
|
|
1540
|
+
this.driver = driver;
|
|
1541
|
+
}
|
|
1542
|
+
createTable(name, callback) {
|
|
1543
|
+
const builder = new TableQueryBuilder(this.container, this.driver, name);
|
|
1544
|
+
callback.call(this, builder);
|
|
1545
|
+
return builder;
|
|
1546
|
+
}
|
|
1547
|
+
cloneTable(callback) {
|
|
1548
|
+
const builder = new CloneTableQueryBuilder(this.container, this.driver);
|
|
1549
|
+
callback(builder);
|
|
1550
|
+
return builder;
|
|
1551
|
+
}
|
|
1552
|
+
alterTable(name, callback) {
|
|
1553
|
+
const builder = new AlterTableQueryBuilder(this.container, this.driver, name);
|
|
1554
|
+
callback.call(this, builder);
|
|
1555
|
+
return builder;
|
|
1556
|
+
}
|
|
1557
|
+
dropTable(name, schema) {
|
|
1558
|
+
return new DropTableQueryBuilder(this.container, this.driver, name, schema);
|
|
1559
|
+
}
|
|
1560
|
+
async tableExists(name, schema) {
|
|
1561
|
+
const query = new TableExistsQueryBuilder(this.container, this.driver, name);
|
|
1562
|
+
if (schema) {
|
|
1563
|
+
query.database(schema);
|
|
1564
|
+
}
|
|
1565
|
+
const exists = await query;
|
|
1566
|
+
return exists !== null && exists.length === 1;
|
|
1567
|
+
}
|
|
1568
|
+
event(name) {
|
|
1569
|
+
return new EventQueryBuilder(this.container, this.driver, name);
|
|
1570
|
+
}
|
|
1571
|
+
dropEvent(name) {
|
|
1572
|
+
return new DropEventQueryBuilder(this.container, this.driver, name);
|
|
1573
|
+
}
|
|
1574
|
+
};
|
|
1575
|
+
SchemaQueryBuilder = __decorate([
|
|
1576
|
+
NewInstance(),
|
|
1577
|
+
Inject(Container),
|
|
1578
|
+
__metadata("design:paramtypes", [Container, OrmDriver])
|
|
1579
|
+
], SchemaQueryBuilder);
|
|
1580
|
+
export { SchemaQueryBuilder };
|
|
1581
|
+
Object.values(ColumnType).forEach((type) => {
|
|
1582
|
+
TableQueryBuilder.prototype[type] = function (name, ...args) {
|
|
1583
|
+
const _builder = new ColumnQueryBuilder(this.Container, name, type, ...args);
|
|
1584
|
+
this._columns.push(_builder);
|
|
1585
|
+
return _builder;
|
|
1586
|
+
};
|
|
1587
|
+
});
|
|
1588
|
+
Object.values(ColumnType).forEach((type) => {
|
|
1589
|
+
AlterTableQueryBuilder.prototype[type] = function (name, ...args) {
|
|
1590
|
+
const _builder = new AlterColumnQueryBuilder(this.Container, name, type, ...args);
|
|
1591
|
+
this._columns.push(_builder);
|
|
1592
|
+
return _builder;
|
|
1593
|
+
};
|
|
1594
|
+
});
|
|
1595
1595
|
//# sourceMappingURL=builders.js.map
|