@abedmurrar/objection 4.0.0-beta.4 → 4.0.0-beta.6

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 (31) hide show
  1. package/lib/queryBuilder/InternalOptions.js +4 -4
  2. package/lib/queryBuilder/QueryBuilder.js +161 -135
  3. package/lib/queryBuilder/QueryBuilderBase.js +203 -203
  4. package/lib/queryBuilder/QueryBuilderContext.js +6 -6
  5. package/lib/queryBuilder/QueryBuilderContextBase.js +12 -8
  6. package/lib/queryBuilder/QueryBuilderOperationSupport.js +30 -25
  7. package/lib/queryBuilder/QueryBuilderUserContext.js +4 -4
  8. package/lib/queryBuilder/StaticHookArguments.js +11 -11
  9. package/lib/queryBuilder/graph/GraphAction.js +7 -7
  10. package/lib/queryBuilder/graph/GraphData.js +4 -4
  11. package/lib/queryBuilder/graph/GraphFetcher.js +18 -17
  12. package/lib/queryBuilder/graph/GraphNodeDbExistence.js +45 -12
  13. package/lib/queryBuilder/graph/GraphOperation.js +4 -4
  14. package/lib/queryBuilder/graph/GraphOptions.js +5 -5
  15. package/lib/queryBuilder/graph/GraphUpsert.js +36 -36
  16. package/lib/queryBuilder/graph/delete/GraphDelete.js +8 -8
  17. package/lib/queryBuilder/graph/delete/GraphDeleteAction.js +46 -11
  18. package/lib/queryBuilder/graph/insert/GraphInsert.js +12 -12
  19. package/lib/queryBuilder/graph/insert/GraphInsertAction.js +59 -21
  20. package/lib/queryBuilder/graph/insert/JoinRowGraphInsertAction.js +49 -11
  21. package/lib/queryBuilder/graph/patch/GraphPatch.js +8 -8
  22. package/lib/queryBuilder/graph/patch/GraphPatchAction.js +52 -19
  23. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsert.js +8 -8
  24. package/lib/queryBuilder/graph/recursiveUpsert/GraphRecursiveUpsertAction.js +54 -18
  25. package/lib/queryBuilder/operations/types.js +2 -2
  26. package/lib/queryBuilder/transformations/CompositeQueryTransformation.js +6 -6
  27. package/lib/queryBuilder/transformations/QueryTransformation.js +4 -4
  28. package/lib/queryBuilder/transformations/WrapMysqlModifySubqueryTransformation.js +11 -8
  29. package/lib/queryBuilder/transformations/index.js +8 -8
  30. package/lib/queryBuilder/whereScopes.js +15 -21
  31. package/package.json +1 -1
