orchid-orm 1.6.3 → 1.6.4

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
@@ -363,36 +363,41 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
363
363
  if ("through" in relation.options) {
364
364
  const { through, source } = relation.options;
365
365
  const throughRelation = getThroughRelation(table, through);
366
- const throughTable = throughRelation.table;
367
366
  const sourceRelation = getSourceRelation(throughRelation, source);
368
367
  const sourceQuery = sourceRelation.joinQuery(throughRelation.query, sourceRelation.query).as(relationName);
368
+ const whereExistsCallback = () => sourceQuery;
369
369
  return {
370
370
  returns: "one",
371
371
  method: (params) => {
372
372
  const throughQuery = table[through](params);
373
- return query.whereExists(throughTable, () => sourceQuery.merge(throughQuery));
373
+ return query.whereExists(
374
+ throughQuery,
375
+ whereExistsCallback
376
+ );
374
377
  },
375
378
  joinQuery(fromQuery, toQuery) {
376
- return toQuery.whereExists(throughTable, () => {
377
- const as = getQueryAs(toQuery);
378
- return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
379
- sourceRelation.joinQuery(
379
+ return toQuery.whereExists(
380
+ throughRelation.joinQuery(fromQuery, throughRelation.query),
381
+ () => {
382
+ const as = getQueryAs(toQuery);
383
+ return sourceRelation.joinQuery(
380
384
  throughRelation.query,
381
385
  sourceRelation.query.as(as)
382
- )
383
- );
384
- });
386
+ );
387
+ }
388
+ );
385
389
  },
386
390
  reverseJoin(fromQuery, toQuery) {
387
- return fromQuery.whereExists(throughTable, () => {
388
- const as = getQueryAs(toQuery);
389
- return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
390
- sourceRelation.joinQuery(
391
+ return fromQuery.whereExists(
392
+ throughRelation.joinQuery(fromQuery, throughRelation.query),
393
+ () => {
394
+ const as = getQueryAs(toQuery);
395
+ return sourceRelation.joinQuery(
391
396
  throughRelation.query,
392
397
  sourceRelation.query.as(as)
393
- )
394
- );
395
- });
398
+ );
399
+ }
400
+ );
396
401
  },
397
402
  primaryKey: sourceRelation.primaryKey
398
403
  };
@@ -550,40 +555,45 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
550
555
  if ("through" in relation.options) {
551
556
  const { through, source } = relation.options;
552
557
  const throughRelation = getThroughRelation(table, through);
553
- const throughTable = throughRelation.table.as(through);
554
558
  const sourceRelation = getSourceRelation(throughRelation, source);
555
559
  const sourceRelationQuery = sourceRelation.query.as(relationName);
556
560
  const sourceQuery = sourceRelation.joinQuery(
557
561
  throughRelation.query,
558
562
  sourceRelationQuery
559
563
  );
564
+ const whereExistsCallback = () => sourceQuery;
560
565
  return {
561
566
  returns: "many",
562
567
  method: (params) => {
563
568
  const throughQuery = table[through](params);
564
- return query.whereExists(throughTable, () => sourceQuery.merge(throughQuery));
569
+ return query.whereExists(
570
+ throughQuery,
571
+ whereExistsCallback
572
+ );
565
573
  },
566
574
  joinQuery(fromQuery, toQuery) {
567
- return toQuery.whereExists(throughTable, () => {
568
- const as = getQueryAs(toQuery);
569
- return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
570
- sourceRelation.joinQuery(
575
+ return toQuery.whereExists(
576
+ throughRelation.joinQuery(fromQuery, throughRelation.query),
577
+ () => {
578
+ const as = getQueryAs(toQuery);
579
+ return sourceRelation.joinQuery(
571
580
  throughRelation.query,
572
581
  sourceRelation.query.as(as)
573
- )
574
- );
575
- });
582
+ );
583
+ }
584
+ );
576
585
  },
577
586
  reverseJoin(fromQuery, toQuery) {
578
- return fromQuery.whereExists(throughTable, () => {
579
- const as = getQueryAs(toQuery);
580
- return throughRelation.joinQuery(fromQuery, throughRelation.query).merge(
581
- sourceRelation.joinQuery(
587
+ return fromQuery.whereExists(
588
+ throughRelation.joinQuery(fromQuery, throughRelation.query),
589
+ () => {
590
+ const as = getQueryAs(toQuery);
591
+ return sourceRelation.joinQuery(
582
592
  throughRelation.query,
583
593
  sourceRelation.query.as(as)
584
- )
585
- );
586
- });
594
+ );
595
+ }
596
+ );
587
597
  },
588
598
  primaryKey: sourceRelation.primaryKey
589
599
  };
