@spinajs/orm 1.2.47 → 1.2.57

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.
Files changed (63) hide show
  1. package/lib/builders.d.ts +8 -0
  2. package/lib/builders.js +25 -1
  3. package/lib/builders.js.map +1 -1
  4. package/lib/interfaces.d.ts +3 -0
  5. package/lib/interfaces.js +7 -1
  6. package/lib/interfaces.js.map +1 -1
  7. package/lib/orm.js +2 -3
  8. package/lib/orm.js.map +1 -1
  9. package/package.json +5 -5
  10. package/lib/helpers.d.ts +0 -1
  11. package/lib/helpers.js +0 -14
  12. package/lib/helpers.js.map +0 -1
  13. package/lib/log-common/src/index.d.ts +0 -180
  14. package/lib/log-common/src/index.js +0 -49
  15. package/lib/log-common/src/index.js.map +0 -1
  16. package/lib/orm/src/builders.d.ts +0 -429
  17. package/lib/orm/src/builders.js +0 -1082
  18. package/lib/orm/src/builders.js.map +0 -1
  19. package/lib/orm/src/cli.d.ts +0 -1
  20. package/lib/orm/src/cli.js +0 -2
  21. package/lib/orm/src/cli.js.map +0 -1
  22. package/lib/orm/src/converters.d.ts +0 -9
  23. package/lib/orm/src/converters.js +0 -22
  24. package/lib/orm/src/converters.js.map +0 -1
  25. package/lib/orm/src/decorators.d.ts +0 -122
  26. package/lib/orm/src/decorators.js +0 -380
  27. package/lib/orm/src/decorators.js.map +0 -1
  28. package/lib/orm/src/driver.d.ts +0 -77
  29. package/lib/orm/src/driver.js +0 -84
  30. package/lib/orm/src/driver.js.map +0 -1
  31. package/lib/orm/src/enums.d.ts +0 -111
  32. package/lib/orm/src/enums.js +0 -122
  33. package/lib/orm/src/enums.js.map +0 -1
  34. package/lib/orm/src/exceptions.d.ts +0 -6
  35. package/lib/orm/src/exceptions.js +0 -11
  36. package/lib/orm/src/exceptions.js.map +0 -1
  37. package/lib/orm/src/hydrators.d.ts +0 -16
  38. package/lib/orm/src/hydrators.js +0 -70
  39. package/lib/orm/src/hydrators.js.map +0 -1
  40. package/lib/orm/src/index.d.ts +0 -12
  41. package/lib/orm/src/index.js +0 -25
  42. package/lib/orm/src/index.js.map +0 -1
  43. package/lib/orm/src/interfaces.d.ts +0 -615
  44. package/lib/orm/src/interfaces.js +0 -186
  45. package/lib/orm/src/interfaces.js.map +0 -1
  46. package/lib/orm/src/model.d.ts +0 -135
  47. package/lib/orm/src/model.js +0 -449
  48. package/lib/orm/src/model.js.map +0 -1
  49. package/lib/orm/src/orm.d.ts +0 -59
  50. package/lib/orm/src/orm.js +0 -278
  51. package/lib/orm/src/orm.js.map +0 -1
  52. package/lib/orm/src/relations.d.ts +0 -96
  53. package/lib/orm/src/relations.js +0 -503
  54. package/lib/orm/src/relations.js.map +0 -1
  55. package/lib/orm/src/statements.d.ts +0 -132
  56. package/lib/orm/src/statements.js +0 -257
  57. package/lib/orm/src/statements.js.map +0 -1
  58. package/lib/orm/src/types.d.ts +0 -2
  59. package/lib/orm/src/types.js +0 -3
  60. package/lib/orm/src/types.js.map +0 -1
  61. package/lib/orm/src/wrappers.d.ts +0 -5
  62. package/lib/orm/src/wrappers.js +0 -13
  63. package/lib/orm/src/wrappers.js.map +0 -1