@@ -1,45 +1,47 @@
1
- 'use strict';
2
- const { withWhereScope } = require('./whereScopes');
3
- const { wrapError } = require('db-errors');
4
- const { raw } = require('./RawBuilder');
5
- const { createModifier } = require('../utils/createModifier');
6
- const { ValidationErrorType } = require('../model/ValidationError');
7
- const { isObject, isString, isFunction, last, flatten } = require('../utils/objectUtils');
8
- const { RelationExpression, DuplicateRelationError } = require('./RelationExpression');
9
- const { assertIdNotUndefined } = require('../utils/assert');
10
- const { Selection } = require('./operations/select/Selection');
11
- const { QueryBuilderContext } = require('./QueryBuilderContext');
12
- const { QueryBuilderBase } = require('./QueryBuilderBase');
13
- const { FindOperation } = require('./operations/FindOperation');
14
- const { DeleteOperation } = require('./operations/DeleteOperation');
15
- const { UpdateOperation } = require('./operations/UpdateOperation');
16
- const { InsertOperation } = require('./operations/InsertOperation');
17
- const { RelateOperation } = require('./operations/RelateOperation');
18
- const { UnrelateOperation } = require('./operations/UnrelateOperation');
19
- const { JoinEagerOperation } = require('./operations/eager/JoinEagerOperation');
20
- const { NaiveEagerOperation } = require('./operations/eager/NaiveEagerOperation');
21
- const { WhereInEagerOperation } = require('./operations/eager/WhereInEagerOperation');
22
- const { InsertGraphAndFetchOperation } = require('./operations/InsertGraphAndFetchOperation');
23
- const { UpsertGraphAndFetchOperation } = require('./operations/UpsertGraphAndFetchOperation');
24
- const { InsertAndFetchOperation } = require('./operations/InsertAndFetchOperation');
25
- const { UpdateAndFetchOperation } = require('./operations/UpdateAndFetchOperation');
26
- const { JoinRelatedOperation } = require('./operations/JoinRelatedOperation');
27
- const { OnBuildKnexOperation } = require('./operations/OnBuildKnexOperation');
28
- const { InsertGraphOperation } = require('./operations/InsertGraphOperation');
29
- const { UpsertGraphOperation } = require('./operations/UpsertGraphOperation');
30
- const { RunBeforeOperation } = require('./operations/RunBeforeOperation');
31
- const { RunAfterOperation } = require('./operations/RunAfterOperation');
32
- const { FindByIdOperation } = require('./operations/FindByIdOperation');
33
- const { FindByIdsOperation } = require('./operations/FindByIdsOperation');
34
- const { OnBuildOperation } = require('./operations/OnBuildOperation');
35
- const { OnErrorOperation } = require('./operations/OnErrorOperation');
36
- const { SelectOperation } = require('./operations/select/SelectOperation');
37
- const { EagerOperation } = require('./operations/eager/EagerOperation');
38
- const { RangeOperation } = require('./operations/RangeOperation');
39
- const { FirstOperation } = require('./operations/FirstOperation');
40
- const { FromOperation } = require('./operations/FromOperation');
41
- const { KnexOperation } = require('./operations/KnexOperation');
42
- class QueryBuilder extends QueryBuilderBase {
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.QueryBuilder = void 0;
4
+ const whereScopes_1 = require("./whereScopes");
5
+ const db_errors_1 = require("db-errors");
6
+ const RawBuilder_1 = require("./RawBuilder");
7
+ const createModifier_1 = require("../utils/createModifier");
8
+ const ValidationError_1 = require("../model/ValidationError");
9
+ const objectUtils_1 = require("../utils/objectUtils");
10
+ const RelationExpression_1 = require("./RelationExpression");
11
+ const assert_1 = require("../utils/assert");
12
+ const Selection_1 = require("./operations/select/Selection");
13
+ const QueryBuilderContext_1 = require("./QueryBuilderContext");
14
+ const QueryBuilderBase_1 = require("./QueryBuilderBase");
15
+ const FindOperation_1 = require("./operations/FindOperation");
16
+ const DeleteOperation_1 = require("./operations/DeleteOperation");
17
+ const UpdateOperation_1 = require("./operations/UpdateOperation");
18
+ const InsertOperation_1 = require("./operations/InsertOperation");
19
+ const RelateOperation_1 = require("./operations/RelateOperation");
20
+ const UnrelateOperation_1 = require("./operations/UnrelateOperation");
21
+ const JoinEagerOperation_1 = require("./operations/eager/JoinEagerOperation");
22
+ const NaiveEagerOperation_1 = require("./operations/eager/NaiveEagerOperation");
23
+ const WhereInEagerOperation_1 = require("./operations/eager/WhereInEagerOperation");
24
+ const InsertGraphAndFetchOperation_1 = require("./operations/InsertGraphAndFetchOperation");
25
+ const UpsertGraphAndFetchOperation_1 = require("./operations/UpsertGraphAndFetchOperation");
26
+ const InsertAndFetchOperation_1 = require("./operations/InsertAndFetchOperation");
27
+ const UpdateAndFetchOperation_1 = require("./operations/UpdateAndFetchOperation");
28
+ const JoinRelatedOperation_1 = require("./operations/JoinRelatedOperation");
29
+ const OnBuildKnexOperation_1 = require("./operations/OnBuildKnexOperation");
30
+ const InsertGraphOperation_1 = require("./operations/InsertGraphOperation");
31
+ const UpsertGraphOperation_1 = require("./operations/UpsertGraphOperation");
32
+ const RunBeforeOperation_1 = require("./operations/RunBeforeOperation");
33
+ const RunAfterOperation_1 = require("./operations/RunAfterOperation");
34
+ const FindByIdOperation_1 = require("./operations/FindByIdOperation");
35
+ const FindByIdsOperation_1 = require("./operations/FindByIdsOperation");
36
+ const OnBuildOperation_1 = require("./operations/OnBuildOperation");
37
+ const OnErrorOperation_1 = require("./operations/OnErrorOperation");
38
+ const SelectOperation_1 = require("./operations/select/SelectOperation");
39
+ const EagerOperation_1 = require("./operations/eager/EagerOperation");
40
+ const RangeOperation_1 = require("./operations/RangeOperation");
41
+ const FirstOperation_1 = require("./operations/FirstOperation");
42
+ const FromOperation_1 = require("./operations/FromOperation");
43
+ const KnexOperation_1 = require("./operations/KnexOperation");
44
+ class QueryBuilder extends QueryBuilderBase_1.QueryBuilderBase {
43
45
  static init(self, modelClass) {
44
46
  super.init(self, modelClass);
45
47
  self._resultModelClass = null;
@@ -60,10 +62,10 @@ class QueryBuilder extends QueryBuilderBase {
60
62
  self._deleteOperationFactory = deleteOperationFactory;
61
63
  }
62
64
  static get QueryBuilderContext() {
63
- return QueryBuilderContext;
65
+ return QueryBuilderContext_1.QueryBuilderContext;
64
66
  }
65
67
  static parseRelationExpression(expr) {
66
- return RelationExpression.create(expr).toPojo();
68
+ return RelationExpression_1.RelationExpression.create(expr).toPojo();
67
69
  }
68
70
  tableName(newTableName) {
69
71
  return this.tableNameFor(this.modelClass().getTableName(), newTableName);
@@ -100,7 +102,7 @@ class QueryBuilder extends QueryBuilderBase {
100
102
  if (!modifier) {
101
103
  return this;
102
104
  }
103
- modifier = createModifier({
105
+ modifier = (0, createModifier_1.createModifier)({
104
106
  modifier,
105
107
  modelClass: this.modelClass(),
106
108
  modifiers: this._modifiers,
@@ -165,7 +167,7 @@ class QueryBuilder extends QueryBuilderBase {
165
167
  return this;
166
168
  }
167
169
  allowGraph(exp) {
168
- const currentExpr = this._allowedGraphExpression || RelationExpression.create();
170
+ const currentExpr = this._allowedGraphExpression || RelationExpression_1.RelationExpression.create();
169
171
  this._allowedGraphExpression = currentExpr.merge(parseRelationExpression(this.modelClass(), exp));
170
172
  checkEager(this);
171
173
  return this;
@@ -174,7 +176,7 @@ class QueryBuilder extends QueryBuilderBase {
174
176
  return this._allowedGraphExpression;
175
177
  }
176
178
  graphExpressionObject() {
177
- const eagerOp = this.findOperation(EagerOperation);
179
+ const eagerOp = this.findOperation(EagerOperation_1.EagerOperation);
178
180
  if (eagerOp && !eagerOp.expression.isEmpty) {
179
181
  return eagerOp.expression.toPojo();
180
182
  }
@@ -183,7 +185,7 @@ class QueryBuilder extends QueryBuilderBase {
183
185
  }
184
186
  }
185
187
  graphModifiersAtPath() {
186
- const eagerOp = this.findOperation(EagerOperation);
188
+ const eagerOp = this.findOperation(EagerOperation_1.EagerOperation);
187
189
  if (eagerOp && !eagerOp.expression.isEmpty) {
188
190
  return eagerOp.modifiersAtPath.map((it) => Object.assign({}, it));
189
191
  }
@@ -224,29 +226,29 @@ class QueryBuilder extends QueryBuilderBase {
224
226
  this.isUnrelate());
225
227
  }
226
228
  isInsert() {
227
- return this.has(InsertOperation);
229
+ return this.has(InsertOperation_1.InsertOperation);
228
230
  }
229
231
  isUpdate() {
230
- return this.has(UpdateOperation);
232
+ return this.has(UpdateOperation_1.UpdateOperation);
231
233
  }
232
234
  isDelete() {
233
- return this.has(DeleteOperation);
235
+ return this.has(DeleteOperation_1.DeleteOperation);
234
236
  }
235
237
  isRelate() {
236
- return this.has(RelateOperation);
238
+ return this.has(RelateOperation_1.RelateOperation);
237
239
  }
238
240
  isUnrelate() {
239
- return this.has(UnrelateOperation);
241
+ return this.has(UnrelateOperation_1.UnrelateOperation);
240
242
  }
241
243
  hasWheres() {
242
244
  const queryWithoutGraph = this.clone().clearWithGraph();
243
- return prebuildQuery(queryWithoutGraph).has(QueryBuilderBase.WhereSelector);
245
+ return prebuildQuery(queryWithoutGraph).has(QueryBuilderBase_1.QueryBuilderBase.WhereSelector);
244
246
  }
245
247
  hasSelects() {
246
- return this.has(QueryBuilderBase.SelectSelector);
248
+ return this.has(QueryBuilderBase_1.QueryBuilderBase.SelectSelector);
247
249
  }
248
250
  hasWithGraph() {
249
- const eagerOp = this.findOperation(EagerOperation);
251
+ const eagerOp = this.findOperation(EagerOperation_1.EagerOperation);
250
252
  return !!eagerOp && !eagerOp.expression.isEmpty;
251
253
  }
252
254
  isSelectAll() {
@@ -256,7 +258,9 @@ class QueryBuilder extends QueryBuilderBase {
256
258
  const tableRef = this.tableRef();
257
259
  const tableName = this.tableName();
258
260
  return this.everyOperation((op) => {
259
- if (op.constructor === SelectOperation) {
261
+ // TypeScript narrows `op` to `never` when its constructor is compared with
262
+ // a subclass, hence `as unknown` and the casts in the branches.
263
+ if (op.constructor === SelectOperation_1.SelectOperation) {
260
264
  // SelectOperations with zero selections are the ones that only have
261
265
  // raw items or other non-trivial selections.
262
266
  return (op.selections.length > 0 &&
@@ -264,10 +268,10 @@ class QueryBuilder extends QueryBuilderBase {
264
268
  return (!select.table || select.table === tableRef) && select.column === '*';
265
269
  }));
266
270
  }
267
- else if (op.constructor === FromOperation) {
271
+ else if (op.constructor === FromOperation_1.FromOperation) {
268
272
  return op.table === tableName;
269
273
  }
270
- else if (op.name === 'as' || op.is(FindOperation) || op.is(OnErrorOperation)) {
274
+ else if (op.name === 'as' || op.is(FindOperation_1.FindOperation) || op.is(OnErrorOperation_1.OnErrorOperation)) {
271
275
  return true;
272
276
  }
273
277
  else {
@@ -306,11 +310,11 @@ class QueryBuilder extends QueryBuilderBase {
306
310
  return builder;
307
311
  }
308
312
  clearWithGraph() {
309
- this.clear(EagerOperation);
313
+ this.clear(EagerOperation_1.EagerOperation);
310
314
  return this;
311
315
  }
312
316
  clearWithGraphFetched() {
313
- this.clear(WhereInEagerOperation);
317
+ this.clear(WhereInEagerOperation_1.WhereInEagerOperation);
314
318
  return this;
315
319
  }
316
320
  clearAllowGraph() {
@@ -348,7 +352,10 @@ class QueryBuilder extends QueryBuilderBase {
348
352
  async resultSize() {
349
353
  const knex = this.knex();
350
354
  const builder = this.clone().clear(/orderBy|offset|limit/);
351
- const countQuery = knex.count('* as count').from((knexBuilder) => {
355
+ // knex's types don't accept this `from()` callback or `debug()` without an argument.
356
+ const countQuery = knex
357
+ .count('* as count')
358
+ .from((knexBuilder) => {
352
359
  builder.toKnexQuery(knexBuilder).as('temp');
353
360
  });
354
361
  if (this.internalOptions().debug) {
@@ -361,6 +368,7 @@ class QueryBuilder extends QueryBuilderBase {
361
368
  const prebuiltQuery = prebuildQuery(this.clone());
362
369
  return buildKnexQuery(prebuiltQuery, knexBuilder);
363
370
  }
371
+ // Resolves to whatever the operations produce: models, rows, counts...
364
372
  async execute() {
365
373
  // Take a clone so that we don't modify this instance during execution.
366
374
  const builder = this.clone();
@@ -390,7 +398,8 @@ class QueryBuilder extends QueryBuilderBase {
390
398
  let noSelectStatements = true;
391
399
  let selectionInstance = null;
392
400
  this.forEachOperation(true, (op) => {
393
- if (op.constructor === SelectOperation) {
401
+ // See `isSelectAll()` for the casts.
402
+ if (op.constructor === SelectOperation_1.SelectOperation) {
394
403
  selectionInstance = op.findSelection(this, selection);
395
404
  noSelectStatements = false;
396
405
  if (selectionInstance) {
@@ -402,8 +411,8 @@ class QueryBuilder extends QueryBuilderBase {
402
411
  return selectionInstance;
403
412
  }
404
413
  if (noSelectStatements && !explicit) {
405
- const selectAll = new Selection(this.tableRef(), '*');
406
- if (Selection.doesSelect(this, selectAll, selection)) {
414
+ const selectAll = new Selection_1.Selection(this.tableRef(), '*');
415
+ if (Selection_1.Selection.doesSelect(this, selectAll, selection)) {
407
416
  return selectAll;
408
417
  }
409
418
  else {
@@ -417,7 +426,8 @@ class QueryBuilder extends QueryBuilderBase {
417
426
  findAllSelections() {
418
427
  let allSelections = [];
419
428
  this.forEachOperation(true, (op) => {
420
- if (op.constructor === SelectOperation) {
429
+ // See `isSelectAll()` for the casts.
430
+ if (op.constructor === SelectOperation_1.SelectOperation) {
421
431
  allSelections = allSelections.concat(op.selections);
422
432
  }
423
433
  });
@@ -427,7 +437,7 @@ class QueryBuilder extends QueryBuilderBase {
427
437
  return this.findSelection(selection, explicit) !== null;
428
438
  }
429
439
  hasSelectionAs(selection, alias, explicit) {
430
- selection = Selection.create(selection);
440
+ selection = Selection_1.Selection.create(selection);
431
441
  const foundSelection = this.findSelection(selection, explicit);
432
442
  if (foundSelection === null) {
433
443
  return false;
@@ -459,7 +469,8 @@ class QueryBuilder extends QueryBuilderBase {
459
469
  table = table || this.tableName();
460
470
  const knex = this.knex();
461
471
  const tableParts = table.split('.');
462
- const columnInfoQuery = knex(last(tableParts)).columnInfo();
472
+ // knex's types make `columnInfo()` a promise, but it's the query builder.
473
+ const columnInfoQuery = knex((0, objectUtils_1.last)(tableParts)).columnInfo();
463
474
  const schema = this.internalOptions().schema;
464
475
  if (schema) {
465
476
  columnInfoQuery.withSchema(schema);
@@ -478,7 +489,7 @@ class QueryBuilder extends QueryBuilderBase {
478
489
  if (!builder.has(/withSchema/)) {
479
490
  // Need to push this operation to the front because knex doesn't use the
480
491
  // schema for operations called before `withSchema`.
481
- builder.addOperationToFront(new KnexOperation('withSchema'), [schema]);
492
+ builder.addOperationToFront(new KnexOperation_1.KnexOperation('withSchema'), [schema]);
482
493
  }
483
494
  });
484
495
  return this;
@@ -486,7 +497,7 @@ class QueryBuilder extends QueryBuilderBase {
486
497
  debug /* istanbul ignore next */() {
487
498
  this.internalOptions().debug = true;
488
499
  this.internalContext().onBuild.push((builder) => {
489
- builder.addOperation(new KnexOperation('debug'), []);
500
+ builder.addOperation(new KnexOperation_1.KnexOperation('debug'), []);
490
501
  });
491
502
  return this;
492
503
  }
@@ -499,7 +510,7 @@ class QueryBuilder extends QueryBuilderBase {
499
510
  insertAndFetch(modelsOrObjects) {
500
511
  return writeOperation(this, () => {
501
512
  const insertOperation = this._insertOperationFactory(this);
502
- const insertAndFetchOperation = new InsertAndFetchOperation('insertAndFetch', {
513
+ const insertAndFetchOperation = new InsertAndFetchOperation_1.InsertAndFetchOperation('insertAndFetch', {
503
514
  delegate: insertOperation,
504
515
  });
505
516
  this.addOperation(insertAndFetchOperation, [modelsOrObjects]);
@@ -508,7 +519,7 @@ class QueryBuilder extends QueryBuilderBase {
508
519
  insertGraph(modelsOrObjects, opt) {
509
520
  return writeOperation(this, () => {
510
521
  const insertOperation = this._insertOperationFactory(this);
511
- const insertGraphOperation = new InsertGraphOperation('insertGraph', {
522
+ const insertGraphOperation = new InsertGraphOperation_1.InsertGraphOperation('insertGraph', {
512
523
  delegate: insertOperation,
513
524
  opt,
514
525
  });
@@ -518,11 +529,11 @@ class QueryBuilder extends QueryBuilderBase {
518
529
  insertGraphAndFetch(modelsOrObjects, opt) {
519
530
  return writeOperation(this, () => {
520
531
  const insertOperation = this._insertOperationFactory(this);
521
- const insertGraphOperation = new InsertGraphOperation('insertGraph', {
532
+ const insertGraphOperation = new InsertGraphOperation_1.InsertGraphOperation('insertGraph', {
522
533
  delegate: insertOperation,
523
534
  opt,
524
535
  });
525
- const insertGraphAndFetchOperation = new InsertGraphAndFetchOperation('insertGraphAndFetch', {
536
+ const insertGraphAndFetchOperation = new InsertGraphAndFetchOperation_1.InsertGraphAndFetchOperation('insertGraphAndFetch', {
526
537
  delegate: insertGraphOperation,
527
538
  });
528
539
  return this.addOperation(insertGraphAndFetchOperation, [modelsOrObjects]);
@@ -537,22 +548,26 @@ class QueryBuilder extends QueryBuilderBase {
537
548
  updateAndFetch(modelOrObject) {
538
549
  return writeOperation(this, () => {
539
550
  const updateOperation = this._updateOperationFactory(this);
551
+ // Only instance operations have `instance`, and this throws for the others.
540
552
  if (!(updateOperation.instance instanceof this.modelClass())) {
541
553
  throw new Error('updateAndFetch can only be called for instance operations');
542
554
  }
543
- const updateAndFetch = new UpdateAndFetchOperation('updateAndFetch', {
555
+ const updateAndFetch = new UpdateAndFetchOperation_1.UpdateAndFetchOperation('updateAndFetch', {
544
556
  delegate: updateOperation,
545
557
  });
546
558
  // patchOperation is an instance update operation that already adds the
547
559
  // required "where id = $" clause.
548
560
  updateAndFetch.skipIdWhere = true;
549
- this.addOperation(updateAndFetch, [updateOperation.instance.$id(), modelOrObject]);
561
+ this.addOperation(updateAndFetch, [
562
+ updateOperation.instance.$id(),
563
+ modelOrObject,
564
+ ]);
550
565
  });
551
566
  }
552
567
  updateAndFetchById(id, modelOrObject) {
553
568
  return writeOperation(this, () => {
554
569
  const updateOperation = this._updateOperationFactory(this);
555
- const updateAndFetch = new UpdateAndFetchOperation('updateAndFetch', {
570
+ const updateAndFetch = new UpdateAndFetchOperation_1.UpdateAndFetchOperation('updateAndFetch', {
556
571
  delegate: updateOperation,
557
572
  });
558
573
  this.addOperation(updateAndFetch, [id, modelOrObject]);
@@ -560,7 +575,7 @@ class QueryBuilder extends QueryBuilderBase {
560
575
  }
561
576
  upsertGraph(modelsOrObjects, upsertOptions) {
562
577
  return writeOperation(this, () => {
563
- const upsertGraphOperation = new UpsertGraphOperation('upsertGraph', {
578
+ const upsertGraphOperation = new UpsertGraphOperation_1.UpsertGraphOperation('upsertGraph', {
564
579
  upsertOptions,
565
580
  });
566
581
  this.addOperation(upsertGraphOperation, [modelsOrObjects]);
@@ -568,10 +583,10 @@ class QueryBuilder extends QueryBuilderBase {
568
583
  }
569
584
  upsertGraphAndFetch(modelsOrObjects, upsertOptions) {
570
585
  return writeOperation(this, () => {
571
- const upsertGraphOperation = new UpsertGraphOperation('upsertGraph', {
586
+ const upsertGraphOperation = new UpsertGraphOperation_1.UpsertGraphOperation('upsertGraph', {
572
587
  upsertOptions,
573
588
  });
574
- const upsertGraphAndFetchOperation = new UpsertGraphAndFetchOperation('upsertGraphAndFetch', {
589
+ const upsertGraphAndFetchOperation = new UpsertGraphAndFetchOperation_1.UpsertGraphAndFetchOperation('upsertGraphAndFetch', {
575
590
  delegate: upsertGraphOperation,
576
591
  });
577
592
  return this.addOperation(upsertGraphAndFetchOperation, [modelsOrObjects]);
@@ -586,22 +601,26 @@ class QueryBuilder extends QueryBuilderBase {
586
601
  patchAndFetch(modelOrObject) {
587
602
  return writeOperation(this, () => {
588
603
  const patchOperation = this._patchOperationFactory(this);
604
+ // Only instance operations have `instance`, and this throws for the others.
589
605
  if (!(patchOperation.instance instanceof this.modelClass())) {
590
606
  throw new Error('patchAndFetch can only be called for instance operations');
591
607
  }
592
- const patchAndFetch = new UpdateAndFetchOperation('patchAndFetch', {
608
+ const patchAndFetch = new UpdateAndFetchOperation_1.UpdateAndFetchOperation('patchAndFetch', {
593
609
  delegate: patchOperation,
594
610
  });
595
611
  // patchOperation is an instance update operation that already adds the
596
612
  // required "where id = $" clause.
597
613
  patchAndFetch.skipIdWhere = true;
598
- this.addOperation(patchAndFetch, [patchOperation.instance.$id(), modelOrObject]);
614
+ this.addOperation(patchAndFetch, [
615
+ patchOperation.instance.$id(),
616
+ modelOrObject,
617
+ ]);
599
618
  });
600
619
  }
601
620
  patchAndFetchById(id, modelOrObject) {
602
621
  return writeOperation(this, () => {
603
622
  const patchOperation = this._patchOperationFactory(this);
604
- const patchAndFetch = new UpdateAndFetchOperation('patchAndFetch', {
623
+ const patchAndFetch = new UpdateAndFetchOperation_1.UpdateAndFetchOperation('patchAndFetch', {
605
624
  delegate: patchOperation,
606
625
  });
607
626
  this.addOperation(patchAndFetch, [id, modelOrObject]);
@@ -637,23 +656,23 @@ class QueryBuilder extends QueryBuilderBase {
637
656
  increment(propertyName, howMuch) {
638
657
  const columnName = this.modelClass().propertyNameToColumnName(propertyName);
639
658
  return this.patch({
640
- [columnName]: raw('?? + ?', [columnName, howMuch]),
659
+ [columnName]: (0, RawBuilder_1.raw)('?? + ?', [columnName, howMuch]),
641
660
  });
642
661
  }
643
662
  decrement(propertyName, howMuch) {
644
663
  const columnName = this.modelClass().propertyNameToColumnName(propertyName);
645
664
  return this.patch({
646
- [columnName]: raw('?? - ?', [columnName, howMuch]),
665
+ [columnName]: (0, RawBuilder_1.raw)('?? - ?', [columnName, howMuch]),
647
666
  });
648
667
  }
649
668
  findOne(...args) {
650
669
  return this.where.apply(this, args).first();
651
670
  }
652
671
  range(...args) {
653
- return this.clear(RangeOperation).addOperation(new RangeOperation('range'), args);
672
+ return this.clear(RangeOperation_1.RangeOperation).addOperation(new RangeOperation_1.RangeOperation('range'), args);
654
673
  }
655
674
  first(...args) {
656
- return this.addOperation(new FirstOperation('first'), args);
675
+ return this.addOperation(new FirstOperation_1.FirstOperation('first'), args);
657
676
  }
658
677
  joinRelated(expression, options) {
659
678
  ensureJoinRelatedOperation(this, 'innerJoin').addCall({
@@ -712,43 +731,43 @@ class QueryBuilder extends QueryBuilderBase {
712
731
  return this;
713
732
  }
714
733
  deleteById(id) {
715
- return withWhereScope(this, () => this.findById(id))
734
+ return (0, whereScopes_1.withWhereScope)(this, () => this.findById(id))
716
735
  .delete()
717
736
  .runBefore((_, builder) => {
718
737
  if (!builder.internalOptions().skipUndefined) {
719
- assertIdNotUndefined(id, `undefined was passed to deleteById`);
738
+ (0, assert_1.assertIdNotUndefined)(id, `undefined was passed to deleteById`);
720
739
  }
721
740
  });
722
741
  }
723
742
  findById(...args) {
724
- return this.addOperation(new FindByIdOperation('findById'), args).first();
743
+ return this.addOperation(new FindByIdOperation_1.FindByIdOperation('findById'), args).first();
725
744
  }
726
745
  findByIds(...args) {
727
- return this.addOperation(new FindByIdsOperation('findByIds'), args);
746
+ return this.addOperation(new FindByIdsOperation_1.FindByIdsOperation('findByIds'), args);
728
747
  }
729
748
  runBefore(...args) {
730
- return this.addOperation(new RunBeforeOperation('runBefore'), args);
749
+ return this.addOperation(new RunBeforeOperation_1.RunBeforeOperation('runBefore'), args);
731
750
  }
732
751
  onBuild(...args) {
733
- return this.addOperation(new OnBuildOperation('onBuild'), args);
752
+ return this.addOperation(new OnBuildOperation_1.OnBuildOperation('onBuild'), args);
734
753
  }
735
754
  onBuildKnex(...args) {
736
- return this.addOperation(new OnBuildKnexOperation('onBuildKnex'), args);
755
+ return this.addOperation(new OnBuildKnexOperation_1.OnBuildKnexOperation('onBuildKnex'), args);
737
756
  }
738
757
  runAfter(...args) {
739
- return this.addOperation(new RunAfterOperation('runAfter'), args);
758
+ return this.addOperation(new RunAfterOperation_1.RunAfterOperation('runAfter'), args);
740
759
  }
741
760
  onError(...args) {
742
- return this.addOperation(new OnErrorOperation('onError'), args);
761
+ return this.addOperation(new OnErrorOperation_1.OnErrorOperation('onError'), args);
743
762
  }
744
763
  from(...args) {
745
- return this.addOperation(new FromOperation('from'), args);
764
+ return this.addOperation(new FromOperation_1.FromOperation('from'), args);
746
765
  }
747
766
  updateFrom(...args) {
748
- return this.addOperation(new KnexOperation('updateFrom'), args);
767
+ return this.addOperation(new KnexOperation_1.KnexOperation('updateFrom'), args);
749
768
  }
750
769
  table(...args) {
751
- return this.addOperation(new FromOperation('table'), args);
770
+ return this.addOperation(new FromOperation_1.FromOperation('table'), args);
752
771
  }
753
772
  for(relatedQueryFor) {
754
773
  if (arguments.length === 0) {
@@ -770,6 +789,7 @@ class QueryBuilder extends QueryBuilderBase {
770
789
  return this;
771
790
  }
772
791
  }
792
+ exports.QueryBuilder = QueryBuilder;
773
793
  Object.defineProperties(QueryBuilder.prototype, {
774
794
  isObjectionQueryBuilder: {
775
795
  enumerable: false,
@@ -778,16 +798,17 @@ Object.defineProperties(QueryBuilder.prototype, {
778
798
  },
779
799
  });
780
800
  function isWriteOperation(op) {
781
- return (op.is(InsertOperation) ||
782
- op.is(UpdateOperation) ||
783
- op.is(DeleteOperation) ||
784
- op.is(RelateOperation) ||
785
- op.is(UnrelateOperation));
801
+ return (op.is(InsertOperation_1.InsertOperation) ||
802
+ op.is(UpdateOperation_1.UpdateOperation) ||
803
+ op.is(DeleteOperation_1.DeleteOperation) ||
804
+ op.is(RelateOperation_1.RelateOperation) ||
805
+ op.is(UnrelateOperation_1.UnrelateOperation));
786
806
  }
807
+ // Returns the eager operation, whose class depends on the algorithm.
787
808
  function ensureEagerOperation(builder, algorithm = null) {
788
809
  const modelClass = builder.modelClass();
789
810
  const defaultGraphOptions = modelClass.getDefaultGraphOptions();
790
- const eagerOp = builder.findOperation(EagerOperation);
811
+ const eagerOp = builder.findOperation(EagerOperation_1.EagerOperation);
791
812
  if (algorithm) {
792
813
  const EagerOperationClass = getOperationClassForEagerAlgorithm(builder, algorithm);
793
814
  if (eagerOp instanceof EagerOperationClass) {
@@ -800,7 +821,7 @@ function ensureEagerOperation(builder, algorithm = null) {
800
821
  if (eagerOp) {
801
822
  newEagerOp.cloneFrom(eagerOp);
802
823
  }
803
- builder.clear(EagerOperation);
824
+ builder.clear(EagerOperation_1.EagerOperation);
804
825
  builder.addOperation(newEagerOp);
805
826
  return newEagerOp;
806
827
  }
@@ -830,36 +851,36 @@ function getNaiveEagerAlgorithm(builder) {
830
851
  }
831
852
  function getOperationClassForEagerAlgorithm(builder, algorithm) {
832
853
  if (algorithm === getJoinEagerAlgorithm(builder)) {
833
- return JoinEagerOperation;
854
+ return JoinEagerOperation_1.JoinEagerOperation;
834
855
  }
835
856
  else if (algorithm === getNaiveEagerAlgorithm(builder)) {
836
- return NaiveEagerOperation;
857
+ return NaiveEagerOperation_1.NaiveEagerOperation;
837
858
  }
838
859
  else {
839
- return WhereInEagerOperation;
860
+ return WhereInEagerOperation_1.WhereInEagerOperation;
840
861
  }
841
862
  }
842
863
  function parseRelationExpression(modelClass, exp) {
843
864
  try {
844
- return RelationExpression.create(exp);
865
+ return RelationExpression_1.RelationExpression.create(exp);
845
866
  }
846
867
  catch (err) {
847
- if (err instanceof DuplicateRelationError) {
868
+ if (err instanceof RelationExpression_1.DuplicateRelationError) {
848
869
  throw modelClass.createValidationError({
849
- type: ValidationErrorType.RelationExpression,
870
+ type: ValidationError_1.ValidationErrorType.RelationExpression,
850
871
  message: `Duplicate relation name "${err.relationName}" in relation expression "${exp}". For example, use "${err.relationName}.[foo, bar]" instead of "[${err.relationName}.foo, ${err.relationName}.bar]".`,
851
872
  });
852
873
  }
853
874
  else {
854
875
  throw modelClass.createValidationError({
855
- type: ValidationErrorType.RelationExpression,
876
+ type: ValidationError_1.ValidationErrorType.RelationExpression,
856
877
  message: `Invalid relation expression "${exp}"`,
857
878
  });
858
879
  }
859
880
  }
860
881
  }
861
882
  function checkEager(builder) {
862
- const eagerOp = builder.findOperation(EagerOperation);
883
+ const eagerOp = builder.findOperation(EagerOperation_1.EagerOperation);
863
884
  if (!eagerOp) {
864
885
  return;
865
886
  }
@@ -868,7 +889,7 @@ function checkEager(builder) {
868
889
  if (expr.numChildren > 0 && allowedExpr && !allowedExpr.isSubExpression(expr)) {
869
890
  const modelClass = builder.modelClass();
870
891
  builder.reject(modelClass.createValidationError({
871
- type: ValidationErrorType.UnallowedRelation,
892
+ type: ValidationError_1.ValidationErrorType.UnallowedRelation,
872
893
  message: 'eager expression not allowed',
873
894
  }));
874
895
  }
@@ -941,7 +962,7 @@ function handleExecuteError(builder, err) {
941
962
  if (err instanceof ReturnImmediatelyException) {
942
963
  return Promise.resolve(err.value);
943
964
  }
944
- let promise = Promise.reject(wrapError(err));
965
+ let promise = Promise.reject((0, db_errors_1.wrapError)(err));
945
966
  builder.forEachOperation(true, (op) => {
946
967
  if (op.hasOnError()) {
947
968
  promise = promise.catch((err) => builder.callAsyncOperationMethod(op, 'onError', [builder, err]));
@@ -966,9 +987,10 @@ function chainOperationHooks(promise, builder, hookName) {
966
987
  }
967
988
  function ensureJoinRelatedOperation(builder, joinOperation) {
968
989
  const opName = joinOperation + 'Relation';
990
+ // The operation named `opName` is the join related operation added below.
969
991
  let op = builder.findOperation(opName);
970
992
  if (!op) {
971
- op = new JoinRelatedOperation(opName, { joinOperation });
993
+ op = new JoinRelatedOperation_1.JoinRelatedOperation(opName, { joinOperation });
972
994
  builder.addOperation(op);
973
995
  }
974
996
  return op;
@@ -979,6 +1001,7 @@ function prebuildQuery(builder) {
979
1001
  builder = callOnBuildHooks(builder);
980
1002
  const queryExecutorOperation = findQueryExecutorOperation(builder);
981
1003
  if (queryExecutorOperation) {
1004
+ // The base hook returns nothing, but a query executor returns the query to run.
982
1005
  return prebuildQuery(queryExecutorOperation.queryExecutor(builder));
983
1006
  }
984
1007
  else {
@@ -997,14 +1020,14 @@ function addImplicitOperations(builder) {
997
1020
  return builder;
998
1021
  }
999
1022
  function addFindOperation(builder) {
1000
- if (!builder.has(FindOperation)) {
1023
+ if (!builder.has(FindOperation_1.FindOperation)) {
1001
1024
  const operation = builder._findOperationFactory(builder);
1002
1025
  builder.addOperationToFront(operation, []);
1003
1026
  }
1004
1027
  }
1005
1028
  function moveEagerOperationToEnd(builder) {
1006
- const eagerOp = builder.findOperation(EagerOperation);
1007
- builder.clear(EagerOperation);
1029
+ const eagerOp = builder.findOperation(EagerOperation_1.EagerOperation);
1030
+ builder.clear(EagerOperation_1.EagerOperation);
1008
1031
  builder.addOperation(eagerOp);
1009
1032
  }
1010
1033
  function callOnBuildHooks(builder) {
@@ -1013,8 +1036,9 @@ function callOnBuildHooks(builder) {
1013
1036
  builder.executeOnBuild();
1014
1037
  return builder;
1015
1038
  }
1039
+ // `func` is a function or an array of functions and arrays.
1016
1040
  function callOnBuildFuncs(builder, func) {
1017
- if (isFunction(func)) {
1041
+ if ((0, objectUtils_1.isFunction)(func)) {
1018
1042
  func.call(builder, builder);
1019
1043
  }
1020
1044
  else if (Array.isArray(func)) {
@@ -1023,7 +1047,7 @@ function callOnBuildFuncs(builder, func) {
1023
1047
  }
1024
1048
  function buildKnexQuery(builder, knexBuilder = builder.knex().queryBuilder()) {
1025
1049
  knexBuilder = builder.executeOnBuildKnex(knexBuilder);
1026
- const fromOperation = builder.findLastOperation(QueryBuilderBase.FromSelector);
1050
+ const fromOperation = builder.findLastOperation(QueryBuilderBase_1.QueryBuilderBase.FromSelector);
1027
1051
  if (!builder.isPartial()) {
1028
1052
  // Set the table only if it hasn't been explicitly set yet.
1029
1053
  if (!fromOperation) {
@@ -1031,7 +1055,9 @@ function buildKnexQuery(builder, knexBuilder = builder.knex().queryBuilder()) {
1031
1055
  }
1032
1056
  // Only add `table.*` select if there are no explicit selects
1033
1057
  // and `from` is a table name and not a subquery.
1034
- if (!builder.hasSelects() && (!fromOperation || fromOperation.table)) {
1058
+ // `fromRaw()` and `into()` aren't `FromOperation`s and have no `table`.
1059
+ if (!builder.hasSelects() &&
1060
+ (!fromOperation || fromOperation.table)) {
1035
1061
  knexBuilder = setDefaultSelect(builder, knexBuilder);
1036
1062
  }
1037
1063
  }
@@ -1051,10 +1077,11 @@ function setDefaultSelect(builder, knexBuilder) {
1051
1077
  const tableRef = builder.tableRef();
1052
1078
  return knexBuilder.select(`${tableRef}.*`);
1053
1079
  }
1080
+ // `func` is a function or an array of functions and arrays.
1054
1081
  async function chainHooks(promise, builder, func) {
1055
1082
  return promise.then((result) => {
1056
1083
  let promise = Promise.resolve(result);
1057
- if (isFunction(func)) {
1084
+ if ((0, objectUtils_1.isFunction)(func)) {
1058
1085
  promise = promise.then((result) => {
1059
1086
  const res = func.call(builder, result, builder);
1060
1087
  handleReturnImmediatelyValue(builder);
@@ -1091,7 +1118,7 @@ function createModels(result, builder) {
1091
1118
  return result;
1092
1119
  }
1093
1120
  function shouldBeConvertedToModel(obj, modelClass) {
1094
- return isObject(obj) && !(obj instanceof modelClass);
1121
+ return (0, objectUtils_1.isObject)(obj) && !(obj instanceof modelClass);
1095
1122
  }
1096
1123
  function writeOperation(builder, cb) {
1097
1124
  if (!builder.isFind()) {
@@ -1109,28 +1136,27 @@ function writeOperation(builder, cb) {
1109
1136
  }
1110
1137
  }
1111
1138
  function findOperationFactory() {
1112
- return new FindOperation('find');
1139
+ return new FindOperation_1.FindOperation('find');
1113
1140
  }
1114
1141
  function insertOperationFactory() {
1115
- return new InsertOperation('insert');
1142
+ return new InsertOperation_1.InsertOperation('insert');
1116
1143
  }
1117
1144
  function updateOperationFactory() {
1118
- return new UpdateOperation('update');
1145
+ return new UpdateOperation_1.UpdateOperation('update');
1119
1146
  }
1120
1147
  function patchOperationFactory() {
1121
- return new UpdateOperation('patch', {
1148
+ return new UpdateOperation_1.UpdateOperation('patch', {
1122
1149
  modelOptions: { patch: true },
1123
1150
  });
1124
1151
  }
1125
1152
  function relateOperationFactory() {
1126
- return new RelateOperation('relate', {});
1153
+ // A plain query has no relation: the relation queries replace this factory.
1154
+ return new RelateOperation_1.RelateOperation('relate', {});
1127
1155
  }
1128
1156
  function unrelateOperationFactory() {
1129
- return new UnrelateOperation('unrelate', {});
1157
+ // A plain query has no relation: the relation queries replace this factory.
1158
+ return new UnrelateOperation_1.UnrelateOperation('unrelate', {});
1130
1159
  }
1131
1160
  function deleteOperationFactory() {
1132
- return new DeleteOperation('delete');
1161
+ return new DeleteOperation_1.DeleteOperation('delete');
1133
1162
  }
1134
- module.exports = {
1135
- QueryBuilder,
1136
- };