@prisma-next/sql-contract 0.3.0-dev.8 → 0.3.0-dev.80

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +84 -10
  3. package/dist/factories.d.mts +48 -0
  4. package/dist/factories.d.mts.map +1 -0
  5. package/dist/factories.mjs +84 -0
  6. package/dist/factories.mjs.map +1 -0
  7. package/dist/pack-types.d.mts +13 -0
  8. package/dist/pack-types.d.mts.map +1 -0
  9. package/dist/pack-types.mjs +1 -0
  10. package/dist/types-CcCSXOlR.d.mts +166 -0
  11. package/dist/types-CcCSXOlR.d.mts.map +1 -0
  12. package/dist/types-DRR5stkj.mjs +13 -0
  13. package/dist/types-DRR5stkj.mjs.map +1 -0
  14. package/dist/types.d.mts +2 -0
  15. package/dist/types.mjs +3 -0
  16. package/dist/validate.d.mts +11 -0
  17. package/dist/validate.d.mts.map +1 -0
  18. package/dist/validate.mjs +244 -0
  19. package/dist/validate.mjs.map +1 -0
  20. package/dist/validators-CQXvLZa7.mjs +216 -0
  21. package/dist/validators-CQXvLZa7.mjs.map +1 -0
  22. package/dist/validators.d.mts +71 -0
  23. package/dist/validators.d.mts.map +1 -0
  24. package/dist/validators.mjs +3 -0
  25. package/package.json +24 -28
  26. package/src/construct.ts +178 -0
  27. package/src/exports/types.ts +13 -0
  28. package/src/exports/validate.ts +6 -0
  29. package/src/exports/validators.ts +1 -1
  30. package/src/factories.ts +41 -8
  31. package/src/index.ts +1 -0
  32. package/src/types.ts +137 -8
  33. package/src/validate.ts +272 -0
  34. package/src/validators.ts +164 -12
  35. package/dist/exports/factories.d.ts +0 -2
  36. package/dist/exports/factories.d.ts.map +0 -1
  37. package/dist/exports/factories.js +0 -83
  38. package/dist/exports/factories.js.map +0 -1
  39. package/dist/exports/pack-types.d.ts +0 -2
  40. package/dist/exports/pack-types.d.ts.map +0 -1
  41. package/dist/exports/pack-types.js +0 -1
  42. package/dist/exports/pack-types.js.map +0 -1
  43. package/dist/exports/types.d.ts +0 -2
  44. package/dist/exports/types.d.ts.map +0 -1
  45. package/dist/exports/types.js +0 -1
  46. package/dist/exports/types.js.map +0 -1
  47. package/dist/exports/validators.d.ts +0 -2
  48. package/dist/exports/validators.d.ts.map +0 -1
  49. package/dist/exports/validators.js +0 -96
  50. package/dist/exports/validators.js.map +0 -1
  51. package/dist/factories.d.ts +0 -38
  52. package/dist/factories.d.ts.map +0 -1
  53. package/dist/index.d.ts +0 -4
  54. package/dist/index.d.ts.map +0 -1
  55. package/dist/pack-types.d.ts +0 -10
  56. package/dist/pack-types.d.ts.map +0 -1
  57. package/dist/types.d.ts +0 -68
  58. package/dist/types.d.ts.map +0 -1
  59. package/dist/validators.d.ts +0 -35
  60. package/dist/validators.d.ts.map +0 -1
