Skip to content

Commit 4db73e0

Browse files
committed
fix: don't instantiate the model if it's partial
1 parent ddfbe79 commit 4db73e0

File tree

4 files changed

+15
-10
lines changed

4 files changed

+15
-10
lines changed

dist/index.cjs.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class Parser {
119119
}
120120
const loadedElement = Parser.load(element, included);
121121
const model = $registeredModels.find(e => e.type === loadedElement.type);
122-
const instance = this.wrapWhenPartial(new (model?.klass || Model)(), loadedElement);
122+
const instance = this.wrapWhenPartial(model, loadedElement);
123123
this.resolved[uniqueKey] = instance;
124124
if (model && model.createFn) {
125125
return model.createFn(instance, loadedElement, relation => this.parse(relation, included));
@@ -132,10 +132,11 @@ class Parser {
132132
this.parseRelationships(instance, loadedElement, relsData, included);
133133
return instance;
134134
}
135-
wrapWhenPartial(instance, loadedElement) {
135+
wrapWhenPartial(model, loadedElement) {
136136
if (!loadedElement.$_partial) {
137-
return instance;
137+
return new (model?.klass || Model)();
138138
}
139+
const instance = new Model();
139140
return new Proxy(instance, {
140141
get: function (target, prop) {
141142
if (prop === "$_partial") {

dist/index.esm.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ class Parser {
115115
}
116116
const loadedElement = Parser.load(element, included);
117117
const model = $registeredModels.find(e => e.type === loadedElement.type);
118-
const instance = this.wrapWhenPartial(new (model?.klass || Model)(), loadedElement);
118+
const instance = this.wrapWhenPartial(model, loadedElement);
119119
this.resolved[uniqueKey] = instance;
120120
if (model && model.createFn) {
121121
return model.createFn(instance, loadedElement, relation => this.parse(relation, included));
@@ -128,10 +128,11 @@ class Parser {
128128
this.parseRelationships(instance, loadedElement, relsData, included);
129129
return instance;
130130
}
131-
wrapWhenPartial(instance, loadedElement) {
131+
wrapWhenPartial(model, loadedElement) {
132132
if (!loadedElement.$_partial) {
133-
return instance;
133+
return new (model?.klass || Model)();
134134
}
135+
const instance = new Model();
135136
return new Proxy(instance, {
136137
get: function (target, prop) {
137138
if (prop === "$_partial") {

dist/types/Parser.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { JSONData } from "./interfaces/JSONData";
22
import { JSONModel } from "./interfaces/JSONModel";
33
import { Model } from "./Model";
4+
import { RegisteredModel } from "./interfaces/RegisteredModel";
45
export declare class Parser {
56
private data;
67
private included;
@@ -10,7 +11,7 @@ export declare class Parser {
1011
private parse;
1112
parseList(list: JSONModel[], included: JSONModel[]): unknown[];
1213
parseElement<T>(element: JSONModel, included: JSONModel[]): T;
13-
wrapWhenPartial(instance: Model, loadedElement: JSONModel & {
14+
wrapWhenPartial(model: RegisteredModel | undefined, loadedElement: JSONModel & {
1415
$_partial?: boolean;
1516
}): Model;
1617
private parseRelationships;

src/Parser.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { RegisteredProperty } from "./interfaces/RegisteredProperty";
55
import { Model } from "./Model";
66
import { $registeredAttributes, $registeredModels, $registeredRelationships } from "./data";
77
import { debug } from "./utils";
8+
import { RegisteredModel } from "./interfaces/RegisteredModel";
89

910
export class Parser {
1011
readonly resolved: Record<string, Model> = {};
@@ -62,7 +63,7 @@ export class Parser {
6263
(e) => e.type === loadedElement.type
6364
);
6465

65-
const instance = this.wrapWhenPartial(new (model?.klass || Model)(), loadedElement);
66+
const instance = this.wrapWhenPartial(model, loadedElement);
6667
this.resolved[uniqueKey] = instance;
6768

6869
if (model && model.createFn) {
@@ -85,10 +86,11 @@ export class Parser {
8586
return instance as T;
8687
}
8788

88-
wrapWhenPartial(instance: Model, loadedElement: JSONModel & { $_partial?: boolean }) {
89+
wrapWhenPartial(model: RegisteredModel | undefined, loadedElement: JSONModel & { $_partial?: boolean }) {
8990
if (!loadedElement.$_partial) {
90-
return instance;
91+
return new (model?.klass || Model)();
9192
}
93+
const instance = new Model();
9294
return new Proxy(
9395
instance,
9496
{

0 commit comments

Comments
 (0)