@prisma-next/sql-contract 0.3.0-dev.1 → 0.3.0-dev.100

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 (49) 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-DRR5stkj.mjs +13 -0
  11. package/dist/types-DRR5stkj.mjs.map +1 -0
  12. package/dist/types-mnqqP_Au.d.mts +183 -0
  13. package/dist/types-mnqqP_Au.d.mts.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-DuHeCiKZ.mjs +218 -0
  21. package/dist/validators-DuHeCiKZ.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 +26 -29
  26. package/src/construct.ts +178 -0
  27. package/src/exports/factories.ts +11 -0
  28. package/src/exports/pack-types.ts +1 -0
  29. package/src/exports/types.ts +35 -0
  30. package/src/exports/validate.ts +6 -0
  31. package/src/exports/validators.ts +1 -0
  32. package/src/factories.ts +166 -0
  33. package/src/index.ts +4 -0
  34. package/src/pack-types.ts +9 -0
  35. package/src/types.ts +236 -0
  36. package/src/validate.ts +272 -0
  37. package/src/validators.ts +309 -0
  38. package/dist/exports/factories.d.ts +0 -41
  39. package/dist/exports/factories.js +0 -83
  40. package/dist/exports/factories.js.map +0 -1
  41. package/dist/exports/pack-types.d.ts +0 -11
  42. package/dist/exports/pack-types.js +0 -1
  43. package/dist/exports/pack-types.js.map +0 -1
  44. package/dist/exports/types.d.ts +0 -70
  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 -38
  48. package/dist/exports/validators.js +0 -96
  49. package/dist/exports/validators.js.map +0 -1