@@ -0,0 +1,216 @@
1
+ import { type } from "arktype";
2
+
3
+ //#region src/validators.ts
4
+ const literalKindSchema = type("'literal'");
5
+ const functionKindSchema = type("'function'");
6
+ const generatorKindSchema = type("'generator'");
7
+ const generatorIdSchema = type("'ulid' | 'nanoid' | 'uuidv7' | 'uuidv4' | 'cuid2' | 'ksuid'");
8
+ const ColumnDefaultLiteralSchema = type.declare().type({
9
+ kind: literalKindSchema,
10
+ value: "string | number | boolean | null | unknown[] | Record<string, unknown>"
11
+ });
12
+ const ColumnDefaultFunctionSchema = type.declare().type({
13
+ kind: functionKindSchema,
14
+ expression: "string"
15
+ });
16
+ const ColumnDefaultSchema = ColumnDefaultLiteralSchema.or(ColumnDefaultFunctionSchema);
17
+ const ExecutionMutationDefaultValueSchema = type({
18
+ "+": "reject",
19
+ kind: generatorKindSchema,
20
+ id: generatorIdSchema,
21
+ "params?": "Record<string, unknown>"
22
+ });
23
+ const ExecutionMutationDefaultSchema = type({
24
+ "+": "reject",
25
+ ref: {
26
+ "+": "reject",
27
+ table: "string",
28
+ column: "string"
29
+ },
30
+ "onCreate?": ExecutionMutationDefaultValueSchema,
31
+ "onUpdate?": ExecutionMutationDefaultValueSchema
32
+ });
33
+ const ExecutionSchema = type({
34
+ "+": "reject",
35
+ mutations: {
36
+ "+": "reject",
37
+ defaults: ExecutionMutationDefaultSchema.array().readonly()
38
+ }
39
+ });
40
+ const StorageColumnSchema = type({
41
+ "+": "reject",
42
+ nativeType: "string",
43
+ codecId: "string",
44
+ nullable: "boolean",
45
+ "typeParams?": "Record<string, unknown>",
46
+ "typeRef?": "string",
47
+ "default?": ColumnDefaultSchema
48
+ }).narrow((col, ctx) => {
49
+ if (col.typeParams !== void 0 && col.typeRef !== void 0) return ctx.mustBe("a column with either typeParams or typeRef, not both");
50
+ return true;
51
+ });
52
+ const StorageTypeInstanceSchema = type.declare().type({
53
+ codecId: "string",
54
+ nativeType: "string",
55
+ typeParams: "Record<string, unknown>"
56
+ });
57
+ const PrimaryKeySchema = type.declare().type({
58
+ columns: type.string.array().readonly(),
59
+ "name?": "string"
60
+ });
61
+ const UniqueConstraintSchema = type.declare().type({
62
+ columns: type.string.array().readonly(),
63
+ "name?": "string"
64
+ });
65
+ const IndexSchema = type({
66
+ columns: type.string.array().readonly(),
67
+ "name?": "string",
68
+ "using?": "string",
69
+ "config?": "Record<string, unknown>"
70
+ });
71
+ const ForeignKeyReferencesSchema = type.declare().type({
72
+ table: "string",
73
+ columns: type.string.array().readonly()
74
+ });
75
+ const ReferentialActionSchema = type.declare().type("'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault'");
76
+ const ForeignKeySchema = type.declare().type({
77
+ columns: type.string.array().readonly(),
78
+ references: ForeignKeyReferencesSchema,
79
+ "name?": "string",
80
+ "onDelete?": ReferentialActionSchema,
81
+ "onUpdate?": ReferentialActionSchema,
82
+ constraint: "boolean",
83
+ index: "boolean"
84
+ });
85
+ const StorageTableSchema = type({
86
+ "+": "reject",
87
+ columns: type({ "[string]": StorageColumnSchema }),
88
+ "primaryKey?": PrimaryKeySchema,
89
+ uniques: UniqueConstraintSchema.array().readonly(),
90
+ indexes: IndexSchema.array().readonly(),
91
+ foreignKeys: ForeignKeySchema.array().readonly()
92
+ });
93
+ const StorageSchema = type({
94
+ "+": "reject",
95
+ tables: type({ "[string]": StorageTableSchema }),
96
+ "types?": type({ "[string]": StorageTypeInstanceSchema })
97
+ });
98
+ const ModelFieldSchema = type.declare().type({ column: "string" });
99
+ const ModelStorageSchema = type.declare().type({ table: "string" });
100
+ const ModelSchema = type.declare().type({
101
+ storage: ModelStorageSchema,
102
+ fields: type({ "[string]": ModelFieldSchema }),
103
+ relations: type({ "[string]": "unknown" })
104
+ });
105
+ const MappingsSchema = type({
106
+ "+": "reject",
107
+ "modelToTable?": "null | Record<string, string>",
108
+ "tableToModel?": "null | Record<string, string>",
109
+ "fieldToColumn?": "null | Record<string, Record<string, string>>",
110
+ "columnToField?": "null | Record<string, Record<string, string>>",
111
+ "codecTypes?": "null | Record<string, unknown>",
112
+ "operationTypes?": "null | Record<string, Record<string, unknown>>"
113
+ });
114
+ const ContractMetaSchema = type({ "[string]": "unknown" });
115
+ const SqlContractSchema = type({
116
+ "+": "reject",
117
+ "schemaVersion?": "'1'",
118
+ target: "string",
119
+ targetFamily: "'sql'",
120
+ "coreHash?": "string",
121
+ storageHash: "string",
122
+ "executionHash?": "string",
123
+ "profileHash?": "string",
124
+ "_generated?": "Record<string, unknown>",
125
+ "capabilities?": "Record<string, Record<string, boolean>>",
126
+ "extensionPacks?": "Record<string, unknown>",
127
+ "meta?": ContractMetaSchema,
128
+ "sources?": "Record<string, unknown>",
129
+ "relations?": type({ "[string]": "unknown" }),
130
+ "mappings?": MappingsSchema,
131
+ models: type({ "[string]": ModelSchema }),
132
+ storage: StorageSchema,
133
+ "execution?": ExecutionSchema
134
+ });
135
+ /**
136
+ * Validates the structural shape of SqlStorage using Arktype.
137
+ *
138
+ * @param value - The storage value to validate
139
+ * @returns The validated storage if structure is valid
140
+ * @throws Error if the storage structure is invalid
141
+ */
142
+ function validateStorage(value) {
143
+ const result = StorageSchema(value);
144
+ if (result instanceof type.errors) {
145
+ const messages = result.map((p) => p.message).join("; ");
146
+ throw new Error(`Storage validation failed: ${messages}`);
147
+ }
148
+ return result;
149
+ }
150
+ /**
151
+ * Validates the structural shape of ModelDefinition using Arktype.
152
+ *
153
+ * @param value - The model value to validate
154
+ * @returns The validated model if structure is valid
155
+ * @throws Error if the model structure is invalid
156
+ */
157
+ function validateModel(value) {
158
+ const result = ModelSchema(value);
159
+ if (result instanceof type.errors) {
160
+ const messages = result.map((p) => p.message).join("; ");
161
+ throw new Error(`Model validation failed: ${messages}`);
162
+ }
163
+ return result;
164
+ }
165
+ /**
166
+ * Validates the structural shape of a SqlContract using Arktype.
167
+ *
168
+ * **Responsibility: Validation Only**
169
+ * This function validates that the contract has the correct structure and types.
170
+ * It does NOT normalize the contract - normalization must happen in the contract builder.
171
+ *
172
+ * The contract passed to this function must already be normalized (all required fields present).
173
+ * If normalization is needed, it should be done by the contract builder before calling this function.
174
+ *
175
+ * This ensures all required fields are present and have the correct types.
176
+ *
177
+ * @param value - The contract value to validate (typically from a JSON import)
178
+ * @returns The validated contract if structure is valid
179
+ * @throws Error if the contract structure is invalid
180
+ */
181
+ function validateSqlContract(value) {
182
+ if (typeof value !== "object" || value === null) throw new Error("Contract structural validation failed: value must be an object");
183
+ const rawValue = value;
184
+ if (rawValue.targetFamily !== void 0 && rawValue.targetFamily !== "sql") throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);
185
+ const contractResult = SqlContractSchema(value);
186
+ if (contractResult instanceof type.errors) {
187
+ const messages = contractResult.map((p) => p.message).join("; ");
188
+ throw new Error(`Contract structural validation failed: ${messages}`);
189
+ }
190
+ return contractResult;
191
+ }
192
+ /**
193
+ * Validates semantic constraints on SqlStorage that cannot be expressed in Arktype schemas.
194
+ *
195
+ * Returns an array of human-readable error strings. Empty array = valid.
196
+ *
197
+ * Currently checks:
198
+ * - `setNull` referential action on a non-nullable FK column (would fail at runtime)
199
+ * - `setDefault` referential action on a non-nullable FK column without a DEFAULT (would fail at runtime)
200
+ */
201
+ function validateStorageSemantics(storage) {
202
+ const errors = [];
203
+ for (const [tableName, table] of Object.entries(storage.tables)) for (const fk of table.foreignKeys) for (const colName of fk.columns) {
204
+ const column = table.columns[colName];
205
+ if (!column) continue;
206
+ if (fk.onDelete === "setNull" && !column.nullable) errors.push(`Table "${tableName}": onDelete setNull on foreign key column "${colName}" which is NOT NULL`);
207
+ if (fk.onUpdate === "setNull" && !column.nullable) errors.push(`Table "${tableName}": onUpdate setNull on foreign key column "${colName}" which is NOT NULL`);
208
+ if (fk.onDelete === "setDefault" && !column.nullable && column.default === void 0) errors.push(`Table "${tableName}": onDelete setDefault on foreign key column "${colName}" which is NOT NULL and has no DEFAULT`);
209
+ if (fk.onUpdate === "setDefault" && !column.nullable && column.default === void 0) errors.push(`Table "${tableName}": onUpdate setDefault on foreign key column "${colName}" which is NOT NULL and has no DEFAULT`);
210
+ }
211
+ return errors;
212
+ }
213
+
214
+ //#endregion
215
+ export { ForeignKeySchema as a, validateModel as c, validateStorageSemantics as d, ForeignKeyReferencesSchema as i, validateSqlContract as l, ColumnDefaultLiteralSchema as n, IndexSchema as o, ColumnDefaultSchema as r, ReferentialActionSchema as s, ColumnDefaultFunctionSchema as t, validateStorage as u };
216
+ //# sourceMappingURL=validators-CQXvLZa7.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators-CQXvLZa7.mjs","names":["errors: string[]"],"sources":["../src/validators.ts"],"sourcesContent":["import { type } from 'arktype';\nimport type {\n ForeignKey,\n ForeignKeyReferences,\n ModelDefinition,\n ModelField,\n ModelStorage,\n PrimaryKey,\n ReferentialAction,\n SqlContract,\n SqlStorage,\n StorageTypeInstance,\n UniqueConstraint,\n} from './types';\n\ntype ColumnDefaultLiteral = {\n readonly kind: 'literal';\n readonly value: string | number | boolean | Record<string, unknown> | unknown[] | null;\n};\ntype ColumnDefaultFunction = { readonly kind: 'function'; readonly expression: string };\nconst literalKindSchema = type(\"'literal'\");\nconst functionKindSchema = type(\"'function'\");\nconst generatorKindSchema = type(\"'generator'\");\nconst generatorIdSchema = type(\"'ulid' | 'nanoid' | 'uuidv7' | 'uuidv4' | 'cuid2' | 'ksuid'\");\n\nexport const ColumnDefaultLiteralSchema = type.declare<ColumnDefaultLiteral>().type({\n kind: literalKindSchema,\n value: 'string | number | boolean | null | unknown[] | Record<string, unknown>',\n});\n\nexport const ColumnDefaultFunctionSchema = type.declare<ColumnDefaultFunction>().type({\n kind: functionKindSchema,\n expression: 'string',\n});\n\nexport const ColumnDefaultSchema = ColumnDefaultLiteralSchema.or(ColumnDefaultFunctionSchema);\n\nconst ExecutionMutationDefaultValueSchema = type({\n '+': 'reject',\n kind: generatorKindSchema,\n id: generatorIdSchema,\n 'params?': 'Record<string, unknown>',\n});\n\nconst ExecutionMutationDefaultSchema = type({\n '+': 'reject',\n ref: {\n '+': 'reject',\n table: 'string',\n column: 'string',\n },\n 'onCreate?': ExecutionMutationDefaultValueSchema,\n 'onUpdate?': ExecutionMutationDefaultValueSchema,\n});\n\nconst ExecutionSchema = type({\n '+': 'reject',\n mutations: {\n '+': 'reject',\n defaults: ExecutionMutationDefaultSchema.array().readonly(),\n },\n});\n\nconst StorageColumnSchema = type({\n '+': 'reject',\n nativeType: 'string',\n codecId: 'string',\n nullable: 'boolean',\n 'typeParams?': 'Record<string, unknown>',\n 'typeRef?': 'string',\n 'default?': ColumnDefaultSchema,\n}).narrow((col, ctx) => {\n if (col.typeParams !== undefined && col.typeRef !== undefined) {\n return ctx.mustBe('a column with either typeParams or typeRef, not both');\n }\n return true;\n});\n\nconst StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({\n codecId: 'string',\n nativeType: 'string',\n typeParams: 'Record<string, unknown>',\n});\n\nconst PrimaryKeySchema = type.declare<PrimaryKey>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nconst UniqueConstraintSchema = type.declare<UniqueConstraint>().type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n});\n\nexport const IndexSchema = type({\n columns: type.string.array().readonly(),\n 'name?': 'string',\n 'using?': 'string',\n 'config?': 'Record<string, unknown>',\n});\n\nexport const ForeignKeyReferencesSchema = type.declare<ForeignKeyReferences>().type({\n table: 'string',\n columns: type.string.array().readonly(),\n});\n\nexport const ReferentialActionSchema = type\n .declare<ReferentialAction>()\n .type(\"'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault'\");\n\nexport const ForeignKeySchema = type.declare<ForeignKey>().type({\n columns: type.string.array().readonly(),\n references: ForeignKeyReferencesSchema,\n 'name?': 'string',\n 'onDelete?': ReferentialActionSchema,\n 'onUpdate?': ReferentialActionSchema,\n constraint: 'boolean',\n index: 'boolean',\n});\n\nconst StorageTableSchema = type({\n '+': 'reject',\n columns: type({ '[string]': StorageColumnSchema }),\n 'primaryKey?': PrimaryKeySchema,\n uniques: UniqueConstraintSchema.array().readonly(),\n indexes: IndexSchema.array().readonly(),\n foreignKeys: ForeignKeySchema.array().readonly(),\n});\n\nconst StorageSchema = type({\n '+': 'reject',\n tables: type({ '[string]': StorageTableSchema }),\n 'types?': type({ '[string]': StorageTypeInstanceSchema }),\n});\n\nconst ModelFieldSchema = type.declare<ModelField>().type({\n column: 'string',\n});\n\nconst ModelStorageSchema = type.declare<ModelStorage>().type({\n table: 'string',\n});\n\nconst ModelSchema = type.declare<ModelDefinition>().type({\n storage: ModelStorageSchema,\n fields: type({ '[string]': ModelFieldSchema }),\n relations: type({ '[string]': 'unknown' }),\n});\n\nconst MappingsSchema = type({\n '+': 'reject',\n 'modelToTable?': 'null | Record<string, string>',\n 'tableToModel?': 'null | Record<string, string>',\n 'fieldToColumn?': 'null | Record<string, Record<string, string>>',\n 'columnToField?': 'null | Record<string, Record<string, string>>',\n 'codecTypes?': 'null | Record<string, unknown>',\n 'operationTypes?': 'null | Record<string, Record<string, unknown>>',\n});\n\nconst ContractMetaSchema = type({\n '[string]': 'unknown',\n});\n\nconst SqlContractSchema = type({\n '+': 'reject',\n 'schemaVersion?': \"'1'\",\n target: 'string',\n targetFamily: \"'sql'\",\n 'coreHash?': 'string',\n storageHash: 'string',\n 'executionHash?': 'string',\n 'profileHash?': 'string',\n '_generated?': 'Record<string, unknown>',\n 'capabilities?': 'Record<string, Record<string, boolean>>',\n 'extensionPacks?': 'Record<string, unknown>',\n 'meta?': ContractMetaSchema,\n 'sources?': 'Record<string, unknown>',\n 'relations?': type({ '[string]': 'unknown' }),\n 'mappings?': MappingsSchema,\n models: type({ '[string]': ModelSchema }),\n storage: StorageSchema,\n 'execution?': ExecutionSchema,\n});\n\n// NOTE: StorageColumnSchema, StorageTableSchema, and StorageSchema use bare type()\n// instead of type.declare<T>().type() because the ColumnDefault union's value field\n// includes bigint | Date (runtime-only types after decoding) which cannot be expressed\n// in Arktype's JSON validation DSL. The `as SqlStorage` cast in validateStorage() bridges\n// the gap between the JSON-safe Arktype output and the runtime TypeScript type.\n// See decodeContractDefaults() in validate.ts for the decoding step.\n\n/**\n * Validates the structural shape of SqlStorage using Arktype.\n *\n * @param value - The storage value to validate\n * @returns The validated storage if structure is valid\n * @throws Error if the storage structure is invalid\n */\nexport function validateStorage(value: unknown): SqlStorage {\n const result = StorageSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Storage validation failed: ${messages}`);\n }\n return result as SqlStorage;\n}\n\n/**\n * Validates the structural shape of ModelDefinition using Arktype.\n *\n * @param value - The model value to validate\n * @returns The validated model if structure is valid\n * @throws Error if the model structure is invalid\n */\nexport function validateModel(value: unknown): ModelDefinition {\n const result = ModelSchema(value);\n if (result instanceof type.errors) {\n const messages = result.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Model validation failed: ${messages}`);\n }\n return result;\n}\n\n/**\n * Validates the structural shape of a SqlContract using Arktype.\n *\n * **Responsibility: Validation Only**\n * This function validates that the contract has the correct structure and types.\n * It does NOT normalize the contract - normalization must happen in the contract builder.\n *\n * The contract passed to this function must already be normalized (all required fields present).\n * If normalization is needed, it should be done by the contract builder before calling this function.\n *\n * This ensures all required fields are present and have the correct types.\n *\n * @param value - The contract value to validate (typically from a JSON import)\n * @returns The validated contract if structure is valid\n * @throws Error if the contract structure is invalid\n */\nexport function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T {\n if (typeof value !== 'object' || value === null) {\n throw new Error('Contract structural validation failed: value must be an object');\n }\n\n // Check targetFamily first to provide a clear error message for unsupported target families\n const rawValue = value as { targetFamily?: string };\n if (rawValue.targetFamily !== undefined && rawValue.targetFamily !== 'sql') {\n throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);\n }\n\n const contractResult = SqlContractSchema(value);\n\n if (contractResult instanceof type.errors) {\n const messages = contractResult.map((p: { message: string }) => p.message).join('; ');\n throw new Error(`Contract structural validation failed: ${messages}`);\n }\n\n // After validation, contractResult matches the schema and preserves the input structure\n // TypeScript needs an assertion here due to exactOptionalPropertyTypes differences\n // between Arktype's inferred type and the generic T, but runtime-wise they're compatible\n return contractResult as T;\n}\n\n/**\n * Validates semantic constraints on SqlStorage that cannot be expressed in Arktype schemas.\n *\n * Returns an array of human-readable error strings. Empty array = valid.\n *\n * Currently checks:\n * - `setNull` referential action on a non-nullable FK column (would fail at runtime)\n * - `setDefault` referential action on a non-nullable FK column without a DEFAULT (would fail at runtime)\n */\nexport function validateStorageSemantics(storage: SqlStorage): string[] {\n const errors: string[] = [];\n\n for (const [tableName, table] of Object.entries(storage.tables)) {\n for (const fk of table.foreignKeys) {\n for (const colName of fk.columns) {\n const column = table.columns[colName];\n if (!column) continue;\n\n if (fk.onDelete === 'setNull' && !column.nullable) {\n errors.push(\n `Table \"${tableName}\": onDelete setNull on foreign key column \"${colName}\" which is NOT NULL`,\n );\n }\n if (fk.onUpdate === 'setNull' && !column.nullable) {\n errors.push(\n `Table \"${tableName}\": onUpdate setNull on foreign key column \"${colName}\" which is NOT NULL`,\n );\n }\n if (fk.onDelete === 'setDefault' && !column.nullable && column.default === undefined) {\n errors.push(\n `Table \"${tableName}\": onDelete setDefault on foreign key column \"${colName}\" which is NOT NULL and has no DEFAULT`,\n );\n }\n if (fk.onUpdate === 'setDefault' && !column.nullable && column.default === undefined) {\n errors.push(\n `Table \"${tableName}\": onUpdate setDefault on foreign key column \"${colName}\" which is NOT NULL and has no DEFAULT`,\n );\n }\n }\n }\n }\n\n return errors;\n}\n"],"mappings":"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"}
@@ -0,0 +1,71 @@
1
+ import { S as SqlStorage, _ as ReferentialAction, b as SqlContract, c as ForeignKey, f as ModelDefinition, u as ForeignKeyReferences } from "./types-CcCSXOlR.mjs";
2
+ import * as arktype_internal_variants_object_ts0 from "arktype/internal/variants/object.ts";
3
+ import * as arktype_internal_variants_string_ts0 from "arktype/internal/variants/string.ts";
4
+
5
+ //#region src/validators.d.ts
6
+ type ColumnDefaultLiteral = {
7
+ readonly kind: 'literal';
8
+ readonly value: string | number | boolean | Record<string, unknown> | unknown[] | null;
9
+ };
10
+ type ColumnDefaultFunction = {
11
+ readonly kind: 'function';
12
+ readonly expression: string;
13
+ };
14
+ declare const ColumnDefaultLiteralSchema: arktype_internal_variants_object_ts0.ObjectType<ColumnDefaultLiteral, {}>;
15
+ declare const ColumnDefaultFunctionSchema: arktype_internal_variants_object_ts0.ObjectType<ColumnDefaultFunction, {}>;
16
+ declare const ColumnDefaultSchema: arktype_internal_variants_object_ts0.ObjectType<ColumnDefaultLiteral | ColumnDefaultFunction, {}>;
17
+ declare const IndexSchema: arktype_internal_variants_object_ts0.ObjectType<{
18
+ columns: readonly string[];
19
+ name?: string;
20
+ using?: string;
21
+ config?: Record<string, unknown>;
22
+ }, {}>;
23
+ declare const ForeignKeyReferencesSchema: arktype_internal_variants_object_ts0.ObjectType<ForeignKeyReferences, {}>;
24
+ declare const ReferentialActionSchema: arktype_internal_variants_string_ts0.StringType<ReferentialAction, {}>;
25
+ declare const ForeignKeySchema: arktype_internal_variants_object_ts0.ObjectType<ForeignKey, {}>;
26
+ /**
27
+ * Validates the structural shape of SqlStorage using Arktype.
28
+ *
29
+ * @param value - The storage value to validate
30
+ * @returns The validated storage if structure is valid
31
+ * @throws Error if the storage structure is invalid
32
+ */
33
+ declare function validateStorage(value: unknown): SqlStorage;
34
+ /**
35
+ * Validates the structural shape of ModelDefinition using Arktype.
36
+ *
37
+ * @param value - The model value to validate
38
+ * @returns The validated model if structure is valid
39
+ * @throws Error if the model structure is invalid
40
+ */
41
+ declare function validateModel(value: unknown): ModelDefinition;
42
+ /**
43
+ * Validates the structural shape of a SqlContract using Arktype.
44
+ *
45
+ * **Responsibility: Validation Only**
46
+ * This function validates that the contract has the correct structure and types.
47
+ * It does NOT normalize the contract - normalization must happen in the contract builder.
48
+ *
49
+ * The contract passed to this function must already be normalized (all required fields present).
50
+ * If normalization is needed, it should be done by the contract builder before calling this function.
51
+ *
52
+ * This ensures all required fields are present and have the correct types.
53
+ *
54
+ * @param value - The contract value to validate (typically from a JSON import)
55
+ * @returns The validated contract if structure is valid
56
+ * @throws Error if the contract structure is invalid
57
+ */
58
+ declare function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T;
59
+ /**
60
+ * Validates semantic constraints on SqlStorage that cannot be expressed in Arktype schemas.
61
+ *
62
+ * Returns an array of human-readable error strings. Empty array = valid.
63
+ *
64
+ * Currently checks:
65
+ * - `setNull` referential action on a non-nullable FK column (would fail at runtime)
66
+ * - `setDefault` referential action on a non-nullable FK column without a DEFAULT (would fail at runtime)
67
+ */
68
+ declare function validateStorageSemantics(storage: SqlStorage): string[];
69
+ //#endregion
70
+ export { ColumnDefaultFunctionSchema, ColumnDefaultLiteralSchema, ColumnDefaultSchema, ForeignKeyReferencesSchema, ForeignKeySchema, IndexSchema, ReferentialActionSchema, validateModel, validateSqlContract, validateStorage, validateStorageSemantics };
71
+ //# sourceMappingURL=validators.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"validators.d.mts","names":[],"sources":["../src/validators.ts"],"sourcesContent":[],"mappings":";;;;;KAeK,oBAAA;;8CAEyC;;AAJ7B,KAMZ,qBAAA,GAJoB;EAIpB,SAAA,IAAA,EAAA,UAAqB;EAMb,SAAA,UAAA,EAAA,MAAA;AAKb,CAAA;AAKa,cAVA,0BAUgF,EAVtD,oCAAA,CAAA,UAUsD,CAVtD,oBAUsD,EAAA,CAAA,CAAA,CAAA;AAA7D,cALnB,2BAKmB,EALQ,oCAAA,CAAA,UAKR,CALQ,qBAKR,EAAA,CAAA,CAAA,CAAA;AAAA,cAAnB,mBAAmB,EAAA,oCAAA,CAAA,UAAA,CAAA,oBAAA,GAAA,qBAAA,EAAA,CAAA,CAAA,CAAA;AAAA,cA2DnB,WA3DmB,EAgE9B,oCAAA,CALsB,UA3DQ,CAAA;EAAA,OAAA,EAAA,SAAA,MAAA,EAAA;EA2DnB,IAAA,CAAA,EAAA,MAAA;EAOA,KAAA,CAAA,EAAA,MAAA;EAKA,MAAA,CAAA,EAPX,MAOW,CAAA,MAAA,EAAA,OAE4D,CAAA;AAEzE,CAAA,EAAA,CAAa,CAAA,CAAA;AAwFG,cAjGH,0BAiG8C,EAjGpB,oCAAA,CAAA,UAiGoB,CAjGpB,oBAiGoB,EAAA,CAAA,CAAA,CAAA;AAgB3C,cA5GH,uBA4GkC,EA5GX,oCAAA,CAAA,UA4G0B,CA5G1B,iBA4G0B,EAAA,CAAA,CAAA,CAAA;AAyB9C,cAjIH,gBAiIsB,EAjIN,oCAAA,CAAA,UAiIM,CAjIN,UAiIM,EAAA,CAAA,CAAA,CAAA;;;;;AAiCnC;;;iBA1EgB,eAAA,kBAAiC;;;;;;;;iBAgBjC,aAAA,kBAA+B;;;;;;;;;;;;;;;;;iBAyB/B,8BAA8B,YAAY,8BAA8B;;;;;;;;;;iBAiCxE,wBAAA,UAAkC"}
@@ -0,0 +1,3 @@
1
+ import { a as ForeignKeySchema, c as validateModel, d as validateStorageSemantics, i as ForeignKeyReferencesSchema, l as validateSqlContract, n as ColumnDefaultLiteralSchema, o as IndexSchema, r as ColumnDefaultSchema, s as ReferentialActionSchema, t as ColumnDefaultFunctionSchema, u as validateStorage } from "./validators-CQXvLZa7.mjs";
2
+
3
+ export { ColumnDefaultFunctionSchema, ColumnDefaultLiteralSchema, ColumnDefaultSchema, ForeignKeyReferencesSchema, ForeignKeySchema, IndexSchema, ReferentialActionSchema, validateModel, validateSqlContract, validateStorage, validateStorageSemantics };
package/package.json CHANGED
@@ -1,50 +1,46 @@
1
1
  {
2
2
  "name": "@prisma-next/sql-contract",
3
- "version": "0.3.0-dev.8",
3
+ "version": "0.3.0-dev.80",
4
4
  "type": "module",
5
5
  "sideEffects": false,
6
6
  "description": "SQL contract types, validators, and IR factories for Prisma Next",
7
7
  "dependencies": {
8
8
  "arktype": "^2.1.25",
9
- "@prisma-next/contract": "0.3.0-dev.8"
9
+ "@prisma-next/contract": "0.3.0-dev.80"
10
10
  },
11
11
  "devDependencies": {
12
- "@vitest/coverage-v8": "4.0.16",
13
- "tsup": "8.5.1",
12
+ "tsdown": "0.18.4",
14
13
  "typescript": "5.9.3",
15
- "vitest": "4.0.16",
16
- "@prisma-next/test-utils": "0.0.1"
14
+ "vitest": "4.0.17",
15
+ "@prisma-next/test-utils": "0.0.1",
16
+ "@prisma-next/tsdown": "0.0.0",
17
+ "@prisma-next/tsconfig": "0.0.0"
17
18
  },
18
19
  "files": [
19
20
  "dist",
20
21
  "src"
21
22
  ],
22
23
  "exports": {
23
- "./types": {
24
- "types": "./dist/exports/types.d.ts",
25
- "import": "./dist/exports/types.js"
26
- },
27
- "./validators": {
28
- "types": "./dist/exports/validators.d.ts",
29
- "import": "./dist/exports/validators.js"
30
- },
31
- "./factories": {
32
- "types": "./dist/exports/factories.d.ts",
33
- "import": "./dist/exports/factories.js"
34
- },
35
- "./pack-types": {
36
- "types": "./dist/exports/pack-types.d.ts",
37
- "import": "./dist/exports/pack-types.js"
38
- }
24
+ "./factories": "./dist/factories.mjs",
25
+ "./pack-types": "./dist/pack-types.mjs",
26
+ "./types": "./dist/types.mjs",
27
+ "./validate": "./dist/validate.mjs",
28
+ "./validators": "./dist/validators.mjs",
29
+ "./package.json": "./package.json"
30
+ },
31
+ "repository": {
32
+ "type": "git",
33
+ "url": "https://github.com/prisma/prisma-next.git",
34
+ "directory": "packages/2-sql/1-core/contract"
39
35
  },
40
36
  "scripts": {
41
- "build": "tsup --config tsup.config.ts && tsc --project tsconfig.build.json",
37
+ "build": "tsdown",
42
38
  "test": "vitest run",
43
39
  "test:coverage": "vitest run --coverage",
44
- "typecheck": "tsc --project tsconfig.json --noEmit",
45
- "lint": "biome check . --config-path ../../../../biome.json --error-on-warnings",
46
- "lint:fix": "biome check --write . --config-path ../../../biome.json",
47
- "lint:fix:unsafe": "biome check --write --unsafe . --config-path ../../../biome.json",
48
- "clean": "node ../../../../scripts/clean.mjs"
40
+ "typecheck": "tsc --noEmit",
41
+ "lint": "biome check . --error-on-warnings",
42
+ "lint:fix": "biome check --write .",
43
+ "lint:fix:unsafe": "biome check --write --unsafe .",
44
+ "clean": "rm -rf dist dist-tsc dist-tsc-prod coverage .tmp-output"
49
45
  }
50
46
  }
