orchid-orm 1.5.37 → 1.5.38

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.js CHANGED
@@ -386,41 +386,36 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
386
386
  if ("through" in relation.options) {
387
387
  const { through, source } = relation.options;
388
388
  const throughRelation = getThroughRelation(table, through);
389
+ const throughTable = throughRelation.table;
389
390
  const sourceRelation = getSourceRelation(throughRelation, source);
390
391
  const sourceQuery = sourceRelation.joinQuery(throughRelation.query, sourceRelation.query).as(relationName);
391
- const whereExistsCallback = () => sourceQuery;
392
392
  return {
393
393
  returns: "one",
394
394
  method: (params) => {
395
395
  const throughQuery = table[through](params);
396
- return query.whereExists(
397
- throughQuery,
398
- whereExistsCallback
399
- );
396
+ return query.whereExists(throughTable, () => sourceQuery.merge(throughQuery));
400
397
  },
401
398
  joinQuery(fromQuery, toQuery) {
402
- return toQuery.whereExists(
403
- throughRelation.joinQuery(fromQuery, throughRelation.query),
404
- () => {
405
- const as = pqb.getQueryAs(toQuery);
406
- return sourceRelation.joinQuery(
399
+ return toQuery.whereExists(throughTable, () => {
400
+ const as = pqb.getQueryAs(toQuery);
401
+ return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
402
+ sourceRelation.joinQuery(
407
403
  throughRelation.query,
408
404
  sourceRelation.query.as(as)
409
- );
410
- }
411
- );
405
+ )
406
+ );
407
+ });
412
408
  },
413
409
  reverseJoin(fromQuery, toQuery) {
414
- return fromQuery.whereExists(
415
- throughRelation.joinQuery(fromQuery, throughRelation.query),
416
- () => {
417
- const as = pqb.getQueryAs(toQuery);
418
- return sourceRelation.joinQuery(
410
+ return fromQuery.whereExists(throughTable, () => {
411
+ const as = pqb.getQueryAs(toQuery);
412
+ return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
413
+ sourceRelation.joinQuery(
419
414
  throughRelation.query,
420
415
  sourceRelation.query.as(as)
421
- );
422
- }
423
- );
416
+ )
417
+ );
418
+ });
424
419
  },
425
420
  primaryKey: sourceRelation.primaryKey
426
421
  };
@@ -578,45 +573,40 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
578
573
  if ("through" in relation.options) {
579
574
  const { through, source } = relation.options;
580
575
  const throughRelation = getThroughRelation(table, through);
576
+ const throughTable = throughRelation.table.as(through);
581
577
  const sourceRelation = getSourceRelation(throughRelation, source);
582
578
  const sourceRelationQuery = sourceRelation.query.as(relationName);
583
579
  const sourceQuery = sourceRelation.joinQuery(
584
580
  throughRelation.query,
585
581
  sourceRelationQuery
586
582
  );
587
- const whereExistsCallback = () => sourceQuery;
588
583
  return {
589
584
  returns: "many",
590
585
  method: (params) => {
591
586
  const throughQuery = table[through](params);
592
- return query.whereExists(
593
- throughQuery,
594
- whereExistsCallback
595
- );
587
+ return query.whereExists(throughTable, () => sourceQuery.merge(throughQuery));
596
588
  },
597
589
  joinQuery(fromQuery, toQuery) {
598
- return toQuery.whereExists(
599
- throughRelation.joinQuery(fromQuery, throughRelation.query),
600
- () => {
601
- const as = pqb.getQueryAs(toQuery);
602
- return sourceRelation.joinQuery(
590
+ return toQuery.whereExists(throughTable, () => {
591
+ const as = pqb.getQueryAs(toQuery);
592
+ return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
593
+ sourceRelation.joinQuery(
603
594
  throughRelation.query,
604
595
  sourceRelation.query.as(as)
605
- );
606
- }
607
- );
596
+ )
597
+ );
598
+ });
608
599
  },
609
600
  reverseJoin(fromQuery, toQuery) {
610
- return fromQuery.whereExists(
611
- throughRelation.joinQuery(fromQuery, throughRelation.query),
612
- () => {
613
- const as = pqb.getQueryAs(toQuery);
614
- return sourceRelation.joinQuery(
601
+ return fromQuery.whereExists(throughTable, () => {
602
+ const as = pqb.getQueryAs(toQuery);
603
+ return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
604
+ sourceRelation.joinQuery(
615
605
  throughRelation.query,
616
606
  sourceRelation.query.as(as)
617
- );
618
- }
619
- );
607
+ )
608
+ );
609
+ });
620
610
  },
621
611
  primaryKey: sourceRelation.primaryKey
622
612
  };
@@ -1239,7 +1229,10 @@ const makeRelationQuery = (table, definedAs, relationName, data) => {
1239
1229
  if (data.returns === "one") {
1240
1230
  toTable._take();
1241
1231
  }
1242
- const query = this.isSubQuery ? toTable : toTable._whereExists(data.reverseJoin(this, toTable), (q) => q);
1232
+ const query = this.isSubQuery ? toTable : toTable._whereExists(
1233
+ this.baseQuery,
1234
+ (q) => data.reverseJoin(this, toTable)
1235
+ );
1243
1236
  query.query[pqb.relationQueryKey] = {
1244
1237
  relationName,
1245
1238
  sourceQuery: this,