@cheetah.js/orm 0.1.104 → 0.1.106

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.
@@ -44,6 +44,8 @@ export declare class SqlBuilder<T> {
44
44
  executeAndReturnFirstOrFail(): Promise<T>;
45
45
  executeAndReturnAll(): Promise<T[]>;
46
46
  private hasOneToManyJoinedJoin;
47
+ private getOriginEntityForJoin;
48
+ private findNestedModel;
47
49
  private processOneToManyJoinedResult;
48
50
  private processAllOneToManyJoinedResults;
49
51
  private attachOneToManyRelations;
@@ -231,10 +231,39 @@ class SqlBuilder {
231
231
  return false;
232
232
  }
233
233
  return this.statements.join.some(join => {
234
- const relationship = this.entity.relations.find(rel => rel.propertyKey === join.joinProperty);
234
+ const originEntity = this.getOriginEntityForJoin(join);
235
+ if (!originEntity) {
236
+ return false;
237
+ }
238
+ const relationship = originEntity.relations.find(rel => rel.propertyKey === join.joinProperty);
235
239
  return relationship?.relation === 'one-to-many';
236
240
  });
237
241
  }
242
+ getOriginEntityForJoin(join) {
243
+ const rootAlias = this.statements.alias;
244
+ if (join.originAlias === rootAlias) {
245
+ return this.entity;
246
+ }
247
+ const parentJoin = this.statements.join.find(j => j.joinAlias === join.originAlias);
248
+ if (parentJoin && parentJoin.joinEntity) {
249
+ return this.entityStorage.get(parentJoin.joinEntity);
250
+ }
251
+ return null;
252
+ }
253
+ findNestedModel(model, targetAlias) {
254
+ if (!this.statements.join) {
255
+ return null;
256
+ }
257
+ for (const join of this.statements.join) {
258
+ if (join.joinAlias === targetAlias) {
259
+ const parentModel = join.originAlias === this.statements.alias
260
+ ? model
261
+ : this.findNestedModel(model, join.originAlias);
262
+ return parentModel?.[join.joinProperty];
263
+ }
264
+ }
265
+ return null;
266
+ }
238
267
  async processOneToManyJoinedResult(rows) {
239
268
  const primaryKey = this.getPrimaryKeyName();
240
269
  const alias = this.statements.alias;
@@ -272,11 +301,20 @@ class SqlBuilder {
272
301
  return;
273
302
  }
274
303
  for (const join of this.statements.join) {
275
- const relationship = this.entity.relations.find(rel => rel.propertyKey === join.joinProperty);
304
+ const originEntity = this.getOriginEntityForJoin(join);
305
+ if (!originEntity) {
306
+ continue;
307
+ }
308
+ const relationship = originEntity.relations.find(rel => rel.propertyKey === join.joinProperty);
276
309
  if (relationship?.relation === 'one-to-many') {
277
310
  const joinedModels = rows.map(row => this.modelTransformer.transform(join.joinEntity, { alias: join.joinAlias }, row));
278
311
  const uniqueModels = this.removeDuplicatesByPrimaryKey(joinedModels, join.joinEntity);
279
- model[join.joinProperty] = uniqueModels;
312
+ const targetModel = join.originAlias === this.statements.alias
313
+ ? model
314
+ : this.findNestedModel(model, join.originAlias);
315
+ if (targetModel) {
316
+ targetModel[join.joinProperty] = uniqueModels;
317
+ }
280
318
  }
281
319
  }
282
320
  }
@@ -31,6 +31,11 @@ class ValueProcessor {
31
31
  newValue[columnName] = values[value].getValue();
32
32
  continue;
33
33
  }
34
+ if (ValueProcessor.isBaseEntity(values[value])) {
35
+ // @ts-ignore
36
+ newValue[columnName] = values[value].id;
37
+ continue;
38
+ }
34
39
  newValue[columnName] = values[value];
35
40
  }
36
41
  return newValue;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cheetah.js/orm",
3
- "version": "0.1.104",
3
+ "version": "0.1.106",
4
4
  "description": "A simple ORM for Cheetah.js",
5
5
  "type": "commonjs",
6
6
  "main": "dist/index.js",
@@ -55,5 +55,5 @@
55
55
  "bun",
56
56
  "value-object"
57
57
  ],
58
- "gitHead": "b1e6711ff0b5df0bb5c72b89ad2ee77120c7c2f5"
58
+ "gitHead": "90721637d6b394e2bbf17278296122111358b881"
59
59
  }