Skip to content

Commit

Permalink
closes Vincit#1109 closes Vincit#1110
Browse files Browse the repository at this point in the history
  • Loading branch information
koskimas authored and flipace committed May 7, 2019
1 parent 0232894 commit 51ab2c6
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/queryBuilder/operations/InsertGraphAndFetchOperation.js
Expand Up @@ -22,6 +22,10 @@ class InsertGraphAndFetchOperation extends DelegateOperation {
}

onAfter2(builder) {
if (this.models.length === 0) {
return this.isArray ? [] : null;
}

const eager = RelationExpression.fromModelGraph(this.models);
const modelClass = this.models[0].constructor;
const ids = this.models.map(model => model.$id());
Expand Down
6 changes: 5 additions & 1 deletion lib/queryBuilder/operations/UpsertGraphAndFetchOperation.js
Expand Up @@ -22,14 +22,18 @@ class UpsertGraphAndFetchOperation extends DelegateOperation {
}

onAfter3(builder) {
if (this.models.length === 0) {
return this.isArray ? [] : null;
}

const eager = RelationExpression.fromModelGraph(this.models);
const modelClass = this.models[0].constructor;
const ids = this.models.map(model => model.$id());

return modelClass
.query()
.childQueryOf(builder)
.whereIn(builder.fullIdColumnFor(modelClass), ids)
.whereInComposite(builder.fullIdColumnFor(modelClass), ids)
.eager(eager)
.then(models => {
return this.isArray ? models : models[0] || null;
Expand Down
8 changes: 8 additions & 0 deletions tests/integration/insertGraph.js
Expand Up @@ -80,6 +80,10 @@ module.exports = session => {
return session.populate(population);
});

it('should do nothing if an empty array is provided', () => {
return Model1.query().insertGraph([]);
})

it('should insert a model with relations', () => {
return Model1.query()
.insertGraph(insertion)
Expand Down Expand Up @@ -552,6 +556,10 @@ module.exports = session => {
return session.populate(population);
});

it('should do nothing if an empty array is provided', () => {
return Model1.query().insertGraphAndFetch([]);
})

it('should insert a model with relations and fetch the inserted graph', () => {
return Model1.query()
.insertGraphAndFetch(insertion)
Expand Down
7 changes: 7 additions & 0 deletions tests/integration/upsertGraph.js
Expand Up @@ -83,6 +83,13 @@ module.exports = session => {
return session.populate(population);
});

it('should do nothing if an empty array is given', () => {
return Promise.all([
Model1.query(session.knex).upsertGraph([]),
Model1.query(session.knex).upsertGraphAndFetch([])
]);
})

for (const passthroughMethodCall of [null, 'forUpdate', 'forShare']) {
const passthroughMethodCallSql = {
null: '',
Expand Down

0 comments on commit 51ab2c6

Please sign in to comment.