@prisma/orm-family-sql 0.16.0 → 0.17.0-dev.1

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.
@@ -1482,31 +1482,42 @@ function buildDistinctNonLeafChildRowsSelect(options) {
1482
1482
  * change which rows are aggregated.
1483
1483
  */
1484
1484
  function buildIncludeChildScalarSelect(contract, parentSource, include, scalar) {
1485
- const parentLocalRefs = resolveParentLocalRefs(parentSource, include, [include.localColumn]);
1486
- const parentLocalRef = parentLocalRefs[0];
1487
- assertDefined(parentLocalRef, `Include '${include.relationName}' has no parent-local column ref`);
1485
+ const parentLocalRefs = resolveParentLocalRefs(parentSource, include, localColumnsForRowInclude(include));
1488
1486
  const childSource = resolveChildTableSource(include, parentLocalRefs);
1489
1487
  const childTableAlias = childSource.alias;
1490
1488
  const childTableRef = childSource.tableRef;
1491
1489
  const state = scalar.state;
1492
- const joinExpr = BinaryExpr.eq(ColumnRef.of(childTableRef, include.targetColumn), parentLocalRef);
1493
1490
  const childWhere = buildStateWhere(contract, childTableRef, state, {
1494
1491
  filterTableName: include.relatedTableName,
1495
1492
  namespaceId: include.relatedNamespaceId
1496
1493
  });
1497
- const whereExpr = childWhere ? AndExpr.of([joinExpr, childWhere]) : joinExpr;
1494
+ let whereExpr;
1495
+ let junctionJoins = [];
1496
+ if (include.through !== void 0) {
1497
+ const artifacts = buildManyToManyJunctionArtifacts(parentLocalRefs, childTableRef, include.through);
1498
+ whereExpr = childWhere ? AndExpr.of([artifacts.whereExpr, childWhere]) : artifacts.whereExpr;
1499
+ junctionJoins = [artifacts.junctionJoin];
1500
+ } else {
1501
+ const parentLocalRef = parentLocalRefs[0];
1502
+ assertDefined(parentLocalRef, `Include '${include.relationName}' has no parent-local column ref`);
1503
+ const joinExpr = BinaryExpr.eq(ColumnRef.of(childTableRef, include.targetColumn), parentLocalRef);
1504
+ whereExpr = childWhere ? AndExpr.of([joinExpr, childWhere]) : joinExpr;
1505
+ }
1498
1506
  const remappedOrderBy = childTableAlias && state.orderBy ? state.orderBy.map((item) => item.rewrite(createTableRefRemapper(include.relatedTableName, childTableRef))) : state.orderBy;
1499
1507
  const hasPagination = state.limit !== void 0 || state.offset !== void 0;
1500
1508
  const hasDistinct = state.distinct !== void 0 && state.distinct.length > 0 || state.distinctOn !== void 0 && state.distinctOn.length > 0;
1501
1509
  if (!(hasPagination || hasDistinct)) {
1502
1510
  const aggregateExpr = buildIncludeAggregateExpr(scalar, childTableRef);
1503
1511
  const jsonObjectExpr = JsonObjectExpr.fromEntries([JsonObjectExpr.entry("value", jsonEntryProjection(aggregateExpr, {}))]);
1504
- return SelectAst.from(tableSourceForContract(contract, include.relatedNamespaceId, include.relatedTableName, childTableAlias)).withProjection([ProjectionItem.of(include.relationName, jsonObjectExpr)]).withWhere(whereExpr);
1512
+ let select = SelectAst.from(tableSourceForContract(contract, include.relatedNamespaceId, include.relatedTableName, childTableAlias)).withProjection([ProjectionItem.of(include.relationName, jsonObjectExpr)]).withWhere(whereExpr);
1513
+ if (junctionJoins.length > 0) select = select.withJoins(junctionJoins);
1514
+ return select;
1505
1515
  }
1506
1516
  const innerAlias = `${include.relationName}__scalar`;
1507
1517
  const hiddenOrderProjection = state.distinct !== void 0 && state.distinct.length > 0 && remappedOrderBy !== void 0 && remappedOrderBy.length > 0 ? remappedOrderBy.map((item, index) => ProjectionItem.of(`${include.relationName}__order_${index}`, item.expr)) : [];
1508
1518
  const innerProjection = [...scalar.column !== void 0 ? [ProjectionItem.of(scalar.column, ColumnRef.of(childTableRef, scalar.column))] : [ProjectionItem.of("__row", LiteralExpr.of(1))], ...hiddenOrderProjection];
1509
1519
  let inner = SelectAst.from(tableSourceForContract(contract, include.relatedNamespaceId, include.relatedTableName, childTableAlias)).withProjection(innerProjection).withWhere(whereExpr);
1520
+ if (junctionJoins.length > 0) inner = inner.withJoins(junctionJoins);
1510
1521
  if (state.distinctOn !== void 0 && state.distinctOn.length > 0) {
1511
1522
  inner = inner.withDistinctOn(state.distinctOn.map((column) => ColumnRef.of(childTableRef, column)));
1512
1523
  if (remappedOrderBy !== void 0 && remappedOrderBy.length > 0) inner = inner.withOrderBy(remappedOrderBy);