cloesce 0.0.5-unstable.2 → 0.0.5-unstable.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -18,10 +18,15 @@ import { ConstructorRegistry } from "./router";
18
18
  * @returns the instantiated value (if applicable). On error, returns null.
19
19
  */
20
20
  export declare class RuntimeValidator {
21
- private ast;
22
- private ctorReg;
23
- constructor(ast: CloesceAst, ctorReg: ConstructorRegistry);
24
- static validate(value: any, cidlType: CidlType, ast: CloesceAst, ctorReg: ConstructorRegistry): Either<null, any>;
25
- private recurse;
21
+ private ast;
22
+ private ctorReg;
23
+ constructor(ast: CloesceAst, ctorReg: ConstructorRegistry);
24
+ static validate(
25
+ value: any,
26
+ cidlType: CidlType,
27
+ ast: CloesceAst,
28
+ ctorReg: ConstructorRegistry,
29
+ ): Either<null, any>;
30
+ private recurse;
26
31
  }
27
- //# sourceMappingURL=validator.d.ts.map
32
+ //# sourceMappingURL=validator.d.ts.map
@@ -1,4 +1,8 @@
1
- import { NO_DATA_SOURCE, getNavigationPropertyCidlType, isNullableType, } from "../ast";
1
+ import {
2
+ NO_DATA_SOURCE,
3
+ getNavigationPropertyCidlType,
4
+ isNullableType,
5
+ } from "../ast";
2
6
  import { Either, b64ToU8 } from "../ui/common";
3
7
  /**
4
8
  * Runtime type validation, asserting that the structure of a value follows the
@@ -17,163 +21,173 @@ import { Either, b64ToU8 } from "../ui/common";
17
21
  * @returns the instantiated value (if applicable). On error, returns null.
18
22
  */
