Skip to content

Commit 5953592

Browse files
committed
fix: trying not to instantiate partial models
1 parent d44e3f0 commit 5953592

File tree

4 files changed

+18
-13
lines changed

4 files changed

+18
-13
lines changed

dist/index.cjs.js

+5-4
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));
@@ -135,14 +135,15 @@ class Parser {
135135
this.parseRelationships(instance, loadedElement, relsData, included);
136136
return instance;
137137
}
138-
wrapWhenPartial(instance, loadedElement) {
138+
wrapWhenPartial(model, loadedElement) {
139139
if (!loadedElement.$_partial) {
140-
return instance;
140+
return new (model?.klass || Model)();
141141
}
142+
const instance = new Model();
142143
return new Proxy(instance, {
143144
get: function (target, prop) {
144145
if (prop === "$_partial") {
145-
return true;
146+
return target[prop];
146147
}
147148
if (prop in target) {
148149
return target[prop];

dist/index.esm.js

+5-4
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));
@@ -131,14 +131,15 @@ class Parser {
131131
this.parseRelationships(instance, loadedElement, relsData, included);
132132
return instance;
133133
}
134-
wrapWhenPartial(instance, loadedElement) {
134+
wrapWhenPartial(model, loadedElement) {
135135
if (!loadedElement.$_partial) {
136-
return instance;
136+
return new (model?.klass || Model)();
137137
}
138+
const instance = new Model();
138139
return new Proxy(instance, {
139140
get: function (target, prop) {
140141
if (prop === "$_partial") {
141-
return true;
142+
return target[prop];
142143
}
143144
if (prop in target) {
144145
return target[prop];

dist/types/Parser.d.ts

+2-1
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

+6-4
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) {
@@ -89,16 +90,17 @@ export class Parser {
8990
return instance as T;
9091
}
9192

92-
wrapWhenPartial(instance: Model, loadedElement: JSONModel & { $_partial?: boolean }) {
93+
wrapWhenPartial(model: RegisteredModel | undefined, loadedElement: JSONModel & { $_partial?: boolean }) {
9394
if (!loadedElement.$_partial) {
94-
return instance;
95+
return new (model?.klass || Model)();
9596
}
97+
const instance = new Model();
9698
return new Proxy(
9799
instance,
98100
{
99101
get: function<T extends object>(target: T, prop: keyof T) {
100102
if (prop === "$_partial") {
101-
return true;
103+
return target[prop];
102104
}
103105
if (prop in target) {
104106
return target[prop];

0 commit comments

Comments
 (0)