@prisma-next/sql-lane 0.3.0-dev.6 → 0.3.0-dev.63

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.
Files changed (64) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +20 -3
  3. package/dist/builder-DLGrrQ5F.mjs +1254 -0
  4. package/dist/builder-DLGrrQ5F.mjs.map +1 -0
  5. package/dist/builder-DizPddCD.d.mts +121 -0
  6. package/dist/builder-DizPddCD.d.mts.map +1 -0
  7. package/dist/exports/sql.d.mts +3 -0
  8. package/dist/exports/sql.mjs +3 -0
  9. package/dist/index.d.mts +3 -0
  10. package/dist/index.mjs +3 -0
  11. package/package.json +28 -24
  12. package/src/raw.ts +9 -2
  13. package/src/sql/context.ts +3 -3
  14. package/src/sql/include-builder.ts +3 -3
  15. package/src/sql/mutation-builder.ts +61 -10
  16. package/src/sql/plan.ts +117 -102
  17. package/src/sql/predicate-builder.ts +94 -40
  18. package/src/sql/projection.ts +15 -10
  19. package/src/sql/select-builder.ts +26 -41
  20. package/src/types/internal.ts +5 -4
  21. package/src/utils/errors.ts +1 -1
  22. package/src/utils/state.ts +5 -6
  23. package/dist/chunk-AWSKRSFP.js +0 -1569
  24. package/dist/chunk-AWSKRSFP.js.map +0 -1
  25. package/dist/exports/sql.d.ts +0 -5
  26. package/dist/exports/sql.d.ts.map +0 -1
  27. package/dist/exports/sql.js +0 -11
  28. package/dist/exports/sql.js.map +0 -1
  29. package/dist/index.d.ts +0 -5
  30. package/dist/index.d.ts.map +0 -1
  31. package/dist/index.js +0 -11
  32. package/dist/index.js.map +0 -1
  33. package/dist/raw.d.ts +0 -11
  34. package/dist/raw.d.ts.map +0 -1
  35. package/dist/sql/builder.d.ts +0 -11
  36. package/dist/sql/builder.d.ts.map +0 -1
  37. package/dist/sql/context.d.ts +0 -5
  38. package/dist/sql/context.d.ts.map +0 -1
  39. package/dist/sql/include-builder.d.ts +0 -35
  40. package/dist/sql/include-builder.d.ts.map +0 -1
  41. package/dist/sql/join-builder.d.ts +0 -4
  42. package/dist/sql/join-builder.d.ts.map +0 -1
  43. package/dist/sql/mutation-builder.d.ts +0 -64
  44. package/dist/sql/mutation-builder.d.ts.map +0 -1
  45. package/dist/sql/plan.d.ts +0 -4
  46. package/dist/sql/plan.d.ts.map +0 -1
  47. package/dist/sql/predicate-builder.d.ts +0 -11
  48. package/dist/sql/predicate-builder.d.ts.map +0 -1
  49. package/dist/sql/projection.d.ts +0 -18
  50. package/dist/sql/projection.d.ts.map +0 -1
  51. package/dist/sql/select-builder.d.ts +0 -35
  52. package/dist/sql/select-builder.d.ts.map +0 -1
  53. package/dist/types/internal.d.ts +0 -35
  54. package/dist/types/internal.d.ts.map +0 -1
  55. package/dist/types/public.d.ts +0 -18
  56. package/dist/types/public.d.ts.map +0 -1
  57. package/dist/utils/assertions.d.ts +0 -28
  58. package/dist/utils/assertions.d.ts.map +0 -1
  59. package/dist/utils/capabilities.d.ts +0 -4
  60. package/dist/utils/capabilities.d.ts.map +0 -1
  61. package/dist/utils/errors.d.ts +0 -30
  62. package/dist/utils/errors.d.ts.map +0 -1
  63. package/dist/utils/state.d.ts +0 -30
  64. package/dist/utils/state.d.ts.map +0 -1
@@ -1,6 +1,6 @@
1
1
  import type { TableRef } from '@prisma-next/sql-relational-core/ast';
2
- import type { AnyColumnBuilder, NestedProjection } from '@prisma-next/sql-relational-core/types';
3
- import { isColumnBuilder } from '@prisma-next/sql-relational-core/utils/guards';
2
+ import type { AnyExpressionSource, NestedProjection } from '@prisma-next/sql-relational-core/types';
3
+ import { isExpressionSource } from '@prisma-next/sql-relational-core/utils/guards';
4
4
  import type { ProjectionInput } from '../types/internal';
