@cheetah.js/orm 0.1.103 → 0.1.105

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,7 +44,10 @@ 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;
50
+ private processAllOneToManyJoinedResults;
48
51
  private attachOneToManyRelations;
49
52
  private removeDuplicatesByPrimaryKey;
50
53
  private getPrimaryKeyName;
@@ -210,6 +210,10 @@ class SqlBuilder {
210
210
  return [];
211
211
  }
212
212
  const rows = result.query.rows;
213
+ const hasOneToManyJoinedJoin = this.hasOneToManyJoinedJoin();
214
+ if (hasOneToManyJoinedJoin) {
215
+ return this.processAllOneToManyJoinedResults(rows);
216
+ }
213
217
  const results = [];
214
218
  for (const row of rows) {
215
219
  const models = this.modelTransformer.transform(this.model, this.statements, row);
@@ -227,10 +231,39 @@ class SqlBuilder {
227
231
  return false;
228
232
  }
229
233
  return this.statements.join.some(join => {
230
- 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);
231
239
  return relationship?.relation === 'one-to-many';
232
240
  });
233
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
+ }
234
267
  async processOneToManyJoinedResult(rows) {
235
268
  const primaryKey = this.getPrimaryKeyName();
236
269
  const alias = this.statements.alias;
@@ -242,16 +275,46 @@ class SqlBuilder {
242
275
  this.attachOneToManyRelations(model, relatedRows);
243
276
  return model;
244
277
  }
278
+ async processAllOneToManyJoinedResults(rows) {
279
+ const primaryKey = this.getPrimaryKeyName();
280
+ const alias = this.statements.alias;
281
+ const primaryKeyColumn = `${alias}_${primaryKey}`;
282
+ const groupedRows = new Map();
283
+ for (const row of rows) {
284
+ const pkValue = row[primaryKeyColumn];
285
+ if (!groupedRows.has(pkValue)) {
286
+ groupedRows.set(pkValue, []);
287
+ }
288
+ groupedRows.get(pkValue).push(row);
289
+ }
290
+ const results = [];
291
+ for (const [, relatedRows] of groupedRows) {
292
+ const model = this.modelTransformer.transform(this.model, this.statements, relatedRows[0]);
293
+ this.afterHooks(model);
294
+ this.attachOneToManyRelations(model, relatedRows);
295
+ results.push(model);
296
+ }
297
+ return results;
298
+ }
245
299
  attachOneToManyRelations(model, rows) {
246
300
  if (!this.statements.join) {
247
301
  return;
248
302
  }
249
303
  for (const join of this.statements.join) {
250
- 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);
251
309
  if (relationship?.relation === 'one-to-many') {
252
310
  const joinedModels = rows.map(row => this.modelTransformer.transform(join.joinEntity, { alias: join.joinAlias }, row));
253
311
  const uniqueModels = this.removeDuplicatesByPrimaryKey(joinedModels, join.joinEntity);
254
- 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
+ }
255
318
  }
256
319
  }
257
320
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cheetah.js/orm",
3
- "version": "0.1.103",
3
+ "version": "0.1.105",
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": "d1e6603d2e9c4f45d469eb6443d004ef96bdf76c"
58
+ "gitHead": "e25d2331c9fca65627ad761e8dd4ade19d97fcb8"
59
59
  }