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

Commit 54209f3

Browse files
dekelevDekel Barzilay
andauthored
Changed create to insert & get by returned row fields by default (#90)
* Changed create to insert & get by returned row fields by default * Added test on create with ID when ID gets overridden in model Co-authored-by: Dekel Barzilay <[email protected]>
1 parent 29163cb commit 54209f3

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/index.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -476,18 +476,20 @@ class Service extends AdapterService {
476476
.then(row => {
477477
if (params.query && params.query.$noSelect) { return data; }
478478

479-
let id = null;
479+
let id;
480480

481481
if (Array.isArray(this.id)) {
482482
id = [];
483483

484484
for (const idKey of this.id) {
485-
id.push(typeof data[idKey] !== 'undefined' ? data[idKey] : row[idKey]);
485+
id.push(row && row[idKey] ? row[idKey] : data[idKey]);
486486
}
487487
} else {
488-
id = typeof data[this.id] !== 'undefined' ? data[this.id] : row[this.id];
488+
id = row && row[this.id] ? row[this.id] : data[this.id];
489489
}
490490

491+
if (!id || (Array.isArray(id) && !id.length)) { return data; }
492+
491493
return this._get(id, params);
492494
})
493495
.catch(errorHandler);

test/company.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,8 @@ export default class Company extends Model {
8181
}
8282
}
8383
}
84+
85+
$beforeInsert () {
86+
if (this.id) { this.id = 99; }
87+
}
8488
}

test/index.test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1768,4 +1768,21 @@ describe('Feathers Objection Service', () => {
17681768
});
17691769
});
17701770
});
1771+
1772+
describe('Create with ID', () => {
1773+
beforeEach(async () => {
1774+
await companies.create({ name: 'Apple' });
1775+
});
1776+
1777+
afterEach(async () => {
1778+
await companies.remove(null);
1779+
});
1780+
1781+
it('create with id when id is overridden in model', () => {
1782+
return companies.create({ id: 1, name: 'Google' }).then(data => {
1783+
expect(data).to.be.ok;
1784+
expect(data.name).to.be.equal('Google');
1785+
});
1786+
});
1787+
});
17711788
});

0 commit comments

Comments
 (0)