5
5
  import {
6
6
  errorAliasCollision,
@@ -47,14 +47,14 @@ export function flattenProjection(
47
47
  projection: NestedProjection,
48
48
  tracker: AliasTracker,
49
49
  currentPath: string[] = [],
50
- ): { aliases: string[]; columns: AnyColumnBuilder[] } {
50
+ ): { aliases: string[]; columns: AnyExpressionSource[] } {
51
51
  const aliases: string[] = [];
52
- const columns: AnyColumnBuilder[] = [];
52
+ const columns: AnyExpressionSource[] = [];
53
53
 
54
54
  for (const [key, value] of Object.entries(projection)) {
55
55
  const path = [...currentPath, key];
56
56
 
57
- if (isColumnBuilder(value)) {
57
+ if (isExpressionSource(value)) {
58
58
  const alias = tracker.register(path);
59
59
  aliases.push(alias);
60
60
  columns.push(value);
@@ -77,7 +77,7 @@ export function buildProjectionState(
77
77
  ): ProjectionState {
78
78
  const tracker = new AliasTracker();
79
79
  const aliases: string[] = [];
80
- const columns: (AnyColumnBuilder | null)[] = [];
80
+ const columns: AnyExpressionSource[] = [];
81
81
 
82
82
  for (const [key, value] of Object.entries(projection)) {
83
83
  if (value === true) {
@@ -86,12 +86,17 @@ export function buildProjectionState(
86
86
  if (!matchingInclude) {
87
87
  errorIncludeAliasNotFound(key);
88
88
  }
89
- // For include references, we track the alias but use null as placeholder
89
+ // For include references, we track the alias but use a placeholder object
90
90
  // The actual handling happens in AST building where we create includeRef
91
- // Using null instead of invalid ColumnBuilder avoids code smell
92
91
  aliases.push(key);
93
- columns.push(null);
94
- } else if (isColumnBuilder(value)) {
92
+ columns.push({
93
+ kind: 'column',
94
+ table: matchingInclude.table.name,
95
+ column: '',
96
+ columnMeta: { nativeType: 'jsonb', codecId: 'core/json@1', nullable: true },
97
+ toExpr: () => ({ kind: 'col', table: matchingInclude.table.name, column: '' }),
98
+ } as AnyExpressionSource);
99
+ } else if (isExpressionSource(value)) {
95
100
  const alias = tracker.register([key]);
96
101
  aliases.push(alias);
97
102
  columns.push(value);
@@ -1,7 +1,6 @@
1
1
  import type { ParamDescriptor } from '@prisma-next/contract/types';
2
2
  import type { SqlContract, SqlStorage, StorageColumn } from '@prisma-next/sql-contract/types';
3
3
  import type {
4
- BinaryExpr,
5
4
  ColumnRef,
6
5
  Direction,
7
6
  IncludeAst,
@@ -9,19 +8,20 @@ import type {
9
8
  JoinAst,
10
9
  OperationExpr,
11
10
  TableRef,
11
+ WhereExpr,
12
12
  } from '@prisma-next/sql-relational-core/ast';
13
13
  import {
14
- createColumnRef,
15
14
  createJoinOnBuilder,
16
15
  createOrderByItem,
17
16
  createSelectAst,
18
17
  createTableRef,
19
18
  } from '@prisma-next/sql-relational-core/ast';
20
19
  import type { SqlQueryPlan } from '@prisma-next/sql-relational-core/plan';
21
- import type { QueryLaneContext } from '@prisma-next/sql-relational-core/query-lane-context';
20
+ import type { ExecutionContext } from '@prisma-next/sql-relational-core/query-lane-context';
22
21
  import type {
23
22
  AnyBinaryBuilder,
24
23
  AnyOrderBuilder,
24
+ AnyUnaryBuilder,
25
25
  BinaryBuilder,
26
26
  BuildOptions,
27
27
  InferNestedProjectionRow,
@@ -30,10 +30,10 @@ import type {
30
30
  NestedProjection,
31
31
  OrderBuilder,
32
32
  SqlBuilderOptions,
33
+ UnaryBuilder,
33
34
  } from '@prisma-next/sql-relational-core/types';
34
- import { getOperationExpr } from '@prisma-next/sql-relational-core/utils/guards';
35
+ import { isExpressionBuilder } from '@prisma-next/sql-relational-core/utils/guards';
35
36
  import type { ProjectionInput } from '../types/internal';
36
- import { assertColumnBuilder } from '../utils/assertions';
37
37
  import { checkIncludeCapabilities } from '../utils/capabilities';
38
38
  import {
39
39
  errorChildProjectionEmpty,
@@ -41,7 +41,6 @@ import {
41
41
  errorIncludeAliasCollision,
42
42
  errorLimitMustBeNonNegativeInteger,
43
43
  errorMissingAlias,
44
- errorMissingColumnForAlias,
45
44
  errorSelectMustBeCalled,
46
45
  errorSelfJoinNotSupported,
47
46
  errorUnknownTable,
@@ -65,7 +64,7 @@ export class SelectBuilderImpl<
65
64
  > {
66
65
  private readonly contract: TContract;
67
66
  private readonly codecTypes: CodecTypes;
68
- private readonly context: QueryLaneContext<TContract>;
67
+ private readonly context: ExecutionContext<TContract>;
69
68
  private state: BuilderState = {};
70
69
 
71
70
  constructor(options: SqlBuilderOptions<TContract>, state?: BuilderState) {
@@ -238,7 +237,9 @@ export class SelectBuilderImpl<
238
237
  );
239
238
  }
240
239
 
241
- where(expr: AnyBinaryBuilder): SelectBuilderImpl<TContract, Row, CodecTypes, Includes> {
240
+ where(
241
+ expr: AnyBinaryBuilder | AnyUnaryBuilder,
242
+ ): SelectBuilderImpl<TContract, Row, CodecTypes, Includes> {
242
243
  return new SelectBuilderImpl<TContract, Row, CodecTypes, Includes>(
243
244
  {
244
245
  context: this.context,
@@ -320,15 +321,8 @@ export class SelectBuilderImpl<
320
321
  const orderByClause = this.state.orderBy
321
322
  ? (() => {
322
323
  const orderBy = this.state.orderBy as OrderBuilder<string, StorageColumn, unknown>;
323
- const orderExpr = orderBy.expr;
324
- const operationExpr = getOperationExpr(orderExpr);
325
- const expr: ColumnRef | OperationExpr = operationExpr
326
- ? operationExpr
327
- : (() => {
328
- const colBuilder = orderExpr as { table: string; column: string };
329
- return createColumnRef(colBuilder.table, colBuilder.column);
330
- })();
331
- return [createOrderByItem(expr, orderBy.dir)];
324
+ // orderBy.expr is already an Expression (ColumnRef or OperationExpr)
325
+ return [createOrderByItem(orderBy.expr, orderBy.dir)];
332
326
  })()
333
327
  : undefined;
334
328
 
@@ -357,28 +351,19 @@ export class SelectBuilderImpl<
357
351
  alias,
358
352
  expr: { kind: 'includeRef', alias },
359
353
  });
360
- } else {
361
- // Not an include - column must not be null
362
- if (!column) {
363
- errorMissingColumnForAlias(alias, i);
364
- }
365
- // Check if this column has an operation expression
366
- const operationExpr = (column as { _operationExpr?: OperationExpr })._operationExpr;
367
- if (operationExpr) {
368
- projectEntries.push({
369
- alias,
370
- expr: operationExpr,
371
- });
372
- } else {
373
- // This is a regular column
374
- // TypeScript can't narrow ColumnBuilder properly
375
- const col = column as { table: string; column: string };
376
- assertColumnBuilder(col, 'projection column');
377
- projectEntries.push({
378
- alias,
379
- expr: createColumnRef(col.table, col.column),
380
- });
381
- }
354
+ } else if (column && isExpressionBuilder(column)) {
355
+ // This is an ExpressionBuilder (operation result) - use its expr
356
+ projectEntries.push({
357
+ alias,
358
+ expr: column.expr,
359
+ });
360
+ } else if (column) {
361
+ // This is a regular ColumnBuilder - use toExpr() to get ColumnRef
362
+ const columnRef = column.toExpr();
363
+ projectEntries.push({
364
+ alias,
365
+ expr: columnRef,
366
+ });
382
367
  }
383
368
  }
384
369
 
@@ -395,7 +380,7 @@ export class SelectBuilderImpl<
395
380
  joins?: ReadonlyArray<JoinAst>;
396
381
  includes?: ReadonlyArray<IncludeAst>;
397
382
  project: ReadonlyArray<{ alias: string; expr: ColumnRef | IncludeRef | OperationExpr }>;
398
- where?: BinaryExpr;
383
+ where?: WhereExpr;
399
384
  orderBy?: ReadonlyArray<{ expr: ColumnRef | OperationExpr; dir: Direction }>;
400
385
  limit?: number;
401
386
  });
@@ -416,7 +401,7 @@ export class SelectBuilderImpl<
416
401
  projection: ProjectionState;
417
402
  joins?: ReadonlyArray<JoinState>;
418
403
  includes?: ReadonlyArray<IncludeState>;
419
- where?: BinaryBuilder;
404
+ where?: BinaryBuilder | UnaryBuilder;
420
405
  orderBy?: AnyOrderBuilder;
421
406
  paramDescriptors: ParamDescriptor[];
422
407
  paramCodecs?: Record<string, string>;
@@ -3,13 +3,14 @@ import type { SqlContract, SqlStorage } from '@prisma-next/sql-contract/types';
3
3
  import type { TableRef } from '@prisma-next/sql-relational-core/ast';
4
4
  import type {
5
5
  AnyBinaryBuilder,
6
- AnyColumnBuilder,
6
+ AnyExpressionSource,
7
7
  AnyOrderBuilder,
8
+ AnyUnaryBuilder,
8
9
  NestedProjection,
9
10
  } from '@prisma-next/sql-relational-core/types';
10
11
  import type { ProjectionState } from '../utils/state';
11
12
 
12
- export type ProjectionInput = Record<string, AnyColumnBuilder | boolean | NestedProjection>;
13
+ export type ProjectionInput = Record<string, AnyExpressionSource | boolean | NestedProjection>;
13
14
 
14
15
  export interface MetaBuildArgs {
15
16
  readonly contract: SqlContract<SqlStorage>;
@@ -31,10 +32,10 @@ export interface MetaBuildArgs {
31
32
  readonly right: unknown;
32
33
  };
33
34
  readonly childProjection: ProjectionState;
34
- readonly childWhere?: AnyBinaryBuilder;
35
+ readonly childWhere?: AnyBinaryBuilder | AnyUnaryBuilder;
35
36
  readonly childOrderBy?: AnyOrderBuilder;
36
37
  }>;
37
- readonly where?: AnyBinaryBuilder;
38
+ readonly where?: AnyBinaryBuilder | AnyUnaryBuilder;
38
39
  readonly orderBy?: AnyOrderBuilder;
39
40
  readonly paramDescriptors: ParamDescriptor[];
40
41
  readonly paramCodecs?: Record<string, string>;
@@ -98,7 +98,7 @@ export function errorMissingParameter(paramName: string): never {
98
98
 
99
99
  export function errorInvalidProjectionValue(path: string[]): never {
100
100
  throw planInvalid(
101
- `Invalid projection value at path ${path.join('.')}: expected ColumnBuilder or nested object`,
101
+ `Invalid projection value at path ${path.join('.')}: expected ExpressionSource (ColumnBuilder or ExpressionBuilder) or nested object`,
102
102
  );
103
103
  }
104
104
 
@@ -1,16 +1,15 @@
1
1
  import type { TableRef } from '@prisma-next/sql-relational-core/ast';
2
2
  import type {
3
3
  AnyBinaryBuilder,
4
- AnyColumnBuilder,
4
+ AnyExpressionSource,
5
5
  AnyOrderBuilder,
6
+ AnyUnaryBuilder,
6
7
  JoinOnPredicate,
7
8
  } from '@prisma-next/sql-relational-core/types';
8
9
 
9
10
  export interface ProjectionState {
10
11
  readonly aliases: string[];
11
- // columns can be null for include placeholders (when alias matches an include)
12
- // This maintains array alignment but avoids creating invalid ColumnBuilders
13
- readonly columns: (AnyColumnBuilder | null)[];
12
+ readonly columns: AnyExpressionSource[];
14
13
  }
15
14
 
16
15
  export interface JoinState {
@@ -24,7 +23,7 @@ export interface IncludeState {
24
23
  readonly table: TableRef;
25
24
  readonly on: JoinOnPredicate;
26
25
  readonly childProjection: ProjectionState;
27
- readonly childWhere?: AnyBinaryBuilder;
26
+ readonly childWhere?: AnyBinaryBuilder | AnyUnaryBuilder;
28
27
  readonly childOrderBy?: AnyOrderBuilder;
29
28
  readonly childLimit?: number;
30
29
  }
@@ -34,7 +33,7 @@ export interface BuilderState {
34
33
  joins?: ReadonlyArray<JoinState>;
35
34
  includes?: ReadonlyArray<IncludeState>;
36
35
  projection?: ProjectionState;
37
- where?: AnyBinaryBuilder;
36
+ where?: AnyBinaryBuilder | AnyUnaryBuilder;
38
37
  orderBy?: AnyOrderBuilder;
39
38
  limit?: number;
40
39
  }