orchid-orm 1.5.37 → 1.5.39

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.
package/dist/index.mjs CHANGED
@@ -359,41 +359,36 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
359
359
  if ("through" in relation.options) {
360
360
  const { through, source } = relation.options;
361
361
  const throughRelation = getThroughRelation(table, through);
362
+ const throughTable = throughRelation.table;
362
363
  const sourceRelation = getSourceRelation(throughRelation, source);
363
364
  const sourceQuery = sourceRelation.joinQuery(throughRelation.query, sourceRelation.query).as(relationName);
364
- const whereExistsCallback = () => sourceQuery;
365
365
  return {
366
366
  returns: "one",
367
367
  method: (params) => {
368
368
  const throughQuery = table[through](params);
369
- return query.whereExists(
370
- throughQuery,
371
- whereExistsCallback
372
- );
369
+ return query.whereExists(throughTable, () => sourceQuery.merge(throughQuery));
373
370
  },
374
371
  joinQuery(fromQuery, toQuery) {
375
- return toQuery.whereExists(
376
- throughRelation.joinQuery(fromQuery, throughRelation.query),
377
- () => {
378
- const as = getQueryAs(toQuery);
379
- return sourceRelation.joinQuery(
372
+ return toQuery.whereExists(throughTable, () => {
373
+ const as = getQueryAs(toQuery);
374
+ return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
375
+ sourceRelation.joinQuery(
380
376
  throughRelation.query,
381
377
  sourceRelation.query.as(as)
382
- );
383
- }
384
- );
378
+ )
379
+ );
380
+ });
385
381
  },
386
382
  reverseJoin(fromQuery, toQuery) {
387
- return fromQuery.whereExists(
388
- throughRelation.joinQuery(fromQuery, throughRelation.query),
389
- () => {
390
- const as = getQueryAs(toQuery);
391
- return sourceRelation.joinQuery(
383
+ return fromQuery.whereExists(throughTable, () => {
384
+ const as = getQueryAs(toQuery);
385
+ return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
386
+ sourceRelation.joinQuery(
392
387
  throughRelation.query,
393
388
  sourceRelation.query.as(as)
394
- );
395
- }
396
- );
389
+ )
390
+ );
391
+ });
397
392
  },
398
393
  primaryKey: sourceRelation.primaryKey
399
394
  };
@@ -551,45 +546,40 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
551
546
  if ("through" in relation.options) {
552
547
  const { through, source } = relation.options;
553
548
  const throughRelation = getThroughRelation(table, through);
549
+ const throughTable = throughRelation.table.as(through);
554
550
  const sourceRelation = getSourceRelation(throughRelation, source);
555
551
  const sourceRelationQuery = sourceRelation.query.as(relationName);
556
552
  const sourceQuery = sourceRelation.joinQuery(
557
553
  throughRelation.query,
558
554
  sourceRelationQuery
559
555
  );
560
- const whereExistsCallback = () => sourceQuery;
561
556
  return {
562
557
  returns: "many",
563
558
  method: (params) => {
564
559
  const throughQuery = table[through](params);
565
- return query.whereExists(
566
- throughQuery,
567
- whereExistsCallback
568
- );
560
+ return query.whereExists(throughTable, () => sourceQuery.merge(throughQuery));
569
561
  },
570
562
  joinQuery(fromQuery, toQuery) {
571
- return toQuery.whereExists(
572
- throughRelation.joinQuery(fromQuery, throughRelation.query),
573
- () => {
574
- const as = getQueryAs(toQuery);
575
- return sourceRelation.joinQuery(
563
+ return toQuery.whereExists(throughTable, () => {
564
+ const as = getQueryAs(toQuery);
565
+ return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
566
+ sourceRelation.joinQuery(
576
567
  throughRelation.query,
577
568
  sourceRelation.query.as(as)
578
- );
579
- }
580
- );
569
+ )
570
+ );
571
+ });
581
572
  },
582
573
  reverseJoin(fromQuery, toQuery) {
583
- return fromQuery.whereExists(
584
- throughRelation.joinQuery(fromQuery, throughRelation.query),
585
- () => {
586
- const as = getQueryAs(toQuery);
587
- return sourceRelation.joinQuery(
574
+ return fromQuery.whereExists(throughTable, () => {
575
+ const as = getQueryAs(toQuery);
576
+ return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
577
+ sourceRelation.joinQuery(
588
578
  throughRelation.query,
589
579
  sourceRelation.query.as(as)
590
- );
591
- }
592
- );
580
+ )
581
+ );
582
+ });
593
583
  },
594
584
  primaryKey: sourceRelation.primaryKey
595
585
  };
@@ -1212,7 +1202,10 @@ const makeRelationQuery = (table, definedAs, relationName, data) => {
1212
1202
  if (data.returns === "one") {
1213
1203
  toTable._take();
1214
1204
  }
1215
- const query = this.isSubQuery ? toTable : toTable._whereExists(data.reverseJoin(this, toTable), (q) => q);
1205
+ const query = this.isSubQuery ? toTable : toTable._whereExists(
1206
+ this.baseQuery,
1207
+ (q) => data.reverseJoin(this, toTable)
1208
+ );
1216
1209
  query.query[relationQueryKey] = {
1217
1210
  relationName,
1218
1211
  sourceQuery: this,