@@ -0,0 +1,309 @@
1
+ import { type } from 'arktype';
2
+ import type {
3
+ ForeignKey,
4
+ ForeignKeyReferences,
5
+ ModelDefinition,
6
+ ModelField,
7
+ ModelStorage,
8
+ PrimaryKey,
9
+ ReferentialAction,
10
+ SqlContract,
11
+ SqlStorage,
12
+ StorageTypeInstance,
13
+ UniqueConstraint,
14
+ } from './types';
15
+
16
+ type ColumnDefaultLiteral = {
17
+ readonly kind: 'literal';
18
+ readonly value: string | number | boolean | Record<string, unknown> | unknown[] | null;
19
+ };
20
+ type ColumnDefaultFunction = { readonly kind: 'function'; readonly expression: string };
21
+ const literalKindSchema = type("'literal'");
22
+ const functionKindSchema = type("'function'");
23
+ const generatorKindSchema = type("'generator'");
24
+ const generatorIdSchema = type('string').narrow((value, ctx) => {
25
+ return /^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(value) ? true : ctx.mustBe('a flat generator id');
26
+ });
27
+
28
+ export const ColumnDefaultLiteralSchema = type.declare<ColumnDefaultLiteral>().type({
29
+ kind: literalKindSchema,
30
+ value: 'string | number | boolean | null | unknown[] | Record<string, unknown>',
31
+ });
32
+
33
+ export const ColumnDefaultFunctionSchema = type.declare<ColumnDefaultFunction>().type({
34
+ kind: functionKindSchema,
35
+ expression: 'string',
36
+ });
37
+
38
+ export const ColumnDefaultSchema = ColumnDefaultLiteralSchema.or(ColumnDefaultFunctionSchema);
39
+
40
+ const ExecutionMutationDefaultValueSchema = type({
41
+ '+': 'reject',
42
+ kind: generatorKindSchema,
43
+ id: generatorIdSchema,
44
+ 'params?': 'Record<string, unknown>',
45
+ });
46
+
47
+ const ExecutionMutationDefaultSchema = type({
48
+ '+': 'reject',
49
+ ref: {
50
+ '+': 'reject',
51
+ table: 'string',
52
+ column: 'string',
53
+ },
54
+ 'onCreate?': ExecutionMutationDefaultValueSchema,
55
+ 'onUpdate?': ExecutionMutationDefaultValueSchema,
56
+ });
57
+
58
+ const ExecutionSchema = type({
59
+ '+': 'reject',
60
+ mutations: {
61
+ '+': 'reject',
62
+ defaults: ExecutionMutationDefaultSchema.array().readonly(),
63
+ },
64
+ });
65
+
66
+ const StorageColumnSchema = type({
67
+ '+': 'reject',
68
+ nativeType: 'string',
69
+ codecId: 'string',
70
+ nullable: 'boolean',
71
+ 'typeParams?': 'Record<string, unknown>',
72
+ 'typeRef?': 'string',
73
+ 'default?': ColumnDefaultSchema,
74
+ }).narrow((col, ctx) => {
75
+ if (col.typeParams !== undefined && col.typeRef !== undefined) {
76
+ return ctx.mustBe('a column with either typeParams or typeRef, not both');
77
+ }
78
+ return true;
79
+ });
80
+
81
+ const StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({
82
+ codecId: 'string',
83
+ nativeType: 'string',
84
+ typeParams: 'Record<string, unknown>',
85
+ });
86
+
87
+ const PrimaryKeySchema = type.declare<PrimaryKey>().type({
88
+ columns: type.string.array().readonly(),
89
+ 'name?': 'string',
90
+ });
91
+
92
+ const UniqueConstraintSchema = type.declare<UniqueConstraint>().type({
93
+ columns: type.string.array().readonly(),
94
+ 'name?': 'string',
95
+ });
96
+
97
+ export const IndexSchema = type({
98
+ columns: type.string.array().readonly(),
99
+ 'name?': 'string',
100
+ 'using?': 'string',
101
+ 'config?': 'Record<string, unknown>',
102
+ });
103
+
104
+ export const ForeignKeyReferencesSchema = type.declare<ForeignKeyReferences>().type({
105
+ table: 'string',
106
+ columns: type.string.array().readonly(),
107
+ });
108
+
109
+ export const ReferentialActionSchema = type
110
+ .declare<ReferentialAction>()
111
+ .type("'noAction' | 'restrict' | 'cascade' | 'setNull' | 'setDefault'");
112
+
113
+ export const ForeignKeySchema = type.declare<ForeignKey>().type({
114
+ columns: type.string.array().readonly(),
115
+ references: ForeignKeyReferencesSchema,
116
+ 'name?': 'string',
117
+ 'onDelete?': ReferentialActionSchema,
118
+ 'onUpdate?': ReferentialActionSchema,
119
+ constraint: 'boolean',
120
+ index: 'boolean',
121
+ });
122
+
123
+ const StorageTableSchema = type({
124
+ '+': 'reject',
125
+ columns: type({ '[string]': StorageColumnSchema }),
126
+ 'primaryKey?': PrimaryKeySchema,
127
+ uniques: UniqueConstraintSchema.array().readonly(),
128
+ indexes: IndexSchema.array().readonly(),
129
+ foreignKeys: ForeignKeySchema.array().readonly(),
130
+ });
131
+
132
+ const StorageSchema = type({
133
+ '+': 'reject',
134
+ tables: type({ '[string]': StorageTableSchema }),
135
+ 'types?': type({ '[string]': StorageTypeInstanceSchema }),
136
+ });
137
+
138
+ const ModelFieldSchema = type.declare<ModelField>().type({
139
+ column: 'string',
140
+ });
141
+
142
+ const ModelStorageSchema = type.declare<ModelStorage>().type({
143
+ table: 'string',
144
+ });
145
+
146
+ const ModelSchema = type.declare<ModelDefinition>().type({
147
+ storage: ModelStorageSchema,
148
+ fields: type({ '[string]': ModelFieldSchema }),
149
+ relations: type({ '[string]': 'unknown' }),
150
+ });
151
+
152
+ const MappingsSchema = type({
153
+ '+': 'reject',
154
+ 'modelToTable?': 'null | Record<string, string>',
155
+ 'tableToModel?': 'null | Record<string, string>',
156
+ 'fieldToColumn?': 'null | Record<string, Record<string, string>>',
157
+ 'columnToField?': 'null | Record<string, Record<string, string>>',
158
+ 'codecTypes?': 'null | Record<string, unknown>',
159
+ 'operationTypes?': 'null | Record<string, Record<string, unknown>>',
160
+ });
161
+
162
+ const ContractMetaSchema = type({
163
+ '[string]': 'unknown',
164
+ });
165
+
166
+ const SqlContractSchema = type({
167
+ '+': 'reject',
168
+ 'schemaVersion?': "'1'",
169
+ target: 'string',
170
+ targetFamily: "'sql'",
171
+ 'coreHash?': 'string',
172
+ storageHash: 'string',
173
+ 'executionHash?': 'string',
174
+ 'profileHash?': 'string',
175
+ '_generated?': 'Record<string, unknown>',
176
+ 'capabilities?': 'Record<string, Record<string, boolean>>',
177
+ 'extensionPacks?': 'Record<string, unknown>',
178
+ 'meta?': ContractMetaSchema,
179
+ 'sources?': 'Record<string, unknown>',
180
+ 'relations?': type({ '[string]': 'unknown' }),
181
+ 'mappings?': MappingsSchema,
182
+ models: type({ '[string]': ModelSchema }),
183
+ storage: StorageSchema,
184
+ 'execution?': ExecutionSchema,
185
+ });
186
+
187
+ // NOTE: StorageColumnSchema, StorageTableSchema, and StorageSchema use bare type()
188
+ // instead of type.declare<T>().type() because the ColumnDefault union's value field
189
+ // includes bigint | Date (runtime-only types after decoding) which cannot be expressed
190
+ // in Arktype's JSON validation DSL. The `as SqlStorage` cast in validateStorage() bridges
191
+ // the gap between the JSON-safe Arktype output and the runtime TypeScript type.
192
+ // See decodeContractDefaults() in validate.ts for the decoding step.
193
+
194
+ /**
195
+ * Validates the structural shape of SqlStorage using Arktype.
196
+ *
197
+ * @param value - The storage value to validate
198
+ * @returns The validated storage if structure is valid
199
+ * @throws Error if the storage structure is invalid
200
+ */
201
+ export function validateStorage(value: unknown): SqlStorage {
202
+ const result = StorageSchema(value);
203
+ if (result instanceof type.errors) {
204
+ const messages = result.map((p: { message: string }) => p.message).join('; ');
205
+ throw new Error(`Storage validation failed: ${messages}`);
206
+ }
207
+ return result as SqlStorage;
208
+ }
209
+
210
+ /**
211
+ * Validates the structural shape of ModelDefinition using Arktype.
212
+ *
213
+ * @param value - The model value to validate
214
+ * @returns The validated model if structure is valid
215
+ * @throws Error if the model structure is invalid
216
+ */
217
+ export function validateModel(value: unknown): ModelDefinition {
218
+ const result = ModelSchema(value);
219
+ if (result instanceof type.errors) {
220
+ const messages = result.map((p: { message: string }) => p.message).join('; ');
221
+ throw new Error(`Model validation failed: ${messages}`);
222
+ }
223
+ return result;
224
+ }
225
+
226
+ /**
227
+ * Validates the structural shape of a SqlContract using Arktype.
228
+ *
229
+ * **Responsibility: Validation Only**
230
+ * This function validates that the contract has the correct structure and types.
231
+ * It does NOT normalize the contract - normalization must happen in the contract builder.
232
+ *
233
+ * The contract passed to this function must already be normalized (all required fields present).
234
+ * If normalization is needed, it should be done by the contract builder before calling this function.
235
+ *
236
+ * This ensures all required fields are present and have the correct types.
237
+ *
238
+ * @param value - The contract value to validate (typically from a JSON import)
239
+ * @returns The validated contract if structure is valid
240
+ * @throws Error if the contract structure is invalid
241
+ */
242
+ export function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T {
243
+ if (typeof value !== 'object' || value === null) {
244
+ throw new Error('Contract structural validation failed: value must be an object');
245
+ }
246
+
247
+ // Check targetFamily first to provide a clear error message for unsupported target families
248
+ const rawValue = value as { targetFamily?: string };
249
+ if (rawValue.targetFamily !== undefined && rawValue.targetFamily !== 'sql') {
250
+ throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);
251
+ }
252
+
253
+ const contractResult = SqlContractSchema(value);
254
+
255
+ if (contractResult instanceof type.errors) {
256
+ const messages = contractResult.map((p: { message: string }) => p.message).join('; ');
257
+ throw new Error(`Contract structural validation failed: ${messages}`);
258
+ }
259
+
260
+ // After validation, contractResult matches the schema and preserves the input structure
261
+ // TypeScript needs an assertion here due to exactOptionalPropertyTypes differences
262
+ // between Arktype's inferred type and the generic T, but runtime-wise they're compatible
263
+ return contractResult as T;
264
+ }
265
+
266
+ /**
267
+ * Validates semantic constraints on SqlStorage that cannot be expressed in Arktype schemas.
268
+ *
269
+ * Returns an array of human-readable error strings. Empty array = valid.
270
+ *
271
+ * Currently checks:
272
+ * - `setNull` referential action on a non-nullable FK column (would fail at runtime)
273
+ * - `setDefault` referential action on a non-nullable FK column without a DEFAULT (would fail at runtime)
274
+ */
275
+ export function validateStorageSemantics(storage: SqlStorage): string[] {
276
+ const errors: string[] = [];
277
+
278
+ for (const [tableName, table] of Object.entries(storage.tables)) {
279
+ for (const fk of table.foreignKeys) {
280
+ for (const colName of fk.columns) {
281
+ const column = table.columns[colName];
282
+ if (!column) continue;
283
+
284
+ if (fk.onDelete === 'setNull' && !column.nullable) {
285
+ errors.push(
286
+ `Table "${tableName}": onDelete setNull on foreign key column "${colName}" which is NOT NULL`,
287
+ );
288
+ }
289
+ if (fk.onUpdate === 'setNull' && !column.nullable) {
290
+ errors.push(
291
+ `Table "${tableName}": onUpdate setNull on foreign key column "${colName}" which is NOT NULL`,
292
+ );
293
+ }
294
+ if (fk.onDelete === 'setDefault' && !column.nullable && column.default === undefined) {
295
+ errors.push(
296
+ `Table "${tableName}": onDelete setDefault on foreign key column "${colName}" which is NOT NULL and has no DEFAULT`,
297
+ );
298
+ }
299
+ if (fk.onUpdate === 'setDefault' && !column.nullable && column.default === undefined) {
300
+ errors.push(
301
+ `Table "${tableName}": onUpdate setDefault on foreign key column "${colName}" which is NOT NULL and has no DEFAULT`,
302
+ );
303
+ }
304
+ }
305
+ }
306
+ }
307
+
308
+ return errors;
309
+ }
@@ -1,41 +0,0 @@
1
- import { StorageColumn, SqlStorage, ModelDefinition, SqlMappings, SqlContract, ForeignKey, Index, ModelField, PrimaryKey, StorageTable, UniqueConstraint } from './types.js';
2
- import '@prisma-next/contract/types';
3
-
4
- /**
5
- * Creates a StorageColumn with nativeType and codecId.
6
- *
7
- * @param nativeType - Native database type identifier (e.g., 'int4', 'text', 'vector')
8
- * @param codecId - Codec identifier (e.g., 'pg/int4@1', 'pg/text@1')
9
- * @param nullable - Whether the column is nullable (default: false)
10
- * @returns StorageColumn with nativeType and codecId
11
- */
12
- declare function col(nativeType: string, codecId: string, nullable?: boolean): StorageColumn;
13
- declare function pk(...columns: readonly string[]): PrimaryKey;
14
- declare function unique(...columns: readonly string[]): UniqueConstraint;
15
- declare function index(...columns: readonly string[]): Index;
16
- declare function fk(columns: readonly string[], refTable: string, refColumns: readonly string[], name?: string): ForeignKey;
17
- declare function table(columns: Record<string, StorageColumn>, opts?: {
18
- pk?: PrimaryKey;
19
- uniques?: readonly UniqueConstraint[];
20
- indexes?: readonly Index[];
21
- fks?: readonly ForeignKey[];
22
- }): StorageTable;
23
- declare function model(table: string, fields: Record<string, ModelField>, relations?: Record<string, unknown>): ModelDefinition;
24
- declare function storage(tables: Record<string, StorageTable>): SqlStorage;
25
- declare function contract(opts: {
26
- target: string;
27
- coreHash: string;
28
- storage: SqlStorage;
29
- models?: Record<string, ModelDefinition>;
30
- relations?: Record<string, unknown>;
31
- mappings?: Partial<SqlMappings>;
32
- schemaVersion?: '1';
33
- targetFamily?: 'sql';
34
- profileHash?: string;
35
- capabilities?: Record<string, Record<string, boolean>>;
36
- extensionPacks?: Record<string, unknown>;
37
- meta?: Record<string, unknown>;
38
- sources?: Record<string, unknown>;
39
- }): SqlContract;
40
-
41
- export { col, contract, fk, index, model, pk, storage, table, unique };
@@ -1,83 +0,0 @@
1
- // src/factories.ts
2
- function col(nativeType, codecId, nullable = false) {
3
- return {
4
- nativeType,
5
- codecId,
6
- nullable
7
- };
8
- }
9
- function pk(...columns) {
10
- return {
11
- columns
12
- };
13
- }
14
- function unique(...columns) {
15
- return {
16
- columns
17
- };
18
- }
19
- function index(...columns) {
20
- return {
21
- columns
22
- };
23
- }
24
- function fk(columns, refTable, refColumns, name) {
25
- const references = {
26
- table: refTable,
27
- columns: refColumns
28
- };
29
- return {
30
- columns,
31
- references,
32
- ...name !== void 0 && { name }
33
- };
34
- }
35
- function table(columns, opts) {
36
- return {
37
- columns,
38
- ...opts?.pk !== void 0 && { primaryKey: opts.pk },
39
- uniques: opts?.uniques ?? [],
40
- indexes: opts?.indexes ?? [],
41
- foreignKeys: opts?.fks ?? []
42
- };
43
- }
44
- function model(table2, fields, relations = {}) {
45
- const storage2 = { table: table2 };
46
- return {
47
- storage: storage2,
48
- fields,
49
- relations
50
- };
51
- }
52
- function storage(tables) {
53
- return { tables };
54
- }
55
- function contract(opts) {
56
- return {
57
- schemaVersion: opts.schemaVersion ?? "1",
58
- target: opts.target,
59
- targetFamily: opts.targetFamily ?? "sql",
60
- coreHash: opts.coreHash,
61
- storage: opts.storage,
62
- models: opts.models ?? {},
63
- relations: opts.relations ?? {},
64
- mappings: opts.mappings ?? {},
65
- ...opts.profileHash !== void 0 && { profileHash: opts.profileHash },
66
- ...opts.capabilities !== void 0 && { capabilities: opts.capabilities },
67
- ...opts.extensionPacks !== void 0 && { extensionPacks: opts.extensionPacks },
68
- ...opts.meta !== void 0 && { meta: opts.meta },
69
- ...opts.sources !== void 0 && { sources: opts.sources }
70
- };
71
- }
72
- export {
73
- col,
74
- contract,
75
- fk,
76
- index,
77
- model,
78
- pk,
79
- storage,
80
- table,
81
- unique
82
- };
83
- //# sourceMappingURL=factories.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/factories.ts"],"sourcesContent":["import type {\n ForeignKey,\n ForeignKeyReferences,\n Index,\n ModelDefinition,\n ModelField,\n ModelStorage,\n PrimaryKey,\n SqlContract,\n SqlMappings,\n SqlStorage,\n StorageColumn,\n StorageTable,\n UniqueConstraint,\n} from './types';\n\n/**\n * Creates a StorageColumn with nativeType and codecId.\n *\n * @param nativeType - Native database type identifier (e.g., 'int4', 'text', 'vector')\n * @param codecId - Codec identifier (e.g., 'pg/int4@1', 'pg/text@1')\n * @param nullable - Whether the column is nullable (default: false)\n * @returns StorageColumn with nativeType and codecId\n */\nexport function col(nativeType: string, codecId: string, nullable = false): StorageColumn {\n return {\n nativeType,\n codecId,\n nullable,\n };\n}\n\nexport function pk(...columns: readonly string[]): PrimaryKey {\n return {\n columns,\n };\n}\n\nexport function unique(...columns: readonly string[]): UniqueConstraint {\n return {\n columns,\n };\n}\n\nexport function index(...columns: readonly string[]): Index {\n return {\n columns,\n };\n}\n\nexport function fk(\n columns: readonly string[],\n refTable: string,\n refColumns: readonly string[],\n name?: string,\n): ForeignKey {\n const references: ForeignKeyReferences = {\n table: refTable,\n columns: refColumns,\n };\n return {\n columns,\n references,\n ...(name !== undefined && { name }),\n };\n}\n\nexport function table(\n columns: Record<string, StorageColumn>,\n opts?: {\n pk?: PrimaryKey;\n uniques?: readonly UniqueConstraint[];\n indexes?: readonly Index[];\n fks?: readonly ForeignKey[];\n },\n): StorageTable {\n return {\n columns,\n ...(opts?.pk !== undefined && { primaryKey: opts.pk }),\n uniques: opts?.uniques ?? [],\n indexes: opts?.indexes ?? [],\n foreignKeys: opts?.fks ?? [],\n };\n}\n\nexport function model(\n table: string,\n fields: Record<string, ModelField>,\n relations: Record<string, unknown> = {},\n): ModelDefinition {\n const storage: ModelStorage = { table };\n return {\n storage,\n fields,\n relations,\n };\n}\n\nexport function storage(tables: Record<string, StorageTable>): SqlStorage {\n return { tables };\n}\n\nexport function contract(opts: {\n target: string;\n coreHash: string;\n storage: SqlStorage;\n models?: Record<string, ModelDefinition>;\n relations?: Record<string, unknown>;\n mappings?: Partial<SqlMappings>;\n schemaVersion?: '1';\n targetFamily?: 'sql';\n profileHash?: string;\n capabilities?: Record<string, Record<string, boolean>>;\n extensionPacks?: Record<string, unknown>;\n meta?: Record<string, unknown>;\n sources?: Record<string, unknown>;\n}): SqlContract {\n return {\n schemaVersion: opts.schemaVersion ?? '1',\n target: opts.target,\n targetFamily: opts.targetFamily ?? 'sql',\n coreHash: opts.coreHash,\n storage: opts.storage,\n models: opts.models ?? {},\n relations: opts.relations ?? {},\n mappings: (opts.mappings ?? {}) as SqlMappings,\n ...(opts.profileHash !== undefined && { profileHash: opts.profileHash }),\n ...(opts.capabilities !== undefined && { capabilities: opts.capabilities }),\n ...(opts.extensionPacks !== undefined && { extensionPacks: opts.extensionPacks }),\n ...(opts.meta !== undefined && { meta: opts.meta }),\n ...(opts.sources !== undefined && { sources: opts.sources as Record<string, unknown> }),\n } as SqlContract;\n}\n"],"mappings":";AAwBO,SAAS,IAAI,YAAoB,SAAiB,WAAW,OAAsB;AACxF,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,SAAS,MAAM,SAAwC;AAC5D,SAAO;AAAA,IACL;AAAA,EACF;AACF;AAEO,SAAS,UAAU,SAA8C;AACtE,SAAO;AAAA,IACL;AAAA,EACF;AACF;AAEO,SAAS,SAAS,SAAmC;AAC1D,SAAO;AAAA,IACL;AAAA,EACF;AACF;AAEO,SAAS,GACd,SACA,UACA,YACA,MACY;AACZ,QAAM,aAAmC;AAAA,IACvC,OAAO;AAAA,IACP,SAAS;AAAA,EACX;AACA,SAAO;AAAA,IACL;AAAA,IACA;AAAA,IACA,GAAI,SAAS,UAAa,EAAE,KAAK;AAAA,EACnC;AACF;AAEO,SAAS,MACd,SACA,MAMc;AACd,SAAO;AAAA,IACL;AAAA,IACA,GAAI,MAAM,OAAO,UAAa,EAAE,YAAY,KAAK,GAAG;AAAA,IACpD,SAAS,MAAM,WAAW,CAAC;AAAA,IAC3B,SAAS,MAAM,WAAW,CAAC;AAAA,IAC3B,aAAa,MAAM,OAAO,CAAC;AAAA,EAC7B;AACF;AAEO,SAAS,MACdA,QACA,QACA,YAAqC,CAAC,GACrB;AACjB,QAAMC,WAAwB,EAAE,OAAAD,OAAM;AACtC,SAAO;AAAA,IACL,SAAAC;AAAA,IACA;AAAA,IACA;AAAA,EACF;AACF;AAEO,SAAS,QAAQ,QAAkD;AACxE,SAAO,EAAE,OAAO;AAClB;AAEO,SAAS,SAAS,MAcT;AACd,SAAO;AAAA,IACL,eAAe,KAAK,iBAAiB;AAAA,IACrC,QAAQ,KAAK;AAAA,IACb,cAAc,KAAK,gBAAgB;AAAA,IACnC,UAAU,KAAK;AAAA,IACf,SAAS,KAAK;AAAA,IACd,QAAQ,KAAK,UAAU,CAAC;AAAA,IACxB,WAAW,KAAK,aAAa,CAAC;AAAA,IAC9B,UAAW,KAAK,YAAY,CAAC;AAAA,IAC7B,GAAI,KAAK,gBAAgB,UAAa,EAAE,aAAa,KAAK,YAAY;AAAA,IACtE,GAAI,KAAK,iBAAiB,UAAa,EAAE,cAAc,KAAK,aAAa;AAAA,IACzE,GAAI,KAAK,mBAAmB,UAAa,EAAE,gBAAgB,KAAK,eAAe;AAAA,IAC/E,GAAI,KAAK,SAAS,UAAa,EAAE,MAAM,KAAK,KAAK;AAAA,IACjD,GAAI,KAAK,YAAY,UAAa,EAAE,SAAS,KAAK,QAAmC;AAAA,EACvF;AACF;","names":["table","storage"]}
@@ -1,11 +0,0 @@
1
- /**
2
- * Storage type metadata for pack refs.
3
- */
4
- interface StorageTypeMetadata {
5
- readonly typeId: string;
6
- readonly familyId: string;
7
- readonly targetId: string;
8
- readonly nativeType?: string;
9
- }
10
-
11
- export type { StorageTypeMetadata };
@@ -1 +0,0 @@
1
- //# sourceMappingURL=pack-types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,70 +0,0 @@
1
- import { ContractBase } from '@prisma-next/contract/types';
2
-
3
- type StorageColumn = {
4
- readonly nativeType: string;
5
- readonly codecId: string;
6
- readonly nullable: boolean;
7
- };
8
- type PrimaryKey = {
9
- readonly columns: readonly string[];
10
- readonly name?: string;
11
- };
12
- type UniqueConstraint = {
13
- readonly columns: readonly string[];
14
- readonly name?: string;
15
- };
16
- type Index = {
17
- readonly columns: readonly string[];
18
- readonly name?: string;
19
- };
20
- type ForeignKeyReferences = {
21
- readonly table: string;
22
- readonly columns: readonly string[];
23
- };
24
- type ForeignKey = {
25
- readonly columns: readonly string[];
26
- readonly references: ForeignKeyReferences;
27
- readonly name?: string;
28
- };
29
- type StorageTable = {
30
- readonly columns: Record<string, StorageColumn>;
31
- readonly primaryKey?: PrimaryKey;
32
- readonly uniques: ReadonlyArray<UniqueConstraint>;
33
- readonly indexes: ReadonlyArray<Index>;
34
- readonly foreignKeys: ReadonlyArray<ForeignKey>;
35
- };
36
- type SqlStorage = {
37
- readonly tables: Record<string, StorageTable>;
38
- };
39
- type ModelField = {
40
- readonly column: string;
41
- };
42
- type ModelStorage = {
43
- readonly table: string;
44
- };
45
- type ModelDefinition = {
46
- readonly storage: ModelStorage;
47
- readonly fields: Record<string, ModelField>;
48
- readonly relations: Record<string, unknown>;
49
- };
50
- type SqlMappings = {
51
- readonly modelToTable?: Record<string, string>;
52
- readonly tableToModel?: Record<string, string>;
53
- readonly fieldToColumn?: Record<string, Record<string, string>>;
54
- readonly columnToField?: Record<string, Record<string, string>>;
55
- readonly codecTypes: Record<string, {
56
- readonly output: unknown;
57
- }>;
58
- readonly operationTypes: Record<string, Record<string, unknown>>;
59
- };
60
- type SqlContract<S extends SqlStorage = SqlStorage, M extends Record<string, unknown> = Record<string, unknown>, R extends Record<string, unknown> = Record<string, unknown>, Map extends SqlMappings = SqlMappings> = ContractBase & {
61
- readonly targetFamily: string;
62
- readonly storage: S;
63
- readonly models: M;
64
- readonly relations: R;
65
- readonly mappings: Map;
66
- };
67
- type ExtractCodecTypes<TContract extends SqlContract<SqlStorage>> = TContract['mappings']['codecTypes'];
68
- type ExtractOperationTypes<TContract extends SqlContract<SqlStorage>> = TContract['mappings']['operationTypes'];
69
-
70
- export type { ExtractCodecTypes, ExtractOperationTypes, ForeignKey, ForeignKeyReferences, Index, ModelDefinition, ModelField, ModelStorage, PrimaryKey, SqlContract, SqlMappings, SqlStorage, StorageColumn, StorageTable, UniqueConstraint };
@@ -1 +0,0 @@
1
- //# sourceMappingURL=types.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,38 +0,0 @@
1
- import { ModelDefinition, SqlContract, SqlStorage } from './types.js';
2
- import '@prisma-next/contract/types';
3
-
4
- /**
5
- * Validates the structural shape of SqlStorage using Arktype.
6
- *
7
- * @param value - The storage value to validate
8
- * @returns The validated storage if structure is valid
9
- * @throws Error if the storage structure is invalid
10
- */
11
- declare function validateStorage(value: unknown): SqlStorage;
12
- /**
13
- * Validates the structural shape of ModelDefinition using Arktype.
14
- *
15
- * @param value - The model value to validate
16
- * @returns The validated model if structure is valid
17
- * @throws Error if the model structure is invalid
18
- */
19
- declare function validateModel(value: unknown): ModelDefinition;
20
- /**
21
- * Validates the structural shape of a SqlContract using Arktype.
22
- *
23
- * **Responsibility: Validation Only**
24
- * This function validates that the contract has the correct structure and types.
25
- * It does NOT normalize the contract - normalization must happen in the contract builder.
26
- *
27
- * The contract passed to this function must already be normalized (all required fields present).
28
- * If normalization is needed, it should be done by the contract builder before calling this function.
29
- *
30
- * This ensures all required fields are present and have the correct types.
31
- *
32
- * @param value - The contract value to validate (typically from a JSON import)
33
- * @returns The validated contract if structure is valid
34
- * @throws Error if the contract structure is invalid
35
- */
36
- declare function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T;
37
-
38
- export { validateModel, validateSqlContract, validateStorage };
@@ -1,96 +0,0 @@
1
- // src/validators.ts
2
- import { type } from "arktype";
3
- var StorageColumnSchema = type.declare().type({
4
- nativeType: "string",
5
- codecId: "string",
6
- nullable: "boolean"
7
- });
8
- var PrimaryKeySchema = type.declare().type({
9
- columns: type.string.array().readonly(),
10
- "name?": "string"
11
- });
12
- var UniqueConstraintSchema = type.declare().type({
13
- columns: type.string.array().readonly(),
14
- "name?": "string"
15
- });
16
- var IndexSchema = type.declare().type({
17
- columns: type.string.array().readonly(),
18
- "name?": "string"
19
- });
20
- var ForeignKeyReferencesSchema = type.declare().type({
21
- table: "string",
22
- columns: type.string.array().readonly()
23
- });
24
- var ForeignKeySchema = type.declare().type({
25
- columns: type.string.array().readonly(),
26
- references: ForeignKeyReferencesSchema,
27
- "name?": "string"
28
- });
29
- var StorageTableSchema = type.declare().type({
30
- columns: type({ "[string]": StorageColumnSchema }),
31
- "primaryKey?": PrimaryKeySchema,
32
- uniques: UniqueConstraintSchema.array().readonly(),
33
- indexes: IndexSchema.array().readonly(),
34
- foreignKeys: ForeignKeySchema.array().readonly()
35
- });
36
- var StorageSchema = type.declare().type({
37
- tables: type({ "[string]": StorageTableSchema })
38
- });
39
- var ModelFieldSchema = type.declare().type({
40
- column: "string"
41
- });
42
- var ModelStorageSchema = type.declare().type({
43
- table: "string"
44
- });
45
- var ModelSchema = type.declare().type({
46
- storage: ModelStorageSchema,
47
- fields: type({ "[string]": ModelFieldSchema }),
48
- relations: type({ "[string]": "unknown" })
49
- });
50
- var SqlContractSchema = type({
51
- "schemaVersion?": "'1'",
52
- target: "string",
53
- targetFamily: "'sql'",
54
- coreHash: "string",
55
- "profileHash?": "string",
56
- "capabilities?": "Record<string, Record<string, boolean>>",
57
- "extensionPacks?": "Record<string, unknown>",
58
- "meta?": "Record<string, unknown>",
59
- "sources?": "Record<string, unknown>",
60
- models: type({ "[string]": ModelSchema }),
61
- storage: StorageSchema
62
- });
63
- function validateStorage(value) {
64
- const result = StorageSchema(value);
65
- if (result instanceof type.errors) {
66
- const messages = result.map((p) => p.message).join("; ");
67
- throw new Error(`Storage validation failed: ${messages}`);
68
- }
69
- return result;
70
- }
71
- function validateModel(value) {
72
- const result = ModelSchema(value);
73
- if (result instanceof type.errors) {
74
- const messages = result.map((p) => p.message).join("; ");
75
- throw new Error(`Model validation failed: ${messages}`);
76
- }
77
- return result;
78
- }
79
- function validateSqlContract(value) {
80
- const rawValue = value;
81
- if (rawValue.targetFamily !== void 0 && rawValue.targetFamily !== "sql") {
82
- throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);
83
- }
84
- const contractResult = SqlContractSchema(value);
85
- if (contractResult instanceof type.errors) {
86
- const messages = contractResult.map((p) => p.message).join("; ");
87
- throw new Error(`Contract structural validation failed: ${messages}`);
88
- }
89
- return contractResult;
90
- }
91
- export {
92
- validateModel,
93
- validateSqlContract,
94
- validateStorage
95
- };
96
- //# sourceMappingURL=validators.js.map