@prisma-next/sql-relational-core 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.
- package/LICENSE +201 -0
- package/README.md +18 -2
- package/dist/errors-7_V3El9I.mjs +3 -0
- package/dist/errors-DVufq9PT.d.mts +2 -0
- package/dist/exports/ast.d.mts +192 -0
- package/dist/exports/ast.d.mts.map +1 -0
- package/dist/exports/ast.mjs +361 -0
- package/dist/exports/ast.mjs.map +1 -0
- package/dist/exports/errors.d.mts +2 -0
- package/dist/exports/errors.mjs +3 -0
- package/dist/exports/guards.d.mts +5 -0
- package/dist/exports/guards.mjs +3 -0
- package/dist/exports/operations-registry.d.mts +5 -0
- package/dist/exports/operations-registry.mjs +3 -0
- package/dist/exports/param.d.mts +5 -0
- package/dist/exports/param.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 +2 -0
- package/dist/exports/query-lane-context.mjs +1 -0
- package/dist/exports/schema.d.mts +5 -0
- package/dist/exports/schema.mjs +4 -0
- package/dist/exports/types.d.mts +4 -0
- package/dist/exports/types.mjs +3 -0
- package/dist/exports/utils/guards.d.mts +5 -0
- package/dist/exports/utils/guards.mjs +4 -0
- package/dist/guards-0ycyntYX.mjs +132 -0
- package/dist/guards-0ycyntYX.mjs.map +1 -0
- package/dist/guards-DCCpAlOL.d.mts +87 -0
- package/dist/guards-DCCpAlOL.d.mts.map +1 -0
- package/dist/index.d.mts +12 -0
- package/dist/index.mjs +12 -0
- package/dist/operations-registry-DPZ5aElH.mjs +152 -0
- package/dist/operations-registry-DPZ5aElH.mjs.map +1 -0
- package/dist/operations-registry-wVEaiLyt.d.mts +9 -0
- package/dist/operations-registry-wVEaiLyt.d.mts.map +1 -0
- package/dist/param-C4n0OW59.d.mts +8 -0
- package/dist/param-C4n0OW59.d.mts.map +1 -0
- package/dist/param-DMU3OWfI.mjs +14 -0
- package/dist/param-DMU3OWfI.mjs.map +1 -0
- package/dist/plan-BhSWuTfw.d.mts +194 -0
- package/dist/plan-BhSWuTfw.d.mts.map +1 -0
- package/dist/query-lane-context-CgkPuKaR.d.mts +259 -0
- package/dist/query-lane-context-CgkPuKaR.d.mts.map +1 -0
- package/dist/schema-Bi5p4aAc.mjs +175 -0
- package/dist/schema-Bi5p4aAc.mjs.map +1 -0
- package/dist/schema-CgrEMqEd.d.mts +86 -0
- package/dist/schema-CgrEMqEd.d.mts.map +1 -0
- package/dist/types-CEUzDuDC.d.mts +447 -0
- package/dist/types-CEUzDuDC.d.mts.map +1 -0
- package/dist/types-Cdi4Whda.mjs +18 -0
- package/dist/types-Cdi4Whda.mjs.map +1 -0
- package/package.json +38 -55
- package/src/ast/codec-types.ts +77 -8
- package/src/ast/delete.ts +2 -2
- package/src/ast/driver-types.ts +20 -3
- package/src/ast/predicate.ts +14 -4
- package/src/ast/select.ts +4 -3
- package/src/ast/sql-codecs.ts +67 -0
- package/src/ast/types.ts +89 -19
- package/src/ast/update.ts +2 -2
- package/src/exports/ast.ts +1 -0
- package/src/exports/guards.ts +5 -0
- package/src/exports/utils/guards.ts +1 -0
- package/src/operations-registry.ts +112 -73
- package/src/query-lane-context.ts +77 -1
- package/src/schema.ts +91 -33
- package/src/types.ts +327 -75
- package/src/utils/guards.ts +88 -18
- 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 -118
- 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-2F7DSEOU.js +0 -8
- package/dist/chunk-2F7DSEOU.js.map +0 -1
- package/dist/chunk-36WJWNHT.js +0 -1
- package/dist/chunk-36WJWNHT.js.map +0 -1
- package/dist/chunk-5N34PNVZ.js +0 -62
- package/dist/chunk-5N34PNVZ.js.map +0 -1
- package/dist/chunk-7I3EMQID.js +0 -16
- package/dist/chunk-7I3EMQID.js.map +0 -1
- package/dist/chunk-CBTYMOX2.js +0 -152
- package/dist/chunk-CBTYMOX2.js.map +0 -1
- package/dist/chunk-G52ENULI.js +0 -1
- package/dist/chunk-G52ENULI.js.map +0 -1
- package/dist/chunk-KYSP7L5C.js +0 -16
- package/dist/chunk-KYSP7L5C.js.map +0 -1
- package/dist/chunk-M23L3JHG.js +0 -159
- package/dist/chunk-M23L3JHG.js.map +0 -1
- package/dist/chunk-MM74SVJ4.js +0 -13
- package/dist/chunk-MM74SVJ4.js.map +0 -1
- package/dist/chunk-U7AXAUJA.js +0 -1
- package/dist/chunk-U7AXAUJA.js.map +0 -1
- package/dist/chunk-WZBPVEZI.js +0 -320
- package/dist/chunk-WZBPVEZI.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 -21
- 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 -14
- 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 -16
- package/dist/query-lane-context.d.ts.map +0 -1
- package/dist/schema.d.ts +0 -63
- package/dist/schema.d.ts.map +0 -1
- package/dist/types.d.ts +0 -332
- package/dist/types.d.ts.map +0 -1
- package/dist/utils/guards.d.ts +0 -55
- package/dist/utils/guards.d.ts.map +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types-Cdi4Whda.mjs","names":[],"sources":["../src/types.ts"],"sourcesContent":["import type {\n ResultType as CoreResultType,\n ExecutionPlan,\n PlanRefs,\n} from '@prisma-next/contract/types';\nimport type { ArgSpec, ReturnSpec } from '@prisma-next/operations';\nimport type { SqlContract, SqlStorage, StorageColumn } from '@prisma-next/sql-contract/types';\nimport type { SqlLoweringSpec } from '@prisma-next/sql-operations';\nimport type {\n BinaryOp,\n ColumnRef,\n Direction,\n Expression,\n ExpressionSource,\n OperationExpr,\n ParamRef,\n QueryAst,\n} from './ast/types';\nimport type { SqlQueryPlan } from './plan';\nimport type { ExecutionContext } from './query-lane-context';\n\nexport interface ParamPlaceholder {\n readonly kind: 'param-placeholder';\n readonly name: string;\n}\n\n/**\n * ValueSource represents any value that can appear in a comparison or as an argument.\n * This includes:\n * - ParamPlaceholder: A parameter placeholder (e.g., `param('userId')`)\n * - ExpressionSource: Something that can be converted to an Expression (ColumnBuilder, ExpressionBuilder)\n */\nexport type ValueSource = ParamPlaceholder | ExpressionSource;\n\nexport interface OrderBuilder<\n _ColumnName extends string = string,\n _ColumnMeta extends StorageColumn = StorageColumn,\n _JsType = unknown,\n> {\n readonly kind: 'order';\n readonly expr: Expression;\n readonly dir: Direction;\n}\n\n/**\n * Creates an OrderBuilder for use in orderBy clauses.\n */\nexport function createOrderBuilder(\n expr: AnyColumnBuilder | OperationExpr,\n dir: Direction,\n): AnyOrderBuilder {\n return { kind: 'order', expr, dir } as AnyOrderBuilder;\n}\n\n/**\n * ColumnBuilder with optional operation methods based on the column's typeId.\n * When Operations is provided and the column's typeId matches, operation methods are included.\n * Implements ExpressionSource to provide type-safe conversion to ColumnRef.\n *\n * For nullable columns (ColumnMeta['nullable'] extends true), includes isNull() and isNotNull() methods.\n */\nexport type ColumnBuilder<\n ColumnName extends string = string,\n ColumnMeta extends StorageColumn = StorageColumn,\n JsType = unknown,\n Operations extends OperationTypes = Record<string, never>,\n> = {\n readonly kind: 'column';\n readonly table: string;\n readonly column: ColumnName;\n readonly columnMeta: ColumnMeta;\n // Methods accept ValueSource (ParamPlaceholder or ExpressionSource)\n eq(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;\n neq(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;\n gt(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;\n lt(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;\n gte(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;\n lte(value: ValueSource): BinaryBuilder<ColumnName, ColumnMeta, JsType>;\n asc(): OrderBuilder<ColumnName, ColumnMeta, JsType>;\n desc(): OrderBuilder<ColumnName, ColumnMeta, JsType>;\n /** Converts this column builder to a ColumnRef expression */\n toExpr(): ColumnRef;\n // Helper property for type extraction (not used at runtime)\n readonly __jsType: JsType;\n} & (ColumnMeta['codecId'] extends string\n ? ColumnMeta['codecId'] extends keyof Operations\n ? OperationMethods<\n OperationsForTypeId<ColumnMeta['codecId'] & string, Operations>,\n ColumnName,\n StorageColumn,\n JsType\n >\n : Record<string, never>\n : Record<string, never>) &\n (ColumnMeta['nullable'] extends true\n ? NullableMethods<ColumnName, ColumnMeta, JsType>\n : Record<string, never>);\n\nexport interface BinaryBuilder<\n _ColumnName extends string = string,\n _ColumnMeta extends StorageColumn = StorageColumn,\n _JsType = unknown,\n> {\n readonly kind: 'binary';\n readonly op: BinaryOp;\n readonly left: Expression;\n readonly right: ValueSource;\n}\n\n/**\n * Builder for IS NULL / IS NOT NULL checks.\n * Used to build unary null check expressions in WHERE clauses.\n */\nexport interface NullCheckBuilder<\n _ColumnName extends string = string,\n _ColumnMeta extends StorageColumn = StorageColumn,\n _JsType = unknown,\n> {\n readonly kind: 'nullCheck';\n readonly expr: Expression;\n readonly isNull: boolean;\n}\n\n/**\n * Union type for unary builders (currently just NullCheckBuilder).\n * Extensible for future unary operators.\n */\nexport type UnaryBuilder = NullCheckBuilder;\n\n// Forward declare AnyBinaryBuilder and AnyOrderBuilder for use in ExpressionBuilder\nexport type AnyBinaryBuilder = BinaryBuilder;\nexport type AnyOrderBuilder = OrderBuilder;\nexport type AnyUnaryBuilder = UnaryBuilder;\n\n/**\n * Methods available only on nullable columns.\n * These are conditionally added to ColumnBuilder when ColumnMeta['nullable'] is true.\n * Note: Index signature is required for compatibility with AnyColumnBuilderBase's index signature.\n */\nexport interface NullableMethods<\n ColumnName extends string = string,\n ColumnMeta extends StorageColumn = StorageColumn,\n JsType = unknown,\n> {\n /** Creates an IS NULL check for this column */\n isNull(): NullCheckBuilder<ColumnName, ColumnMeta, JsType>;\n /** Creates an IS NOT NULL check for this column */\n isNotNull(): NullCheckBuilder<ColumnName, ColumnMeta, JsType>;\n /** Index signature for compatibility with AnyColumnBuilderBase */\n readonly [key: string]: unknown;\n}\n\n/**\n * ExpressionBuilder represents the result of an operation (e.g., col.distance(...)).\n * Unlike ColumnBuilder (which represents a column), ExpressionBuilder represents\n * an operation expression and provides the same DSL methods for chaining.\n *\n * Implements ExpressionSource to provide type-safe conversion to OperationExpr.\n */\nexport interface ExpressionBuilder<JsType = unknown> extends ExpressionSource {\n readonly kind: 'expression';\n readonly expr: OperationExpr;\n readonly columnMeta: StorageColumn;\n\n // Methods accept ValueSource (ParamPlaceholder or ExpressionSource)\n eq(value: ValueSource): AnyBinaryBuilder;\n neq(value: ValueSource): AnyBinaryBuilder;\n gt(value: ValueSource): AnyBinaryBuilder;\n lt(value: ValueSource): AnyBinaryBuilder;\n gte(value: ValueSource): AnyBinaryBuilder;\n lte(value: ValueSource): AnyBinaryBuilder;\n asc(): AnyOrderBuilder;\n desc(): AnyOrderBuilder;\n\n /** Converts this expression builder to the underlying OperationExpr */\n toExpr(): OperationExpr;\n\n // Helper property for type extraction (not used at runtime)\n readonly __jsType: JsType;\n}\n\n// Helper aliases for usage sites where the specific column parameters are irrelevant\n// Accepts any ColumnBuilder regardless of its Operations parameter\n// Note: We use `any` here because TypeScript's variance rules don't allow us to express\n// \"any type that extends OperationTypes\" in a way that works for assignment.\n// Contract-specific OperationTypes (e.g., PgVectorOperationTypes) are not assignable\n// to the base OperationTypes in generic parameter position, even though they extend it structurally.\n// Helper type that accepts any ColumnBuilder regardless of its generic parameters\n// This is needed because conditional types in ColumnBuilder create incompatible intersection types\n// when Operations differs, even though structurally they're compatible\nexport type AnyColumnBuilderBase = {\n readonly kind: 'column';\n readonly table: string;\n readonly column: string;\n readonly columnMeta: StorageColumn;\n // Methods accept ValueSource (ParamPlaceholder or ExpressionSource)\n eq(value: ValueSource): AnyBinaryBuilder;\n neq(value: ValueSource): AnyBinaryBuilder;\n gt(value: ValueSource): AnyBinaryBuilder;\n lt(value: ValueSource): AnyBinaryBuilder;\n gte(value: ValueSource): AnyBinaryBuilder;\n lte(value: ValueSource): AnyBinaryBuilder;\n asc(): AnyOrderBuilder;\n desc(): AnyOrderBuilder;\n toExpr(): ColumnRef;\n readonly __jsType: unknown;\n // Optional nullable methods (present when columnMeta.nullable is true)\n isNull?(): AnyUnaryBuilder;\n isNotNull?(): AnyUnaryBuilder;\n // Allow any operation methods (from conditional type)\n readonly [key: string]: unknown;\n};\n\nexport type AnyColumnBuilder =\n | ColumnBuilder<\n string,\n StorageColumn,\n unknown,\n // biome-ignore lint/suspicious/noExplicitAny: AnyColumnBuilder must accept column builders with any operation types\n any\n >\n | AnyColumnBuilderBase;\n\n/**\n * Union type for any builder that can produce an Expression.\n * Used in DSL method signatures where either a column or operation result can be passed.\n */\nexport type AnyExpressionSource = AnyColumnBuilder | ExpressionBuilder;\n\nexport function isColumnBuilder(value: unknown): value is AnyColumnBuilder {\n return (\n typeof value === 'object' &&\n value !== null &&\n 'kind' in value &&\n (value as { kind: unknown }).kind === 'column'\n );\n}\n\nexport interface JoinOnBuilder {\n eqCol(left: AnyColumnBuilder, right: AnyColumnBuilder): JoinOnPredicate;\n}\n\nexport interface JoinOnPredicate {\n readonly kind: 'join-on';\n readonly left: AnyColumnBuilder;\n readonly right: AnyColumnBuilder;\n}\n\nexport type Expr = ColumnRef | ParamRef;\n\n/**\n * Helper type to extract codec output type from CodecTypes.\n * Returns never if the codecId is not found in CodecTypes.\n */\ntype ExtractCodecOutputType<\n CodecId extends string,\n CodecTypes extends Record<string, { readonly output: unknown }>,\n> = CodecId extends keyof CodecTypes\n ? CodecTypes[CodecId] extends { readonly output: infer Output }\n ? Output\n : never\n : never;\n\n/**\n * Extracts the model name for a given table from the contract mappings.\n */\ntype ExtractTableToModel<\n Contract extends SqlContract<SqlStorage>,\n TableName extends string,\n> = Contract['mappings'] extends {\n readonly tableToModel: infer TableToModel;\n}\n ? TableToModel extends Record<string, string>\n ? TableName extends keyof TableToModel\n ? TableToModel[TableName]\n : never\n : never\n : never;\n\n/**\n * Extracts the field name for a given table column from the contract mappings.\n */\ntype ExtractColumnToField<\n Contract extends SqlContract<SqlStorage>,\n TableName extends string,\n ColumnName extends string,\n> = Contract['mappings'] extends {\n readonly columnToField: infer ColumnToField;\n}\n ? ColumnToField extends Record<string, Record<string, string>>\n ? TableName extends keyof ColumnToField\n ? ColumnName extends keyof ColumnToField[TableName]\n ? ColumnToField[TableName][ColumnName]\n : never\n : never\n : never\n : never;\n\n/**\n * Extracts the field value type from a model's fields.\n */\ntype ExtractFieldValue<\n Contract extends SqlContract<SqlStorage>,\n ModelName extends string,\n FieldName extends string,\n> = Contract['models'] extends infer Models\n ? Models extends Record<string, unknown>\n ? ModelName extends keyof Models\n ? Models[ModelName] extends { readonly fields: infer Fields }\n ? Fields extends Record<string, unknown>\n ? FieldName extends keyof Fields\n ? Fields[FieldName]\n : never\n : never\n : never\n : never\n : never\n : never;\n\n/**\n * Extracts the JavaScript type for a column from model mappings if available.\n * Returns `never` if the column maps to a ModelField object (which indicates\n * a relation that should fall through to codec-based type resolution).\n *\n * The check for ModelField uses `Exclude<keyof FieldValue, 'column'> extends never`\n * to ensure we only skip pure `{ column: string }` marker objects, not richer\n * object types that happen to include a `column` property.\n */\ntype ExtractColumnJsTypeFromModels<\n Contract extends SqlContract<SqlStorage>,\n TableName extends string,\n ColumnName extends string,\n> = ExtractTableToModel<Contract, TableName> extends infer ModelName\n ? ModelName extends string\n ? ExtractColumnToField<Contract, TableName, ColumnName> extends infer FieldName\n ? FieldName extends string\n ? ExtractFieldValue<Contract, ModelName, FieldName> extends infer FieldValue\n ? FieldValue extends { readonly column: string }\n ? Exclude<keyof FieldValue, 'column'> extends never\n ? never\n : FieldValue\n : FieldValue\n : never\n : never\n : never\n : never\n : never;\n\n/**\n * Resolves type params for a column from either:\n * - inline `columnMeta.typeParams`, or\n * - `columnMeta.typeRef` (resolving into `contract.storage.types[typeRef].typeParams`).\n */\ntype ResolveColumnTypeParams<\n Contract extends SqlContract<SqlStorage>,\n ColumnMeta extends StorageColumn,\n> = ColumnMeta extends { typeParams: infer Params }\n ? Params extends object\n ? Params\n : undefined\n : ColumnMeta extends { typeRef: infer TypeRef extends string }\n ? Contract['storage'] extends { types: infer Types }\n ? Types extends Record<string, unknown>\n ? TypeRef extends keyof Types\n ? Types[TypeRef] extends { typeParams: infer Params }\n ? Params extends object\n ? Params\n : undefined\n : undefined\n : undefined\n : undefined\n : undefined\n : undefined;\n\n/**\n * If a codec entry exposes a type-level parameterized output surface, compute the output type\n * for a specific params object. Falls back to `never` if not supported.\n *\n * This enables lane typing to incorporate `columnMeta.typeParams` without branching on codec IDs\n * in core lane code.\n */\ntype ExtractParameterizedCodecOutputType<\n CodecId extends string,\n Params,\n CodecTypes extends Record<string, { readonly output: unknown }>,\n> = CodecId extends keyof CodecTypes\n ? CodecTypes[CodecId] extends { readonly parameterizedOutput: infer Fn }\n ? Fn extends (params: Params) => infer Out\n ? Out\n : never\n : never\n : never;\n\n/**\n * Type-level operation signature.\n * Represents an operation at the type level, similar to OperationSignature at runtime.\n */\nexport type OperationTypeSignature = {\n readonly args: ReadonlyArray<ArgSpec>;\n readonly returns: ReturnSpec;\n readonly lowering: SqlLoweringSpec;\n readonly capabilities?: ReadonlyArray<string>;\n};\n\n/**\n * Type-level operation registry.\n * Maps typeId → operations, where operations is a record of method name → operation signature.\n *\n * Example:\n * ```typescript\n * type MyOperations: OperationTypes = {\n * 'pg/vector@1': {\n * cosineDistance: {\n * args: [{ kind: 'typeId'; type: 'pg/vector@1' }];\n * returns: { kind: 'builtin'; type: 'number' };\n * lowering: { targetFamily: 'sql'; strategy: 'function'; template: '...' };\n * };\n * };\n * };\n * ```\n */\nexport type OperationTypes = Record<string, Record<string, OperationTypeSignature>>;\n\n/**\n * CodecTypes represents a map of typeId to codec definitions.\n * Each codec definition must have an `output` property indicating the JavaScript type.\n *\n * Example:\n * ```typescript\n * type MyCodecTypes: CodecTypes = {\n * 'pg/int4@1': { output: number };\n * 'pg/text@1': { output: string };\n * };\n * ```\n */\nexport type CodecTypes = Record<string, { readonly output: unknown }>;\n\n/**\n * Extracts operations for a given typeId from the operation registry.\n * Returns an empty record if the typeId is not found.\n *\n * @example\n * ```typescript\n * type Ops = OperationsForTypeId<'pg/vector@1', MyOperations>;\n * // Ops = { cosineDistance: { ... }, l2Distance: { ... } }\n * ```\n */\nexport type OperationsForTypeId<\n TypeId extends string,\n Operations extends OperationTypes,\n> = Operations extends Record<string, never>\n ? Record<string, never>\n : TypeId extends keyof Operations\n ? Operations[TypeId]\n : Record<string, never>;\n\n/**\n * Maps operation signatures to method signatures on ColumnBuilder.\n * Each operation becomes a method that returns a ColumnBuilder or BinaryBuilder\n * based on the return type.\n */\ntype OperationMethods<\n Ops extends Record<string, OperationTypeSignature>,\n ColumnName extends string,\n ColumnMeta extends StorageColumn,\n JsType,\n> = {\n [K in keyof Ops]: Ops[K] extends OperationTypeSignature\n ? (\n ...args: OperationArgs<Ops[K]['args']>\n ) => OperationReturn<Ops[K]['returns'], ColumnName, ColumnMeta, JsType>\n : never;\n};\n\n/**\n * Maps operation argument specs to TypeScript argument types.\n * - typeId args: ColumnBuilder (accepts base columns or operation results)\n * - param args: ParamPlaceholder\n * - literal args: unknown (could be more specific in future)\n */\ntype OperationArgs<Args extends ReadonlyArray<ArgSpec>> = Args extends readonly [\n infer First,\n ...infer Rest,\n]\n ? First extends ArgSpec\n ? [ArgToType<First>, ...(Rest extends ReadonlyArray<ArgSpec> ? OperationArgs<Rest> : [])]\n : []\n : [];\n\ntype ArgToType<Arg extends ArgSpec> = Arg extends { kind: 'typeId' }\n ? AnyExpressionSource\n : Arg extends { kind: 'param' }\n ? ParamPlaceholder\n : Arg extends { kind: 'literal' }\n ? unknown\n : never;\n\n/**\n * Maps operation return spec to return type.\n * Operations return ExpressionBuilder, not ColumnBuilder, because the result\n * represents an expression (OperationExpr) rather than a column reference.\n */\ntype OperationReturn<\n Returns extends ReturnSpec,\n _ColumnName extends string,\n _ColumnMeta extends StorageColumn,\n _JsType,\n> = Returns extends { kind: 'builtin'; type: infer T }\n ? T extends 'number'\n ? ExpressionBuilder<number>\n : T extends 'boolean'\n ? ExpressionBuilder<boolean>\n : T extends 'string'\n ? ExpressionBuilder<string>\n : ExpressionBuilder<unknown>\n : Returns extends { kind: 'typeId' }\n ? ExpressionBuilder<unknown>\n : ExpressionBuilder<unknown>;\n\n/**\n * Computes JavaScript type for a column at column creation time.\n *\n * Type inference:\n * - Read columnMeta.codecId as typeId string literal\n * - Look up CodecTypes[typeId].output\n * - Apply nullability: nullable ? Output | null : Output\n */\ntype ColumnMetaTypeId<ColumnMeta> = ColumnMeta extends { codecId: infer CodecId extends string }\n ? CodecId\n : ColumnMeta extends { type: infer TypeId extends string }\n ? TypeId\n : never;\n\nexport type ComputeColumnJsType<\n Contract extends SqlContract<SqlStorage>,\n TableName extends string,\n ColumnName extends string,\n ColumnMeta extends StorageColumn,\n CodecTypes extends Record<string, { readonly output: unknown }>,\n> = ExtractColumnJsTypeFromModels<Contract, TableName, ColumnName> extends infer FromModels\n ? [FromModels] extends [never]\n ? ColumnMeta extends { nullable: infer Nullable }\n ? ColumnMetaTypeId<ColumnMeta> extends infer TypeId\n ? TypeId extends string\n ? ResolveColumnTypeParams<Contract, ColumnMeta> extends infer Params\n ? Params extends object\n ? ExtractParameterizedCodecOutputType<\n TypeId,\n Params,\n CodecTypes\n > extends infer ParamOutput\n ? [ParamOutput] extends [never]\n ? ExtractCodecOutputType<TypeId, CodecTypes> extends infer CodecOutput\n ? [CodecOutput] extends [never]\n ? unknown // Codec not found in CodecTypes\n : Nullable extends true\n ? CodecOutput | null\n : CodecOutput\n : unknown\n : Nullable extends true\n ? ParamOutput | null\n : ParamOutput\n : unknown\n : ExtractCodecOutputType<TypeId, CodecTypes> extends infer CodecOutput\n ? [CodecOutput] extends [never]\n ? unknown // Codec not found in CodecTypes\n : Nullable extends true\n ? CodecOutput | null\n : CodecOutput\n : unknown\n : unknown\n : unknown\n : unknown\n : unknown\n : FromModels\n : unknown;\n\n/**\n * Infers Row type from a projection object.\n * Maps Record<string, ColumnBuilder> to Record<string, JSType>\n *\n * Extracts the pre-computed JsType from each ColumnBuilder in the projection.\n */\n/**\n * Extracts the inferred JsType carried by a ColumnBuilder.\n */\ntype ExtractJsTypeFromColumnBuilder<CB extends AnyColumnBuilder> =\n CB extends ColumnBuilder<\n infer _ColumnName extends string,\n infer _ColumnMeta extends StorageColumn,\n infer JsType,\n infer _Ops\n >\n ? JsType\n : never;\n\nexport type InferProjectionRow<P extends Record<string, AnyColumnBuilder>> = {\n [K in keyof P]: ExtractJsTypeFromColumnBuilder<P[K]>;\n};\n\n/**\n * Nested projection type - allows recursive nesting of ColumnBuilder, ExpressionBuilder, or nested objects.\n */\nexport type NestedProjection = Record<\n string,\n | AnyExpressionSource\n | Record<\n string,\n | AnyExpressionSource\n | Record<\n string,\n | AnyExpressionSource\n | Record<string, AnyExpressionSource | Record<string, AnyExpressionSource>>\n >\n >\n>;\n\n/**\n * Helper type to extract include type from Includes map.\n * Returns the value type if K is a key of Includes, otherwise returns unknown.\n */\ntype ExtractIncludeType<\n K extends string,\n Includes extends Record<string, unknown>,\n> = K extends keyof Includes ? Includes[K] : unknown;\n\n/**\n * Infers Row type from a nested projection object.\n * Recursively maps Record<string, ColumnBuilder | boolean | NestedProjection> to nested object types.\n *\n * Extracts the pre-computed JsType from each ColumnBuilder at leaves.\n * When a value is `true`, it represents an include reference and infers `Array<ChildShape>`\n * by looking up the include alias in the Includes type map.\n */\nexport type InferNestedProjectionRow<\n P extends Record<string, AnyExpressionSource | boolean | NestedProjection>,\n CodecTypes extends Record<string, { readonly output: unknown }> = Record<string, never>,\n Includes extends Record<string, unknown> = Record<string, never>,\n> = {\n [K in keyof P]: P[K] extends ExpressionBuilder<infer JsType>\n ? JsType\n : P[K] extends AnyColumnBuilder\n ? ExtractJsTypeFromColumnBuilder<P[K]>\n : P[K] extends true\n ? Array<ExtractIncludeType<K & string, Includes>> // Include reference - infers Array<ChildShape> from Includes map\n : P[K] extends NestedProjection\n ? InferNestedProjectionRow<P[K], CodecTypes, Includes>\n : never;\n};\n\n/**\n * Infers Row type from a tuple of ColumnBuilders used in returning() clause.\n * Extracts column name and JsType from each ColumnBuilder and creates a Record.\n */\nexport type InferReturningRow<Columns extends readonly AnyColumnBuilder[]> =\n Columns extends readonly [infer First, ...infer Rest]\n ? First extends ColumnBuilder<\n infer Name,\n infer _Meta,\n infer JsType,\n infer _Ops extends OperationTypes\n >\n ? Name extends string\n ? Rest extends readonly AnyColumnBuilder[]\n ? { [K in Name]: JsType } & InferReturningRow<Rest>\n : { [K in Name]: JsType }\n : never\n : never\n : Record<string, never>;\n\n/**\n * Utility type to check if a contract has the required capabilities for includeMany.\n * Requires both `lateral` and `jsonAgg` to be `true` in the contract's capabilities for the target.\n * Capabilities are nested by target: `{ [target]: { lateral: true, jsonAgg: true } }`\n */\nexport type HasIncludeManyCapabilities<TContract extends SqlContract<SqlStorage>> =\n TContract extends { capabilities: infer C; target: infer T }\n ? T extends string\n ? C extends Record<string, Record<string, boolean>>\n ? C extends { [K in T]: infer TargetCaps }\n ? TargetCaps extends { lateral: true; jsonAgg: true }\n ? true\n : false\n : false\n : false\n : false\n : false;\n\n/**\n * SQL-specific Plan type that refines the ast field to use QueryAst.\n * This is the type used by SQL query builders.\n */\nexport type SqlPlan<Row = unknown> = ExecutionPlan<Row, QueryAst>;\n\n/**\n * Helper types for extracting contract structure.\n */\nexport type TablesOf<TContract> = TContract extends {\n storage: { tables: infer U };\n}\n ? U\n : never;\n\nexport type TableKey<TContract> = Extract<keyof TablesOf<TContract>, string>;\n\n// Common types for contract.d.ts generation (SQL-specific)\n// These types are used by emitted contract.d.ts files to provide type-safe DSL/ORM types\n\n/**\n * Unique symbol for metadata property to avoid collisions with user-defined properties\n */\nexport declare const META: unique symbol;\n\n/**\n * Extracts metadata from a type that has a META property\n */\nexport type Meta<T extends { [META]: unknown }> = T[typeof META];\n\n/**\n * Metadata interface for table definitions\n */\nexport interface TableMetadata<Name extends string> {\n name: Name;\n}\n\n/**\n * Metadata interface for model definitions\n */\nexport interface ModelMetadata<Name extends string> {\n name: Name;\n}\n\n/**\n * Base interface for table definitions with metadata\n * Used in contract.d.ts to define storage-level table types\n */\nexport interface TableDef<Name extends string> {\n readonly [META]: TableMetadata<Name>;\n}\n\n/**\n * Base interface for model definitions with metadata\n * Used in contract.d.ts to define application-level model types\n */\nexport interface ModelDef<Name extends string> {\n readonly [META]: ModelMetadata<Name>;\n}\n\nexport type ColumnsOf<\n TContract,\n K extends TableKey<TContract>,\n> = K extends keyof TablesOf<TContract>\n ? TablesOf<TContract>[K] extends { columns: infer C }\n ? C\n : never\n : never;\n\nexport interface RawTemplateOptions {\n readonly refs?: PlanRefs;\n readonly annotations?: Record<string, unknown>;\n readonly projection?: ReadonlyArray<string>;\n}\n\nexport interface RawFunctionOptions extends RawTemplateOptions {\n readonly params: ReadonlyArray<unknown>;\n}\n\nexport type RawTemplateFactory = (\n strings: TemplateStringsArray,\n ...values: readonly unknown[]\n) => ExecutionPlan;\n\nexport interface RawFactory extends RawTemplateFactory {\n (text: string, options: RawFunctionOptions): ExecutionPlan;\n with(options: RawTemplateOptions): RawTemplateFactory;\n}\n\nexport type { RuntimeError } from '@prisma-next/plan';\n\nexport interface BuildParamsMap {\n readonly [name: string]: unknown;\n}\n\nexport interface BuildOptions {\n readonly params?: BuildParamsMap;\n}\n\nexport interface SqlBuilderOptions<\n TContract extends SqlContract<SqlStorage> = SqlContract<SqlStorage>,\n> {\n readonly context: ExecutionContext<TContract>;\n}\n\n/**\n * SQL-specific ResultType that works with both Plan and SqlQueryPlan.\n * This extends the core ResultType to also handle SqlQueryPlan.\n * Example: `type Row = ResultType<typeof plan>`\n */\nexport type ResultType<P> = P extends SqlQueryPlan<infer R> ? R : CoreResultType<P>;\n"],"mappings":";;;;AA+CA,SAAgB,mBACd,MACA,KACiB;AACjB,QAAO;EAAE,MAAM;EAAS;EAAM;EAAK;;AAkLrC,SAAgB,gBAAgB,OAA2C;AACzE,QACE,OAAO,UAAU,YACjB,UAAU,QACV,UAAU,SACT,MAA4B,SAAS"}
|
package/package.json
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@prisma-next/sql-relational-core",
|
|
3
|
-
"version": "0.3.0-dev.
|
|
3
|
+
"version": "0.3.0-dev.63",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"sideEffects": false,
|
|
6
6
|
"description": "Schema and column builders, operation attachment, and AST types for Prisma Next",
|
|
7
7
|
"dependencies": {
|
|
8
|
+
"arktype": "^2.1.29",
|
|
8
9
|
"ts-toolbelt": "^9.6.0",
|
|
9
|
-
"@prisma-next/contract": "0.3.0-dev.
|
|
10
|
-
"@prisma-next/operations": "0.3.0-dev.
|
|
11
|
-
"@prisma-next/plan": "0.3.0-dev.
|
|
12
|
-
"@prisma-next/sql-contract": "0.3.0-dev.
|
|
13
|
-
"@prisma-next/sql-operations": "0.3.0-dev.
|
|
10
|
+
"@prisma-next/contract": "0.3.0-dev.63",
|
|
11
|
+
"@prisma-next/operations": "0.3.0-dev.63",
|
|
12
|
+
"@prisma-next/plan": "0.3.0-dev.63",
|
|
13
|
+
"@prisma-next/sql-contract": "0.3.0-dev.63",
|
|
14
|
+
"@prisma-next/sql-operations": "0.3.0-dev.63",
|
|
15
|
+
"@prisma-next/utils": "0.3.0-dev.63"
|
|
14
16
|
},
|
|
15
17
|
"devDependencies": {
|
|
16
|
-
"
|
|
17
|
-
"tsup": "8.5.1",
|
|
18
|
+
"tsdown": "0.18.4",
|
|
18
19
|
"typescript": "5.9.3",
|
|
19
|
-
"vitest": "4.0.
|
|
20
|
-
"@prisma-next/sql-contract-ts": "0.3.0-dev.
|
|
20
|
+
"vitest": "4.0.17",
|
|
21
|
+
"@prisma-next/sql-contract-ts": "0.3.0-dev.63",
|
|
22
|
+
"@prisma-next/tsconfig": "0.0.0",
|
|
23
|
+
"@prisma-next/tsdown": "0.0.0",
|
|
21
24
|
"@prisma-next/test-utils": "0.0.1"
|
|
22
25
|
},
|
|
23
26
|
"files": [
|
|
@@ -25,55 +28,35 @@
|
|
|
25
28
|
"src"
|
|
26
29
|
],
|
|
27
30
|
"exports": {
|
|
28
|
-
".":
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
"./
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
"./
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
"./errors": {
|
|
49
|
-
"types": "./dist/exports/errors.d.ts",
|
|
50
|
-
"import": "./dist/exports/errors.js"
|
|
51
|
-
},
|
|
52
|
-
"./ast": {
|
|
53
|
-
"types": "./dist/exports/ast.d.ts",
|
|
54
|
-
"import": "./dist/exports/ast.js"
|
|
55
|
-
},
|
|
56
|
-
"./plan": {
|
|
57
|
-
"types": "./dist/exports/plan.d.ts",
|
|
58
|
-
"import": "./dist/exports/plan.js"
|
|
59
|
-
},
|
|
60
|
-
"./query-lane-context": {
|
|
61
|
-
"types": "./dist/exports/query-lane-context.d.ts",
|
|
62
|
-
"import": "./dist/exports/query-lane-context.js"
|
|
63
|
-
},
|
|
64
|
-
"./utils/guards": {
|
|
65
|
-
"types": "./dist/exports/guards.d.ts",
|
|
66
|
-
"import": "./dist/exports/guards.js"
|
|
67
|
-
}
|
|
31
|
+
".": "./dist/index.mjs",
|
|
32
|
+
"./ast": "./dist/exports/ast.mjs",
|
|
33
|
+
"./errors": "./dist/exports/errors.mjs",
|
|
34
|
+
"./guards": "./dist/exports/guards.mjs",
|
|
35
|
+
"./operations-registry": "./dist/exports/operations-registry.mjs",
|
|
36
|
+
"./param": "./dist/exports/param.mjs",
|
|
37
|
+
"./plan": "./dist/exports/plan.mjs",
|
|
38
|
+
"./query-lane-context": "./dist/exports/query-lane-context.mjs",
|
|
39
|
+
"./schema": "./dist/exports/schema.mjs",
|
|
40
|
+
"./types": "./dist/exports/types.mjs",
|
|
41
|
+
"./utils/guards": "./dist/exports/utils/guards.mjs",
|
|
42
|
+
"./package.json": "./package.json"
|
|
43
|
+
},
|
|
44
|
+
"main": "./dist/index.mjs",
|
|
45
|
+
"module": "./dist/index.mjs",
|
|
46
|
+
"types": "./dist/index.d.mts",
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "https://github.com/prisma/prisma-next.git",
|
|
50
|
+
"directory": "packages/2-sql/4-lanes/relational-core"
|
|
68
51
|
},
|
|
69
52
|
"scripts": {
|
|
70
|
-
"build": "
|
|
53
|
+
"build": "tsdown",
|
|
71
54
|
"test": "vitest run",
|
|
72
55
|
"test:coverage": "vitest run --coverage",
|
|
73
56
|
"typecheck": "tsc --project tsconfig.json --noEmit",
|
|
74
|
-
"lint": "biome check . --
|
|
75
|
-
"lint:fix": "biome check --write .
|
|
76
|
-
"lint:fix:unsafe": "biome check --write --unsafe .
|
|
77
|
-
"clean": "
|
|
57
|
+
"lint": "biome check . --error-on-warnings",
|
|
58
|
+
"lint:fix": "biome check --write .",
|
|
59
|
+
"lint:fix:unsafe": "biome check --write --unsafe .",
|
|
60
|
+
"clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
|
|
78
61
|
}
|
|
79
62
|
}
|
package/src/ast/codec-types.ts
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
|
+
import { ifDefined } from '@prisma-next/utils/defined';
|
|
2
|
+
import type { Type } from 'arktype';
|
|
1
3
|
import type { O } from 'ts-toolbelt';
|
|
2
4
|
|
|
5
|
+
/**
|
|
6
|
+
* Descriptor for parameterized codecs that require type parameter validation.
|
|
7
|
+
* Shared between adapter (compile-time) and runtime layers to avoid duplication.
|
|
8
|
+
*
|
|
9
|
+
* @template TParams - The shape of the type parameters (e.g., `{ length: number }`)
|
|
10
|
+
* @template THelper - The type returned by the optional `init` hook
|
|
11
|
+
*/
|
|
12
|
+
export interface CodecParamsDescriptor<TParams = Record<string, unknown>, THelper = unknown> {
|
|
13
|
+
/** The codec ID this descriptor applies to (e.g., 'pg/vector@1') */
|
|
14
|
+
readonly codecId: string;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Arktype schema for validating typeParams.
|
|
18
|
+
* Used to validate both storage.types entries and inline column typeParams.
|
|
19
|
+
*/
|
|
20
|
+
readonly paramsSchema: Type<TParams>;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Optional init hook called during runtime context creation.
|
|
24
|
+
* Receives validated params and returns a helper object to be stored in context.types.
|
|
25
|
+
* If not provided, the validated params are stored directly.
|
|
26
|
+
*/
|
|
27
|
+
readonly init?: (params: TParams) => THelper;
|
|
28
|
+
}
|
|
29
|
+
|
|
3
30
|
/**
|
|
4
31
|
* Codec metadata for database-specific type information.
|
|
5
32
|
* Used for schema introspection and verification.
|
|
@@ -20,7 +47,13 @@ export interface CodecMeta {
|
|
|
20
47
|
* Codecs are pure, synchronous functions with no side effects or IO.
|
|
21
48
|
* They provide deterministic conversion between database wire types and JS values.
|
|
22
49
|
*/
|
|
23
|
-
export interface Codec<
|
|
50
|
+
export interface Codec<
|
|
51
|
+
Id extends string = string,
|
|
52
|
+
TWire = unknown,
|
|
53
|
+
TJs = unknown,
|
|
54
|
+
TParams = Record<string, unknown>,
|
|
55
|
+
THelper = unknown,
|
|
56
|
+
> {
|
|
24
57
|
/**
|
|
25
58
|
* Namespaced codec identifier in format 'namespace/name@version'
|
|
26
59
|
* Examples: 'pg/text@1', 'pg/uuid@1', 'pg/timestamptz@1'
|
|
@@ -39,6 +72,31 @@ export interface Codec<Id extends string = string, TWire = unknown, TJs = unknow
|
|
|
39
72
|
*/
|
|
40
73
|
readonly meta?: CodecMeta;
|
|
41
74
|
|
|
75
|
+
/**
|
|
76
|
+
* Optional params schema for parameterized codecs.
|
|
77
|
+
* If provided, typeParams are validated against this schema.
|
|
78
|
+
*/
|
|
79
|
+
readonly paramsSchema?: Type<TParams>;
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Optional init hook for building runtime helper state from validated params.
|
|
83
|
+
*
|
|
84
|
+
* Useful when parameterized types need derived data at runtime, for example:
|
|
85
|
+
* - normalize typeParams into a stable helper shape consumed by lanes/adapters
|
|
86
|
+
* - precompute reusable values once during context creation
|
|
87
|
+
* - avoid repeating typeParams parsing logic during query execution
|
|
88
|
+
*
|
|
89
|
+
* Example:
|
|
90
|
+
* { length: 255 } -> { kind: 'variable', maxLength: 255 }
|
|
91
|
+
*
|
|
92
|
+
* **Convention for JSON/JSONB codecs**: When the helper includes a `validate`
|
|
93
|
+
* property of type `JsonSchemaValidateFn`, the runtime will use it to enforce
|
|
94
|
+
* JSON Schema conformance during encoding and decoding. The property is
|
|
95
|
+
* discovered via duck typing (`helper?.validate`) for flexibility across
|
|
96
|
+
* different codec types.
|
|
97
|
+
*/
|
|
98
|
+
readonly init?: (params: TParams) => THelper;
|
|
99
|
+
|
|
42
100
|
/**
|
|
43
101
|
* Decode a wire value (from database) to JavaScript type.
|
|
44
102
|
* Must be synchronous and pure (no side effects).
|
|
@@ -157,17 +215,27 @@ class CodecRegistryImpl implements CodecRegistry {
|
|
|
157
215
|
/**
|
|
158
216
|
* Codec factory - creates a codec with typeId and encode/decode functions.
|
|
159
217
|
*/
|
|
160
|
-
export function codec<
|
|
218
|
+
export function codec<
|
|
219
|
+
Id extends string,
|
|
220
|
+
TWire,
|
|
221
|
+
TJs,
|
|
222
|
+
TParams = Record<string, unknown>,
|
|
223
|
+
THelper = unknown,
|
|
224
|
+
>(config: {
|
|
161
225
|
typeId: Id;
|
|
162
226
|
targetTypes: readonly string[];
|
|
163
227
|
encode: (value: TJs) => TWire;
|
|
164
228
|
decode: (wire: TWire) => TJs;
|
|
165
229
|
meta?: CodecMeta;
|
|
166
|
-
|
|
230
|
+
paramsSchema?: Type<TParams>;
|
|
231
|
+
init?: (params: TParams) => THelper;
|
|
232
|
+
}): Codec<Id, TWire, TJs, TParams, THelper> {
|
|
167
233
|
return {
|
|
168
234
|
id: config.typeId,
|
|
169
235
|
targetTypes: config.targetTypes,
|
|
170
|
-
...(
|
|
236
|
+
...ifDefined('meta', config.meta),
|
|
237
|
+
...ifDefined('paramsSchema', config.paramsSchema),
|
|
238
|
+
...ifDefined('init', config.init),
|
|
171
239
|
encode: config.encode,
|
|
172
240
|
decode: config.decode,
|
|
173
241
|
};
|
|
@@ -176,11 +244,12 @@ export function codec<Id extends string, TWire, TJs>(config: {
|
|
|
176
244
|
/**
|
|
177
245
|
* Type helpers to extract codec types.
|
|
178
246
|
*/
|
|
179
|
-
export type CodecId<T> =
|
|
180
|
-
|
|
181
|
-
: T extends { readonly id: infer Id }
|
|
247
|
+
export type CodecId<T> =
|
|
248
|
+
T extends Codec<infer Id, unknown, unknown>
|
|
182
249
|
? Id
|
|
183
|
-
:
|
|
250
|
+
: T extends { readonly id: infer Id }
|
|
251
|
+
? Id
|
|
252
|
+
: never;
|
|
184
253
|
export type CodecInput<T> = T extends Codec<string, unknown, infer JsT> ? JsT : never;
|
|
185
254
|
export type CodecOutput<T> = T extends Codec<string, unknown, infer JsT> ? JsT : never;
|
|
186
255
|
|
package/src/ast/delete.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { ColumnRef, DeleteAst, TableRef, WhereExpr } from './types';
|
|
2
2
|
import { compact } from './util';
|
|
3
3
|
|
|
4
4
|
export interface CreateDeleteAstOptions {
|
|
5
5
|
readonly table: TableRef;
|
|
6
|
-
readonly where:
|
|
6
|
+
readonly where: WhereExpr;
|
|
7
7
|
readonly returning?: ReadonlyArray<ColumnRef>;
|
|
8
8
|
}
|
|
9
9
|
|
package/src/ast/driver-types.ts
CHANGED
|
@@ -13,13 +13,30 @@ export interface SqlExplainResult<Row = Record<string, unknown>> {
|
|
|
13
13
|
readonly rows: ReadonlyArray<Row>;
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
-
export
|
|
17
|
-
|
|
16
|
+
export type SqlDriverState = 'unbound' | 'connected' | 'closed';
|
|
17
|
+
|
|
18
|
+
export interface SqlDriver<TBinding = void> extends SqlQueryable {
|
|
19
|
+
readonly state?: SqlDriverState;
|
|
20
|
+
connect(binding: TBinding): Promise<void>;
|
|
21
|
+
acquireConnection(): Promise<SqlConnection>;
|
|
22
|
+
close(): Promise<void>;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface SqlConnection extends SqlQueryable {
|
|
26
|
+
beginTransaction(): Promise<SqlTransaction>;
|
|
27
|
+
release(): Promise<void>;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export interface SqlTransaction extends SqlQueryable {
|
|
31
|
+
commit(): Promise<void>;
|
|
32
|
+
rollback(): Promise<void>;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface SqlQueryable {
|
|
18
36
|
execute<Row = Record<string, unknown>>(request: SqlExecuteRequest): AsyncIterable<Row>;
|
|
19
37
|
explain?(request: SqlExecuteRequest): Promise<SqlExplainResult>;
|
|
20
38
|
query<Row = Record<string, unknown>>(
|
|
21
39
|
sql: string,
|
|
22
40
|
params?: readonly unknown[],
|
|
23
41
|
): Promise<SqlQueryResult<Row>>;
|
|
24
|
-
close(): Promise<void>;
|
|
25
42
|
}
|
package/src/ast/predicate.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
1
|
import type {
|
|
2
2
|
BinaryExpr,
|
|
3
3
|
BinaryOp,
|
|
4
|
-
ColumnRef,
|
|
5
4
|
ExistsExpr,
|
|
6
|
-
|
|
5
|
+
Expression,
|
|
6
|
+
ListLiteralExpr,
|
|
7
|
+
LiteralExpr,
|
|
8
|
+
NullCheckExpr,
|
|
7
9
|
ParamRef,
|
|
8
10
|
SelectAst,
|
|
9
11
|
} from './types';
|
|
10
12
|
|
|
11
13
|
export function createBinaryExpr(
|
|
12
14
|
op: BinaryOp,
|
|
13
|
-
left:
|
|
14
|
-
right:
|
|
15
|
+
left: Expression,
|
|
16
|
+
right: Expression | ParamRef | LiteralExpr | ListLiteralExpr,
|
|
15
17
|
): BinaryExpr {
|
|
16
18
|
return {
|
|
17
19
|
kind: 'bin',
|
|
@@ -28,3 +30,11 @@ export function createExistsExpr(not: boolean, subquery: SelectAst): ExistsExpr
|
|
|
28
30
|
subquery,
|
|
29
31
|
};
|
|
30
32
|
}
|
|
33
|
+
|
|
34
|
+
export function createNullCheckExpr(expr: Expression, isNull: boolean): NullCheckExpr {
|
|
35
|
+
return {
|
|
36
|
+
kind: 'nullCheck',
|
|
37
|
+
expr,
|
|
38
|
+
isNull,
|
|
39
|
+
};
|
|
40
|
+
}
|
package/src/ast/select.ts
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
import type {
|
|
2
|
-
BinaryExpr,
|
|
3
2
|
ColumnRef,
|
|
4
3
|
Direction,
|
|
5
|
-
ExistsExpr,
|
|
6
4
|
IncludeAst,
|
|
7
5
|
IncludeRef,
|
|
8
6
|
JoinAst,
|
|
9
7
|
OperationExpr,
|
|
10
8
|
SelectAst,
|
|
11
9
|
TableRef,
|
|
10
|
+
WhereExpr,
|
|
12
11
|
} from './types';
|
|
13
12
|
import { compact } from './util';
|
|
14
13
|
|
|
@@ -20,9 +19,10 @@ export interface CreateSelectAstOptions {
|
|
|
20
19
|
alias: string;
|
|
21
20
|
expr: ColumnRef | IncludeRef | OperationExpr;
|
|
22
21
|
}>;
|
|
23
|
-
readonly where?:
|
|
22
|
+
readonly where?: WhereExpr;
|
|
24
23
|
readonly orderBy?: ReadonlyArray<{ expr: ColumnRef | OperationExpr; dir: Direction }>;
|
|
25
24
|
readonly limit?: number;
|
|
25
|
+
readonly selectAllIntent?: { table?: string };
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
export function createSelectAst(options: CreateSelectAstOptions): SelectAst {
|
|
@@ -35,5 +35,6 @@ export function createSelectAst(options: CreateSelectAstOptions): SelectAst {
|
|
|
35
35
|
where: options.where,
|
|
36
36
|
orderBy: options.orderBy,
|
|
37
37
|
limit: options.limit,
|
|
38
|
+
selectAllIntent: options.selectAllIntent,
|
|
38
39
|
}) as SelectAst;
|
|
39
40
|
}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { type as arktype } from 'arktype';
|
|
2
|
+
import { codec, defineCodecs } from './codec-types';
|
|
3
|
+
|
|
4
|
+
export const SQL_CHAR_CODEC_ID = 'sql/char@1' as const;
|
|
5
|
+
export const SQL_VARCHAR_CODEC_ID = 'sql/varchar@1' as const;
|
|
6
|
+
export const SQL_INT_CODEC_ID = 'sql/int@1' as const;
|
|
7
|
+
export const SQL_FLOAT_CODEC_ID = 'sql/float@1' as const;
|
|
8
|
+
|
|
9
|
+
const lengthParamsSchema = arktype({
|
|
10
|
+
length: 'number.integer > 0',
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
type LengthTypeHelper = {
|
|
14
|
+
readonly kind: 'fixed' | 'variable';
|
|
15
|
+
readonly maxLength: number;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function createLengthTypeHelper(
|
|
19
|
+
kind: LengthTypeHelper['kind'],
|
|
20
|
+
): (params: Record<string, unknown>) => LengthTypeHelper {
|
|
21
|
+
return (params) => ({
|
|
22
|
+
kind,
|
|
23
|
+
maxLength: params['length'] as number,
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const sqlCharCodec = codec<typeof SQL_CHAR_CODEC_ID, string, string>({
|
|
28
|
+
typeId: SQL_CHAR_CODEC_ID,
|
|
29
|
+
targetTypes: ['char'],
|
|
30
|
+
encode: (value: string): string => value,
|
|
31
|
+
decode: (wire: string): string => wire.trimEnd(),
|
|
32
|
+
paramsSchema: lengthParamsSchema,
|
|
33
|
+
init: createLengthTypeHelper('fixed'),
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const sqlVarcharCodec = codec<typeof SQL_VARCHAR_CODEC_ID, string, string>({
|
|
37
|
+
typeId: SQL_VARCHAR_CODEC_ID,
|
|
38
|
+
targetTypes: ['varchar'],
|
|
39
|
+
encode: (value: string): string => value,
|
|
40
|
+
decode: (wire: string): string => wire,
|
|
41
|
+
paramsSchema: lengthParamsSchema,
|
|
42
|
+
init: createLengthTypeHelper('variable'),
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const sqlIntCodec = codec<typeof SQL_INT_CODEC_ID, number, number>({
|
|
46
|
+
typeId: SQL_INT_CODEC_ID,
|
|
47
|
+
targetTypes: ['int'],
|
|
48
|
+
encode: (value) => value,
|
|
49
|
+
decode: (wire) => wire,
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
const sqlFloatCodec = codec<typeof SQL_FLOAT_CODEC_ID, number, number>({
|
|
53
|
+
typeId: SQL_FLOAT_CODEC_ID,
|
|
54
|
+
targetTypes: ['float'],
|
|
55
|
+
encode: (value) => value,
|
|
56
|
+
decode: (wire) => wire,
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
const codecs = defineCodecs()
|
|
60
|
+
.add('char', sqlCharCodec)
|
|
61
|
+
.add('varchar', sqlVarcharCodec)
|
|
62
|
+
.add('int', sqlIntCodec)
|
|
63
|
+
.add('float', sqlFloatCodec);
|
|
64
|
+
|
|
65
|
+
export const sqlCodecDefinitions = codecs.codecDefinitions;
|
|
66
|
+
export const sqlDataTypes = codecs.dataTypes;
|
|
67
|
+
export type SqlCodecTypes = typeof codecs.CodecTypes;
|
package/src/ast/types.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { ParamDescriptor } from '@prisma-next/contract/types';
|
|
1
2
|
import type { ReturnSpec } from '@prisma-next/operations';
|
|
2
3
|
import type { SqlLoweringSpec } from '@prisma-next/sql-operations';
|
|
3
4
|
|
|
@@ -32,23 +33,62 @@ export interface OperationExpr {
|
|
|
32
33
|
readonly kind: 'operation';
|
|
33
34
|
readonly method: string;
|
|
34
35
|
readonly forTypeId: string;
|
|
35
|
-
readonly self:
|
|
36
|
-
readonly args: ReadonlyArray<
|
|
36
|
+
readonly self: Expression;
|
|
37
|
+
readonly args: ReadonlyArray<Expression | ParamRef | LiteralExpr>;
|
|
37
38
|
readonly returns: ReturnSpec;
|
|
38
39
|
readonly lowering: SqlLoweringSpec;
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
|
|
42
|
+
/**
|
|
43
|
+
* Unified expression type - the canonical AST representation for column references
|
|
44
|
+
* and operation expressions. This is what all builders convert to via toExpr().
|
|
45
|
+
*/
|
|
46
|
+
export type Expression = ColumnRef | OperationExpr;
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Interface for any builder that can produce an Expression.
|
|
50
|
+
* Implemented by ColumnBuilder and ExpressionBuilder.
|
|
51
|
+
*/
|
|
52
|
+
export interface ExpressionSource {
|
|
53
|
+
toExpr(): Expression;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function isOperationExpr(expr: Expression): expr is OperationExpr {
|
|
42
57
|
return expr.kind === 'operation';
|
|
43
58
|
}
|
|
44
59
|
|
|
45
|
-
export type BinaryOp =
|
|
60
|
+
export type BinaryOp =
|
|
61
|
+
| 'eq'
|
|
62
|
+
| 'neq'
|
|
63
|
+
| 'gt'
|
|
64
|
+
| 'lt'
|
|
65
|
+
| 'gte'
|
|
66
|
+
| 'lte'
|
|
67
|
+
| 'like'
|
|
68
|
+
| 'ilike'
|
|
69
|
+
| 'in'
|
|
70
|
+
| 'notIn';
|
|
46
71
|
|
|
47
72
|
export interface BinaryExpr {
|
|
48
73
|
readonly kind: 'bin';
|
|
49
74
|
readonly op: BinaryOp;
|
|
50
|
-
readonly left:
|
|
51
|
-
readonly right:
|
|
75
|
+
readonly left: Expression;
|
|
76
|
+
readonly right: Expression | ParamRef | LiteralExpr | ListLiteralExpr;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export interface ListLiteralExpr {
|
|
80
|
+
readonly kind: 'listLiteral';
|
|
81
|
+
readonly values: ReadonlyArray<ParamRef | LiteralExpr>;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export interface AndExpr {
|
|
85
|
+
readonly kind: 'and';
|
|
86
|
+
readonly exprs: ReadonlyArray<WhereExpr>;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export interface OrExpr {
|
|
90
|
+
readonly kind: 'or';
|
|
91
|
+
readonly exprs: ReadonlyArray<WhereExpr>;
|
|
52
92
|
}
|
|
53
93
|
|
|
54
94
|
export interface ExistsExpr {
|
|
@@ -57,11 +97,40 @@ export interface ExistsExpr {
|
|
|
57
97
|
readonly subquery: SelectAst;
|
|
58
98
|
}
|
|
59
99
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
100
|
+
/**
|
|
101
|
+
* Unary expression for IS NULL / IS NOT NULL checks.
|
|
102
|
+
* Used in WHERE clauses to check for null values.
|
|
103
|
+
*/
|
|
104
|
+
export interface NullCheckExpr {
|
|
105
|
+
readonly kind: 'nullCheck';
|
|
106
|
+
readonly expr: Expression;
|
|
107
|
+
readonly isNull: boolean;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Union type for WHERE clause expressions.
|
|
112
|
+
*/
|
|
113
|
+
export type WhereExpr = BinaryExpr | ExistsExpr | NullCheckExpr | AndExpr | OrExpr;
|
|
114
|
+
|
|
115
|
+
export interface BoundWhereExpr {
|
|
116
|
+
readonly expr: WhereExpr;
|
|
117
|
+
readonly params: readonly unknown[];
|
|
118
|
+
readonly paramDescriptors: ReadonlyArray<ParamDescriptor>;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export interface ToWhereExpr {
|
|
122
|
+
toWhereExpr(): BoundWhereExpr;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
export type WhereArg = WhereExpr | ToWhereExpr;
|
|
126
|
+
|
|
127
|
+
export type JoinOnExpr =
|
|
128
|
+
| {
|
|
129
|
+
readonly kind: 'eqCol';
|
|
130
|
+
readonly left: ColumnRef;
|
|
131
|
+
readonly right: ColumnRef;
|
|
132
|
+
}
|
|
133
|
+
| WhereExpr;
|
|
65
134
|
|
|
66
135
|
export interface JoinAst {
|
|
67
136
|
readonly kind: 'join';
|
|
@@ -81,10 +150,10 @@ export interface IncludeAst {
|
|
|
81
150
|
readonly child: {
|
|
82
151
|
readonly table: TableRef;
|
|
83
152
|
readonly on: JoinOnExpr;
|
|
84
|
-
readonly where?:
|
|
85
|
-
readonly orderBy?: ReadonlyArray<{ expr:
|
|
153
|
+
readonly where?: WhereExpr;
|
|
154
|
+
readonly orderBy?: ReadonlyArray<{ expr: Expression; dir: Direction }>;
|
|
86
155
|
readonly limit?: number;
|
|
87
|
-
readonly project: ReadonlyArray<{ alias: string; expr:
|
|
156
|
+
readonly project: ReadonlyArray<{ alias: string; expr: Expression }>;
|
|
88
157
|
};
|
|
89
158
|
}
|
|
90
159
|
|
|
@@ -95,11 +164,12 @@ export interface SelectAst {
|
|
|
95
164
|
readonly includes?: ReadonlyArray<IncludeAst>;
|
|
96
165
|
readonly project: ReadonlyArray<{
|
|
97
166
|
alias: string;
|
|
98
|
-
expr:
|
|
167
|
+
expr: Expression | IncludeRef | LiteralExpr;
|
|
99
168
|
}>;
|
|
100
|
-
readonly where?:
|
|
101
|
-
readonly orderBy?: ReadonlyArray<{ expr:
|
|
169
|
+
readonly where?: WhereExpr;
|
|
170
|
+
readonly orderBy?: ReadonlyArray<{ expr: Expression; dir: Direction }>;
|
|
102
171
|
readonly limit?: number;
|
|
172
|
+
readonly selectAllIntent?: { table?: string };
|
|
103
173
|
}
|
|
104
174
|
|
|
105
175
|
export interface InsertAst {
|
|
@@ -113,14 +183,14 @@ export interface UpdateAst {
|
|
|
113
183
|
readonly kind: 'update';
|
|
114
184
|
readonly table: TableRef;
|
|
115
185
|
readonly set: Record<string, ColumnRef | ParamRef>;
|
|
116
|
-
readonly where
|
|
186
|
+
readonly where?: WhereExpr;
|
|
117
187
|
readonly returning?: ReadonlyArray<ColumnRef>;
|
|
118
188
|
}
|
|
119
189
|
|
|
120
190
|
export interface DeleteAst {
|
|
121
191
|
readonly kind: 'delete';
|
|
122
192
|
readonly table: TableRef;
|
|
123
|
-
readonly where
|
|
193
|
+
readonly where?: WhereExpr;
|
|
124
194
|
readonly returning?: ReadonlyArray<ColumnRef>;
|
|
125
195
|
}
|
|
126
196
|
|