@prisma-next/sql-lane 0.3.0-dev.3 → 0.3.0-dev.31

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 (61) hide show
  1. package/README.md +2 -2
  2. package/dist/{chunk-WA646VY6.js → chunk-72PNERR5.js} +133 -161
  3. package/dist/chunk-72PNERR5.js.map +1 -0
  4. package/dist/exports/sql.d.ts +5 -5
  5. package/dist/exports/sql.d.ts.map +1 -0
  6. package/dist/exports/sql.js +1 -1
  7. package/dist/index.d.ts +5 -116
  8. package/dist/index.d.ts.map +1 -0
  9. package/dist/index.js +1 -1
  10. package/dist/raw.d.ts +11 -0
  11. package/dist/raw.d.ts.map +1 -0
  12. package/dist/sql/builder.d.ts +11 -0
  13. package/dist/sql/builder.d.ts.map +1 -0
  14. package/dist/sql/context.d.ts +5 -0
  15. package/dist/sql/context.d.ts.map +1 -0
  16. package/dist/sql/include-builder.d.ts +35 -0
  17. package/dist/sql/include-builder.d.ts.map +1 -0
  18. package/dist/sql/join-builder.d.ts +4 -0
  19. package/dist/sql/join-builder.d.ts.map +1 -0
  20. package/dist/sql/mutation-builder.d.ts +64 -0
  21. package/dist/sql/mutation-builder.d.ts.map +1 -0
  22. package/dist/sql/plan.d.ts +4 -0
  23. package/dist/sql/plan.d.ts.map +1 -0
  24. package/dist/sql/predicate-builder.d.ts +11 -0
  25. package/dist/sql/predicate-builder.d.ts.map +1 -0
  26. package/dist/sql/projection.d.ts +18 -0
  27. package/dist/sql/projection.d.ts.map +1 -0
  28. package/dist/sql/select-builder.d.ts +35 -0
  29. package/dist/sql/select-builder.d.ts.map +1 -0
  30. package/dist/types/internal.d.ts +35 -0
  31. package/dist/types/internal.d.ts.map +1 -0
  32. package/dist/types/public.d.ts +18 -0
  33. package/dist/types/public.d.ts.map +1 -0
  34. package/dist/utils/assertions.d.ts +28 -0
  35. package/dist/utils/assertions.d.ts.map +1 -0
  36. package/dist/utils/capabilities.d.ts +4 -0
  37. package/dist/utils/capabilities.d.ts.map +1 -0
  38. package/dist/utils/errors.d.ts +30 -0
  39. package/dist/utils/errors.d.ts.map +1 -0
  40. package/dist/utils/state.d.ts +30 -0
  41. package/dist/utils/state.d.ts.map +1 -0
  42. package/package.json +20 -20
  43. package/src/exports/sql.ts +12 -0
  44. package/src/index.ts +13 -0
  45. package/src/raw.ts +230 -0
  46. package/src/sql/builder.ts +66 -0
  47. package/src/sql/context.ts +10 -0
  48. package/src/sql/include-builder.ts +248 -0
  49. package/src/sql/join-builder.ts +18 -0
  50. package/src/sql/mutation-builder.ts +494 -0
  51. package/src/sql/plan.ts +290 -0
  52. package/src/sql/predicate-builder.ts +127 -0
  53. package/src/sql/projection.ts +117 -0
  54. package/src/sql/select-builder.ts +430 -0
  55. package/src/types/internal.ts +41 -0
  56. package/src/types/public.ts +36 -0
  57. package/src/utils/assertions.ts +34 -0
  58. package/src/utils/capabilities.ts +39 -0
  59. package/src/utils/errors.ts +168 -0
  60. package/src/utils/state.ts +38 -0
  61. package/dist/chunk-WA646VY6.js.map +0 -1