19
23
  export class RuntimeValidator {
20
- ast;
21
- ctorReg;
22
- constructor(ast, ctorReg) {
23
- this.ast = ast;
24
- this.ctorReg = ctorReg;
24
+ ast;
25
+ ctorReg;
26
+ constructor(ast, ctorReg) {
27
+ this.ast = ast;
28
+ this.ctorReg = ctorReg;
29
+ }
30
+ static validate(value, cidlType, ast, ctorReg) {
31
+ return new RuntimeValidator(ast, ctorReg).recurse(value, cidlType, false);
32
+ }
33
+ recurse(value, cidlType, isPartial) {
34
+ isPartial ||= typeof cidlType !== "string" && "Partial" in cidlType;
35
+ if (value === undefined) {
36
+ // We will let arrays be undefined and interpret that as an empty array.
37
+ if (typeof cidlType !== "string" && "Array" in cidlType) {
38
+ return Either.right([]);
39
+ }
40
+ return rightIf(() => value, isPartial === true);
25
41
  }
26
- static validate(value, cidlType, ast, ctorReg) {
27
- return new RuntimeValidator(ast, ctorReg).recurse(value, cidlType, false);
42
+ // TODO: consequences of null checking like this? 'null' is passed in
43
+ // as a string for GET requests
44
+ const nullable = isNullableType(cidlType);
45
+ if (value == null || value === "null") {
46
+ return rightIf(() => null, nullable);
28
47
  }
29
- recurse(value, cidlType, isPartial) {
30
- isPartial ||= typeof cidlType !== "string" && "Partial" in cidlType;
31
- if (value === undefined) {
32
- // We will let arrays be undefined and interpret that as an empty array.
33
- if (typeof cidlType !== "string" && "Array" in cidlType) {
34
- return Either.right([]);
35
- }
36
- return rightIf(() => value, isPartial === true);
37
- }
38
- // TODO: consequences of null checking like this? 'null' is passed in
39
- // as a string for GET requests
40
- const nullable = isNullableType(cidlType);
41
- if (value == null || value === "null") {
42
- return rightIf(() => null, nullable);
48
+ // Unwrap nullable types
49
+ if (nullable) {
50
+ cidlType = cidlType.Nullable;
51
+ }
52
+ // Primitives
53
+ if (typeof cidlType === "string") {
54
+ switch (cidlType) {
55
+ case "Integer":
56
+ return rightIf(() => Number(value), Number.isInteger(Number(value)));
57
+ case "Real":
58
+ return rightIf(() => Number(value), !Number.isNaN(Number(value)));
59
+ case "Text":
60
+ return rightIf(() => String(value), typeof value === "string");
61
+ case "Boolean": {
62
+ if (typeof value === "boolean") return Either.right(value);
63
+ if (value === "true") return Either.right(true);
64
+ if (value === "false") return Either.right(false);
65
+ return Either.left(null);
43
66
  }
44
- // Unwrap nullable types
45
- if (nullable) {
46
- cidlType = cidlType.Nullable;
67
+ case "DateIso": {
68
+ // Instantiate
69
+ const date = new Date(value);
70
+ return rightIf(() => date, !isNaN(date.getTime()));
47
71
  }
48
- // Primitives
49
- if (typeof cidlType === "string") {
50
- switch (cidlType) {
51
- case "Integer":
52
- return rightIf(() => Number(value), Number.isInteger(Number(value)));
53
- case "Real":
54
- return rightIf(() => Number(value), !Number.isNaN(Number(value)));
55
- case "Text":
56
- return rightIf(() => String(value), typeof value === "string");
57
- case "Boolean": {
58
- if (typeof value === "boolean")
59
- return Either.right(value);
60
- if (value === "true")
61
- return Either.right(true);
62
- if (value === "false")
63
- return Either.right(false);
64
- return Either.left(null);
65
- }
66
- case "DateIso": {
67
- // Instantiate
68
- const date = new Date(value);
69
- return rightIf(() => date, !isNaN(date.getTime()));
70
- }
71
- case "Blob": {
72
- // Instantiate
73
- return Either.right(b64ToU8(value));
74
- }
75
- default:
76
- return Either.left(null);
77
- }
72
+ case "Blob": {
73
+ // Instantiate
74
+ return Either.right(b64ToU8(value));
78
75
  }
79
- // Data Sources
80
- if ("DataSource" in cidlType) {
81
- const objectName = cidlType.DataSource;
82
- return rightIf(() => value, typeof value === "string" &&
83
- (value === NO_DATA_SOURCE ||
84
- this.ast.models[objectName]?.data_sources[value] !== undefined));
76
+ default:
77
+ return Either.left(null);
78
+ }
79
+ }
80
+ // Data Sources
81
+ if ("DataSource" in cidlType) {
82
+ const objectName = cidlType.DataSource;
83
+ return rightIf(
84
+ () => value,
85
+ typeof value === "string" &&
86
+ (value === NO_DATA_SOURCE ||
87
+ this.ast.models[objectName]?.data_sources[value] !== undefined),
88
+ );
89
+ }
90
+ const objName = getObjectName(cidlType);
91
+ // Models
92
+ if (objName && this.ast.models[objName]) {
93
+ const model = this.ast.models[objName];
94
+ if (!model || typeof value !== "object") return Either.left(null);
95
+ const valueObj = value;
96
+ // Validate + instantiate PK
97
+ {
98
+ const pk = model.primary_key;
99
+ const res = this.recurse(valueObj[pk.name], pk.cidl_type, isPartial);
100
+ if (res.isLeft()) {
101
+ return res;
85
102
  }
86
- const objName = getObjectName(cidlType);
87
- // Models
88
- if (objName && this.ast.models[objName]) {
89
- const model = this.ast.models[objName];
90
- if (!model || typeof value !== "object")
91
- return Either.left(null);
92
- const valueObj = value;
93
- // Validate + instantiate PK
94
- {
95
- const pk = model.primary_key;
96
- const res = this.recurse(valueObj[pk.name], pk.cidl_type, isPartial);
97
- if (res.isLeft()) {
98
- return res;
99
- }
100
- value[pk.name] = res.unwrap();
101
- }
102
- // Validate + instantiate attributes
103
- for (let i = 0; i < model.attributes.length; i++) {
104
- const attr = model.attributes[i];
105
- const res = this.recurse(valueObj[attr.value.name], attr.value.cidl_type, isPartial);
106
- if (res.isLeft()) {
107
- return res;
108
- }
109
- value[attr.value.name] = res.unwrap();
110
- }
111
- // Validate + instantiate navigation properties
112
- for (let i = 0; i < model.navigation_properties.length; i++) {
113
- const nav = model.navigation_properties[i];
114
- const res = this.recurse(valueObj[nav.var_name], getNavigationPropertyCidlType(nav), isPartial);
115
- if (res.isLeft()) {
116
- return res;
117
- }
118
- value[nav.var_name] = res.unwrap();
119
- }
120
- // Don't instantiate partials
121
- if (isPartial) {
122
- return Either.right(value);
123
- }
124
- // Instantiate
125
- return Either.right(Object.assign(new this.ctorReg[objName](), value));
103
+ value[pk.name] = res.unwrap();
104
+ }
105
+ // Validate + instantiate attributes
106
+ for (let i = 0; i < model.attributes.length; i++) {
107
+ const attr = model.attributes[i];
108
+ const res = this.recurse(
109
+ valueObj[attr.value.name],
110
+ attr.value.cidl_type,
111
+ isPartial,
112
+ );
113
+ if (res.isLeft()) {
114
+ return res;
126
115
  }
127
- // Plain old Objects
128
- if (objName && this.ast.poos[objName]) {
129
- const poo = this.ast.poos[objName];
130
- if (!poo || typeof value !== "object")
131
- return Either.left(null);
132
- const valueObj = value;
133
- // Validate + instantiate attributes
134
- for (let i = 0; i < poo.attributes.length; i++) {
135
- const attr = poo.attributes[i];
136
- const res = this.recurse(valueObj[attr.name], attr.cidl_type, isPartial);
137
- if (res.isLeft()) {
138
- return res;
139
- }
140
- value[attr.name] = res.unwrap();
141
- }
142
- if (isPartial) {
143
- return Either.right(value);
144
- }
145
- // Instantiate
146
- return Either.right(Object.assign(new this.ctorReg[objName](), value));
116
+ value[attr.value.name] = res.unwrap();
117
+ }
118
+ // Validate + instantiate navigation properties
119
+ for (let i = 0; i < model.navigation_properties.length; i++) {
120
+ const nav = model.navigation_properties[i];
121
+ const res = this.recurse(
122
+ valueObj[nav.var_name],
123
+ getNavigationPropertyCidlType(nav),
124
+ isPartial,
125
+ );
126
+ if (res.isLeft()) {
127
+ return res;
147
128
  }
148
- // Arrays
149
- if ("Array" in cidlType) {
150
- if (!Array.isArray(value)) {
151
- return Either.left(null);
152
- }
153
- for (let i = 0; i < value.length; i++) {
154
- const res = this.recurse(value[i], cidlType.Array, isPartial);
155
- if (res.isLeft()) {
156
- return res;
157
- }
158
- value[i] = res.unwrap();
159
- }
160
- return Either.right(value);
129
+ value[nav.var_name] = res.unwrap();
130
+ }
131
+ // Don't instantiate partials
132
+ if (isPartial) {
133
+ return Either.right(value);
134
+ }
135
+ // Instantiate
136
+ return Either.right(Object.assign(new this.ctorReg[objName](), value));
137
+ }
138
+ // Plain old Objects
139
+ if (objName && this.ast.poos[objName]) {
140
+ const poo = this.ast.poos[objName];
141
+ if (!poo || typeof value !== "object") return Either.left(null);
142
+ const valueObj = value;
143
+ // Validate + instantiate attributes
144
+ for (let i = 0; i < poo.attributes.length; i++) {
145
+ const attr = poo.attributes[i];
146
+ const res = this.recurse(
147
+ valueObj[attr.name],
148
+ attr.cidl_type,
149
+ isPartial,
150
+ );
151
+ if (res.isLeft()) {
152
+ return res;
161
153
  }
154
+ value[attr.name] = res.unwrap();
155
+ }
156
+ if (isPartial) {
157
+ return Either.right(value);
158
+ }
159
+ // Instantiate
160
+ return Either.right(Object.assign(new this.ctorReg[objName](), value));
161
+ }
162
+ // Arrays
163
+ if ("Array" in cidlType) {
164
+ if (!Array.isArray(value)) {
162
165
  return Either.left(null);
166
+ }
167
+ for (let i = 0; i < value.length; i++) {
168
+ const res = this.recurse(value[i], cidlType.Array, isPartial);
169
+ if (res.isLeft()) {
170
+ return res;
171
+ }
172
+ value[i] = res.unwrap();
173
+ }
174
+ return Either.right(value);
163
175
  }
176
+ return Either.left(null);
177
+ }
164
178
  }
165
179
  function getObjectName(ty) {
166
- if (typeof ty === "string") {
167
- return undefined;
168
- }
169
- if ("Partial" in ty) {
170
- return ty.Partial;
171
- }
172
- if ("Object" in ty) {
173
- return ty.Object;
174
- }
180
+ if (typeof ty === "string") {
175
181
  return undefined;
182
+ }
183
+ if ("Partial" in ty) {
184
+ return ty.Partial;
185
+ }
186
+ if ("Object" in ty) {
187
+ return ty.Object;
188
+ }
189
+ return undefined;
176
190
  }
177
191
  function rightIf(value, cond) {
178
- return cond ? Either.right(value()) : Either.left(null);
192
+ return cond ? Either.right(value()) : Either.left(null);
179
193
  }
@@ -5,41 +5,75 @@ import { Either } from "../ui/common.js";
5
5
  * WASM ABI
6
6
  */
7
7
  export interface OrmWasmExports {
8
- memory: WebAssembly.Memory;
9
- get_return_len(): number;
10
- get_return_ptr(): number;
11
- set_meta_ptr(ptr: number, len: number): number;
12
- alloc(len: number): number;
13
- dealloc(ptr: number, len: number): void;
14
- map_sql(model_name_ptr: number, model_name_len: number, sql_rows_ptr: number, sql_rows_len: number, include_tree_ptr: number, include_tree_len: number): boolean;
15
- upsert_model(model_name_ptr: number, model_name_len: number, new_model_ptr: number, new_model_len: number, include_tree_ptr: number, include_tree_len: number): boolean;
16
- list_models(model_name_ptr: number, model_name_len: number, include_tree_ptr: number, include_tree_len: number, tag_cte_ptr: number, tag_cte_len: number, custom_from_ptr: number, custom_from_len: number): boolean;
8
+ memory: WebAssembly.Memory;
9
+ get_return_len(): number;
10
+ get_return_ptr(): number;
11
+ set_meta_ptr(ptr: number, len: number): number;
12
+ alloc(len: number): number;
13
+ dealloc(ptr: number, len: number): void;
14
+ map_sql(
15
+ model_name_ptr: number,
16
+ model_name_len: number,
17
+ sql_rows_ptr: number,
18
+ sql_rows_len: number,
19
+ include_tree_ptr: number,
20
+ include_tree_len: number,
21
+ ): boolean;
22
+ upsert_model(
23
+ model_name_ptr: number,
24
+ model_name_len: number,
25
+ new_model_ptr: number,
26
+ new_model_len: number,
27
+ include_tree_ptr: number,
28
+ include_tree_len: number,
29
+ ): boolean;
30
+ list_models(
31
+ model_name_ptr: number,
32
+ model_name_len: number,
33
+ include_tree_ptr: number,
34
+ include_tree_len: number,
35
+ tag_cte_ptr: number,
36
+ tag_cte_len: number,
37
+ custom_from_ptr: number,
38
+ custom_from_len: number,
39
+ ): boolean;
17
40
  }
18
41
  /**
19
42
  * RAII for wasm memory
20
43
  */
21
44
  export declare class WasmResource {
22
- private wasm;
23
- ptr: number;
24
- len: number;
25
- constructor(wasm: OrmWasmExports, ptr: number, len: number);
26
- free(): void;
27
- /**
28
- * Copies a value from TS memory to WASM memory. A subsequent `free` is necessary.
29
- */
30
- static fromString(str: string, wasm: OrmWasmExports): WasmResource;
45
+ private wasm;
46
+ ptr: number;
47
+ len: number;
48
+ constructor(wasm: OrmWasmExports, ptr: number, len: number);
49
+ free(): void;
50
+ /**
51
+ * Copies a value from TS memory to WASM memory. A subsequent `free` is necessary.
52
+ */
53
+ static fromString(str: string, wasm: OrmWasmExports): WasmResource;
31
54
  }
32
- export declare function loadOrmWasm(ast: CloesceAst, wasm?: WebAssembly.Instance): Promise<OrmWasmExports>;
55
+ export declare function loadOrmWasm(
56
+ ast: CloesceAst,
57
+ wasm?: WebAssembly.Instance,
58
+ ): Promise<OrmWasmExports>;
33
59
  /**
34
60
  * Invokes a WASM ORM function with the provided arguments, handling memory
35
61
  * allocation and deallocation.
36
62
  *
37
63
  * Returns an Either where Left is an error message and Right the raw string result.
38
64
  */
39
- export declare function invokeOrmWasm(fn: (...args: number[]) => boolean, args: WasmResource[], wasm: OrmWasmExports): Either<string, string>;
65
+ export declare function invokeOrmWasm(
66
+ fn: (...args: number[]) => boolean,
67
+ args: WasmResource[],
68
+ wasm: OrmWasmExports,
69
+ ): Either<string, string>;
40
70
  /**
41
71
  * Calls the object relational mapping function to turn a row of SQL records into
42
72
  * an instantiated object.
43
73
  */
44
- export declare function mapSql<T extends object>(ctor: new () => T, records: Record<string, any>[], includeTree: IncludeTree<T> | CidlIncludeTree | null): Either<string, T[]>;
45
- //# sourceMappingURL=wasm.d.ts.map
74
+ export declare function mapSql<T extends object>(
75
+ ctor: new () => T,
76
+ records: Record<string, any>[],
77
+ includeTree: IncludeTree<T> | CidlIncludeTree | null,
78
+ ): Either<string, T[]>;
79
+ //# sourceMappingURL=wasm.d.ts.map
@@ -6,40 +6,42 @@ import { Either } from "../ui/common.js";
6
6
  * RAII for wasm memory
7
7
  */
8
8
  export class WasmResource {
9
- wasm;
10
- ptr;
11
- len;
12
- constructor(wasm, ptr, len) {
13
- this.wasm = wasm;
14
- this.ptr = ptr;
15
- this.len = len;
16
- }
17
- free() {
18
- this.wasm.dealloc(this.ptr, this.len);
19
- }
20
- /**
21
- * Copies a value from TS memory to WASM memory. A subsequent `free` is necessary.
22
- */
23
- static fromString(str, wasm) {
24
- const encoder = new TextEncoder();
25
- const bytes = encoder.encode(str);
26
- const ptr = wasm.alloc(bytes.length);
27
- const mem = new Uint8Array(wasm.memory.buffer, ptr, bytes.length);
28
- mem.set(bytes);
29
- return new this(wasm, ptr, bytes.length);
30
- }
9
+ wasm;
10
+ ptr;
11
+ len;
12
+ constructor(wasm, ptr, len) {
13
+ this.wasm = wasm;
14
+ this.ptr = ptr;
15
+ this.len = len;
16
+ }
17
+ free() {
18
+ this.wasm.dealloc(this.ptr, this.len);
19
+ }
20
+ /**
21
+ * Copies a value from TS memory to WASM memory. A subsequent `free` is necessary.
22
+ */
23
+ static fromString(str, wasm) {
24
+ const encoder = new TextEncoder();
25
+ const bytes = encoder.encode(str);
26
+ const ptr = wasm.alloc(bytes.length);
27
+ const mem = new Uint8Array(wasm.memory.buffer, ptr, bytes.length);
28
+ mem.set(bytes);
29
+ return new this(wasm, ptr, bytes.length);
30
+ }
31
31
  }
32
32
  export async function loadOrmWasm(ast, wasm) {
33
- // Load WASM
34
- const wasmInstance = (wasm ??
35
- (await WebAssembly.instantiate(mod)));
36
- const modelMeta = WasmResource.fromString(JSON.stringify(ast.models), wasmInstance.exports);
37
- if (wasmInstance.exports.set_meta_ptr(modelMeta.ptr, modelMeta.len) != 0) {
38
- modelMeta.free();
39
- throw Error("The WASM Module failed to load due to an invalid CIDL");
40
- }
41
- // Intentionally leak `modelMeta`, it should exist for the programs lifetime.
42
- return wasmInstance.exports;
33
+ // Load WASM
34
+ const wasmInstance = wasm ?? (await WebAssembly.instantiate(mod));
35
+ const modelMeta = WasmResource.fromString(
36
+ JSON.stringify(ast.models),
37
+ wasmInstance.exports,
38
+ );
39
+ if (wasmInstance.exports.set_meta_ptr(modelMeta.ptr, modelMeta.len) != 0) {
40
+ modelMeta.free();
41
+ throw Error("The WASM Module failed to load due to an invalid CIDL");
42
+ }
43
+ // Intentionally leak `modelMeta`, it should exist for the programs lifetime.
44
+ return wasmInstance.exports;
43
45
  }
44
46
  /**
45
47
  * Invokes a WASM ORM function with the provided arguments, handling memory
@@ -48,59 +50,69 @@ export async function loadOrmWasm(ast, wasm) {
48
50
  * Returns an Either where Left is an error message and Right the raw string result.
49
51
  */
50
52
  export function invokeOrmWasm(fn, args, wasm) {
51
- let resPtr;
52
- let resLen;
53
- try {
54
- const failed = fn(...args.flatMap((a) => [a.ptr, a.len]));
55
- resPtr = wasm.get_return_ptr();
56
- resLen = wasm.get_return_len();
57
- const result = new TextDecoder().decode(new Uint8Array(wasm.memory.buffer, resPtr, resLen));
58
- return failed ? Either.left(result) : Either.right(result);
59
- }
60
- finally {
61
- args.forEach((a) => a.free());
62
- if (resPtr && resLen)
63
- wasm.dealloc(resPtr, resLen);
64
- }
53
+ let resPtr;
54
+ let resLen;
55
+ try {
56
+ const failed = fn(...args.flatMap((a) => [a.ptr, a.len]));
57
+ resPtr = wasm.get_return_ptr();
58
+ resLen = wasm.get_return_len();
59
+ const result = new TextDecoder().decode(
60
+ new Uint8Array(wasm.memory.buffer, resPtr, resLen),
61
+ );
62
+ return failed ? Either.left(result) : Either.right(result);
63
+ } finally {
64
+ args.forEach((a) => a.free());
65
+ if (resPtr && resLen) wasm.dealloc(resPtr, resLen);
66
+ }
65
67
  }
66
68
  /**
67
69
  * Calls the object relational mapping function to turn a row of SQL records into
68
70
  * an instantiated object.
69
71
  */
70
72
  export function mapSql(ctor, records, includeTree) {
71
- const { ast, constructorRegistry, wasm } = RuntimeContainer.get();
72
- const args = [
73
- WasmResource.fromString(ctor.name, wasm),
74
- WasmResource.fromString(JSON.stringify(records), wasm),
75
- WasmResource.fromString(JSON.stringify(includeTree), wasm),
76
- ];
77
- const jsonResults = invokeOrmWasm(wasm.map_sql, args, wasm);
78
- if (jsonResults.isLeft())
79
- return jsonResults;
80
- const parsed = JSON.parse(jsonResults.value);
81
- return Either.right(parsed.map((obj) => instantiateDepthFirst(obj, ast.models[ctor.name], includeTree)));
82
- // TODO: Lazy instantiation via Proxy?
83
- function instantiateDepthFirst(m, meta, includeTree) {
84
- m = Object.assign(new constructorRegistry[meta.name](), m);
85
- if (!includeTree) {
86
- return m;
87
- }
88
- for (const navProp of meta.navigation_properties) {
89
- const nestedIncludeTree = includeTree[navProp.var_name];
90
- if (!nestedIncludeTree)
91
- continue;
92
- const nestedMeta = ast.models[navProp.model_name];
93
- // One to Many, Many to Many
94
- if (Array.isArray(m[navProp.var_name])) {
95
- for (let i = 0; i < m[navProp.var_name].length; i++) {
96
- m[navProp.var_name][i] = instantiateDepthFirst(m[navProp.var_name][i], nestedMeta, nestedIncludeTree);
97
- }
98
- }
99
- // One to one
100
- else if (m[navProp.var_name]) {
101
- m[navProp.var_name] = instantiateDepthFirst(m[navProp.var_name], nestedMeta, nestedIncludeTree);
102
- }
73
+ const { ast, constructorRegistry, wasm } = RuntimeContainer.get();
74
+ const args = [
75
+ WasmResource.fromString(ctor.name, wasm),
76
+ WasmResource.fromString(JSON.stringify(records), wasm),
77
+ WasmResource.fromString(JSON.stringify(includeTree), wasm),
78
+ ];
79
+ const jsonResults = invokeOrmWasm(wasm.map_sql, args, wasm);
80
+ if (jsonResults.isLeft()) return jsonResults;
81
+ const parsed = JSON.parse(jsonResults.value);
82
+ return Either.right(
83
+ parsed.map((obj) =>
84
+ instantiateDepthFirst(obj, ast.models[ctor.name], includeTree),
85
+ ),
86
+ );
87
+ // TODO: Lazy instantiation via Proxy?
88
+ function instantiateDepthFirst(m, meta, includeTree) {
89
+ m = Object.assign(new constructorRegistry[meta.name](), m);
90
+ if (!includeTree) {
91
+ return m;
92
+ }
93
+ for (const navProp of meta.navigation_properties) {
94
+ const nestedIncludeTree = includeTree[navProp.var_name];
95
+ if (!nestedIncludeTree) continue;
96
+ const nestedMeta = ast.models[navProp.model_name];
97
+ // One to Many, Many to Many
98
+ if (Array.isArray(m[navProp.var_name])) {
99
+ for (let i = 0; i < m[navProp.var_name].length; i++) {
100
+ m[navProp.var_name][i] = instantiateDepthFirst(
101
+ m[navProp.var_name][i],
102
+ nestedMeta,
103
+ nestedIncludeTree,
104
+ );
103
105
  }
104
- return m;
106
+ }
107
+ // One to one
108
+ else if (m[navProp.var_name]) {
109
+ m[navProp.var_name] = instantiateDepthFirst(
110
+ m[navProp.var_name],
111
+ nestedMeta,
112
+ nestedIncludeTree,
113
+ );
114
+ }
105
115
  }
116
+ return m;
117
+ }
106
118
  }