@prisma-next/sql-relational-core 0.3.0-pr.99.6 → 0.3.0
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/LICENSE +201 -0
- package/README.md +20 -6
- package/dist/codec-types-DcEITed4.d.mts +144 -0
- package/dist/codec-types-DcEITed4.d.mts.map +1 -0
- package/dist/errors-Cs52upp3.d.mts +8 -0
- package/dist/errors-Cs52upp3.d.mts.map +1 -0
- package/dist/errors-D3xmG4h-.mjs +35 -0
- package/dist/errors-D3xmG4h-.mjs.map +1 -0
- package/dist/exports/ast.d.mts +158 -0
- package/dist/exports/ast.d.mts.map +1 -0
- package/dist/exports/ast.mjs +1358 -0
- package/dist/exports/ast.mjs.map +1 -0
- package/dist/exports/errors.d.mts +5 -0
- package/dist/exports/errors.mjs +3 -0
- package/dist/exports/plan.d.mts +2 -0
- package/dist/exports/plan.mjs +17 -0
- package/dist/exports/plan.mjs.map +1 -0
- package/dist/exports/query-lane-context.d.mts +3 -0
- package/dist/exports/query-lane-context.mjs +1 -0
- package/dist/exports/types.d.mts +4 -0
- package/dist/exports/types.mjs +1 -0
- package/dist/index.d.mts +11 -0
- package/dist/index.mjs +7 -0
- package/dist/plan-5qUWdlJM.d.mts +28 -0
- package/dist/plan-5qUWdlJM.d.mts.map +1 -0
- package/dist/query-lane-context-UlR8vOkd.d.mts +89 -0
- package/dist/query-lane-context-UlR8vOkd.d.mts.map +1 -0
- package/dist/types-CLCtwVWx.d.mts +485 -0
- package/dist/types-CLCtwVWx.d.mts.map +1 -0
- package/dist/types-V1qiC5DO.d.mts +200 -0
- package/dist/types-V1qiC5DO.d.mts.map +1 -0
- package/package.json +29 -51
- package/src/ast/adapter-types.ts +11 -0
- package/src/ast/codec-types.ts +101 -53
- package/src/ast/driver-types.ts +20 -3
- package/src/ast/sql-codecs.ts +142 -0
- package/src/ast/types.ts +1678 -88
- package/src/errors.ts +47 -1
- package/src/exports/ast.ts +1 -8
- package/src/index.ts +0 -3
- package/src/plan.ts +4 -4
- package/src/query-lane-context.ts +74 -11
- package/src/types.ts +102 -404
- package/dist/ast/adapter-types.d.ts +0 -28
- package/dist/ast/adapter-types.d.ts.map +0 -1
- package/dist/ast/codec-types.d.ts +0 -141
- package/dist/ast/codec-types.d.ts.map +0 -1
- package/dist/ast/common.d.ts +0 -7
- package/dist/ast/common.d.ts.map +0 -1
- package/dist/ast/delete.d.ts +0 -8
- package/dist/ast/delete.d.ts.map +0 -1
- package/dist/ast/driver-types.d.ts +0 -20
- package/dist/ast/driver-types.d.ts.map +0 -1
- package/dist/ast/insert.d.ts +0 -8
- package/dist/ast/insert.d.ts.map +0 -1
- package/dist/ast/join.d.ts +0 -6
- package/dist/ast/join.d.ts.map +0 -1
- package/dist/ast/order.d.ts +0 -6
- package/dist/ast/order.d.ts.map +0 -1
- package/dist/ast/predicate.d.ts +0 -4
- package/dist/ast/predicate.d.ts.map +0 -1
- package/dist/ast/select.d.ts +0 -18
- package/dist/ast/select.d.ts.map +0 -1
- package/dist/ast/types.d.ts +0 -130
- package/dist/ast/types.d.ts.map +0 -1
- package/dist/ast/update.d.ts +0 -9
- package/dist/ast/update.d.ts.map +0 -1
- package/dist/ast/util.d.ts +0 -2
- package/dist/ast/util.d.ts.map +0 -1
- package/dist/chunk-2BWK6XEY.js +0 -13
- package/dist/chunk-2BWK6XEY.js.map +0 -1
- package/dist/chunk-2F7DSEOU.js +0 -8
- package/dist/chunk-2F7DSEOU.js.map +0 -1
- package/dist/chunk-2MAKNVCP.js +0 -86
- package/dist/chunk-2MAKNVCP.js.map +0 -1
- package/dist/chunk-36WJWNHT.js +0 -1
- package/dist/chunk-36WJWNHT.js.map +0 -1
- package/dist/chunk-3F4RFQIB.js +0 -169
- package/dist/chunk-3F4RFQIB.js.map +0 -1
- package/dist/chunk-7I3EMQID.js +0 -16
- package/dist/chunk-7I3EMQID.js.map +0 -1
- package/dist/chunk-G52ENULI.js +0 -1
- package/dist/chunk-G52ENULI.js.map +0 -1
- package/dist/chunk-HV334QHG.js +0 -320
- package/dist/chunk-HV334QHG.js.map +0 -1
- package/dist/chunk-KYSP7L5C.js +0 -16
- package/dist/chunk-KYSP7L5C.js.map +0 -1
- package/dist/chunk-U7AXAUJA.js +0 -1
- package/dist/chunk-U7AXAUJA.js.map +0 -1
- package/dist/chunk-YXD25S5I.js +0 -171
- package/dist/chunk-YXD25S5I.js.map +0 -1
- package/dist/errors.d.ts +0 -2
- package/dist/errors.d.ts.map +0 -1
- package/dist/exports/ast.d.ts +0 -14
- package/dist/exports/ast.d.ts.map +0 -1
- package/dist/exports/ast.js +0 -46
- package/dist/exports/ast.js.map +0 -1
- package/dist/exports/errors.d.ts +0 -2
- package/dist/exports/errors.d.ts.map +0 -1
- package/dist/exports/errors.js +0 -9
- package/dist/exports/errors.js.map +0 -1
- package/dist/exports/guards.d.ts +0 -2
- package/dist/exports/guards.d.ts.map +0 -1
- package/dist/exports/guards.js +0 -31
- package/dist/exports/guards.js.map +0 -1
- package/dist/exports/operations-registry.d.ts +0 -2
- package/dist/exports/operations-registry.d.ts.map +0 -1
- package/dist/exports/operations-registry.js +0 -9
- package/dist/exports/operations-registry.js.map +0 -1
- package/dist/exports/param.d.ts +0 -3
- package/dist/exports/param.d.ts.map +0 -1
- package/dist/exports/param.js +0 -7
- package/dist/exports/param.js.map +0 -1
- package/dist/exports/plan.d.ts +0 -2
- package/dist/exports/plan.d.ts.map +0 -1
- package/dist/exports/plan.js +0 -7
- package/dist/exports/plan.js.map +0 -1
- package/dist/exports/query-lane-context.d.ts +0 -2
- package/dist/exports/query-lane-context.d.ts.map +0 -1
- package/dist/exports/query-lane-context.js +0 -2
- package/dist/exports/query-lane-context.js.map +0 -1
- package/dist/exports/schema.d.ts +0 -3
- package/dist/exports/schema.d.ts.map +0 -1
- package/dist/exports/schema.js +0 -13
- package/dist/exports/schema.js.map +0 -1
- package/dist/exports/types.d.ts +0 -2
- package/dist/exports/types.d.ts.map +0 -1
- package/dist/exports/types.js +0 -10
- package/dist/exports/types.js.map +0 -1
- package/dist/index.d.ts +0 -9
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -81
- package/dist/index.js.map +0 -1
- package/dist/operations-registry.d.ts +0 -5
- package/dist/operations-registry.d.ts.map +0 -1
- package/dist/param.d.ts +0 -4
- package/dist/param.d.ts.map +0 -1
- package/dist/plan.d.ts +0 -23
- package/dist/plan.d.ts.map +0 -1
- package/dist/query-lane-context.d.ts +0 -27
- package/dist/query-lane-context.d.ts.map +0 -1
- package/dist/schema.d.ts +0 -83
- package/dist/schema.d.ts.map +0 -1
- package/dist/types.d.ts +0 -371
- package/dist/types.d.ts.map +0 -1
- package/dist/utils/guards.d.ts +0 -82
- package/dist/utils/guards.d.ts.map +0 -1
- package/src/ast/common.ts +0 -36
- package/src/ast/delete.ts +0 -17
- package/src/ast/insert.ts +0 -17
- package/src/ast/join.ts +0 -54
- package/src/ast/order.ts +0 -11
- package/src/ast/predicate.ts +0 -22
- package/src/ast/select.ts +0 -39
- package/src/ast/update.ts +0 -19
- package/src/exports/guards.ts +0 -15
- package/src/exports/operations-registry.ts +0 -1
- package/src/exports/param.ts +0 -2
- package/src/exports/schema.ts +0 -6
- package/src/operations-registry.ts +0 -276
- package/src/param.ts +0 -15
- package/src/schema.ts +0 -406
- package/src/utils/guards.ts +0 -193
package/src/schema.ts
DELETED
|
@@ -1,406 +0,0 @@
|
|
|
1
|
-
import type { OperationRegistry } from '@prisma-next/operations';
|
|
2
|
-
import { planInvalid } from '@prisma-next/plan';
|
|
3
|
-
import type {
|
|
4
|
-
ExtractCodecTypes,
|
|
5
|
-
ExtractOperationTypes,
|
|
6
|
-
SqlContract,
|
|
7
|
-
SqlStorage,
|
|
8
|
-
StorageColumn,
|
|
9
|
-
} from '@prisma-next/sql-contract/types';
|
|
10
|
-
import type { BinaryOp, ColumnRef, ExpressionSource, TableRef } from './ast/types';
|
|
11
|
-
import { attachOperationsToColumnBuilder } from './operations-registry';
|
|
12
|
-
import type { QueryLaneContext } from './query-lane-context';
|
|
13
|
-
import type {
|
|
14
|
-
BinaryBuilder,
|
|
15
|
-
CodecTypes as CodecTypesType,
|
|
16
|
-
ColumnBuilder,
|
|
17
|
-
ComputeColumnJsType,
|
|
18
|
-
OperationTypeSignature,
|
|
19
|
-
OperationTypes,
|
|
20
|
-
OrderBuilder,
|
|
21
|
-
ParamPlaceholder,
|
|
22
|
-
} from './types';
|
|
23
|
-
|
|
24
|
-
type TableColumns<Table extends { columns: Record<string, StorageColumn> }> = Table['columns'];
|
|
25
|
-
|
|
26
|
-
type ColumnBuilders<
|
|
27
|
-
Contract extends SqlContract<SqlStorage>,
|
|
28
|
-
TableName extends string,
|
|
29
|
-
Columns extends Record<string, StorageColumn>,
|
|
30
|
-
CodecTypes extends CodecTypesType,
|
|
31
|
-
Operations extends OperationTypes,
|
|
32
|
-
> = {
|
|
33
|
-
readonly [K in keyof Columns]: ColumnBuilder<
|
|
34
|
-
K & string,
|
|
35
|
-
Columns[K],
|
|
36
|
-
ComputeColumnJsType<Contract, TableName, K & string, Columns[K], CodecTypes>,
|
|
37
|
-
Operations
|
|
38
|
-
>;
|
|
39
|
-
};
|
|
40
|
-
|
|
41
|
-
export class ColumnBuilderImpl<
|
|
42
|
-
ColumnName extends string,
|
|
43
|
-
ColumnMeta extends StorageColumn,
|
|
44
|
-
JsType = unknown,
|
|
45
|
-
> implements ExpressionSource
|
|
46
|
-
{
|
|
47
|
-
readonly kind = 'column' as const;
|
|
48
|
-
|
|
49
|
-
constructor(
|
|
50
|
-
readonly table: string,
|
|
51
|
-
readonly column: ColumnName,
|
|
52
|
-
private readonly storageColumn: ColumnMeta,
|
|
53
|
-
) {}
|
|
54
|
-
|
|
55
|
-
get columnMeta(): ColumnMeta {
|
|
56
|
-
return this.storageColumn;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
// Type-level helper property (not used at runtime)
|
|
60
|
-
get __jsType(): JsType {
|
|
61
|
-
return undefined as unknown as JsType;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
/**
|
|
65
|
-
* Converts this column builder to a ColumnRef expression.
|
|
66
|
-
* This is the canonical way to get an AST node from a builder.
|
|
67
|
-
*/
|
|
68
|
-
toExpr(): ColumnRef {
|
|
69
|
-
return Object.freeze({
|
|
70
|
-
kind: 'col' as const,
|
|
71
|
-
table: this.table,
|
|
72
|
-
column: this.column,
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
private createBinaryBuilder(
|
|
77
|
-
op: BinaryOp,
|
|
78
|
-
value: ParamPlaceholder | ExpressionSource,
|
|
79
|
-
): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
|
|
80
|
-
if (value == null) {
|
|
81
|
-
throw planInvalid(
|
|
82
|
-
'Parameter placeholder or expression source required for column comparison',
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
// Check for ExpressionSource first (has toExpr method)
|
|
86
|
-
if ('toExpr' in value && typeof value.toExpr === 'function') {
|
|
87
|
-
return Object.freeze({
|
|
88
|
-
kind: 'binary' as const,
|
|
89
|
-
op,
|
|
90
|
-
left: this.toExpr(),
|
|
91
|
-
right: value,
|
|
92
|
-
}) as BinaryBuilder<ColumnName, ColumnMeta, JsType>;
|
|
93
|
-
}
|
|
94
|
-
// Must be a ParamPlaceholder
|
|
95
|
-
if ('kind' in value && value.kind === 'param-placeholder') {
|
|
96
|
-
return Object.freeze({
|
|
97
|
-
kind: 'binary' as const,
|
|
98
|
-
op,
|
|
99
|
-
left: this.toExpr(),
|
|
100
|
-
right: value,
|
|
101
|
-
}) as BinaryBuilder<ColumnName, ColumnMeta, JsType>;
|
|
102
|
-
}
|
|
103
|
-
throw planInvalid('Parameter placeholder or expression source required for column comparison');
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
eq(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
|
|
107
|
-
return this.createBinaryBuilder('eq', value);
|
|
108
|
-
}
|
|
109
|
-
|
|
110
|
-
neq(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
|
|
111
|
-
return this.createBinaryBuilder('neq', value);
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
gt(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
|
|
115
|
-
return this.createBinaryBuilder('gt', value);
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
lt(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
|
|
119
|
-
return this.createBinaryBuilder('lt', value);
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
gte(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
|
|
123
|
-
return this.createBinaryBuilder('gte', value);
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
lte(value: ParamPlaceholder | ExpressionSource): BinaryBuilder<ColumnName, ColumnMeta, JsType> {
|
|
127
|
-
return this.createBinaryBuilder('lte', value);
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
asc(): OrderBuilder<ColumnName, ColumnMeta, JsType> {
|
|
131
|
-
return Object.freeze({
|
|
132
|
-
kind: 'order' as const,
|
|
133
|
-
expr: this.toExpr(),
|
|
134
|
-
dir: 'asc' as const,
|
|
135
|
-
}) as OrderBuilder<ColumnName, ColumnMeta, JsType>;
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
desc(): OrderBuilder<ColumnName, ColumnMeta, JsType> {
|
|
139
|
-
return Object.freeze({
|
|
140
|
-
kind: 'order' as const,
|
|
141
|
-
expr: this.toExpr(),
|
|
142
|
-
dir: 'desc' as const,
|
|
143
|
-
}) as OrderBuilder<ColumnName, ColumnMeta, JsType>;
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
export class TableBuilderImpl<
|
|
148
|
-
Contract extends SqlContract<SqlStorage>,
|
|
149
|
-
TableName extends string,
|
|
150
|
-
Columns extends Record<string, StorageColumn>,
|
|
151
|
-
CodecTypes extends CodecTypesType,
|
|
152
|
-
Operations extends OperationTypes,
|
|
153
|
-
> implements TableRef
|
|
154
|
-
{
|
|
155
|
-
readonly kind = 'table' as const;
|
|
156
|
-
readonly columns: ColumnBuilders<Contract, TableName, Columns, CodecTypes, Operations>;
|
|
157
|
-
private readonly _name: TableName;
|
|
158
|
-
|
|
159
|
-
constructor(
|
|
160
|
-
name: TableName,
|
|
161
|
-
columns: ColumnBuilders<Contract, TableName, Columns, CodecTypes, Operations>,
|
|
162
|
-
) {
|
|
163
|
-
this._name = name;
|
|
164
|
-
this.columns = columns;
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
get name(): string {
|
|
168
|
-
return this._name;
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
function buildColumns<
|
|
173
|
-
Contract extends SqlContract<SqlStorage>,
|
|
174
|
-
TableName extends keyof Contract['storage']['tables'] & string,
|
|
175
|
-
CodecTypes extends CodecTypesType,
|
|
176
|
-
Operations extends OperationTypes,
|
|
177
|
-
>(
|
|
178
|
-
tableName: TableName,
|
|
179
|
-
storage: SqlStorage,
|
|
180
|
-
_contract: Contract,
|
|
181
|
-
operationRegistry?: OperationRegistry,
|
|
182
|
-
contractCapabilities?: Record<string, Record<string, boolean>>,
|
|
183
|
-
): ColumnBuilders<
|
|
184
|
-
Contract,
|
|
185
|
-
TableName,
|
|
186
|
-
Contract['storage']['tables'][TableName]['columns'],
|
|
187
|
-
CodecTypes,
|
|
188
|
-
Operations
|
|
189
|
-
> {
|
|
190
|
-
const table = storage.tables[tableName];
|
|
191
|
-
|
|
192
|
-
if (!table) {
|
|
193
|
-
throw planInvalid(`Unknown table ${tableName}`);
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
type Columns = Contract['storage']['tables'][TableName]['columns'];
|
|
197
|
-
const tableColumns = table.columns as Columns;
|
|
198
|
-
|
|
199
|
-
const result = {} as {
|
|
200
|
-
[K in keyof Columns]: ColumnBuilder<
|
|
201
|
-
K & string,
|
|
202
|
-
Columns[K],
|
|
203
|
-
ComputeColumnJsType<Contract, TableName, K & string, Columns[K], CodecTypes>,
|
|
204
|
-
Operations
|
|
205
|
-
>;
|
|
206
|
-
};
|
|
207
|
-
|
|
208
|
-
const assignColumn = <ColumnKey extends keyof Columns & string>(
|
|
209
|
-
columnName: ColumnKey,
|
|
210
|
-
columnDef: Columns[ColumnKey],
|
|
211
|
-
) => {
|
|
212
|
-
type JsType = ComputeColumnJsType<
|
|
213
|
-
Contract,
|
|
214
|
-
TableName,
|
|
215
|
-
ColumnKey,
|
|
216
|
-
Columns[ColumnKey],
|
|
217
|
-
CodecTypes
|
|
218
|
-
>;
|
|
219
|
-
|
|
220
|
-
const columnBuilder = new ColumnBuilderImpl<ColumnKey, Columns[ColumnKey], JsType>(
|
|
221
|
-
tableName,
|
|
222
|
-
columnName,
|
|
223
|
-
columnDef,
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
const builderWithOps = attachOperationsToColumnBuilder<
|
|
227
|
-
ColumnKey,
|
|
228
|
-
Columns[ColumnKey],
|
|
229
|
-
JsType,
|
|
230
|
-
Operations
|
|
231
|
-
>(
|
|
232
|
-
columnBuilder as unknown as ColumnBuilder<
|
|
233
|
-
ColumnKey,
|
|
234
|
-
Columns[ColumnKey],
|
|
235
|
-
JsType,
|
|
236
|
-
Record<string, never>
|
|
237
|
-
>,
|
|
238
|
-
columnDef,
|
|
239
|
-
operationRegistry,
|
|
240
|
-
contractCapabilities,
|
|
241
|
-
);
|
|
242
|
-
|
|
243
|
-
(result as Record<string, unknown>)[columnName] = builderWithOps;
|
|
244
|
-
};
|
|
245
|
-
|
|
246
|
-
for (const columnName of Object.keys(tableColumns) as Array<keyof Columns & string>) {
|
|
247
|
-
const columnDef = tableColumns[columnName];
|
|
248
|
-
if (!columnDef) continue;
|
|
249
|
-
assignColumn(columnName, columnDef);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
return result as ColumnBuilders<Contract, TableName, Columns, CodecTypes, Operations>;
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
/**
|
|
256
|
-
* Creates a Proxy that enables accessing table columns directly on the table object,
|
|
257
|
-
* in addition to the standard `table.columns.columnName` syntax.
|
|
258
|
-
*
|
|
259
|
-
* This allows both access patterns:
|
|
260
|
-
* - `tables.user.columns.id` (standard access)
|
|
261
|
-
* - `tables.user.id` (convenience access via proxy)
|
|
262
|
-
*
|
|
263
|
-
* The proxy intercepts property access and routes column name lookups to
|
|
264
|
-
* `table.columns[prop]`, while preserving direct access to table properties
|
|
265
|
-
* like `name`, `kind`, and `columns`.
|
|
266
|
-
*/
|
|
267
|
-
function createTableProxy<
|
|
268
|
-
Contract extends SqlContract<SqlStorage>,
|
|
269
|
-
TableName extends string,
|
|
270
|
-
Columns extends Record<string, StorageColumn>,
|
|
271
|
-
CodecTypes extends CodecTypesType,
|
|
272
|
-
Operations extends OperationTypes,
|
|
273
|
-
>(
|
|
274
|
-
table: TableBuilderImpl<Contract, TableName, Columns, CodecTypes, Operations>,
|
|
275
|
-
): TableBuilderImpl<Contract, TableName, Columns, CodecTypes, Operations> {
|
|
276
|
-
return new Proxy(table, {
|
|
277
|
-
get(target, prop) {
|
|
278
|
-
if (prop === 'name' || prop === 'kind' || prop === 'columns') {
|
|
279
|
-
return Reflect.get(target, prop);
|
|
280
|
-
}
|
|
281
|
-
if (typeof prop === 'string' && prop in target.columns) {
|
|
282
|
-
return target.columns[prop as keyof typeof target.columns];
|
|
283
|
-
}
|
|
284
|
-
return undefined;
|
|
285
|
-
},
|
|
286
|
-
});
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
type ExtractSchemaTables<
|
|
290
|
-
Contract extends SqlContract<SqlStorage>,
|
|
291
|
-
CodecTypes extends CodecTypesType,
|
|
292
|
-
Operations extends OperationTypes,
|
|
293
|
-
> = {
|
|
294
|
-
readonly [TableName in keyof Contract['storage']['tables']]: TableBuilderImpl<
|
|
295
|
-
Contract,
|
|
296
|
-
TableName & string,
|
|
297
|
-
TableColumns<Contract['storage']['tables'][TableName]>,
|
|
298
|
-
CodecTypes,
|
|
299
|
-
Operations
|
|
300
|
-
> &
|
|
301
|
-
TableRef;
|
|
302
|
-
};
|
|
303
|
-
|
|
304
|
-
/**
|
|
305
|
-
* Extracts the types registry shape from a contract.
|
|
306
|
-
* Each key is a type name from storage.types, and the value preserves the
|
|
307
|
-
* literal type from the contract (including codecId, nativeType, and typeParams).
|
|
308
|
-
* Returns an empty object type {} when storage.types is undefined.
|
|
309
|
-
*/
|
|
310
|
-
type ExtractSchemaTypes<Contract extends SqlContract<SqlStorage>> =
|
|
311
|
-
Contract['storage']['types'] extends infer Types
|
|
312
|
-
? Types extends Record<string, unknown>
|
|
313
|
-
? { readonly [TypeName in keyof Types]: Types[TypeName] }
|
|
314
|
-
: Record<string, never>
|
|
315
|
-
: Record<string, never>;
|
|
316
|
-
|
|
317
|
-
export type SchemaHandle<
|
|
318
|
-
Contract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,
|
|
319
|
-
CodecTypes extends CodecTypesType = CodecTypesType,
|
|
320
|
-
Operations extends OperationTypes = Record<string, never>,
|
|
321
|
-
> = {
|
|
322
|
-
readonly tables: ExtractSchemaTables<Contract, CodecTypes, Operations>;
|
|
323
|
-
/**
|
|
324
|
-
* Initialized type helpers from storage.types.
|
|
325
|
-
* Each entry corresponds to a named type instance in the contract's storage.types.
|
|
326
|
-
*/
|
|
327
|
-
readonly types: ExtractSchemaTypes<Contract>;
|
|
328
|
-
};
|
|
329
|
-
|
|
330
|
-
type SchemaReturnType<Contract extends SqlContract<SqlStorage>> = SchemaHandle<
|
|
331
|
-
Contract,
|
|
332
|
-
ExtractCodecTypes<Contract>,
|
|
333
|
-
ToOperationTypes<ExtractOperationTypes<Contract>>
|
|
334
|
-
>;
|
|
335
|
-
|
|
336
|
-
type NormalizeOperationTypes<T> = {
|
|
337
|
-
[TypeId in keyof T]: {
|
|
338
|
-
[Method in keyof T[TypeId]]: T[TypeId][Method] extends OperationTypeSignature
|
|
339
|
-
? T[TypeId][Method]
|
|
340
|
-
: OperationTypeSignature;
|
|
341
|
-
};
|
|
342
|
-
};
|
|
343
|
-
|
|
344
|
-
type ToOperationTypes<T> = T extends OperationTypes ? T : NormalizeOperationTypes<T>;
|
|
345
|
-
|
|
346
|
-
/**
|
|
347
|
-
* Creates a schema handle for building SQL queries.
|
|
348
|
-
*
|
|
349
|
-
* @param context - Query lane context containing contract, codec and operation registries
|
|
350
|
-
* @returns A schema handle with typed table builders and type helpers
|
|
351
|
-
*
|
|
352
|
-
* @example
|
|
353
|
-
* ```typescript
|
|
354
|
-
* const schemaHandle = schema<Contract>(context);
|
|
355
|
-
* const userTable = schemaHandle.tables.user;
|
|
356
|
-
* const vectorType = schemaHandle.types.Vector1536;
|
|
357
|
-
* ```
|
|
358
|
-
*/
|
|
359
|
-
export function schema<Contract extends SqlContract<SqlStorage>>(
|
|
360
|
-
context: QueryLaneContext<Contract>,
|
|
361
|
-
): SchemaReturnType<Contract> {
|
|
362
|
-
const contract = context.contract;
|
|
363
|
-
const storage = contract.storage;
|
|
364
|
-
type CodecTypes = ExtractCodecTypes<Contract>;
|
|
365
|
-
type Operations = ToOperationTypes<ExtractOperationTypes<Contract>>;
|
|
366
|
-
const tables = {} as ExtractSchemaTables<Contract, CodecTypes, Operations>;
|
|
367
|
-
const contractCapabilities = contract.capabilities;
|
|
368
|
-
|
|
369
|
-
const operationRegistry = context.operations;
|
|
370
|
-
|
|
371
|
-
for (const tableName of Object.keys(storage.tables) as Array<
|
|
372
|
-
keyof Contract['storage']['tables'] & string
|
|
373
|
-
>) {
|
|
374
|
-
const columns = buildColumns<Contract, typeof tableName, CodecTypes, Operations>(
|
|
375
|
-
tableName,
|
|
376
|
-
storage,
|
|
377
|
-
contract,
|
|
378
|
-
operationRegistry,
|
|
379
|
-
contractCapabilities,
|
|
380
|
-
);
|
|
381
|
-
const table = new TableBuilderImpl<
|
|
382
|
-
Contract,
|
|
383
|
-
typeof tableName & string,
|
|
384
|
-
Contract['storage']['tables'][typeof tableName]['columns'],
|
|
385
|
-
CodecTypes,
|
|
386
|
-
Operations
|
|
387
|
-
>(tableName, columns);
|
|
388
|
-
const proxiedTable = createTableProxy<
|
|
389
|
-
Contract,
|
|
390
|
-
typeof tableName & string,
|
|
391
|
-
Contract['storage']['tables'][typeof tableName]['columns'],
|
|
392
|
-
CodecTypes,
|
|
393
|
-
Operations
|
|
394
|
-
>(table);
|
|
395
|
-
(tables as Record<string, unknown>)[tableName] = Object.freeze(
|
|
396
|
-
proxiedTable,
|
|
397
|
-
) as ExtractSchemaTables<Contract, CodecTypes, Operations>[typeof tableName];
|
|
398
|
-
}
|
|
399
|
-
|
|
400
|
-
// Get type helpers from context (populated by createRuntimeContext)
|
|
401
|
-
const types = (context.types ?? {}) as ExtractSchemaTypes<Contract>;
|
|
402
|
-
|
|
403
|
-
return Object.freeze({ tables, types }) as SchemaReturnType<Contract>;
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
export type { ColumnBuilderImpl as Column, TableBuilderImpl as Table };
|
package/src/utils/guards.ts
DELETED
|
@@ -1,193 +0,0 @@
|
|
|
1
|
-
import type { StorageColumn } from '@prisma-next/sql-contract/types';
|
|
2
|
-
import type {
|
|
3
|
-
ColumnRef,
|
|
4
|
-
Expression,
|
|
5
|
-
ExpressionSource,
|
|
6
|
-
LiteralExpr,
|
|
7
|
-
OperationExpr,
|
|
8
|
-
ParamRef,
|
|
9
|
-
} from '../ast/types';
|
|
10
|
-
import type {
|
|
11
|
-
AnyColumnBuilder,
|
|
12
|
-
AnyExpressionSource,
|
|
13
|
-
ExpressionBuilder,
|
|
14
|
-
ParamPlaceholder,
|
|
15
|
-
ValueSource,
|
|
16
|
-
} from '../types';
|
|
17
|
-
|
|
18
|
-
/**
|
|
19
|
-
* Helper to extract columnMeta from a ColumnBuilder or ExpressionBuilder.
|
|
20
|
-
* Returns StorageColumn if present, undefined otherwise.
|
|
21
|
-
* Both ColumnBuilder and ExpressionBuilder have columnMeta property.
|
|
22
|
-
*/
|
|
23
|
-
export function getColumnMeta(expr: AnyExpressionSource): StorageColumn | undefined {
|
|
24
|
-
// Both ColumnBuilder and ExpressionBuilder have columnMeta: StorageColumn
|
|
25
|
-
// TypeScript should narrow the type after the 'in' check
|
|
26
|
-
if ('columnMeta' in expr) {
|
|
27
|
-
return expr.columnMeta;
|
|
28
|
-
}
|
|
29
|
-
return undefined;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Type predicate to check if a value is a ParamPlaceholder.
|
|
34
|
-
*/
|
|
35
|
-
export function isParamPlaceholder(value: unknown): value is ParamPlaceholder {
|
|
36
|
-
return (
|
|
37
|
-
typeof value === 'object' &&
|
|
38
|
-
value !== null &&
|
|
39
|
-
'kind' in value &&
|
|
40
|
-
(value as { kind: unknown }).kind === 'param-placeholder' &&
|
|
41
|
-
'name' in value &&
|
|
42
|
-
typeof (value as { name: unknown }).name === 'string'
|
|
43
|
-
);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
/**
|
|
47
|
-
* Recursively extracts the base ColumnRef from an OperationExpr.
|
|
48
|
-
* If the expression is already a ColumnRef, it is returned directly.
|
|
49
|
-
*/
|
|
50
|
-
export function extractBaseColumnRef(expr: ColumnRef | OperationExpr): ColumnRef {
|
|
51
|
-
if (expr.kind === 'col') {
|
|
52
|
-
return expr;
|
|
53
|
-
}
|
|
54
|
-
return extractBaseColumnRef(expr.self);
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Recursively collects all ColumnRef nodes from an expression tree.
|
|
59
|
-
* Handles nested OperationExpr structures by traversing both self and args.
|
|
60
|
-
*/
|
|
61
|
-
export function collectColumnRefs(
|
|
62
|
-
expr: ColumnRef | ParamRef | LiteralExpr | OperationExpr,
|
|
63
|
-
): ColumnRef[] {
|
|
64
|
-
if (expr.kind === 'col') {
|
|
65
|
-
return [expr];
|
|
66
|
-
}
|
|
67
|
-
if (expr.kind === 'operation') {
|
|
68
|
-
const refs: ColumnRef[] = collectColumnRefs(expr.self);
|
|
69
|
-
for (const arg of expr.args) {
|
|
70
|
-
refs.push(...collectColumnRefs(arg));
|
|
71
|
-
}
|
|
72
|
-
return refs;
|
|
73
|
-
}
|
|
74
|
-
return [];
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
* Type predicate to check if an expression is an OperationExpr.
|
|
79
|
-
*/
|
|
80
|
-
export function isOperationExpr(
|
|
81
|
-
expr: AnyExpressionSource | OperationExpr | Expression,
|
|
82
|
-
): expr is OperationExpr {
|
|
83
|
-
return typeof expr === 'object' && expr !== null && 'kind' in expr && expr.kind === 'operation';
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Helper to extract table and column from a ColumnBuilder, ExpressionBuilder, or OperationExpr.
|
|
88
|
-
* For ExpressionBuilder or OperationExpr, recursively unwraps to find the base ColumnRef.
|
|
89
|
-
*/
|
|
90
|
-
export function getColumnInfo(expr: AnyExpressionSource | OperationExpr): {
|
|
91
|
-
table: string;
|
|
92
|
-
column: string;
|
|
93
|
-
} {
|
|
94
|
-
if (isOperationExpr(expr)) {
|
|
95
|
-
const baseCol = extractBaseColumnRef(expr);
|
|
96
|
-
return { table: baseCol.table, column: baseCol.column };
|
|
97
|
-
}
|
|
98
|
-
if (isExpressionBuilder(expr)) {
|
|
99
|
-
const baseCol = extractBaseColumnRef(expr.expr);
|
|
100
|
-
return { table: baseCol.table, column: baseCol.column };
|
|
101
|
-
}
|
|
102
|
-
// expr is ColumnBuilder - TypeScript can't narrow properly
|
|
103
|
-
const colBuilder = expr as unknown as { table: string; column: string };
|
|
104
|
-
return { table: colBuilder.table, column: colBuilder.column };
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Type predicate to check if a value is a ColumnBuilder.
|
|
109
|
-
*/
|
|
110
|
-
export function isColumnBuilder(value: unknown): value is AnyColumnBuilder {
|
|
111
|
-
return (
|
|
112
|
-
typeof value === 'object' &&
|
|
113
|
-
value !== null &&
|
|
114
|
-
'kind' in value &&
|
|
115
|
-
(value as { kind: unknown }).kind === 'column'
|
|
116
|
-
);
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
/**
|
|
120
|
-
* Type predicate to check if a value is an ExpressionBuilder.
|
|
121
|
-
*/
|
|
122
|
-
export function isExpressionBuilder(value: unknown): value is ExpressionBuilder {
|
|
123
|
-
return (
|
|
124
|
-
typeof value === 'object' &&
|
|
125
|
-
value !== null &&
|
|
126
|
-
'kind' in value &&
|
|
127
|
-
(value as { kind: unknown }).kind === 'expression'
|
|
128
|
-
);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
/**
|
|
132
|
-
* Type predicate to check if a value is an ExpressionSource (has toExpr method).
|
|
133
|
-
*/
|
|
134
|
-
export function isExpressionSource(value: unknown): value is ExpressionSource {
|
|
135
|
-
return (
|
|
136
|
-
typeof value === 'object' &&
|
|
137
|
-
value !== null &&
|
|
138
|
-
'toExpr' in value &&
|
|
139
|
-
typeof (value as ExpressionSource).toExpr === 'function'
|
|
140
|
-
);
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
/**
|
|
144
|
-
* Converts any ExpressionSource to an Expression.
|
|
145
|
-
* This is the canonical way to get an AST Expression from a builder.
|
|
146
|
-
*
|
|
147
|
-
* @param source - A ColumnBuilder or ExpressionBuilder
|
|
148
|
-
* @returns The corresponding Expression (ColumnRef or OperationExpr)
|
|
149
|
-
*/
|
|
150
|
-
export function toExpression(source: ExpressionSource): Expression {
|
|
151
|
-
return source.toExpr();
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
/**
|
|
155
|
-
* Converts an AnyExpressionSource to an Expression.
|
|
156
|
-
* Handles both ColumnBuilder and ExpressionBuilder.
|
|
157
|
-
*
|
|
158
|
-
* @param source - A ColumnBuilder or ExpressionBuilder
|
|
159
|
-
* @returns The corresponding Expression (ColumnRef or OperationExpr)
|
|
160
|
-
*/
|
|
161
|
-
export function expressionFromSource(source: AnyExpressionSource): Expression {
|
|
162
|
-
return source.toExpr();
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
/**
|
|
166
|
-
* Type predicate to check if a value is a ValueSource.
|
|
167
|
-
* ValueSource is either a ParamPlaceholder or an ExpressionSource.
|
|
168
|
-
*/
|
|
169
|
-
export function isValueSource(value: unknown): value is ValueSource {
|
|
170
|
-
return isParamPlaceholder(value) || isExpressionSource(value);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
/**
|
|
174
|
-
* Extracts and returns an OperationExpr from a builder.
|
|
175
|
-
* Returns the OperationExpr if the builder is an OperationExpr or has an _operationExpr property,
|
|
176
|
-
* otherwise returns undefined.
|
|
177
|
-
*
|
|
178
|
-
* @deprecated Use isExpressionBuilder() instead. This function exists for backward compatibility
|
|
179
|
-
* with code that uses the hidden _operationExpr property pattern.
|
|
180
|
-
*
|
|
181
|
-
* @design-note: This function accesses the hidden `_operationExpr` property, which is a code smell.
|
|
182
|
-
* The ExpressionBuilder type (introduced in the operation-expr-refactoring) provides a cleaner
|
|
183
|
-
* approach by explicitly representing operation results as a distinct type.
|
|
184
|
-
*/
|
|
185
|
-
export function getOperationExpr(
|
|
186
|
-
builder: AnyColumnBuilder | OperationExpr,
|
|
187
|
-
): OperationExpr | undefined {
|
|
188
|
-
if (isOperationExpr(builder)) {
|
|
189
|
-
return builder;
|
|
190
|
-
}
|
|
191
|
-
const builderWithExpr = builder as unknown as { _operationExpr?: OperationExpr };
|
|
192
|
-
return builderWithExpr._operationExpr;
|
|
193
|
-
}
|