@@ -790,6 +800,13 @@ class HasAndBelongsToManyVirtualColumn extends VirtualColumn {
790
800
  );
791
801
  }
792
802
  }
803
+ const removeColumnName = (column) => {
804
+ if (!column.data.name)
805
+ return column;
806
+ const cloned = Object.create(column);
807
+ cloned.data = __spreadProps$3(__spreadValues$4({}, column.data), { name: void 0 });
808
+ return cloned;
809
+ };
793
810
  const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query) => {
794
811
  const {
795
812
  primaryKey: pk,
@@ -805,8 +822,8 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
805
822
  baseQuery.baseQuery = baseQuery;
806
823
  baseQuery.table = joinTable;
807
824
  baseQuery.shape = {
808
- [fk]: table.shape[pk],
809
- [afk]: query.shape[apk]
825
+ [fk]: removeColumnName(table.shape[pk]),
826
+ [afk]: removeColumnName(query.shape[apk])
810
827
  };
811
828
  baseQuery.query = __spreadProps$3(__spreadValues$4({}, baseQuery.query), {
812
829
  shape: baseQuery.shape
@@ -839,15 +856,19 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
839
856
  state
840
857
  ),
841
858
  joinQuery(fromQuery, toQuery) {
842
- return toQuery.whereExists(
859
+ const join = toQuery.whereExists(
843
860
  subQuery,
844
- (q) => q._on(associationForeignKeyFull, `${getQueryAs(toQuery)}.${pk}`)._on(foreignKeyFull, `${getQueryAs(fromQuery)}.${pk}`)
861
+ (q) => q._on(associationForeignKeyFull, `${getQueryAs(toQuery)}.${apk}`)._on(foreignKeyFull, `${getQueryAs(fromQuery)}.${pk}`)
845
862
  );
863
+ join.query.joinedShapes = __spreadProps$3(__spreadValues$4({}, join.query.joinedShapes), {
864
+ [fromQuery.query.as || fromQuery.table]: fromQuery.query.shape
865
+ });
866
+ return join;
846
867
  },
847
868
  reverseJoin(fromQuery, toQuery) {
848
869
  return fromQuery.whereExists(
849
870
  subQuery,
850
- (q) => q._on(associationForeignKeyFull, `${getQueryAs(toQuery)}.${pk}`)._on(foreignKeyFull, `${getQueryAs(fromQuery)}.${pk}`)
871
+ (q) => q._on(associationForeignKeyFull, `${getQueryAs(toQuery)}.${apk}`)._on(foreignKeyFull, `${getQueryAs(fromQuery)}.${pk}`)
851
872
  );
852
873
  },
853
874
  primaryKey: pk,
@@ -1211,11 +1232,8 @@ const makeRelationQuery = (table, definedAs, relationName, data) => {
1211
1232
  toTable._take();
1212
1233
  }
1213
1234
  const query = this.isSubQuery ? toTable : toTable._whereExists(
1214
- this,
1215
- (q) => data.reverseJoin(
1216
- q,
1217
- toTable
1218
- )
1235
+ this.baseQuery,
1236
+ (q) => data.reverseJoin(this, toTable)
1219
1237
  );
1220
1238
  query.query[relationQueryKey] = {
1221
1239
  relationName,
@@ -1760,7 +1778,7 @@ const createTable = async (_a) => {
1760
1778
  if (ast.schema) {
1761
1779
  props.push(`schema = ${singleQuote(ast.schema)};`);
1762
1780
  }
1763
- props.push(`table = ${singleQuote(ast.name)};`);
1781
+ props.push(`readonly table = ${singleQuote(ast.name)};`);
1764
1782
  if (ast.noPrimaryKey === "ignore") {
1765
1783
  props.push("noPrimaryKey = true;");
1766
1784
  }