@prisma-next/sql-lane 0.3.0-pr.96.1 → 0.3.0-pr.96.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.
@@ -160,7 +160,7 @@ import { createJoinOnBuilder as createJoinOnBuilder2 } from "@prisma-next/sql-re
160
160
 
161
161
  // src/sql/mutation-builder.ts
162
162
  import {
163
- createColumnRef as createColumnRef2,
163
+ createColumnRef,
164
164
  createDeleteAst,
165
165
  createInsertAst,
166
166
  createParamRef as createParamRef2,
@@ -246,7 +246,7 @@ function errorMissingParameter(paramName) {
246
246
  }
247
247
  function errorInvalidProjectionValue(path) {
248
248
  throw planInvalid2(
249
- `Invalid projection value at path ${path.join(".")}: expected ColumnBuilder or nested object`
249
+ `Invalid projection value at path ${path.join(".")}: expected ExpressionSource (ColumnBuilder or ExpressionBuilder) or nested object`
250
250
  );
251
251
  }
252
252
  function errorIncludeAliasNotFound(alias) {
@@ -337,9 +337,8 @@ function checkReturningCapability(contract) {
337
337
  import { compact } from "@prisma-next/sql-relational-core/ast";
338
338
  import {
339
339
  collectColumnRefs,
340
- getColumnInfo,
341
- getOperationExpr,
342
340
  isColumnBuilder,
341
+ isExpressionBuilder,
343
342
  isOperationExpr
344
343
  } from "@prisma-next/sql-relational-core/utils/guards";
345
344
 
@@ -353,28 +352,44 @@ function assertColumnBuilder(col, context) {
353
352
  }
354
353
 
355
354
  // src/sql/plan.ts
355
+ function collectRefsFromExpressionSource(source, refsColumns) {
356
+ if (isExpressionBuilder(source)) {
357
+ const allRefs = collectColumnRefs(source.expr);
358
+ for (const ref of allRefs) {
359
+ refsColumns.set(`${ref.table}.${ref.column}`, {
360
+ table: ref.table,
361
+ column: ref.column
362
+ });
363
+ }
364
+ } else if (isColumnBuilder(source)) {
365
+ const col = source;
366
+ refsColumns.set(`${col.table}.${col.column}`, {
367
+ table: col.table,
368
+ column: col.column
369
+ });
370
+ }
371
+ }
372
+ function collectRefsFromExpression(expr, refsColumns) {
373
+ if (isOperationExpr(expr)) {
374
+ const allRefs = collectColumnRefs(expr);
375
+ for (const ref of allRefs) {
376
+ refsColumns.set(`${ref.table}.${ref.column}`, {
377
+ table: ref.table,
378
+ column: ref.column
379
+ });
380
+ }
381
+ } else if (expr.kind === "col") {
382
+ refsColumns.set(`${expr.table}.${expr.column}`, {
383
+ table: expr.table,
384
+ column: expr.column
385
+ });
386
+ }
387
+ }
356
388
  function buildMeta(args) {
357
389
  const refsColumns = /* @__PURE__ */ new Map();
358
390
  const refsTables = /* @__PURE__ */ new Set([args.table.name]);
359
391
  for (const column of args.projection.columns) {
360
- if (!column) {
361
- continue;
362
- }
363
- const operationExpr = getOperationExpr(column);
364
- if (operationExpr) {
365
- const allRefs = collectColumnRefs(operationExpr);
366
- for (const ref of allRefs) {
367
- refsColumns.set(`${ref.table}.${ref.column}`, {
368
- table: ref.table,
369
- column: ref.column
370
- });
371
- }
372
- } else {
373
- refsColumns.set(`${column.table}.${column.column}`, {
374
- table: column.table,
375
- column: column.column
376
- });
377
- }
392
+ collectRefsFromExpressionSource(column, refsColumns);
378
393
  }
379
394
  if (args.joins) {
380
395
  for (const join of args.joins) {
@@ -412,36 +427,21 @@ function buildMeta(args) {
412
427
  });
413
428
  }
414
429
  if (include.childWhere) {
415
- const colInfo = getColumnInfo(include.childWhere.left);
416
- refsColumns.set(`${colInfo.table}.${colInfo.column}`, {
417
- table: colInfo.table,
418
- column: colInfo.column
419
- });
430
+ collectRefsFromExpression(include.childWhere.left, refsColumns);
420
431
  const childWhereRight = include.childWhere.right;
421
- if (isColumnBuilder(childWhereRight)) {
422
- const rightColInfo = getColumnInfo(childWhereRight);
423
- refsColumns.set(`${rightColInfo.table}.${rightColInfo.column}`, {
424
- table: rightColInfo.table,
425
- column: rightColInfo.column
426
- });
432
+ if (isColumnBuilder(childWhereRight) || isExpressionBuilder(childWhereRight)) {
433
+ collectRefsFromExpressionSource(childWhereRight, refsColumns);
427
434
  }
428
435
  }
429
436
  if (include.childOrderBy) {
430
- const orderBy = include.childOrderBy;
431
- if (orderBy.expr) {
432
- const colInfo = getColumnInfo(orderBy.expr);
433
- refsColumns.set(`${colInfo.table}.${colInfo.column}`, {
434
- table: colInfo.table,
435
- column: colInfo.column
436
- });
437
- }
437
+ collectRefsFromExpression(include.childOrderBy.expr, refsColumns);
438
438
  }
439
439
  }
440
440
  }
441
441
  if (args.where) {
442
- const whereLeft = args.where.left;
443
- if (isOperationExpr(whereLeft)) {
444
- const allRefs = collectColumnRefs(whereLeft);
442
+ const leftExpr = args.where.left;
443
+ if (isOperationExpr(leftExpr)) {
444
+ const allRefs = collectColumnRefs(leftExpr);
445
445
  for (const ref of allRefs) {
446
446
  refsColumns.set(`${ref.table}.${ref.column}`, {
447
447
  table: ref.table,
@@ -449,50 +449,31 @@ function buildMeta(args) {
449
449
  });
450
450
  }
451
451
  } else {
452
- const operationExpr = whereLeft._operationExpr;
453
- if (operationExpr) {
454
- const allRefs = collectColumnRefs(operationExpr);
455
- for (const ref of allRefs) {
456
- refsColumns.set(`${ref.table}.${ref.column}`, {
457
- table: ref.table,
458
- column: ref.column
459
- });
460
- }
461
- } else {
462
- const colBuilder = assertColumnBuilder(whereLeft, "where clause must be a ColumnBuilder");
463
- refsColumns.set(`${colBuilder.table}.${colBuilder.column}`, {
464
- table: colBuilder.table,
465
- column: colBuilder.column
466
- });
467
- }
452
+ refsColumns.set(`${leftExpr.table}.${leftExpr.column}`, {
453
+ table: leftExpr.table,
454
+ column: leftExpr.column
455
+ });
468
456
  }
469
457
  const whereRight = args.where.right;
470
- if (isColumnBuilder(whereRight)) {
471
- const colInfo = getColumnInfo(whereRight);
472
- refsColumns.set(`${colInfo.table}.${colInfo.column}`, {
473
- table: colInfo.table,
474
- column: colInfo.column
475
- });
458
+ if (isColumnBuilder(whereRight) || isExpressionBuilder(whereRight)) {
459
+ collectRefsFromExpressionSource(whereRight, refsColumns);
476
460
  }
477
461
  }
478
462
  if (args.orderBy) {
479
- const orderBy = args.orderBy;
480
- const orderByExpr = orderBy.expr;
481
- if (orderByExpr) {
482
- if (isOperationExpr(orderByExpr)) {
483
- const allRefs = collectColumnRefs(orderByExpr);
484
- for (const ref of allRefs) {
485
- refsColumns.set(`${ref.table}.${ref.column}`, {
486
- table: ref.table,
487
- column: ref.column
488
- });
489
- }
490
- } else {
491
- refsColumns.set(`${orderByExpr.table}.${orderByExpr.column}`, {
492
- table: orderByExpr.table,
493
- column: orderByExpr.column
463
+ const orderByExpr = args.orderBy.expr;
464
+ if (isOperationExpr(orderByExpr)) {
465
+ const allRefs = collectColumnRefs(orderByExpr);
466
+ for (const ref of allRefs) {
467
+ refsColumns.set(`${ref.table}.${ref.column}`, {
468
+ table: ref.table,
469
+ column: ref.column
494
470
  });
495
471
  }
472
+ } else {
473
+ refsColumns.set(`${orderByExpr.table}.${orderByExpr.column}`, {
474
+ table: orderByExpr.table,
475
+ column: orderByExpr.column
476
+ });
496
477
  }
497
478
  }
498
479
  const includeAliases = new Set(args.includes?.map((inc) => inc.alias) ?? []);
@@ -505,11 +486,14 @@ function buildMeta(args) {
505
486
  if (!column) {
506
487
  errorMissingColumnForAlias(alias, index);
507
488
  }
508
- const operationExpr = getOperationExpr(column);
509
- if (operationExpr) {
510
- return [alias, `operation:${operationExpr.method}`];
489
+ if (isExpressionBuilder(column)) {
490
+ return [alias, `operation:${column.expr.method}`];
511
491
  }
512
- return [alias, `${column.table}.${column.column}`];
492
+ const col = column;
493
+ if (!col.table || !col.column) {
494
+ return [alias, `include:${alias}`];
495
+ }
496
+ return [alias, `${col.table}.${col.column}`];
513
497
  })
514
498
  );
515
499
  const projectionTypes = {};
@@ -522,8 +506,8 @@ function buildMeta(args) {
522
506
  if (!column) {
523
507
  continue;
524
508
  }
525
- const operationExpr = column._operationExpr;
526
- if (operationExpr) {
509
+ if (isExpressionBuilder(column)) {
510
+ const operationExpr = column.expr;
527
511
  if (operationExpr.returns.kind === "typeId") {
528
512
  projectionTypes[alias] = operationExpr.returns.type;
529
513
  } else if (operationExpr.returns.kind === "builtin") {
@@ -548,8 +532,8 @@ function buildMeta(args) {
548
532
  if (!column) {
549
533
  continue;
550
534
  }
551
- const operationExpr = column._operationExpr;
552
- if (operationExpr) {
535
+ if (isExpressionBuilder(column)) {
536
+ const operationExpr = column.expr;
553
537
  if (operationExpr.returns.kind === "typeId") {
554
538
  projectionCodecs[alias] = operationExpr.returns.type;
555
539
  }
@@ -586,15 +570,10 @@ function buildMeta(args) {
586
570
  }
587
571
 
588
572
  // src/sql/predicate-builder.ts
573
+ import { createBinaryExpr, createParamRef } from "@prisma-next/sql-relational-core/ast";
589
574
  import {
590
- createBinaryExpr,
591
- createColumnRef,
592
- createParamRef
593
- } from "@prisma-next/sql-relational-core/ast";
594
- import {
595
- getColumnInfo as getColumnInfo2,
596
- getOperationExpr as getOperationExpr2,
597
575
  isColumnBuilder as isColumnBuilder2,
576
+ isExpressionBuilder as isExpressionBuilder2,
598
577
  isParamPlaceholder
599
578
  } from "@prisma-next/sql-relational-core/utils/guards";
600
579
  function buildWhereExpr(contract, where, paramsMap, descriptors, values) {
@@ -602,11 +581,13 @@ function buildWhereExpr(contract, where, paramsMap, descriptors, values) {
602
581
  let codecId;
603
582
  let rightExpr;
604
583
  let paramName;
605
- const operationExpr = getOperationExpr2(where.left);
606
- if (operationExpr) {
607
- leftExpr = operationExpr;
608
- } else if (isColumnBuilder2(where.left)) {
609
- const { table, column } = getColumnInfo2(where.left);
584
+ const validExpressionKinds = ["col", "operation"];
585
+ if (!where.left || typeof where.left !== "object" || !validExpressionKinds.includes(where.left.kind ?? "")) {
586
+ errorFailedToBuildWhereClause();
587
+ }
588
+ leftExpr = where.left;
589
+ if (leftExpr.kind === "col") {
590
+ const { table, column } = leftExpr;
610
591
  const contractTable = contract.storage.tables[table];
611
592
  if (!contractTable) {
612
593
  errorUnknownTable(table);
@@ -616,9 +597,6 @@ function buildWhereExpr(contract, where, paramsMap, descriptors, values) {
616
597
  errorUnknownColumn(column, table);
617
598
  }
618
599
  codecId = columnMeta.codecId;
619
- leftExpr = createColumnRef(table, column);
620
- } else {
621
- errorFailedToBuildWhereClause();
622
600
  }
623
601
  if (isParamPlaceholder(where.right)) {
624
602
  const placeholder = where.right;
@@ -628,8 +606,8 @@ function buildWhereExpr(contract, where, paramsMap, descriptors, values) {
628
606
  }
629
607
  const value = paramsMap[paramName];
630
608
  const index = values.push(value);
631
- if (isColumnBuilder2(where.left)) {
632
- const { table, column } = getColumnInfo2(where.left);
609
+ if (leftExpr.kind === "col") {
610
+ const { table, column } = leftExpr;
633
611
  const contractTable = contract.storage.tables[table];
634
612
  const columnMeta = contractTable?.columns[column];
635
613
  if (columnMeta) {
@@ -644,17 +622,19 @@ function buildWhereExpr(contract, where, paramsMap, descriptors, values) {
644
622
  }
645
623
  }
646
624
  rightExpr = createParamRef(index, paramName);
647
- } else if (isColumnBuilder2(where.right)) {
648
- const { table, column } = getColumnInfo2(where.right);
649
- const contractTable = contract.storage.tables[table];
650
- if (!contractTable) {
651
- errorUnknownTable(table);
652
- }
653
- const columnMeta = contractTable.columns[column];
654
- if (!columnMeta) {
655
- errorUnknownColumn(column, table);
625
+ } else if (isColumnBuilder2(where.right) || isExpressionBuilder2(where.right)) {
626
+ rightExpr = where.right.toExpr();
627
+ if (rightExpr.kind === "col") {
628
+ const { table, column } = rightExpr;
629
+ const contractTable = contract.storage.tables[table];
630
+ if (!contractTable) {
631
+ errorUnknownTable(table);
632
+ }
633
+ const columnMeta = contractTable.columns[column];
634
+ if (!columnMeta) {
635
+ errorUnknownColumn(column, table);
636
+ }
656
637
  }
657
- rightExpr = createColumnRef(table, column);
658
638
  paramName = "";
659
639
  } else {
660
640
  errorFailedToBuildWhereClause();
@@ -728,7 +708,7 @@ var InsertBuilderImpl = class _InsertBuilderImpl {
728
708
  }
729
709
  const returning = this.returningColumns.map((col) => {
730
710
  const c = col;
731
- return createColumnRef2(c.table, c.column);
711
+ return createColumnRef(c.table, c.column);
732
712
  });
733
713
  const ast = createInsertAst({
734
714
  table: createTableRef(this.table.name),
@@ -859,7 +839,7 @@ var UpdateBuilderImpl = class _UpdateBuilderImpl {
859
839
  }
860
840
  const returning = this.returningColumns.map((col) => {
861
841
  const c = col;
862
- return createColumnRef2(c.table, c.column);
842
+ return createColumnRef(c.table, c.column);
863
843
  });
864
844
  const ast = createUpdateAst({
865
845
  table: createTableRef(this.table.name),
@@ -963,7 +943,7 @@ var DeleteBuilderImpl = class _DeleteBuilderImpl {
963
943
  }
964
944
  const returning = this.returningColumns.map((col) => {
965
945
  const c = col;
966
- return createColumnRef2(c.table, c.column);
946
+ return createColumnRef(c.table, c.column);
967
947
  });
968
948
  const ast = createDeleteAst({
969
949
  table: createTableRef(this.table.name),
@@ -1005,17 +985,16 @@ var DeleteBuilderImpl = class _DeleteBuilderImpl {
1005
985
 
1006
986
  // src/sql/select-builder.ts
1007
987
  import {
1008
- createColumnRef as createColumnRef5,
1009
988
  createJoinOnBuilder,
1010
989
  createOrderByItem as createOrderByItem2,
1011
990
  createSelectAst,
1012
991
  createTableRef as createTableRef4
1013
992
  } from "@prisma-next/sql-relational-core/ast";
1014
- import { getOperationExpr as getOperationExpr3 } from "@prisma-next/sql-relational-core/utils/guards";
993
+ import { isExpressionBuilder as isExpressionBuilder3 } from "@prisma-next/sql-relational-core/utils/guards";
1015
994
 
1016
995
  // src/sql/include-builder.ts
1017
996
  import {
1018
- createColumnRef as createColumnRef3,
997
+ createColumnRef as createColumnRef2,
1019
998
  createJoinOnExpr,
1020
999
  createOrderByItem,
1021
1000
  createTableRef as createTableRef2
@@ -1026,7 +1005,7 @@ import {
1026
1005
  } from "@prisma-next/sql-relational-core/utils/guards";
1027
1006
 
1028
1007
  // src/sql/projection.ts
1029
- import { isColumnBuilder as isColumnBuilder3 } from "@prisma-next/sql-relational-core/utils/guards";
1008
+ import { isExpressionSource } from "@prisma-next/sql-relational-core/utils/guards";
1030
1009
  function generateAlias(path) {
1031
1010
  if (path.length === 0) {
1032
1011
  errorAliasPathEmpty();
@@ -1058,7 +1037,7 @@ function flattenProjection(projection, tracker, currentPath = []) {
1058
1037
  const columns = [];
1059
1038
  for (const [key, value] of Object.entries(projection)) {
1060
1039
  const path = [...currentPath, key];
1061
- if (isColumnBuilder3(value)) {
1040
+ if (isExpressionSource(value)) {
1062
1041
  const alias = tracker.register(path);
1063
1042
  aliases.push(alias);
1064
1043
  columns.push(value);
@@ -1083,8 +1062,14 @@ function buildProjectionState(_table, projection, includes) {
1083
1062
  errorIncludeAliasNotFound(key);
1084
1063
  }
1085
1064
  aliases.push(key);
1086
- columns.push(null);
1087
- } else if (isColumnBuilder3(value)) {
1065
+ columns.push({
1066
+ kind: "column",
1067
+ table: matchingInclude.table.name,
1068
+ column: "",
1069
+ columnMeta: { nativeType: "jsonb", codecId: "core/json@1", nullable: true },
1070
+ toExpr: () => ({ kind: "col", table: matchingInclude.table.name, column: "" })
1071
+ });
1072
+ } else if (isExpressionSource(value)) {
1088
1073
  const alias = tracker.register([key]);
1089
1074
  aliases.push(alias);
1090
1075
  columns.push(value);
@@ -1214,10 +1199,10 @@ function buildIncludeAst(include, contract, paramsMap, paramDescriptors, paramVa
1214
1199
  const expr = (() => {
1215
1200
  if (isOperationExpr2(orderExpr)) {
1216
1201
  const baseCol = extractBaseColumnRef(orderExpr);
1217
- return createColumnRef3(baseCol.table, baseCol.column);
1202
+ return createColumnRef2(baseCol.table, baseCol.column);
1218
1203
  }
1219
1204
  const colBuilder = orderExpr;
1220
- return createColumnRef3(colBuilder.table, colBuilder.column);
1205
+ return createColumnRef2(colBuilder.table, colBuilder.column);
1221
1206
  })();
1222
1207
  return [createOrderByItem(expr, orderBy.dir)];
1223
1208
  })() : void 0;
@@ -1234,8 +1219,8 @@ function buildIncludeAst(include, contract, paramsMap, paramDescriptors, paramVa
1234
1219
  }
1235
1220
  const onLeft = include.on.left;
1236
1221
  const onRight = include.on.right;
1237
- const leftCol = createColumnRef3(onLeft.table, onLeft.column);
1238
- const rightCol = createColumnRef3(onRight.table, onRight.column);
1222
+ const leftCol = createColumnRef2(onLeft.table, onLeft.column);
1223
+ const rightCol = createColumnRef2(onRight.table, onRight.column);
1239
1224
  const onExpr = createJoinOnExpr(leftCol, rightCol);
1240
1225
  return {
1241
1226
  kind: "includeMany",
@@ -1253,7 +1238,7 @@ function buildIncludeAst(include, contract, paramsMap, paramDescriptors, paramVa
1253
1238
  }
1254
1239
  return {
1255
1240
  alias,
1256
- expr: createColumnRef3(column.table, column.column)
1241
+ expr: column.toExpr()
1257
1242
  };
1258
1243
  })
1259
1244
  }
@@ -1262,7 +1247,7 @@ function buildIncludeAst(include, contract, paramsMap, paramDescriptors, paramVa
1262
1247
 
1263
1248
  // src/sql/join-builder.ts
1264
1249
  import {
1265
- createColumnRef as createColumnRef4,
1250
+ createColumnRef as createColumnRef3,
1266
1251
  createJoin,
1267
1252
  createJoinOnExpr as createJoinOnExpr2,
1268
1253
  createTableRef as createTableRef3
@@ -1270,8 +1255,8 @@ import {
1270
1255
  function buildJoinAst(join) {
1271
1256
  const onLeft = join.on.left;
1272
1257
  const onRight = join.on.right;
1273
- const leftCol = createColumnRef4(onLeft.table, onLeft.column);
1274
- const rightCol = createColumnRef4(onRight.table, onRight.column);
1258
+ const leftCol = createColumnRef3(onLeft.table, onLeft.column);
1259
+ const rightCol = createColumnRef3(onRight.table, onRight.column);
1275
1260
  const onExpr = createJoinOnExpr2(leftCol, rightCol);
1276
1261
  return createJoin(join.joinType, createTableRef3(join.table.name), onExpr);
1277
1262
  }
@@ -1440,13 +1425,7 @@ var SelectBuilderImpl = class _SelectBuilderImpl {
1440
1425
  }
1441
1426
  const orderByClause = this.state.orderBy ? (() => {
1442
1427
  const orderBy = this.state.orderBy;
1443
- const orderExpr = orderBy.expr;
1444
- const operationExpr = getOperationExpr3(orderExpr);
1445
- const expr = operationExpr ? operationExpr : (() => {
1446
- const colBuilder = orderExpr;
1447
- return createColumnRef5(colBuilder.table, colBuilder.column);
1448
- })();
1449
- return [createOrderByItem2(expr, orderBy.dir)];
1428
+ return [createOrderByItem2(orderBy.expr, orderBy.dir)];
1450
1429
  })() : void 0;
1451
1430
  const joins = this.state.joins?.map((join) => buildJoinAst(join));
1452
1431
  const includes = this.state.includes?.map(
@@ -1465,24 +1444,17 @@ var SelectBuilderImpl = class _SelectBuilderImpl {
1465
1444
  alias,
1466
1445
  expr: { kind: "includeRef", alias }
1467
1446
  });
1468
- } else {
1469
- if (!column) {
1470
- errorMissingColumnForAlias(alias, i);
1471
- }
1472
- const operationExpr = column._operationExpr;
1473
- if (operationExpr) {
1474
- projectEntries.push({
1475
- alias,
1476
- expr: operationExpr
1477
- });
1478
- } else {
1479
- const col = column;
1480
- assertColumnBuilder(col, "projection column");
1481
- projectEntries.push({
1482
- alias,
1483
- expr: createColumnRef5(col.table, col.column)
1484
- });
1485
- }
1447
+ } else if (column && isExpressionBuilder3(column)) {
1448
+ projectEntries.push({
1449
+ alias,
1450
+ expr: column.expr
1451
+ });
1452
+ } else if (column) {
1453
+ const columnRef = column.toExpr();
1454
+ projectEntries.push({
1455
+ alias,
1456
+ expr: columnRef
1457
+ });
1486
1458
  }
1487
1459
  }
1488
1460
  const ast = createSelectAst({
@@ -1566,4 +1538,4 @@ export {
1566
1538
  createJoinOnBuilder2 as createJoinOnBuilder,
1567
1539
  sql
1568
1540
  };
1569
- //# sourceMappingURL=chunk-AWSKRSFP.js.map
1541
+ //# sourceMappingURL=chunk-72PNERR5.js.map