@@ -1,1082 +0,0 @@
1
- "use strict";
2
- /* eslint-disable prettier/prettier */
3
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
4
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
5
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
6
- 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;
7
- return c > 3 && r && Object.defineProperty(target, key, r), r;
8
- };
9
- var __metadata = (this && this.__metadata) || function (k, v) {
10
- if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
11
- };
12
- var RawQuery_1, WhereBuilder_1;
13
- Object.defineProperty(exports, "__esModule", { value: true });
14
- exports.SchemaQueryBuilder = exports.TableQueryBuilder = exports.ColumnQueryBuilder = exports.ForeignKeyBuilder = exports.IndexQueryBuilder = exports.InsertQueryBuilder = exports.UpdateQueryBuilder = exports.OnDuplicateQueryBuilder = exports.DeleteQueryBuilder = exports.SelectQueryBuilder = exports.WhereBuilder = exports.WithRecursiveBuilder = exports.JoinBuilder = exports.GroupByBuilder = exports.RawQuery = exports.ColumnsBuilder = exports.OrderByBuilder = exports.LimitBuilder = exports.QueryBuilder = exports.Builder = void 0;
15
- const di_1 = require("@spinajs/di");
16
- const exceptions_1 = require("@spinajs/exceptions");
17
- const _ = require("lodash");
18
- const typescript_mix_1 = require("typescript-mix");
19
- const enums_1 = require("./enums");
20
- const interfaces_1 = require("./interfaces");
21
- const statements_1 = require("./statements");
22
- const driver_1 = require("./driver");
23
- const model_1 = require("./model");
24
- const relations_1 = require("./relations");
25
- const orm_1 = require("./orm");
26
- function isWhereOperator(val) {
27
- return _.isString(val) && Object.values(enums_1.WhereOperators).includes(val.toLowerCase());
28
- }
29
- let Builder = class Builder {
30
- constructor(container, driver, model) {
31
- this._middlewares = [];
32
- this._driver = driver;
33
- this._container = container;
34
- this._model = model;
35
- this._nonSelect = true;
36
- this._asRaw = false;
37
- }
38
- middleware(middleware) {
39
- this._middlewares.push(middleware);
40
- return this;
41
- }
42
- /**
43
- * Builds query that is ready to use in DB
44
- */
45
- toDB() {
46
- throw new exceptions_1.MethodNotImplemented();
47
- }
48
- then(resolve, reject) {
49
- const compiled = this.toDB();
50
- return this._driver.execute(compiled.expression, compiled.bindings, this._queryContext).then((result) => {
51
- try {
52
- if (this._asRaw) {
53
- resolve(result);
54
- return;
55
- }
56
- if (this._model && !this._nonSelect) {
57
- let transformedResult = result;
58
- if (this._middlewares.length > 0) {
59
- transformedResult = this._middlewares.reduce((_, current) => {
60
- return current.afterData(result);
61
- }, []);
62
- }
63
- const models = transformedResult.map((r) => {
64
- let model = null;
65
- for (const middleware of this._middlewares) {
66
- model = middleware.modelCreation(r);
67
- if (model !== null) {
68
- break;
69
- }
70
- }
71
- if (model === null) {
72
- model = new this._model();
73
- model.hydrate(r);
74
- }
75
- return model;
76
- });
77
- const afterMiddlewarePromises = this._middlewares.reduce((prev, current) => {
78
- return prev.concat([current.afterHydration(models)]);
79
- }, []);
80
- if (this._middlewares.length > 0) {
81
- Promise.all(afterMiddlewarePromises).then(() => {
82
- resolve(models);
83
- }, reject);
84
- }
85
- else {
86
- resolve(models);
87
- }
88
- }
89
- else {
90
- resolve(result);
91
- }
92
- }
93
- catch (err) {
94
- reject(err);
95
- }
96
- }, reject);
97
- }
98
- };
99
- Builder = __decorate([
100
- (0, di_1.NewInstance)(),
101
- (0, di_1.Inject)(di_1.Container),
102
- __metadata("design:paramtypes", [di_1.Container, driver_1.OrmDriver, Object])
103
- ], Builder);
104
- exports.Builder = Builder;
105
- /**
106
- * Base class for queires. Implements basic query functionality
107
- *
108
- */
109
- let QueryBuilder = class QueryBuilder extends Builder {
110
- constructor(container, driver, model) {
111
- super(container, driver, model);
112
- }
113
- /**
114
- * SQL table name that query is executed on
115
- *
116
- * @example
117
- * SELECT * FROM `users`
118
- */
119
- get Table() {
120
- return this._table;
121
- }
122
- /**
123
- * DB table alias
124
- */
125
- get TableAlias() {
126
- return this._tableAlias;
127
- }
128
- /**
129
- * SQL schema/database name that query is executed on.
130
- *
131
- * @example
132
- * SELECT * FROM `spinejs`.`users` as u
133
- */
134
- get Schema() {
135
- return this._schema;
136
- }
137
- /**
138
- * Sets schema to this query.
139
- *
140
- * @param schema - schema or database name in database
141
- */
142
- schema(schema) {
143
- if (!schema) {
144
- throw new exceptions_1.InvalidArgument(`schema argument cannot be null or empty`);
145
- }
146
- this._schema = schema;
147
- return this;
148
- }
149
- /**
150
- * Sets table that query is executed on
151
- *
152
- * @param table - sql table name
153
- * @param alias - sql table alias
154
- *
155
- * @example
156
- *
157
- * this.setTable("user","u")
158
- *
159
- */
160
- setTable(table, alias) {
161
- if (!table.trim()) {
162
- throw new exceptions_1.InvalidArgument('table name is empty');
163
- }
164
- this._table = table;
165
- this.setAlias(alias);
166
- return this;
167
- }
168
- /**
169
- * Sets table alias for query
170
- *
171
- * @param alias - sql table alias
172
- */
173
- setAlias(alias) {
174
- this._tableAlias = alias;
175
- return this;
176
- }
177
- from(table, alias) {
178
- return this.setTable(table, alias);
179
- }
180
- };
181
- QueryBuilder = __decorate([
182
- (0, di_1.NewInstance)(),
183
- (0, di_1.Inject)(di_1.Container),
184
- __metadata("design:paramtypes", [di_1.Container, driver_1.OrmDriver, Object])
185
- ], QueryBuilder);
186
- exports.QueryBuilder = QueryBuilder;
187
- let LimitBuilder = class LimitBuilder {
188
- constructor() {
189
- this._fail = false;
190
- this._first = false;
191
- this._limit = {
192
- limit: -1,
193
- offset: -1,
194
- };
195
- }
196
- take(count) {
197
- if (count <= 0) {
198
- throw new exceptions_1.InvalidArgument(`take count cannot be negative number`);
199
- }
200
- this._limit.limit = count;
201
- return this;
202
- }
203
- skip(count) {
204
- if (count < 0) {
205
- throw new exceptions_1.InvalidArgument(`skip count cannot be negative number`);
206
- }
207
- this._limit.offset = count;
208
- return this;
209
- }
210
- async first() {
211
- this._first = true;
212
- this._limit.limit = 1;
213
- return (await this);
214
- }
215
- firstOrFail() {
216
- this._fail = true;
217
- return this.first();
218
- }
219
- getLimits() {
220
- return this._limit;
221
- }
222
- };
223
- LimitBuilder = __decorate([
224
- (0, di_1.NewInstance)(),
225
- __metadata("design:paramtypes", [])
226
- ], LimitBuilder);
227
- exports.LimitBuilder = LimitBuilder;
228
- let OrderByBuilder = class OrderByBuilder {
229
- constructor() {
230
- this._sort = {
231
- column: '',
232
- order: enums_1.SORT_ORDER.ASC,
233
- };
234
- }
235
- order(column, direction) {
236
- this._sort = {
237
- column,
238
- order: direction,
239
- };
240
- return this;
241
- }
242
- orderBy(column) {
243
- this._sort = {
244
- column,
245
- order: enums_1.SORT_ORDER.ASC,
246
- };
247
- return this;
248
- }
249
- orderByDescending(column) {
250
- this._sort = {
251
- column,
252
- order: enums_1.SORT_ORDER.DESC,
253
- };
254
- return this;
255
- }
256
- getSort() {
257
- return this._sort.column.trim() !== '' ? this._sort : null;
258
- }
259
- };
260
- OrderByBuilder = __decorate([
261
- (0, di_1.NewInstance)(),
262
- __metadata("design:paramtypes", [])
263
- ], OrderByBuilder);
264
- exports.OrderByBuilder = OrderByBuilder;
265
- let ColumnsBuilder = class ColumnsBuilder {
266
- constructor() {
267
- this._columns = [];
268
- }
269
- /**
270
- * Clears all select clauses from the query.
271
- *
272
- * @example
273
- *
274
- * query.columns()
275
- *
276
- */
277
- clearColumns() {
278
- this._columns = [];
279
- return this;
280
- }
281
- columns(names) {
282
- const descriptor = (0, model_1.extractModelDescriptor)(this._model);
283
- this._columns = names.map((n) => {
284
- return this._container.resolve(statements_1.ColumnStatement, [n, null, this._tableAlias, descriptor === null || descriptor === void 0 ? void 0 : descriptor.Columns.find((c) => c.Name === n)]);
285
- });
286
- return this;
287
- }
288
- select(column, alias) {
289
- const descriptor = (0, model_1.extractModelDescriptor)(this._model);
290
- if (column instanceof Map) {
291
- column.forEach((alias, colName) => {
292
- this._columns.push(this._container.resolve(statements_1.ColumnStatement, [colName, alias, this._tableAlias, descriptor === null || descriptor === void 0 ? void 0 : descriptor.Columns.find((c) => c.Name === colName)]));
293
- });
294
- }
295
- if (column instanceof RawQuery) {
296
- this._columns.push(this._container.resolve(statements_1.ColumnRawStatement, [column, null, this._tableAlias]));
297
- }
298
- else {
299
- this._columns.push(this._container.resolve(statements_1.ColumnStatement, [column, alias, this._tableAlias, descriptor === null || descriptor === void 0 ? void 0 : descriptor.Columns.find((c) => c.Name === column)]));
300
- }
301
- return this;
302
- }
303
- getColumns() {
304
- return this._columns;
305
- }
306
- };
307
- ColumnsBuilder = __decorate([
308
- (0, di_1.NewInstance)(),
309
- __metadata("design:paramtypes", [])
310
- ], ColumnsBuilder);
311
- exports.ColumnsBuilder = ColumnsBuilder;
312
- let RawQuery = RawQuery_1 = class RawQuery {
313
- constructor(query, bindings) {
314
- this._query = '';
315
- this._bindings = [];
316
- this._query = query;
317
- this._bindings = bindings;
318
- }
319
- get Query() {
320
- return this._query;
321
- }
322
- get Bindings() {
323
- return this._bindings;
324
- }
325
- static create(query, bindings) {
326
- return new RawQuery_1(query, bindings);
327
- }
328
- };
329
- RawQuery = RawQuery_1 = __decorate([
330
- (0, di_1.NewInstance)(),
331
- __metadata("design:paramtypes", [String, Array])
332
- ], RawQuery);
333
- exports.RawQuery = RawQuery;
334
- class GroupByBuilder {
335
- constructor() {
336
- this._groupStatements = [];
337
- }
338
- get GroupStatements() {
339
- return this._groupStatements;
340
- }
341
- clearGroupBy() {
342
- this._groupStatements = [];
343
- return this;
344
- }
345
- groupBy(expression) {
346
- this._groupStatements.push(this._container.resolve(statements_1.GroupByStatement, [expression]));
347
- return this;
348
- }
349
- }
350
- exports.GroupByBuilder = GroupByBuilder;
351
- class JoinBuilder {
352
- constructor(container) {
353
- this._joinStatements = [];
354
- this._container = container;
355
- this._joinStatements = [];
356
- }
357
- get JoinStatements() {
358
- return this._joinStatements;
359
- }
360
- clearJoins() {
361
- this._joinStatements = [];
362
- return this;
363
- }
364
- innerJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey) {
365
- this.addJoinStatement.call(this, enums_1.JoinMethod.INNER, ...arguments);
366
- return this;
367
- }
368
- leftJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey) {
369
- this.addJoinStatement.call(this, enums_1.JoinMethod.LEFT, ...arguments);
370
- return this;
371
- }
372
- leftOuterJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey) {
373
- this.addJoinStatement.call(this, enums_1.JoinMethod.LEFT_OUTER, ...arguments);
374
- return this;
375
- }
376
- rightJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey) {
377
- this.addJoinStatement.call(this, enums_1.JoinMethod.RIGHT, ...arguments);
378
- return this;
379
- }
380
- rightOuterJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey) {
381
- this.addJoinStatement.call(this, enums_1.JoinMethod.RIGHT_OUTER, ...arguments);
382
- return this;
383
- }
384
- fullOuterJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey) {
385
- this.addJoinStatement.call(this, enums_1.JoinMethod.FULL_OUTER, ...arguments);
386
- return this;
387
- }
388
- crossJoin(_table, _AliasOrForeignKey, _fkOrPkKey, _primaryKey) {
389
- this.addJoinStatement.call(this, enums_1.JoinMethod.CROSS, ...arguments);
390
- return this;
391
- }
392
- addJoinStatement(method, table, AliasOrForeignKey, fkOrPkKey, primaryKey) {
393
- let stmt = null;
394
- if (arguments.length === 4) {
395
- stmt = this._container.resolve(statements_1.JoinStatement, [table, method, AliasOrForeignKey, fkOrPkKey, null, this._tableAlias]);
396
- }
397
- else if (arguments.length === 5) {
398
- stmt = this._container.resolve(statements_1.JoinStatement, [table, method, fkOrPkKey, primaryKey, AliasOrForeignKey, this._tableAlias]);
399
- }
400
- else {
401
- stmt = this._container.resolve(statements_1.JoinStatement, [table, method]);
402
- }
403
- this.JoinStatements.push(stmt);
404
- }
405
- }
406
- exports.JoinBuilder = JoinBuilder;
407
- let WithRecursiveBuilder = class WithRecursiveBuilder {
408
- get CteRecursive() {
409
- return this._cteStatement;
410
- }
411
- withRecursive(rcKeyName, pkName) {
412
- this._cteStatement = this._container.resolve(statements_1.WithRecursiveStatement, ['cte', this, rcKeyName, pkName]);
413
- return this;
414
- }
415
- };
416
- WithRecursiveBuilder = __decorate([
417
- (0, di_1.NewInstance)()
418
- ], WithRecursiveBuilder);
419
- exports.WithRecursiveBuilder = WithRecursiveBuilder;
420
- let WhereBuilder = WhereBuilder_1 = class WhereBuilder {
421
- constructor(container, tableAlias) {
422
- this._statements = [];
423
- this._boolean = enums_1.WhereBoolean.AND;
424
- this._container = container;
425
- this._boolean = enums_1.WhereBoolean.AND;
426
- this._statements = [];
427
- this._tableAlias = tableAlias;
428
- }
429
- get Statements() {
430
- return this._statements;
431
- }
432
- get Op() {
433
- return this._boolean;
434
- }
435
- where(column, operator, value) {
436
- const self = this;
437
- // Support "where true || where false"
438
- if (_.isBoolean(column)) {
439
- return this.where(RawQuery.create(column ? 'TRUE' : 'FALSE'));
440
- }
441
- if (column instanceof RawQuery) {
442
- this.Statements.push(this._container.resolve(statements_1.RawQueryStatement, [column.Query, column.Bindings, self._tableAlias]));
443
- return this;
444
- }
445
- // handle nested where's
446
- if (_.isFunction(column)) {
447
- const builder = new WhereBuilder_1(this._container, this._tableAlias);
448
- column.call(builder);
449
- self.Statements.push(this._container.resolve(statements_1.WhereQueryStatement, [builder, self._tableAlias]));
450
- return this;
451
- }
452
- // handle simple key = object[key] AND ....
453
- if (_.isObject(column) && !(column instanceof statements_1.Wrap)) {
454
- return this.whereObject(column);
455
- }
456
- if (typeof value === 'undefined') {
457
- return _handleForTwo.call(this, column, operator);
458
- }
459
- return _handleForThree.call(this, column, operator, value);
460
- /**
461
- * handles for where("foo", 1).where(...) cases
462
- * it produces WHERE foo = 1
463
- */
464
- function _handleForTwo(c, v) {
465
- if (v === undefined) {
466
- throw new exceptions_1.InvalidArgument(`value cannot be undefined`);
467
- }
468
- if (!_.isString(c) && !(c instanceof statements_1.Wrap)) {
469
- throw new exceptions_1.InvalidArgument(`column is not of type string or wrapped.`);
470
- }
471
- if (v === null) {
472
- return this.whereNull(c);
473
- }
474
- self._statements.push(self._container.resolve(statements_1.WhereStatement, [c, enums_1.WhereOperators.EQ, v, self._tableAlias, this._container]));
475
- return self;
476
- }
477
- /**
478
- * Handles for where("foo",'!=',1) etc
479
- * it produces WHERE foo != 1
480
- */
481
- function _handleForThree(c, o, v) {
482
- if (!isWhereOperator(o)) {
483
- throw new exceptions_1.InvalidArgument(`operator ${o} is invalid`);
484
- }
485
- if (!_.isString(c) && !(c instanceof statements_1.Wrap)) {
486
- throw new exceptions_1.InvalidArgument(`column is not of type string or wrapped.`);
487
- }
488
- if (v === null) {
489
- return this.whereNull(c);
490
- }
491
- if (v === null) {
492
- return o === enums_1.WhereOperators.NOT_NULL ? this.whereNotNull(c) : this.whereNull(c);
493
- }
494
- self._statements.push(self._container.resolve(statements_1.WhereStatement, [c, o, v, self._tableAlias, this._container]));
495
- return this;
496
- }
497
- }
498
- orWhere(column, ..._args) {
499
- this._boolean = enums_1.WhereBoolean.OR;
500
- return this.where(column, ...Array.from(arguments).slice(1));
501
- }
502
- andWhere(column, ..._args) {
503
- this._boolean = enums_1.WhereBoolean.AND;
504
- return this.where(column, ...Array.from(arguments).slice(1));
505
- }
506
- whereObject(obj) {
507
- for (const key of Object.keys(obj)) {
508
- this.andWhere(key, enums_1.WhereOperators.EQ, obj[key]);
509
- }
510
- return this;
511
- }
512
- whereNotNull(column) {
513
- this._statements.push(this._container.resolve(statements_1.WhereStatement, [column, enums_1.WhereOperators.NOT_NULL, null, this._tableAlias]));
514
- return this;
515
- }
516
- whereNull(column) {
517
- this._statements.push(this._container.resolve(statements_1.WhereStatement, [column, enums_1.WhereOperators.NULL, null, this._tableAlias]));
518
- return this;
519
- }
520
- whereNot(column, val) {
521
- return this.where(column, enums_1.WhereOperators.NOT, val);
522
- }
523
- whereIn(column, val) {
524
- this._statements.push(this._container.resolve(statements_1.InStatement, [column, val, false, this._tableAlias]));
525
- return this;
526
- }
527
- whereNotIn(column, val) {
528
- this._statements.push(this._container.resolve(statements_1.InStatement, [column, val, true, this._tableAlias]));
529
- return this;
530
- }
531
- whereExist(query) {
532
- this._statements.push(this._container.resolve(statements_1.ExistsQueryStatement, [query, false]));
533
- return this;
534
- }
535
- whereNotExists(query) {
536
- this._statements.push(this._container.resolve(statements_1.ExistsQueryStatement, [query, true]));
537
- return this;
538
- }
539
- whereBetween(column, val) {
540
- this._statements.push(this._container.resolve(statements_1.BetweenStatement, [column, val, false, this._tableAlias]));
541
- return this;
542
- }
543
- whereNotBetween(column, val) {
544
- this._statements.push(this._container.resolve(statements_1.BetweenStatement, [column, val, true, this._tableAlias]));
545
- return this;
546
- }
547
- whereInSet(column, val) {
548
- this._statements.push(this._container.resolve(statements_1.InSetStatement, [column, val, false, this._tableAlias]));
549
- return this;
550
- }
551
- whereNotInSet(column, val) {
552
- this._statements.push(this._container.resolve(statements_1.InSetStatement, [column, val, true, this._tableAlias]));
553
- return this;
554
- }
555
- clearWhere() {
556
- this._statements = [];
557
- return this;
558
- }
559
- };
560
- WhereBuilder = WhereBuilder_1 = __decorate([
561
- (0, di_1.NewInstance)(),
562
- __metadata("design:paramtypes", [di_1.Container, String])
563
- ], WhereBuilder);
564
- exports.WhereBuilder = WhereBuilder;
565
- class SelectQueryBuilder extends QueryBuilder {
566
- constructor(container, driver, model, owner) {
567
- super(container, driver, model);
568
- this._columns = [];
569
- /**
570
- * where query props
571
- */
572
- this._statements = [];
573
- this._joinStatements = [];
574
- this._groupStatements = [];
575
- this._relations = [];
576
- this._distinct = false;
577
- this._method = enums_1.QueryMethod.SELECT;
578
- this._boolean = enums_1.WhereBoolean.AND;
579
- this._sort = {
580
- column: '',
581
- order: enums_1.SORT_ORDER.ASC,
582
- };
583
- this._first = false;
584
- this._limit = {
585
- limit: -1,
586
- offset: -1,
587
- };
588
- this._nonSelect = false;
589
- this._queryContext = interfaces_1.QueryContext.Select;
590
- this._owner = owner;
591
- }
592
- get IsDistinct() {
593
- return this._distinct;
594
- }
595
- get Owner() {
596
- return this._owner;
597
- }
598
- get Relations() {
599
- return this._relations;
600
- }
601
- async asRaw() {
602
- this._asRaw = true;
603
- return (await this);
604
- }
605
- setAlias(alias) {
606
- this._tableAlias = alias;
607
- this._columns.forEach((c) => (c.TableAlias = alias));
608
- this._joinStatements.forEach((c) => (c.TableAlias = alias));
609
- this._statements.forEach((c) => (c.TableAlias = alias));
610
- return this;
611
- }
612
- clone() {
613
- const builder = new SelectQueryBuilder(this._container, this._driver, this._model, this._owner);
614
- builder._columns = this._columns.slice(0);
615
- builder._joinStatements = this._joinStatements.slice(0);
616
- builder._statements = this._statements.slice(0);
617
- builder._limit = Object.assign({}, this._limit);
618
- builder._sort = Object.assign({}, this._sort);
619
- builder._boolean = this._boolean;
620
- builder._distinct = this._distinct;
621
- builder._table = this._table;
622
- builder._tableAlias = this._tableAlias;
623
- return builder;
624
- }
625
- populate(relation, callback) {
626
- var _a;
627
- let relInstance = null;
628
- const descriptor = (0, model_1.extractModelDescriptor)(this._model);
629
- if (!descriptor.Relations.has(relation)) {
630
- throw new exceptions_1.InvalidArgument(`Relation ${relation} not exists in model ${(_a = this._model) === null || _a === void 0 ? void 0 : _a.constructor.name}`);
631
- }
632
- const relDescription = descriptor.Relations.get(relation);
633
- if (relDescription.Type === interfaces_1.RelationType.One && relDescription.Recursive) {
634
- relInstance = this._container.resolve(relations_1.BelongsToRecursiveRelation, [this._container.get(orm_1.Orm), this, relDescription, this._owner]);
635
- }
636
- else {
637
- if (relDescription.Recursive) {
638
- throw new exceptions_1.InvalidOperation(`cannot mark relation as recursive with non one-to-one relation type`);
639
- }
640
- switch (relDescription.Type) {
641
- case interfaces_1.RelationType.One:
642
- relInstance = this._container.resolve(relations_1.BelongsToRelation, [this._container.get(orm_1.Orm), this, relDescription, this._owner]);
643
- break;
644
- case interfaces_1.RelationType.Many:
645
- relInstance = this._container.resolve(relations_1.OneToManyRelation, [this._container.get(orm_1.Orm), this, relDescription, this._owner]);
646
- break;
647
- case interfaces_1.RelationType.ManyToMany:
648
- relInstance = this._container.resolve(relations_1.ManyToManyRelation, [this._container.get(orm_1.Orm), this, relDescription, null]);
649
- break;
650
- }
651
- }
652
- relInstance.execute(callback);
653
- this._relations.push(relInstance);
654
- return this;
655
- }
656
- mergeStatements(builder) {
657
- this._joinStatements = this._joinStatements.concat(builder._joinStatements);
658
- this._columns = this._columns.concat(builder._columns);
659
- this._statements = this._statements.concat(builder._statements);
660
- this._relations = this._relations.concat(builder._relations);
661
- this._middlewares = this._middlewares.concat(builder._middlewares);
662
- }
663
- min(column, as) {
664
- this._columns.push(this._container.resolve(statements_1.ColumnMethodStatement, [column, enums_1.ColumnMethods.MIN, as, this._tableAlias]));
665
- return this;
666
- }
667
- max(column, as) {
668
- this._columns.push(this._container.resolve(statements_1.ColumnMethodStatement, [column, enums_1.ColumnMethods.MAX, as, this._tableAlias]));
669
- return this;
670
- }
671
- count(column, as) {
672
- this._columns.push(this._container.resolve(statements_1.ColumnMethodStatement, [column, enums_1.ColumnMethods.COUNT, as, this._tableAlias]));
673
- return this;
674
- }
675
- sum(column, as) {
676
- this._columns.push(this._container.resolve(statements_1.ColumnMethodStatement, [column, enums_1.ColumnMethods.SUM, as, this._tableAlias]));
677
- return this;
678
- }
679
- avg(column, as) {
680
- this._columns.push(this._container.resolve(statements_1.ColumnMethodStatement, [column, enums_1.ColumnMethods.AVG, as, this._tableAlias]));
681
- return this;
682
- }
683
- distinct() {
684
- if (this._columns.length === 0 || this._columns[0].IsWildcard) {
685
- throw new exceptions_1.InvalidOperation('Cannot force DISTINCT on unknown column');
686
- }
687
- this._distinct = true;
688
- return this;
689
- }
690
- toDB() {
691
- const compiler = this._container.resolve(interfaces_1.SelectQueryCompiler, [this]);
692
- return compiler.compile();
693
- }
694
- then(resolve, reject) {
695
- return super.then((result) => {
696
- if (this._first) {
697
- if (this._fail && result.length === 0) {
698
- reject(new Error('empty results'));
699
- }
700
- else {
701
- resolve(result ? result[0] : null);
702
- }
703
- }
704
- else {
705
- resolve(result);
706
- }
707
- }, reject);
708
- }
709
- async execute() {
710
- return (await this);
711
- }
712
- }
713
- __decorate([
714
- (0, typescript_mix_1.use)(WhereBuilder, LimitBuilder, OrderByBuilder, ColumnsBuilder, JoinBuilder, WithRecursiveBuilder, GroupByBuilder),
715
- __metadata("design:type", Object)
716
- ], SelectQueryBuilder.prototype, "this", void 0);
717
- exports.SelectQueryBuilder = SelectQueryBuilder;
718
- class DeleteQueryBuilder extends QueryBuilder {
719
- constructor(container, driver, model) {
720
- super(container, driver, model);
721
- this._truncate = false;
722
- this._method = enums_1.QueryMethod.DELETE;
723
- this._statements = [];
724
- this._boolean = enums_1.WhereBoolean.AND;
725
- this._limit = {
726
- limit: -1,
727
- offset: -1,
728
- };
729
- this._queryContext = interfaces_1.QueryContext.Delete;
730
- }
731
- get Truncate() {
732
- return this._truncate;
733
- }
734
- toDB() {
735
- return this._container.resolve(interfaces_1.DeleteQueryCompiler, [this]).compile();
736
- }
737
- truncate() {
738
- this._truncate = true;
739
- return this;
740
- }
741
- }
742
- __decorate([
743
- (0, typescript_mix_1.use)(WhereBuilder, LimitBuilder),
744
- __metadata("design:type", Object)
745
- ], DeleteQueryBuilder.prototype, "this", void 0);
746
- exports.DeleteQueryBuilder = DeleteQueryBuilder;
747
- class OnDuplicateQueryBuilder {
748
- constructor(container, insertQueryBuilder, column) {
749
- this._parent = insertQueryBuilder;
750
- this._container = container;
751
- this._column = _.isArray(column) ? column : [column];
752
- }
753
- getColumn() {
754
- return this._column;
755
- }
756
- getColumnsToUpdate() {
757
- return this._columnsToUpdate;
758
- }
759
- getParent() {
760
- return this._parent;
761
- }
762
- update(columns) {
763
- this._columnsToUpdate = columns;
764
- return this;
765
- }
766
- then(resolve, reject) {
767
- return this._parent.then(resolve, reject);
768
- }
769
- toDB() {
770
- return this._parent.toDB();
771
- }
772
- }
773
- exports.OnDuplicateQueryBuilder = OnDuplicateQueryBuilder;
774
- class UpdateQueryBuilder extends QueryBuilder {
775
- constructor(container, driver, model) {
776
- super(container, driver, model);
777
- this._value = {};
778
- this._method = enums_1.QueryMethod.UPDATE;
779
- this._boolean = enums_1.WhereBoolean.AND;
780
- this._statements = [];
781
- this._queryContext = interfaces_1.QueryContext.Update;
782
- }
783
- get Value() {
784
- return this._value;
785
- }
786
- in(name) {
787
- this.setTable(name);
788
- return this;
789
- }
790
- update(value) {
791
- this._value = value;
792
- return this;
793
- }
794
- toDB() {
795
- return this._container.resolve(interfaces_1.UpdateQueryCompiler, [this]).compile();
796
- }
797
- }
798
- __decorate([
799
- (0, typescript_mix_1.use)(WhereBuilder),
800
- __metadata("design:type", Object)
801
- ], UpdateQueryBuilder.prototype, "this", void 0);
802
- exports.UpdateQueryBuilder = UpdateQueryBuilder;
803
- class InsertQueryBuilder extends QueryBuilder {
804
- constructor(container, driver, model) {
805
- super(container, driver, model);
806
- this._method = enums_1.QueryMethod.INSERT;
807
- this._columns = [];
808
- this._values = [];
809
- this._queryContext = interfaces_1.QueryContext.Insert;
810
- }
811
- get Values() {
812
- return this._values;
813
- }
814
- get Ignore() {
815
- return this._ignore;
816
- }
817
- /**
818
- * Sets insert to ignore on duplicate
819
- */
820
- ignore() {
821
- this._ignore = true;
822
- return this;
823
- }
824
- values(data) {
825
- const self = this;
826
- if (Array.isArray(data)) {
827
- this.columns(_.chain(data).map(_.keys).flatten().uniq().value());
828
- data.forEach((d) => {
829
- _addData(d);
830
- });
831
- }
832
- else {
833
- this.columns(_.keysIn(data));
834
- _addData(data);
835
- }
836
- function _addData(d) {
837
- const binding = [];
838
- self._columns
839
- .filter((c) => !(c.Column instanceof RawQuery))
840
- .map((c) => {
841
- return c.Column;
842
- })
843
- .forEach((c) => {
844
- binding.push(d[c]);
845
- });
846
- self._values.push(binding);
847
- }
848
- return this;
849
- }
850
- into(table, schema) {
851
- this.setTable(table, schema);
852
- return this;
853
- }
854
- onDuplicate(column) {
855
- let columnToCheck = column;
856
- if (!columnToCheck && this.this._model) {
857
- columnToCheck = (0, model_1.extractModelDescriptor)(this._model)
858
- .Columns.filter((c) => c.Unique && !c.PrimaryKey)
859
- .map((c) => c.Name);
860
- }
861
- this.DuplicateQueryBuilder = new OnDuplicateQueryBuilder(this._container, this, columnToCheck);
862
- return this.DuplicateQueryBuilder;
863
- }
864
- toDB() {
865
- return this._container.resolve(interfaces_1.InsertQueryCompiler, [this]).compile();
866
- }
867
- }
868
- __decorate([
869
- (0, typescript_mix_1.use)(ColumnsBuilder),
870
- __metadata("design:type", Object)
871
- ], InsertQueryBuilder.prototype, "this", void 0);
872
- exports.InsertQueryBuilder = InsertQueryBuilder;
873
- let IndexQueryBuilder = class IndexQueryBuilder extends Builder {
874
- constructor(container, driver) {
875
- super(container, driver);
876
- this._queryContext = interfaces_1.QueryContext.Schema;
877
- }
878
- name(name) {
879
- this.Name = name;
880
- return this;
881
- }
882
- unique() {
883
- this.Unique = true;
884
- return this;
885
- }
886
- table(name) {
887
- this.Table = name;
888
- return this;
889
- }
890
- columns(colNames) {
891
- this.Columns = colNames;
892
- return this;
893
- }
894
- toDB() {
895
- return this._container.resolve(interfaces_1.IndexQueryCompiler, [this]).compile();
896
- }
897
- };
898
- IndexQueryBuilder = __decorate([
899
- (0, di_1.NewInstance)(),
900
- (0, di_1.Inject)(di_1.Container),
901
- __metadata("design:paramtypes", [di_1.Container, driver_1.OrmDriver])
902
- ], IndexQueryBuilder);
903
- exports.IndexQueryBuilder = IndexQueryBuilder;
904
- let ForeignKeyBuilder = class ForeignKeyBuilder {
905
- constructor() {
906
- this.OnDeleteAction = interfaces_1.ReferentialAction.NoAction;
907
- this.OnUpdateAction = interfaces_1.ReferentialAction.NoAction;
908
- }
909
- /**
910
- *
911
- * Referenced field in child table
912
- *
913
- * @param fkName - name of foreign field in child table
914
- */
915
- foreignKey(fkName) {
916
- this.ForeignKeyField = fkName;
917
- return this;
918
- }
919
- /**
920
- *
921
- * Referenced parent table & key
922
- *
923
- * @param table - parent table
924
- * @param pKey - parant table key field
925
- */
926
- references(table, pKey) {
927
- this.Table = table;
928
- this.PrimaryKey = pKey;
929
- return this;
930
- }
931
- /**
932
- *
933
- * On delete action
934
- *
935
- * @param action - action to take on delete
936
- */
937
- onDelete(action) {
938
- this.OnDeleteAction = action;
939
- return this;
940
- }
941
- /**
942
- *
943
- * On update action
944
- *
945
- * @param action - action to take on update
946
- */
947
- onUpdate(action) {
948
- this.OnUpdateAction = action;
949
- return this;
950
- }
951
- /**
952
- * Shorhand for on update and on delete cascade settings
953
- */
954
- cascade() {
955
- this.OnUpdateAction = interfaces_1.ReferentialAction.Cascade;
956
- this.OnDeleteAction = interfaces_1.ReferentialAction.Cascade;
957
- return this;
958
- }
959
- };
960
- ForeignKeyBuilder = __decorate([
961
- (0, di_1.NewInstance)(),
962
- __metadata("design:paramtypes", [])
963
- ], ForeignKeyBuilder);
964
- exports.ForeignKeyBuilder = ForeignKeyBuilder;
965
- let ColumnQueryBuilder = class ColumnQueryBuilder {
966
- constructor(name, type, ...args) {
967
- this.Name = name;
968
- this.Type = type;
969
- this.Charset = '';
970
- this.Args = [];
971
- this.AutoIncrement = false;
972
- this.NotNull = false;
973
- this.Default = '';
974
- this.Collation = '';
975
- this.Comment = '';
976
- this.Unique = false;
977
- this.Unsigned = false;
978
- this.Args.push(...args);
979
- }
980
- notNull() {
981
- this.NotNull = true;
982
- return this;
983
- }
984
- unique() {
985
- this.Unique = true;
986
- return this;
987
- }
988
- unsigned() {
989
- this.Unsigned = true;
990
- return this;
991
- }
992
- autoIncrement() {
993
- this.AutoIncrement = true;
994
- return this;
995
- }
996
- default(val) {
997
- this.Default = val;
998
- return this;
999
- }
1000
- primaryKey() {
1001
- this.PrimaryKey = true;
1002
- return this;
1003
- }
1004
- comment(comment) {
1005
- this.Comment = comment;
1006
- return this;
1007
- }
1008
- charset(charset) {
1009
- this.Charset = charset;
1010
- return this;
1011
- }
1012
- collation(collation) {
1013
- this.Collation = collation;
1014
- return this;
1015
- }
1016
- };
1017
- ColumnQueryBuilder = __decorate([
1018
- (0, di_1.NewInstance)(),
1019
- __metadata("design:paramtypes", [String, String, Object])
1020
- ], ColumnQueryBuilder);
1021
- exports.ColumnQueryBuilder = ColumnQueryBuilder;
1022
- class TableQueryBuilder extends QueryBuilder {
1023
- constructor(container, driver, name) {
1024
- super(container, driver, null);
1025
- this._charset = '';
1026
- this._comment = '';
1027
- this._columns = [];
1028
- this._foreignKeys = [];
1029
- this.setTable(name);
1030
- this._queryContext = interfaces_1.QueryContext.Schema;
1031
- }
1032
- get Columns() {
1033
- return this._columns;
1034
- }
1035
- get ForeignKeys() {
1036
- return this._foreignKeys;
1037
- }
1038
- increments(name) {
1039
- return this.int(name).autoIncrement().notNull().primaryKey();
1040
- }
1041
- comment(comment) {
1042
- this._comment = comment;
1043
- }
1044
- charset(charset) {
1045
- this._charset = charset;
1046
- }
1047
- foreignKey(foreignKey) {
1048
- const builder = new ForeignKeyBuilder();
1049
- builder.foreignKey(foreignKey);
1050
- this._foreignKeys.push(builder);
1051
- return builder;
1052
- }
1053
- toDB() {
1054
- return this._container.resolve(interfaces_1.TableQueryCompiler, [this]).compile();
1055
- }
1056
- }
1057
- exports.TableQueryBuilder = TableQueryBuilder;
1058
- let SchemaQueryBuilder = class SchemaQueryBuilder {
1059
- constructor(container, driver) {
1060
- this.container = container;
1061
- this.driver = driver;
1062
- }
1063
- createTable(name, callback) {
1064
- const builder = new TableQueryBuilder(this.container, this.driver, name);
1065
- callback.call(this, builder);
1066
- return builder;
1067
- }
1068
- };
1069
- SchemaQueryBuilder = __decorate([
1070
- (0, di_1.NewInstance)(),
1071
- (0, di_1.Inject)(di_1.Container),
1072
- __metadata("design:paramtypes", [di_1.Container, driver_1.OrmDriver])
1073
- ], SchemaQueryBuilder);
1074
- exports.SchemaQueryBuilder = SchemaQueryBuilder;
1075
- Object.values(enums_1.ColumnType).forEach((type) => {
1076
- TableQueryBuilder.prototype[type] = function (name, ...args) {
1077
- const _builder = new ColumnQueryBuilder(name, type, ...args);
1078
- this._columns.push(_builder);
1079
- return _builder;
1080
- };
1081
- });
1082
- //# sourceMappingURL=builders.js.map