Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c2569c1

Browse files
committed
use withGraphFetched instead of eager internally
1 parent c479ca8 commit c2569c1

File tree

9 files changed

+27
-19
lines changed

9 files changed

+27
-19
lines changed

lib/model/Model.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,10 @@ class Model {
514514
return cachedGet(this, '$$virtualAttributes', getVirtualAttributes);
515515
}
516516

517+
static getDefaultGraphOptions() {
518+
return this.defaultGraphOptions || this.defaultEagerOptions;
519+
}
520+
517521
static query(trx) {
518522
const query = this.QueryBuilder.forClass(this).transacting(trx);
519523
this.onCreateQuery(query);

lib/queryBuilder/QueryBuilder.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ class QueryBuilder extends QueryBuilderBase {
214214
const parsedExp = parseRelationExpression(this.modelClass(), exp);
215215

216216
eagerOp.expression = eagerOp.expression.merge(parsedExp);
217-
eagerOp.eagerOptions = { ...eagerOp.eagerOptions, ...options };
217+
eagerOp.graphOptions = { ...eagerOp.graphOptions, ...options };
218218

219219
checkEager(this);
220220
return this;
@@ -431,7 +431,7 @@ class QueryBuilder extends QueryBuilderBase {
431431

432432
_eagerOptions(opt) {
433433
const eagerOp = ensureEagerOperation(this);
434-
eagerOp.eagerOptions = Object.assign({}, eagerOp.eagerOptions, opt);
434+
eagerOp.graphOptions = Object.assign({}, eagerOp.graphOptions, opt);
435435
return this;
436436
}
437437

@@ -1235,7 +1235,7 @@ function getTableName(modelClassOrTableName) {
12351235

12361236
function ensureEagerOperation(builder, algorithm = null) {
12371237
const modelClass = builder.modelClass();
1238-
const defaultEagerOptions = modelClass.defaultGraphOptions || modelClass.defaultEagerOptions;
1238+
const defaultGraphOptions = modelClass.getDefaultGraphOptions();
12391239
const eagerOp = builder.findOperation(EagerOperation);
12401240

12411241
if (algorithm) {
@@ -1245,7 +1245,7 @@ function ensureEagerOperation(builder, algorithm = null) {
12451245
return eagerOp;
12461246
} else {
12471247
const newEagerOp = new EagerOperationClass('eager', {
1248-
defaultEagerOptions
1248+
defaultGraphOptions
12491249
});
12501250

12511251
if (eagerOp) {
@@ -1267,7 +1267,7 @@ function ensureEagerOperation(builder, algorithm = null) {
12671267
);
12681268

12691269
const newEagerOp = new EagerOperationClass('eager', {
1270-
defaultEagerOptions
1270+
defaultGraphOptions
12711271
});
12721272

12731273
builder.addOperation(newEagerOp);

lib/queryBuilder/RelationExpression.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,14 @@ class RelationExpression {
7474
// Merges this relation expression with another. `expr` can be a string,
7575
// a pojo, or a RelationExpression instance.
7676
merge(expr) {
77-
const node = RelationExpression.create(expr).node;
78-
return new RelationExpression(mergeNodes(this.node, node));
77+
expr = RelationExpression.create(expr);
78+
79+
if (this.isEmpty) {
80+
// Nothing to merge.
81+
return expr;
82+
}
83+
84+
return new RelationExpression(mergeNodes(this.node, expr.node));
7985
}
8086

8187
// Returns true if `expr` is contained by this expression. For example

lib/queryBuilder/graph/GraphOperation.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ class GraphOperation {
2929
.modify(propagateMethodCallsFromQuery(builder))
3030
.modify(buildFetchQuerySelects(graph, graphOptions, eagerExpr))
3131
.findByIds(rootIds)
32-
.eagerAlgorithm(modelClass.WhereInEagerAlgorighm)
33-
.eager(eagerExpr)
32+
.withGraphFetched(eagerExpr)
3433
.internalOptions(fetchQueryInternalOptions())
3534
.then(models => ModelGraph.create(modelClass, models));
3635
}

lib/queryBuilder/operations/InsertGraphAndFetchOperation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class InsertGraphAndFetchOperation extends DelegateOperation {
3434
.query()
3535
.childQueryOf(builder)
3636
.findByIds(ids)
37-
.eager(eager)
37+
.withGraphFetched(eager)
3838
.then(models => {
3939
return this.isArray ? models : models[0] || null;
4040
});

lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class UpsertGraphAndFetchOperation extends DelegateOperation {
3434
.query()
3535
.childQueryOf(builder)
3636
.findByIds(ids)
37-
.eager(eager)
37+
.withGraphFetched(eager)
3838
.then(models => {
3939
return this.isArray ? models : models[0] || null;
4040
});

lib/queryBuilder/operations/eager/EagerOperation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class EagerOperation extends QueryBuilderOperation {
99

1010
this.expression = RelationExpression.create();
1111
this.modifiersAtPath = [];
12-
this.eagerOptions = this.opt.defaultEagerOptions;
12+
this.graphOptions = this.opt.defaultGraphOptions;
1313
}
1414

1515
buildFinalExpression() {
@@ -42,7 +42,7 @@ class EagerOperation extends QueryBuilderOperation {
4242
cloneFrom(eagerOp) {
4343
this.expression = eagerOp.expression.clone();
4444
this.modifiersAtPath = eagerOp.modifiersAtPath.slice();
45-
this.eagerOptions = Object.assign({}, eagerOp.eagerOptions);
45+
this.graphOptions = Object.assign({}, eagerOp.graphOptions);
4646
}
4747

4848
clone() {

lib/queryBuilder/operations/eager/JoinEagerOperation.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,15 +23,15 @@ class JoinEagerOperation extends EagerOperation {
2323
return this.joiner
2424
.setExpression(this.buildFinalExpression())
2525
.setModifiers(this.buildFinalModifiers(builder))
26-
.setOptions(this.eagerOptions)
26+
.setOptions(this.graphOptions)
2727
.fetchColumnInfo(builder);
2828
}
2929

3030
onBuild(builder) {
3131
this.joiner
3232
.setExpression(this.buildFinalExpression())
3333
.setModifiers(this.buildFinalModifiers(builder))
34-
.setOptions(this.eagerOptions)
34+
.setOptions(this.graphOptions)
3535
.build(builder);
3636
}
3737

lib/queryBuilder/operations/eager/WhereInEagerOperation.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ class WhereInEagerOperation extends EagerOperation {
1919
}
2020

2121
batchSize(knex) {
22-
if (this.eagerOptions.maxBatchSize) {
23-
return this.eagerOptions.maxBatchSize;
22+
if (this.graphOptions.maxBatchSize) {
23+
return this.graphOptions.maxBatchSize;
2424
} else if (isMsSql(knex)) {
2525
// On MSSQL the parameter limit is actually 2100, but since I couldn't figure out
2626
// if the limit is for all parameters in a query or for individual clauses, we set
@@ -139,8 +139,7 @@ class WhereInEagerOperation extends EagerOperation {
139139
return relation.relatedModelClass
140140
.query()
141141
.childQueryOf(builder)
142-
.eagerOptions(this.eagerOptions)
143-
.eager(childExpression)
142+
.withGraphFetched(childExpression, this.graphOptions)
144143
.modifiers(this.buildFinalModifiers(builder));
145144
}
146145

0 commit comments

Comments
 (0)