Skip to content

Commit

Permalink
added support for Upsert Graph in patch method
Browse files Browse the repository at this point in the history
  • Loading branch information
Dekel Barzilay committed Jul 26, 2019
1 parent 27b9f69 commit fb3c2ac
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 16 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,8 @@ app.service('companies').find({
Arbitrary relation graphs can be upserted (insert + update + delete) using the
upsertGraph method. See
[`examples`](https://vincit.github.io/objection.js/#graph-upserts) for a better
explanation. Runs on the `.update(id, data, params)` service method.
explanation.
Runs on `update` and `patch` service methods when `id` is set.
_The relation being upserted must also be present in `allowedEager` option and
included in `$eager` query._
Expand Down
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "feathers-objection",
"description": "A service plugin for ObjectionJS an ORM based on KnexJS",
"version": "4.0.0",
"version": "4.1.0",
"homepage": "https://github.com/feathersjs-ecosystem/feathers-objection",
"keywords": [
"feathers",
Expand Down
16 changes: 13 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,9 @@ class Service extends AdapterService {
* Create a new query that re-queries all ids that were originally changed
* @param id
* @param idList
* @param addTableName
*/
getIdsQuery (id, idList) {
getIdsQuery (id, idList, addTableName = true) {
const query = {};

if (Array.isArray(this.id)) {
Expand All @@ -132,7 +133,7 @@ class Service extends AdapterService {
}
});
} else {
query[`${this.Model.tableName}.${this.id}`] = idList ? (idList.length === 1 ? idList[0] : { $in: idList }) : id;
query[addTableName ? `${this.Model.tableName}.${this.id}` : this.id] = idList ? (idList.length === 1 ? idList[0] : { $in: idList }) : id;
}

return query;
Expand Down Expand Up @@ -481,7 +482,16 @@ class Service extends AdapterService {
*/
_patch (id, data, params) {
let { filters, query } = this.filterQuery(params);
const dataCopy = Object.assign({}, data);

if (this.allowedUpsert && id !== null) {
let dataCopy = Object.assign({}, data, this.getIdsQuery(id, null, false));

return this._createQuery(params)
.allowUpsert(this.allowedUpsert)
.upsertGraphAndFetch(dataCopy, this.upsertGraphOptions);
}

let dataCopy = Object.assign({}, data);

const mapIds = page => Array.isArray(this.id)
? this.id.map(idKey => [...new Set((page.data || page).map(current => current[idKey]))])
Expand Down
44 changes: 34 additions & 10 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -998,12 +998,15 @@ describe('Feathers Objection Service', () => {
});

describe('Graph Upsert Queries', () => {
before(async () => {
let google;

beforeEach(async () => {
await companies.remove(null);
const [google] = await companies
[google] = await companies
.create([
{
name: 'Google',
ceo: 1,
clients: [
{
name: 'Dan Davis'
Expand All @@ -1014,25 +1017,46 @@ describe('Feathers Objection Service', () => {
name: 'Apple'
}
], { query: { $eager: 'clients' } });
});

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

await companies
return companies
.update(google.id, {
id: google.id,
name: 'Alphabet',
clients: newClients
}, { query: { $eager: 'clients' } });
}, { query: { $eager: 'clients' } }).then(() => {
return companies.find({ query: { $eager: 'clients' } }).then(data => {
expect(data[0].name).equal('Alphabet');
expect(data[0].clients).to.be.an('array');
expect(data[0].clients).to.have.lengthOf(2);
});
});
});

it('allows upsertGraph queries on update', () => {
return companies.find({ query: { $eager: 'clients' } }).then(data => {
expect(data[0].name).equal('Alphabet');
expect(data[0].clients).to.be.an('array');
expect(data[0].clients).to.have.lengthOf(2);
});
it('allows upsertGraph queries on patch', () => {
const newClients = (google.clients) ? google.clients.concat([{
name: 'Ken Patrick'
}]) : [];

return companies
.patch(google.id, {
name: 'Google Alphabet',
clients: newClients
}).then(data => {
expect(data.name).equal('Google Alphabet');
expect(data.ceo).equal(1);

return companies.find({ query: { $eager: 'clients' } }).then(data => {
expect(data[0].name).equal('Google Alphabet');
expect(data[0].clients).to.be.an('array');
expect(data[0].clients).to.have.lengthOf(2);
});
});
});
});

Expand Down

0 comments on commit fb3c2ac

Please sign in to comment.