Skip to content

Commit bee4dcd

Browse files
committed
feat(model): cast to model if set relationship with an object
1 parent 4818ab3 commit bee4dcd

1 file changed

Lines changed: 13 additions & 0 deletions

File tree

src/lib/model.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,19 @@ BaseModel.prototype.set = function (path, value, options) {
431431
} else if (schema.relationships[path]) {
432432
const property = schema.relationships[path];
433433

434+
if (value && !(value instanceof Model) && value.type && models[value.type]) {
435+
const model = models[value.type];
436+
value = new model(value);
437+
} else if (Array.isArray(value) && value.some((val) => !(val instanceof Model))) {
438+
value = value.map((val) => {
439+
if (val && !(val instanceof Model) && val.type && models[val.type]) {
440+
const model = models[val.type];
441+
return new model(val);
442+
}
443+
return val;
444+
});
445+
}
446+
434447
if (property?.set) {
435448
value = property.set(value);
436449
}

0 commit comments

Comments
 (0)