@@ -0,0 +1,66 @@
1
+ import type {
2
+ ExtractCodecTypes,
3
+ ExtractOperationTypes,
4
+ SqlContract,
5
+ SqlStorage,
6
+ } from '@prisma-next/sql-contract/types';
7
+ import type { TableRef } from '@prisma-next/sql-relational-core/ast';
8
+ import { createJoinOnBuilder } from '@prisma-next/sql-relational-core/ast';
9
+ import type { ParamPlaceholder, SqlBuilderOptions } from '@prisma-next/sql-relational-core/types';
10
+ import { createRawFactory } from '../raw';
11
+ import type { SelectBuilder } from '../types/public';
12
+ import { DeleteBuilderImpl, InsertBuilderImpl, UpdateBuilderImpl } from './mutation-builder';
13
+ import { SelectBuilderImpl } from './select-builder';
14
+
15
+ export { createJoinOnBuilder };
16
+ export type { DeleteBuilder, InsertBuilder, SelectBuilder, UpdateBuilder } from '../types/public';
17
+ export type { IncludeChildBuilder } from './include-builder';
18
+
19
+ export function sql<
20
+ TContract extends SqlContract<SqlStorage>,
21
+ CodecTypesOverride extends Record<
22
+ string,
23
+ { readonly output: unknown }
24
+ > = ExtractCodecTypes<TContract>,
25
+ >(
26
+ options: SqlBuilderOptions<TContract>,
27
+ ): SelectBuilder<TContract, unknown, CodecTypesOverride, ExtractOperationTypes<TContract>> {
28
+ type CodecTypes = CodecTypesOverride;
29
+ type Operations = ExtractOperationTypes<TContract>;
30
+ const builder = new SelectBuilderImpl<TContract, unknown, CodecTypes, Record<string, never>>(
31
+ options,
32
+ ) as SelectBuilder<TContract, unknown, CodecTypes, Operations>;
33
+ const rawFactory = createRawFactory(options.context.contract);
34
+
35
+ Object.defineProperty(builder, 'raw', {
36
+ value: rawFactory,
37
+ enumerable: true,
38
+ configurable: false,
39
+ });
40
+
41
+ Object.defineProperty(builder, 'insert', {
42
+ value: (table: TableRef, values: Record<string, ParamPlaceholder>) => {
43
+ return new InsertBuilderImpl<TContract, CodecTypes>(options, table, values);
44
+ },
45
+ enumerable: true,
46
+ configurable: false,
47
+ });
48
+
49
+ Object.defineProperty(builder, 'update', {
50
+ value: (table: TableRef, set: Record<string, ParamPlaceholder>) => {
51
+ return new UpdateBuilderImpl<TContract, CodecTypes>(options, table, set);
52
+ },
53
+ enumerable: true,
54
+ configurable: false,
55
+ });
56
+
57
+ Object.defineProperty(builder, 'delete', {
58
+ value: (table: TableRef) => {
59
+ return new DeleteBuilderImpl<TContract, CodecTypes>(options, table);
60
+ },
61
+ enumerable: true,
62
+ configurable: false,
63
+ });
64
+
65
+ return builder;
66
+ }
@@ -0,0 +1,10 @@
1
+ import type { SqlContract, SqlStorage } from '@prisma-next/sql-contract/types';
2
+ import type { QueryLaneContext } from '@prisma-next/sql-relational-core/query-lane-context';
3
+
4
+ export type SqlContext<TContract extends SqlContract<SqlStorage>> = QueryLaneContext<TContract>;
5
+
6
+ export function createSqlContext<TContract extends SqlContract<SqlStorage>>(
7
+ context: QueryLaneContext<TContract>,
8
+ ): SqlContext<TContract> {
9
+ return context;
10
+ }
@@ -0,0 +1,248 @@
1
+ import type { ParamDescriptor } from '@prisma-next/contract/types';
2
+ import type { SqlContract, SqlStorage, StorageColumn } from '@prisma-next/sql-contract/types';
3
+ import type {
4
+ BinaryExpr,
5
+ ColumnRef,
6
+ IncludeAst,
7
+ OperationExpr,
8
+ TableRef,
9
+ } from '@prisma-next/sql-relational-core/ast';
10
+ import {
11
+ createColumnRef,
12
+ createJoinOnExpr,
13
+ createOrderByItem,
14
+ createTableRef,
15
+ } from '@prisma-next/sql-relational-core/ast';
16
+ import type {
17
+ AnyBinaryBuilder,
18
+ AnyOrderBuilder,
19
+ BinaryBuilder,
20
+ CodecTypes as CodecTypesMap,
21
+ InferNestedProjectionRow,
22
+ NestedProjection,
23
+ OrderBuilder,
24
+ } from '@prisma-next/sql-relational-core/types';
25
+ import {
26
+ extractBaseColumnRef,
27
+ isOperationExpr,
28
+ } from '@prisma-next/sql-relational-core/utils/guards';
29
+ import {
30
+ errorChildProjectionMustBeSpecified,
31
+ errorLimitMustBeNonNegativeInteger,
32
+ errorMissingColumnForAlias,
33
+ } from '../utils/errors';
34
+ import type { IncludeState, ProjectionState } from '../utils/state';
35
+ import { buildWhereExpr } from './predicate-builder';
36
+ import { buildProjectionState } from './projection';
37
+
38
+ export interface IncludeChildBuilder<
39
+ TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
40
+ CodecTypes extends Record<string, { readonly output: unknown }> = Record<string, never>,
41
+ ChildRow = unknown,
42
+ > {
43
+ select<P extends NestedProjection>(
44
+ projection: P,
45
+ ): IncludeChildBuilder<TContract, CodecTypes, InferNestedProjectionRow<P, CodecTypes>>;
46
+ where(expr: AnyBinaryBuilder): IncludeChildBuilder<TContract, CodecTypes, ChildRow>;
47
+ orderBy(order: AnyOrderBuilder): IncludeChildBuilder<TContract, CodecTypes, ChildRow>;
48
+ limit(count: number): IncludeChildBuilder<TContract, CodecTypes, ChildRow>;
49
+ }
50
+
51
+ export class IncludeChildBuilderImpl<
52
+ TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
53
+ CodecTypes extends CodecTypesMap = CodecTypesMap,
54
+ ChildRow = unknown,
55
+ > implements IncludeChildBuilder<TContract, CodecTypes, ChildRow>
56
+ {
57
+ private readonly contract: TContract;
58
+ private readonly codecTypes: CodecTypes;
59
+ private readonly table: TableRef;
60
+ private childProjection?: ProjectionState;
61
+ private childWhere?: BinaryBuilder;
62
+ private childOrderBy?: OrderBuilder;
63
+ private childLimit?: number;
64
+
65
+ constructor(contract: TContract, codecTypes: CodecTypes, table: TableRef) {
66
+ this.contract = contract;
67
+ this.codecTypes = codecTypes;
68
+ this.table = table;
69
+ }
70
+
71
+ select<P extends NestedProjection>(
72
+ projection: P,
73
+ ): IncludeChildBuilderImpl<TContract, CodecTypes, InferNestedProjectionRow<P, CodecTypes>> {
74
+ const projectionState = buildProjectionState(this.table, projection);
75
+ const builder = new IncludeChildBuilderImpl<
76
+ TContract,
77
+ CodecTypes,
78
+ InferNestedProjectionRow<P, CodecTypes>
79
+ >(this.contract, this.codecTypes, this.table);
80
+ builder.childProjection = projectionState;
81
+ if (this.childWhere !== undefined) {
82
+ builder.childWhere = this.childWhere;
83
+ }
84
+ if (this.childOrderBy !== undefined) {
85
+ builder.childOrderBy = this.childOrderBy;
86
+ }
87
+ if (this.childLimit !== undefined) {
88
+ builder.childLimit = this.childLimit;
89
+ }
90
+ return builder;
91
+ }
92
+
93
+ where(expr: AnyBinaryBuilder): IncludeChildBuilderImpl<TContract, CodecTypes, ChildRow> {
94
+ const builder = new IncludeChildBuilderImpl<TContract, CodecTypes, ChildRow>(
95
+ this.contract,
96
+ this.codecTypes,
97
+ this.table,
98
+ );
99
+ if (this.childProjection !== undefined) {
100
+ builder.childProjection = this.childProjection;
101
+ }
102
+ builder.childWhere = expr;
103
+ if (this.childOrderBy !== undefined) {
104
+ builder.childOrderBy = this.childOrderBy;
105
+ }
106
+ if (this.childLimit !== undefined) {
107
+ builder.childLimit = this.childLimit;
108
+ }
109
+ return builder;
110
+ }
111
+
112
+ orderBy(order: AnyOrderBuilder): IncludeChildBuilderImpl<TContract, CodecTypes, ChildRow> {
113
+ const builder = new IncludeChildBuilderImpl<TContract, CodecTypes, ChildRow>(
114
+ this.contract,
115
+ this.codecTypes,
116
+ this.table,
117
+ );
118
+ if (this.childProjection !== undefined) {
119
+ builder.childProjection = this.childProjection;
120
+ }
121
+ if (this.childWhere !== undefined) {
122
+ builder.childWhere = this.childWhere;
123
+ }
124
+ builder.childOrderBy = order;
125
+ if (this.childLimit !== undefined) {
126
+ builder.childLimit = this.childLimit;
127
+ }
128
+ return builder;
129
+ }
130
+
131
+ limit(count: number): IncludeChildBuilderImpl<TContract, CodecTypes, ChildRow> {
132
+ if (!Number.isInteger(count) || count < 0) {
133
+ errorLimitMustBeNonNegativeInteger();
134
+ }
135
+
136
+ const builder = new IncludeChildBuilderImpl<TContract, CodecTypes, ChildRow>(
137
+ this.contract,
138
+ this.codecTypes,
139
+ this.table,
140
+ );
141
+ if (this.childProjection !== undefined) {
142
+ builder.childProjection = this.childProjection;
143
+ }
144
+ if (this.childWhere !== undefined) {
145
+ builder.childWhere = this.childWhere;
146
+ }
147
+ if (this.childOrderBy !== undefined) {
148
+ builder.childOrderBy = this.childOrderBy;
149
+ }
150
+ builder.childLimit = count;
151
+ return builder;
152
+ }
153
+
154
+ getState(): {
155
+ childProjection: ProjectionState;
156
+ childWhere?: AnyBinaryBuilder;
157
+ childOrderBy?: AnyOrderBuilder;
158
+ childLimit?: number;
159
+ } {
160
+ if (!this.childProjection) {
161
+ errorChildProjectionMustBeSpecified();
162
+ }
163
+ const state: {
164
+ childProjection: ProjectionState;
165
+ childWhere?: AnyBinaryBuilder;
166
+ childOrderBy?: AnyOrderBuilder;
167
+ childLimit?: number;
168
+ } = {
169
+ childProjection: this.childProjection,
170
+ };
171
+ if (this.childWhere !== undefined) {
172
+ state.childWhere = this.childWhere;
173
+ }
174
+ if (this.childOrderBy !== undefined) {
175
+ state.childOrderBy = this.childOrderBy;
176
+ }
177
+ if (this.childLimit !== undefined) {
178
+ state.childLimit = this.childLimit;
179
+ }
180
+ return state;
181
+ }
182
+ }
183
+
184
+ export function buildIncludeAst(
185
+ include: IncludeState,
186
+ contract: SqlContract<SqlStorage>,
187
+ paramsMap: Record<string, unknown>,
188
+ paramDescriptors: ParamDescriptor[],
189
+ paramValues: unknown[],
190
+ ): IncludeAst {
191
+ const childOrderBy = include.childOrderBy
192
+ ? (() => {
193
+ const orderBy = include.childOrderBy as OrderBuilder<string, StorageColumn, unknown>;
194
+ const orderExpr = orderBy.expr;
195
+ const expr: ColumnRef | OperationExpr = (() => {
196
+ if (isOperationExpr(orderExpr)) {
197
+ const baseCol = extractBaseColumnRef(orderExpr);
198
+ return createColumnRef(baseCol.table, baseCol.column);
199
+ }
200
+ // orderExpr is ColumnBuilder - TypeScript can't narrow properly
201
+ const colBuilder = orderExpr as { table: string; column: string };
202
+ return createColumnRef(colBuilder.table, colBuilder.column);
203
+ })();
204
+ return [createOrderByItem(expr, orderBy.dir)];
205
+ })()
206
+ : undefined;
207
+
208
+ let childWhere: BinaryExpr | undefined;
209
+ if (include.childWhere) {
210
+ const whereResult = buildWhereExpr(
211
+ contract,
212
+ include.childWhere,
213
+ paramsMap,
214
+ paramDescriptors,
215
+ paramValues,
216
+ );
217
+ childWhere = whereResult.expr;
218
+ }
219
+
220
+ const onLeft = include.on.left as { table: string; column: string };
221
+ const onRight = include.on.right as { table: string; column: string };
222
+ const leftCol = createColumnRef(onLeft.table, onLeft.column);
223
+ const rightCol = createColumnRef(onRight.table, onRight.column);
224
+ const onExpr = createJoinOnExpr(leftCol, rightCol);
225
+
226
+ return {
227
+ kind: 'includeMany' as const,
228
+ alias: include.alias,
229
+ child: {
230
+ table: createTableRef(include.table.name),
231
+ on: onExpr,
232
+ ...(childWhere ? { where: childWhere } : {}),
233
+ ...(childOrderBy ? { orderBy: childOrderBy } : {}),
234
+ ...(typeof include.childLimit === 'number' ? { limit: include.childLimit } : {}),
235
+ project: include.childProjection.aliases.map((alias, idx) => {
236
+ const column = include.childProjection.columns[idx];
237
+ if (!column || !alias) {
238
+ errorMissingColumnForAlias(alias ?? 'unknown', idx);
239
+ }
240
+
241
+ return {
242
+ alias,
243
+ expr: column.toExpr(),
244
+ };
245
+ }),
246
+ },
247
+ };
248
+ }
@@ -0,0 +1,18 @@
1
+ import type { JoinAst } from '@prisma-next/sql-relational-core/ast';
2
+ import {
3
+ createColumnRef,
4
+ createJoin,
5
+ createJoinOnExpr,
6
+ createTableRef,
7
+ } from '@prisma-next/sql-relational-core/ast';
8
+ import type { JoinState } from '../utils/state';
9
+
10
+ export function buildJoinAst(join: JoinState): JoinAst {
11
+ // TypeScript can't narrow ColumnBuilder properly, so we assert
12
+ const onLeft = join.on.left as { table: string; column: string };
13
+ const onRight = join.on.right as { table: string; column: string };
14
+ const leftCol = createColumnRef(onLeft.table, onLeft.column);
15
+ const rightCol = createColumnRef(onRight.table, onRight.column);
16
+ const onExpr = createJoinOnExpr(leftCol, rightCol);
17
+ return createJoin(join.joinType, createTableRef(join.table.name), onExpr);
18
+ }