@prisma-next/sql-relational-core 0.3.0-dev.3 → 0.3.0-dev.30
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/README.md +1 -1
- package/dist/ast/adapter-types.d.ts +28 -0
- package/dist/ast/adapter-types.d.ts.map +1 -0
- package/dist/{query-lane-context-BhOMmb_K.d.ts → ast/codec-types.d.ts} +14 -31
- package/dist/ast/codec-types.d.ts.map +1 -0
- package/dist/ast/common.d.ts +7 -0
- package/dist/ast/common.d.ts.map +1 -0
- package/dist/ast/delete.d.ts +8 -0
- package/dist/ast/delete.d.ts.map +1 -0
- package/dist/ast/driver-types.d.ts +20 -0
- package/dist/ast/driver-types.d.ts.map +1 -0
- package/dist/ast/insert.d.ts +8 -0
- package/dist/ast/insert.d.ts.map +1 -0
- package/dist/ast/join.d.ts +6 -0
- package/dist/ast/join.d.ts.map +1 -0
- package/dist/ast/order.d.ts +6 -0
- package/dist/ast/order.d.ts.map +1 -0
- package/dist/ast/predicate.d.ts +4 -0
- package/dist/ast/predicate.d.ts.map +1 -0
- package/dist/ast/select.d.ts +18 -0
- package/dist/ast/select.d.ts.map +1 -0
- package/dist/ast/types.d.ts +130 -0
- package/dist/ast/types.d.ts.map +1 -0
- package/dist/ast/update.d.ts +9 -0
- package/dist/ast/update.d.ts.map +1 -0
- package/dist/ast/util.d.ts +2 -0
- package/dist/ast/util.d.ts.map +1 -0
- package/dist/{chunk-MM74SVJ4.js → chunk-2BWK6XEY.js} +1 -1
- package/dist/chunk-2BWK6XEY.js.map +1 -0
- package/dist/{chunk-5N34PNVZ.js → chunk-2MAKNVCP.js} +25 -1
- package/dist/chunk-2MAKNVCP.js.map +1 -0
- package/dist/{chunk-M23L3JHG.js → chunk-3F4RFQIB.js} +45 -35
- package/dist/chunk-3F4RFQIB.js.map +1 -0
- package/dist/{chunk-WZBPVEZI.js → chunk-HV334QHG.js} +2 -2
- package/dist/chunk-HV334QHG.js.map +1 -0
- package/dist/{chunk-CBTYMOX2.js → chunk-YXD25S5I.js} +31 -12
- package/dist/chunk-YXD25S5I.js.map +1 -0
- package/dist/errors.d.ts +2 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/exports/ast.d.ts +14 -119
- package/dist/exports/ast.d.ts.map +1 -0
- package/dist/exports/ast.js +2 -2
- package/dist/exports/errors.d.ts +2 -1
- package/dist/exports/errors.d.ts.map +1 -0
- package/dist/exports/guards.d.ts +2 -63
- package/dist/exports/guards.d.ts.map +1 -0
- package/dist/exports/guards.js +13 -3
- package/dist/exports/operations-registry.d.ts +2 -13
- package/dist/exports/operations-registry.d.ts.map +1 -0
- package/dist/exports/operations-registry.js +2 -2
- package/dist/exports/param.d.ts +3 -14
- package/dist/exports/param.d.ts.map +1 -0
- package/dist/exports/plan.d.ts +2 -5
- package/dist/exports/plan.d.ts.map +1 -0
- package/dist/exports/query-lane-context.d.ts +2 -4
- package/dist/exports/query-lane-context.d.ts.map +1 -0
- package/dist/exports/schema.d.ts +3 -69
- package/dist/exports/schema.d.ts.map +1 -0
- package/dist/exports/schema.js +3 -4
- package/dist/exports/types.d.ts +2 -335
- package/dist/exports/types.d.ts.map +1 -0
- package/dist/exports/types.js +1 -1
- package/dist/index.d.ts +9 -13
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -5
- package/dist/operations-registry.d.ts +5 -0
- package/dist/operations-registry.d.ts.map +1 -0
- package/dist/param.d.ts +4 -0
- package/dist/param.d.ts.map +1 -0
- package/dist/plan.d.ts +23 -0
- package/dist/plan.d.ts.map +1 -0
- package/dist/query-lane-context.d.ts +27 -0
- package/dist/query-lane-context.d.ts.map +1 -0
- package/dist/schema.d.ts +83 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/types.d.ts +371 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/guards.d.ts +82 -0
- package/dist/utils/guards.d.ts.map +1 -0
- package/package.json +19 -19
- package/src/ast/adapter-types.ts +36 -0
- package/src/ast/codec-types.ts +376 -0
- package/src/ast/common.ts +36 -0
- package/src/ast/delete.ts +17 -0
- package/src/ast/driver-types.ts +25 -0
- package/src/ast/insert.ts +17 -0
- package/src/ast/join.ts +54 -0
- package/src/ast/order.ts +11 -0
- package/src/ast/predicate.ts +22 -0
- package/src/ast/select.ts +39 -0
- package/src/ast/types.ts +147 -0
- package/src/ast/update.ts +19 -0
- package/src/ast/util.ts +9 -0
- package/src/errors.ts +1 -0
- package/src/exports/ast.ts +13 -0
- package/src/exports/errors.ts +1 -0
- package/src/exports/guards.ts +15 -0
- package/src/exports/operations-registry.ts +1 -0
- package/src/exports/param.ts +2 -0
- package/src/exports/plan.ts +1 -0
- package/src/exports/query-lane-context.ts +1 -0
- package/src/exports/schema.ts +6 -0
- package/src/exports/types.ts +1 -0
- package/src/index.ts +8 -0
- package/src/operations-registry.ts +276 -0
- package/src/param.ts +15 -0
- package/src/plan.ts +39 -0
- package/src/query-lane-context.ts +30 -0
- package/src/schema.ts +406 -0
- package/src/types.ts +598 -0
- package/src/utils/guards.ts +193 -0
- package/dist/chunk-5N34PNVZ.js.map +0 -1
- package/dist/chunk-CBTYMOX2.js.map +0 -1
- package/dist/chunk-M23L3JHG.js.map +0 -1
- package/dist/chunk-MM74SVJ4.js.map +0 -1
- package/dist/chunk-WZBPVEZI.js.map +0 -1
- package/dist/plan-D0OG5qzy.d.ts +0 -142
package/src/schema.ts
ADDED
|
@@ -0,0 +1,406 @@
|
|
|
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 };
|