Skip to content

Commit 485e3ef

Browse files
committed
feat(model): stop calling transform in toObject method
1 parent e5779a0 commit 485e3ef

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

src/lib/model.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ export class ModelClass<DocType> {
112112

113113
toJsonApi!: () => JsonApiBody<JsonApiResource>;
114114

115-
toObject!: () => DocType;
115+
toObject!: (
116+
options?: {
117+
transform?: boolean,
118+
},
119+
) => DocType;
116120

117121
unmarkModified!: <T extends keyof DocType>(path: T) => void;
118122
}
@@ -359,7 +363,7 @@ BaseModel.prototype.set = function (path, value, options) {
359363
};
360364

361365
BaseModel.prototype.toJSON = function () {
362-
return this.toObject();
366+
return this.toObject({ transform: true });
363367
};
364368

365369
BaseModel.prototype.toJsonApi = function () {
@@ -425,18 +429,18 @@ BaseModel.prototype.toJsonApi = function () {
425429
return body;
426430
};
427431

428-
BaseModel.prototype.toObject = function () {
432+
BaseModel.prototype.toObject = function (options) {
429433
const schema = this.schema;
430434

431435
const obj: any = {
432436
id: this.id,
433437
};
434438

435-
for (const [path, options] of Object.entries(schema.attributes).concat(Object.entries(schema.relationships))) {
439+
for (const [path, property] of Object.entries(schema.attributes).concat(Object.entries(schema.relationships))) {
436440
let value = this.get(path);
437441

438-
if (options?.transform) {
439-
value = options.transform(value);
442+
if (options?.transform && property?.transform) {
443+
value = property.transform(value);
440444
}
441445

442446
if (value) {

0 commit comments

Comments
 (0)