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

Commit 7ccd56e

Browse files
committed
Added support for Graph Insert
1 parent d6f683f commit 7ccd56e

File tree

4 files changed

+98
-4
lines changed

4 files changed

+98
-4
lines changed

src/index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ class Service {
6262
this.allowedEager = options.allowedEager || '[]'
6363
this.namedEagerFilters = options.namedEagerFilters
6464
this.eagerFilters = options.eagerFilters
65+
this.allowedInsert = options.allowedInsert
66+
this.insertGraphOptions = options.insertGraphOptions
6567
}
6668

6769
extend (obj) {
@@ -338,8 +340,15 @@ class Service {
338340
}
339341

340342
_create (data, params) {
341-
return this._createQuery(params)
342-
.insert(data, this.id)
343+
let q = this._createQuery(params)
344+
345+
if (this.allowedInsert) {
346+
q.allowInsert(this.allowedInsert)
347+
q.insertGraph(data, this.insertGraphOptions)
348+
} else {
349+
q.insert(data, this.id)
350+
}
351+
return q
343352
.then(row => {
344353
let id = null
345354

test/client.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { Model } from 'objection'
2+
3+
export default class Client extends Model {
4+
static tableName = 'clients'
5+
static jsonSchema = {
6+
type: 'object',
7+
required: ['name'],
8+
9+
properties: {
10+
id: { type: 'integer' },
11+
companyId: { type: 'integer' },
12+
name: { type: 'string' }
13+
}
14+
}
15+
16+
static get relationMappings () {
17+
return {
18+
company: {
19+
relation: Model.BelongsToOneRelation,
20+
modelClass: require('./company'),
21+
join: {
22+
from: 'clients.companyId',
23+
to: 'companies.id'
24+
}
25+
}
26+
}
27+
}
28+
}

test/company.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,14 @@ export default class Company extends Model {
3232
from: 'companies.id',
3333
to: 'employees.companyId'
3434
}
35+
},
36+
clients: {
37+
relation: Model.HasManyRelation,
38+
modelClass: path.join(__dirname, '/client'),
39+
join: {
40+
from: 'companies.id',
41+
to: 'clients.companyId'
42+
}
3543
}
3644
}
3745
}

test/index.test.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import PeopleRoomsCustomIdSeparator from './people-rooms-custom-id-separator'
1515
import Company from './company'
1616
import { Model } from 'objection'
1717
import Employee from './employee'
18+
import Client from './client'
1819

1920
const db = knex({
2021
client: 'sqlite3',
@@ -68,7 +69,7 @@ const app = feathers()
6869
model: Company,
6970
id: 'id',
7071
events: ['testing'],
71-
allowedEager: 'ceos',
72+
allowedEager: '[ceos, clients]',
7273
namedEagerFilters: {
7374
notSnoop (builder) {
7475
return builder.whereNot('name', 'Snoop')
@@ -81,7 +82,8 @@ const app = feathers()
8182
return builder.where('age', '<', '25')
8283
}
8384
}
84-
]
85+
],
86+
allowedInsert: 'clients'
8587
})
8688
)
8789
.use(
@@ -91,6 +93,13 @@ const app = feathers()
9193
allowedEager: 'company'
9294
})
9395
)
96+
.use(
97+
'/clients',
98+
service({
99+
model: Client,
100+
allowedEager: 'company'
101+
})
102+
)
94103

95104
let people = app.service('people')
96105
let peopleRooms = app.service('people-rooms')
@@ -158,6 +167,16 @@ function clean (done) {
158167
table.integer('companyId').references('companies.id')
159168
table.string('name')
160169
})
170+
})
171+
})
172+
.then(() => {
173+
return db.schema.dropTableIfExists('clients').then(() => {
174+
return db.schema
175+
.createTable('clients', table => {
176+
table.increments('id')
177+
table.integer('companyId').references('companies.id')
178+
table.string('name')
179+
})
161180
.then(() => done())
162181
})
163182
})
@@ -457,6 +476,36 @@ describe('Feathers Objection Service', () => {
457476
})
458477
})
459478

479+
describe('Graph Insert Queries', () => {
480+
before(async () => {
481+
await companies.remove(null)
482+
await companies
483+
.create([
484+
{
485+
name: 'Google',
486+
clients: [
487+
{
488+
name: 'Dan Davis'
489+
},
490+
{
491+
name: 'Ken Patrick'
492+
}
493+
]
494+
},
495+
{
496+
name: 'Apple'
497+
}
498+
])
499+
})
500+
501+
it('allows insertGraph queries', () => {
502+
return companies.find({ query: { $eager: 'clients' } }).then(data => {
503+
expect(data[0].clients).to.be.an('array')
504+
expect(data[0].clients).to.have.lengthOf(2)
505+
})
506+
})
507+
})
508+
460509
describe('$like method', () => {
461510
beforeEach(async () => {
462511
await people

0 commit comments

Comments
 (0)