Skip to content
This repository was archived by the owner on Sep 2, 2025. It is now read-only.

Commit fb3c2ac

Browse files
author
Dekel Barzilay
committed
added support for Upsert Graph in patch method
1 parent 27b9f69 commit fb3c2ac

File tree

5 files changed

+51
-16
lines changed

5 files changed

+51
-16
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,8 @@ app.service('companies').find({
270270
Arbitrary relation graphs can be upserted (insert + update + delete) using the
271271
upsertGraph method. See
272272
[`examples`](https://vincit.github.io/objection.js/#graph-upserts) for a better
273-
explanation. Runs on the `.update(id, data, params)` service method.
273+
explanation.
274+
Runs on `update` and `patch` service methods when `id` is set.
274275
275276
_The relation being upserted must also be present in `allowedEager` option and
276277
included in `$eager` query._

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "feathers-objection",
33
"description": "A service plugin for ObjectionJS an ORM based on KnexJS",
4-
"version": "4.0.0",
4+
"version": "4.1.0",
55
"homepage": "https://github.com/feathersjs-ecosystem/feathers-objection",
66
"keywords": [
77
"feathers",

src/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,9 @@ class Service extends AdapterService {
105105
* Create a new query that re-queries all ids that were originally changed
106106
* @param id
107107
* @param idList
108+
* @param addTableName
108109
*/
109-
getIdsQuery (id, idList) {
110+
getIdsQuery (id, idList, addTableName = true) {
110111
const query = {};
111112

112113
if (Array.isArray(this.id)) {
@@ -132,7 +133,7 @@ class Service extends AdapterService {
132133
}
133134
});
134135
} else {
135-
query[`${this.Model.tableName}.${this.id}`] = idList ? (idList.length === 1 ? idList[0] : { $in: idList }) : id;
136+
query[addTableName ? `${this.Model.tableName}.${this.id}` : this.id] = idList ? (idList.length === 1 ? idList[0] : { $in: idList }) : id;
136137
}
137138

138139
return query;
@@ -481,7 +482,16 @@ class Service extends AdapterService {
481482
*/
482483
_patch (id, data, params) {
483484
let { filters, query } = this.filterQuery(params);
484-
const dataCopy = Object.assign({}, data);
485+
486+
if (this.allowedUpsert && id !== null) {
487+
let dataCopy = Object.assign({}, data, this.getIdsQuery(id, null, false));
488+
489+
return this._createQuery(params)
490+
.allowUpsert(this.allowedUpsert)
491+
.upsertGraphAndFetch(dataCopy, this.upsertGraphOptions);
492+
}
493+
494+
let dataCopy = Object.assign({}, data);
485495

486496
const mapIds = page => Array.isArray(this.id)
487497
? this.id.map(idKey => [...new Set((page.data || page).map(current => current[idKey]))])

test/index.test.js

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -998,12 +998,15 @@ describe('Feathers Objection Service', () => {
998998
});
999999

10001000
describe('Graph Upsert Queries', () => {
1001-
before(async () => {
1001+
let google;
1002+
1003+
beforeEach(async () => {
10021004
await companies.remove(null);
1003-
const [google] = await companies
1005+
[google] = await companies
10041006
.create([
10051007
{
10061008
name: 'Google',
1009+
ceo: 1,
10071010
clients: [
10081011
{
10091012
name: 'Dan Davis'
@@ -1014,25 +1017,46 @@ describe('Feathers Objection Service', () => {
10141017
name: 'Apple'
10151018
}
10161019
], { query: { $eager: 'clients' } });
1020+
});
10171021

1022+
it('allows upsertGraph queries on update', () => {
10181023
const newClients = (google.clients) ? google.clients.concat([{
10191024
name: 'Ken Patrick'
10201025
}]) : [];
10211026

1022-
await companies
1027+
return companies
10231028
.update(google.id, {
10241029
id: google.id,
10251030
name: 'Alphabet',
10261031
clients: newClients
1027-
}, { query: { $eager: 'clients' } });
1032+
}, { query: { $eager: 'clients' } }).then(() => {
1033+
return companies.find({ query: { $eager: 'clients' } }).then(data => {
1034+
expect(data[0].name).equal('Alphabet');
1035+
expect(data[0].clients).to.be.an('array');
1036+
expect(data[0].clients).to.have.lengthOf(2);
1037+
});
1038+
});
10281039
});
10291040

1030-
it('allows upsertGraph queries on update', () => {
1031-
return companies.find({ query: { $eager: 'clients' } }).then(data => {
1032-
expect(data[0].name).equal('Alphabet');
1033-
expect(data[0].clients).to.be.an('array');
1034-
expect(data[0].clients).to.have.lengthOf(2);
1035-
});
1041+
it('allows upsertGraph queries on patch', () => {
1042+
const newClients = (google.clients) ? google.clients.concat([{
1043+
name: 'Ken Patrick'
1044+
}]) : [];
1045+
1046+
return companies
1047+
.patch(google.id, {
1048+
name: 'Google Alphabet',
1049+
clients: newClients
1050+
}).then(data => {
1051+
expect(data.name).equal('Google Alphabet');
1052+
expect(data.ceo).equal(1);
1053+
1054+
return companies.find({ query: { $eager: 'clients' } }).then(data => {
1055+
expect(data[0].name).equal('Google Alphabet');
1056+
expect(data[0].clients).to.be.an('array');
1057+
expect(data[0].clients).to.have.lengthOf(2);
1058+
});
1059+
});
10361060
});
10371061
});
10381062

0 commit comments

Comments
 (0)