Skip to content
This repository was archived by the owner on Apr 17, 2020. It is now read-only.

Commit 342aa93

Browse files
committed
fix: delete reference functions when a field is removed
1 parent b835284 commit 342aa93

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

lib/KnormRelations.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,14 @@ class KnormRelations {
6565
const { name, references } = field;
6666

6767
if (references) {
68-
const model = references.model.name;
69-
delete this._config.references[model][name];
70-
if (!Object.keys(this._config.references[model]).length) {
71-
delete this._config.references[model];
68+
if (typeof references === 'function') {
69+
delete this._config.referenceFunctions[name];
70+
} else {
71+
const model = references.model.name;
72+
delete this._config.references[model][name];
73+
if (!Object.keys(this._config.references[model]).length) {
74+
delete this._config.references[model];
75+
}
7276
}
7377
}
7478
}

test/KnormRelations.spec.js

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ describe('KnormRelations', () => {
218218
Foo: { fooId: Quux.fields.fooId, fooId2: Quux.fields.fooId2 }
219219
});
220220
});
221-
});
222221

223222
describe('when a field is removed', () => {
224223
it('removes the field', () => {
@@ -249,6 +248,26 @@ describe('KnormRelations', () => {
249248
Bar.removeField(Bar.fields.barId);
250249
expect(Bar.config.references, 'to be empty');
251250
});
251+
252+
it("removes the field's reference function", () => {
253+
class Foo extends Model {}
254+
class Bar extends Model {}
255+
256+
Foo.fields = { id: { type: 'integer' } };
257+
Bar.fields = {
258+
id: 'integer',
259+
fooId: {
260+
type: 'integer',
261+
references() {
262+
return Foo.fields.id;
263+
}
264+
}
265+
};
266+
267+
expect(Bar.config.referenceFunctions, 'to have key', 'fooId');
268+
Bar.removeField(Bar.fields.fooId);
269+
expect(Bar.config.referenceFunctions, 'to be empty');
270+
});
252271
});
253272
});
254273
});

0 commit comments

Comments
 (0)