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

Commit 81ae168

Browse files
committed
Added support for Graph Upsert
1 parent 7ccd56e commit 81ae168

File tree

2 files changed

+66
-20
lines changed

2 files changed

+66
-20
lines changed

src/index.js

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ class Service {
6464
this.eagerFilters = options.eagerFilters
6565
this.allowedInsert = options.allowedInsert
6666
this.insertGraphOptions = options.insertGraphOptions
67+
this.allowedUpsert = options.allowedUpsert
68+
this.upsertGraphOptions = options.upsertGraphOptions
6769
}
6870

6971
extend (obj) {
@@ -408,28 +410,32 @@ class Service {
408410
}
409411
}
410412

411-
// NOTE (EK): Delete id field so we don't update it
412-
if (Array.isArray(this.id)) {
413-
for (const idKey of this.id) {
414-
delete newObject[idKey]
413+
if (!this.allowedUpsert) {
414+
// NOTE (EK): Delete id field so we don't update it
415+
if (Array.isArray(this.id)) {
416+
for (const idKey of this.id) {
417+
delete newObject[idKey]
418+
}
419+
} else {
420+
delete newObject[this.id]
415421
}
422+
return this._createQuery(params)
423+
.where(this.getIdsQuery(id))
424+
.update(newObject)
425+
.then(() => {
426+
// NOTE (EK): Restore the id field so we can return it to the client
427+
if (Array.isArray(this.id)) {
428+
newObject = Object.assign({}, newObject, this.getIdsQuery(id))
429+
} else {
430+
newObject[this.id] = id
431+
}
432+
433+
return newObject
434+
})
416435
} else {
417-
delete newObject[this.id]
436+
return this._createQuery(params)
437+
.upsertGraphAndFetch(newObject, this.upsertGraphOptions)
418438
}
419-
420-
return this._createQuery(params)
421-
.where(this.getIdsQuery(id))
422-
.update(newObject)
423-
.then(() => {
424-
// NOTE (EK): Restore the id field so we can return it to the client
425-
if (Array.isArray(this.id)) {
426-
newObject = Object.assign({}, newObject, this.getIdsQuery(id))
427-
} else {
428-
newObject[this.id] = id
429-
}
430-
431-
return newObject
432-
})
433439
})
434440
.catch(errorHandler)
435441
}

test/index.test.js

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ const app = feathers()
8383
}
8484
}
8585
],
86-
allowedInsert: 'clients'
86+
allowedInsert: 'clients',
87+
allowedUpsert: 'clients'
8788
})
8889
)
8990
.use(
@@ -506,6 +507,45 @@ describe('Feathers Objection Service', () => {
506507
})
507508
})
508509

510+
describe('Graph Upsert Queries', () => {
511+
before(async () => {
512+
await companies.remove(null)
513+
const [google] = await companies
514+
.create([
515+
{
516+
name: 'Google',
517+
clients: [
518+
{
519+
name: 'Dan Davis'
520+
}
521+
]
522+
},
523+
{
524+
name: 'Apple'
525+
}
526+
], { query: { $eager: 'clients' } })
527+
528+
const newClients = (google.clients) ? google.clients.concat([{
529+
name: 'Ken Patrick'
530+
}]) : []
531+
532+
await companies
533+
.update(google.id, {
534+
id: google.id,
535+
name: 'Alphabet',
536+
clients: newClients
537+
}, { query: { $eager: 'clients' } })
538+
})
539+
540+
it('allows upsertGraph queries on update', () => {
541+
return companies.find({ query: { $eager: 'clients' } }).then(data => {
542+
expect(data[0].name).equal('Alphabet')
543+
expect(data[0].clients).to.be.an('array')
544+
expect(data[0].clients).to.have.lengthOf(2)
545+
})
546+
})
547+
})
548+
509549
describe('$like method', () => {
510550
beforeEach(async () => {
511551
await people

0 commit comments

Comments
 (0)