@@ -0,0 +1,178 @@
1
+ import type { ModelDefinition, SqlContract, SqlMappings, SqlStorage } from './types';
2
+
3
+ type ResolvedMappings = {
4
+ modelToTable: Record<string, string>;
5
+ tableToModel: Record<string, string>;
6
+ fieldToColumn: Record<string, Record<string, string>>;
7
+ columnToField: Record<string, Record<string, string>>;
8
+ };
9
+
10
+ function computeDefaultMappings(models: Record<string, ModelDefinition>): ResolvedMappings {
11
+ const modelToTable: Record<string, string> = {};
12
+ const tableToModel: Record<string, string> = {};
13
+ const fieldToColumn: Record<string, Record<string, string>> = {};
14
+ const columnToField: Record<string, Record<string, string>> = {};
15
+
16
+ for (const [modelName, model] of Object.entries(models)) {
17
+ const tableName = model.storage.table;
18
+ modelToTable[modelName] = tableName;
19
+ tableToModel[tableName] = modelName;
20
+
21
+ const modelFieldToColumn: Record<string, string> = {};
22
+ for (const [fieldName, field] of Object.entries(model.fields)) {
23
+ const columnName = field.column;
24
+ modelFieldToColumn[fieldName] = columnName;
25
+ if (!columnToField[tableName]) {
26
+ columnToField[tableName] = {};
27
+ }
28
+ columnToField[tableName][columnName] = fieldName;
29
+ }
30
+
31
+ fieldToColumn[modelName] = modelFieldToColumn;
32
+ }
33
+
34
+ return {
35
+ modelToTable,
36
+ tableToModel,
37
+ fieldToColumn,
38
+ columnToField,
39
+ };
40
+ }
41
+
42
+ function assertInverseModelMappings(
43
+ modelToTable: Record<string, string>,
44
+ tableToModel: Record<string, string>,
45
+ ): void {
46
+ for (const [model, table] of Object.entries(modelToTable)) {
47
+ if (tableToModel[table] !== model) {
48
+ throw new Error(
49
+ `Mappings override mismatch: modelToTable.${model}="${table}" is not mirrored in tableToModel`,
50
+ );
51
+ }
52
+ }
53
+ for (const [table, model] of Object.entries(tableToModel)) {
54
+ if (modelToTable[model] !== table) {
55
+ throw new Error(
56
+ `Mappings override mismatch: tableToModel.${table}="${model}" is not mirrored in modelToTable`,
57
+ );
58
+ }
59
+ }
60
+ }
61
+
62
+ function assertInverseFieldMappings(
63
+ fieldToColumn: Record<string, Record<string, string>>,
64
+ columnToField: Record<string, Record<string, string>>,
65
+ modelToTable: Record<string, string>,
66
+ tableToModel: Record<string, string>,
67
+ ): void {
68
+ for (const [model, fields] of Object.entries(fieldToColumn)) {
69
+ const table = modelToTable[model];
70
+ if (!table) {
71
+ throw new Error(
72
+ `Mappings override mismatch: fieldToColumn references unknown model "${model}"`,
73
+ );
74
+ }
75
+ const reverseFields = columnToField[table];
76
+ if (!reverseFields) {
77
+ throw new Error(
78
+ `Mappings override mismatch: columnToField is missing table "${table}" for model "${model}"`,
79
+ );
80
+ }
81
+ for (const [field, column] of Object.entries(fields)) {
82
+ if (reverseFields[column] !== field) {
83
+ throw new Error(
84
+ `Mappings override mismatch: fieldToColumn.${model}.${field}="${column}" is not mirrored in columnToField.${table}`,
85
+ );
86
+ }
87
+ }
88
+ }
89
+
90
+ for (const [table, columns] of Object.entries(columnToField)) {
91
+ const model = tableToModel[table];
92
+ if (!model) {
93
+ throw new Error(
94
+ `Mappings override mismatch: columnToField references unknown table "${table}"`,
95
+ );
96
+ }
97
+ const forwardFields = fieldToColumn[model];
98
+ if (!forwardFields) {
99
+ throw new Error(
100
+ `Mappings override mismatch: fieldToColumn is missing model "${model}" for table "${table}"`,
101
+ );
102
+ }
103
+ for (const [column, field] of Object.entries(columns)) {
104
+ if (forwardFields[field] !== column) {
105
+ throw new Error(
106
+ `Mappings override mismatch: columnToField.${table}.${column}="${field}" is not mirrored in fieldToColumn.${model}`,
107
+ );
108
+ }
109
+ }
110
+ }
111
+ }
112
+
113
+ function mergeMappings(
114
+ defaults: ResolvedMappings,
115
+ existingMappings?: Partial<SqlMappings>,
116
+ ): ResolvedMappings {
117
+ const hasModelToTable = existingMappings?.modelToTable !== undefined;
118
+ const hasTableToModel = existingMappings?.tableToModel !== undefined;
119
+ if (hasModelToTable !== hasTableToModel) {
120
+ throw new Error(
121
+ 'Mappings override mismatch: modelToTable and tableToModel must be provided together',
122
+ );
123
+ }
124
+
125
+ const hasFieldToColumn = existingMappings?.fieldToColumn !== undefined;
126
+ const hasColumnToField = existingMappings?.columnToField !== undefined;
127
+ if (hasFieldToColumn !== hasColumnToField) {
128
+ throw new Error(
129
+ 'Mappings override mismatch: fieldToColumn and columnToField must be provided together',
130
+ );
131
+ }
132
+
133
+ const modelToTable: Record<string, string> = hasModelToTable
134
+ ? (existingMappings?.modelToTable ?? {})
135
+ : defaults.modelToTable;
136
+ const tableToModel: Record<string, string> = hasTableToModel
137
+ ? (existingMappings?.tableToModel ?? {})
138
+ : defaults.tableToModel;
139
+ assertInverseModelMappings(modelToTable, tableToModel);
140
+
141
+ const fieldToColumn: Record<string, Record<string, string>> = hasFieldToColumn
142
+ ? (existingMappings?.fieldToColumn ?? {})
143
+ : defaults.fieldToColumn;
144
+ const columnToField: Record<string, Record<string, string>> = hasColumnToField
145
+ ? (existingMappings?.columnToField ?? {})
146
+ : defaults.columnToField;
147
+ assertInverseFieldMappings(fieldToColumn, columnToField, modelToTable, tableToModel);
148
+
149
+ return {
150
+ modelToTable,
151
+ tableToModel,
152
+ fieldToColumn,
153
+ columnToField,
154
+ };
155
+ }
156
+
157
+ type ValidatedContractInput = SqlContract<SqlStorage> & { _generated?: unknown };
158
+
159
+ function stripGenerated(obj: ValidatedContractInput): Omit<ValidatedContractInput, '_generated'> {
160
+ const input = obj as unknown as Record<string, unknown>;
161
+ const { _generated: _, ...rest } = input;
162
+ return rest as Omit<ValidatedContractInput, '_generated'>;
163
+ }
164
+
165
+ export function constructContract<TContract extends SqlContract<SqlStorage>>(
166
+ input: ValidatedContractInput,
167
+ ): TContract {
168
+ const existingMappings = (input as { mappings?: Partial<SqlMappings> }).mappings;
169
+ const defaultMappings = computeDefaultMappings(input.models as Record<string, ModelDefinition>);
170
+ const mappings = mergeMappings(defaultMappings, existingMappings);
171
+
172
+ const contractWithMappings = {
173
+ ...stripGenerated(input),
174
+ mappings,
175
+ };
176
+
177
+ return contractWithMappings as TContract;
178
+ }
@@ -1,17 +1,30 @@
1
1
  export type {
2
+ CodecTypesOf,
3
+ ContractWithTypeMaps,
2
4
  ExtractCodecTypes,
3
5
  ExtractOperationTypes,
6
+ ExtractTypeMapsFromContract,
4
7
  ForeignKey,
8
+ ForeignKeyOptions,
5
9
  ForeignKeyReferences,
6
10
  Index,
7
11
  ModelDefinition,
8
12
  ModelField,
9
13
  ModelStorage,
14
+ OperationTypesOf,
10
15
  PrimaryKey,
16
+ ReferentialAction,
17
+ ResolveCodecTypes,
18
+ ResolveOperationTypes,
11
19
  SqlContract,
12
20
  SqlMappings,
13
21
  SqlStorage,
14
22
  StorageColumn,
15
23
  StorageTable,
24
+ StorageTypeInstance,
25
+ TypeMaps,
26
+ TypeMapsPhantomKey,
16
27
  UniqueConstraint,
17
28
  } from '../types';
29
+
30
+ export { applyFkDefaults, DEFAULT_FK_CONSTRAINT, DEFAULT_FK_INDEX } from '../types';
@@ -0,0 +1,6 @@
1
+ export {
2
+ decodeContractDefaults,
3
+ isBigIntColumn,
4
+ normalizeContract,
5
+ validateContract,
6
+ } from '../validate';
@@ -1 +1 @@
1
- export { validateModel, validateSqlContract, validateStorage } from '../validators';
1
+ export * from '../validators';