@prisma-next/sql-contract 0.3.0-dev.33 → 0.3.0-dev.35
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +24 -8
- package/dist/factories.d.mts +45 -0
- package/dist/factories.d.mts.map +1 -0
- package/dist/factories.mjs +76 -0
- package/dist/factories.mjs.map +1 -0
- package/dist/pack-types.d.mts +13 -0
- package/dist/pack-types.d.mts.map +1 -0
- package/dist/pack-types.mjs +1 -0
- package/dist/types-D3AOQgwo.d.mts +117 -0
- package/dist/types-D3AOQgwo.d.mts.map +1 -0
- package/dist/types.d.mts +2 -0
- package/dist/types.mjs +1 -0
- package/dist/validate.d.mts +7 -0
- package/dist/validate.d.mts.map +1 -0
- package/dist/validate.mjs +171 -0
- package/dist/validate.mjs.map +1 -0
- package/dist/validators-DY_87pfS.mjs +160 -0
- package/dist/validators-DY_87pfS.mjs.map +1 -0
- package/dist/{validators.d.ts → validators.d.mts} +21 -5
- package/dist/validators.d.mts.map +1 -0
- package/dist/validators.mjs +3 -0
- package/package.json +19 -22
- package/src/exports/validate.ts +1 -0
- package/src/exports/validators.ts +1 -1
- package/src/factories.ts +33 -6
- package/src/index.ts +1 -0
- package/src/types.ts +18 -2
- package/src/validate.ts +324 -0
- package/src/validators.ts +60 -17
- package/dist/exports/factories.d.ts +0 -2
- package/dist/exports/factories.d.ts.map +0 -1
- package/dist/exports/factories.js +0 -83
- package/dist/exports/factories.js.map +0 -1
- package/dist/exports/pack-types.d.ts +0 -2
- package/dist/exports/pack-types.d.ts.map +0 -1
- package/dist/exports/pack-types.js +0 -1
- package/dist/exports/pack-types.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 -1
- package/dist/exports/types.js.map +0 -1
- package/dist/exports/validators.d.ts +0 -2
- package/dist/exports/validators.d.ts.map +0 -1
- package/dist/exports/validators.js +0 -109
- package/dist/exports/validators.js.map +0 -1
- package/dist/factories.d.ts +0 -38
- package/dist/factories.d.ts.map +0 -1
- package/dist/index.d.ts +0 -4
- package/dist/index.d.ts.map +0 -1
- package/dist/pack-types.d.ts +0 -10
- package/dist/pack-types.d.ts.map +0 -1
- package/dist/types.d.ts +0 -106
- package/dist/types.d.ts.map +0 -1
- package/dist/validators.d.ts.map +0 -1
package/src/validate.ts
ADDED
|
@@ -0,0 +1,324 @@
|
|
|
1
|
+
import type { ModelDefinition, SqlContract, SqlMappings, SqlStorage } from './types';
|
|
2
|
+
import { validateSqlContract } from './validators';
|
|
3
|
+
|
|
4
|
+
type ResolvedMappings = {
|
|
5
|
+
modelToTable: Record<string, string>;
|
|
6
|
+
tableToModel: Record<string, string>;
|
|
7
|
+
fieldToColumn: Record<string, Record<string, string>>;
|
|
8
|
+
columnToField: Record<string, Record<string, string>>;
|
|
9
|
+
codecTypes: Record<string, { readonly output: unknown }>;
|
|
10
|
+
operationTypes: Record<string, Record<string, unknown>>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
function computeDefaultMappings(models: Record<string, ModelDefinition>): ResolvedMappings {
|
|
14
|
+
const modelToTable: Record<string, string> = {};
|
|
15
|
+
const tableToModel: Record<string, string> = {};
|
|
16
|
+
const fieldToColumn: Record<string, Record<string, string>> = {};
|
|
17
|
+
const columnToField: Record<string, Record<string, string>> = {};
|
|
18
|
+
|
|
19
|
+
for (const [modelName, model] of Object.entries(models)) {
|
|
20
|
+
const tableName = model.storage.table;
|
|
21
|
+
modelToTable[modelName] = tableName;
|
|
22
|
+
tableToModel[tableName] = modelName;
|
|
23
|
+
|
|
24
|
+
const modelFieldToColumn: Record<string, string> = {};
|
|
25
|
+
for (const [fieldName, field] of Object.entries(model.fields)) {
|
|
26
|
+
const columnName = field.column;
|
|
27
|
+
modelFieldToColumn[fieldName] = columnName;
|
|
28
|
+
if (!columnToField[tableName]) {
|
|
29
|
+
columnToField[tableName] = {};
|
|
30
|
+
}
|
|
31
|
+
columnToField[tableName][columnName] = fieldName;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
fieldToColumn[modelName] = modelFieldToColumn;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return {
|
|
38
|
+
modelToTable,
|
|
39
|
+
tableToModel,
|
|
40
|
+
fieldToColumn,
|
|
41
|
+
columnToField,
|
|
42
|
+
codecTypes: {},
|
|
43
|
+
operationTypes: {},
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function assertInverseModelMappings(
|
|
48
|
+
modelToTable: Record<string, string>,
|
|
49
|
+
tableToModel: Record<string, string>,
|
|
50
|
+
) {
|
|
51
|
+
for (const [model, table] of Object.entries(modelToTable)) {
|
|
52
|
+
if (tableToModel[table] !== model) {
|
|
53
|
+
throw new Error(
|
|
54
|
+
`Mappings override mismatch: modelToTable.${model}="${table}" is not mirrored in tableToModel`,
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
for (const [table, model] of Object.entries(tableToModel)) {
|
|
59
|
+
if (modelToTable[model] !== table) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`Mappings override mismatch: tableToModel.${table}="${model}" is not mirrored in modelToTable`,
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function assertInverseFieldMappings(
|
|
68
|
+
fieldToColumn: Record<string, Record<string, string>>,
|
|
69
|
+
columnToField: Record<string, Record<string, string>>,
|
|
70
|
+
modelToTable: Record<string, string>,
|
|
71
|
+
tableToModel: Record<string, string>,
|
|
72
|
+
) {
|
|
73
|
+
for (const [model, fields] of Object.entries(fieldToColumn)) {
|
|
74
|
+
const table = modelToTable[model];
|
|
75
|
+
if (!table) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`Mappings override mismatch: fieldToColumn references unknown model "${model}"`,
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
const reverseFields = columnToField[table];
|
|
81
|
+
if (!reverseFields) {
|
|
82
|
+
throw new Error(
|
|
83
|
+
`Mappings override mismatch: columnToField is missing table "${table}" for model "${model}"`,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
for (const [field, column] of Object.entries(fields)) {
|
|
87
|
+
if (reverseFields[column] !== field) {
|
|
88
|
+
throw new Error(
|
|
89
|
+
`Mappings override mismatch: fieldToColumn.${model}.${field}="${column}" is not mirrored in columnToField.${table}`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
for (const [table, columns] of Object.entries(columnToField)) {
|
|
96
|
+
const model = tableToModel[table];
|
|
97
|
+
if (!model) {
|
|
98
|
+
throw new Error(
|
|
99
|
+
`Mappings override mismatch: columnToField references unknown table "${table}"`,
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
const forwardFields = fieldToColumn[model];
|
|
103
|
+
if (!forwardFields) {
|
|
104
|
+
throw new Error(
|
|
105
|
+
`Mappings override mismatch: fieldToColumn is missing model "${model}" for table "${table}"`,
|
|
106
|
+
);
|
|
107
|
+
}
|
|
108
|
+
for (const [column, field] of Object.entries(columns)) {
|
|
109
|
+
if (forwardFields[field] !== column) {
|
|
110
|
+
throw new Error(
|
|
111
|
+
`Mappings override mismatch: columnToField.${table}.${column}="${field}" is not mirrored in fieldToColumn.${model}`,
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function mergeMappings(
|
|
119
|
+
defaults: ResolvedMappings,
|
|
120
|
+
existingMappings?: Partial<SqlMappings>,
|
|
121
|
+
): ResolvedMappings {
|
|
122
|
+
const hasModelToTable = existingMappings?.modelToTable !== undefined;
|
|
123
|
+
const hasTableToModel = existingMappings?.tableToModel !== undefined;
|
|
124
|
+
if (hasModelToTable !== hasTableToModel) {
|
|
125
|
+
throw new Error(
|
|
126
|
+
'Mappings override mismatch: modelToTable and tableToModel must be provided together',
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const hasFieldToColumn = existingMappings?.fieldToColumn !== undefined;
|
|
131
|
+
const hasColumnToField = existingMappings?.columnToField !== undefined;
|
|
132
|
+
if (hasFieldToColumn !== hasColumnToField) {
|
|
133
|
+
throw new Error(
|
|
134
|
+
'Mappings override mismatch: fieldToColumn and columnToField must be provided together',
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const modelToTable: Record<string, string> = hasModelToTable
|
|
139
|
+
? (existingMappings?.modelToTable ?? {})
|
|
140
|
+
: defaults.modelToTable;
|
|
141
|
+
const tableToModel: Record<string, string> = hasTableToModel
|
|
142
|
+
? (existingMappings?.tableToModel ?? {})
|
|
143
|
+
: defaults.tableToModel;
|
|
144
|
+
assertInverseModelMappings(modelToTable, tableToModel);
|
|
145
|
+
|
|
146
|
+
const fieldToColumn: Record<string, Record<string, string>> = hasFieldToColumn
|
|
147
|
+
? (existingMappings?.fieldToColumn ?? {})
|
|
148
|
+
: defaults.fieldToColumn;
|
|
149
|
+
const columnToField: Record<string, Record<string, string>> = hasColumnToField
|
|
150
|
+
? (existingMappings?.columnToField ?? {})
|
|
151
|
+
: defaults.columnToField;
|
|
152
|
+
assertInverseFieldMappings(fieldToColumn, columnToField, modelToTable, tableToModel);
|
|
153
|
+
|
|
154
|
+
return {
|
|
155
|
+
modelToTable,
|
|
156
|
+
tableToModel,
|
|
157
|
+
fieldToColumn,
|
|
158
|
+
columnToField,
|
|
159
|
+
codecTypes: { ...defaults.codecTypes, ...(existingMappings?.codecTypes ?? {}) },
|
|
160
|
+
operationTypes: { ...defaults.operationTypes, ...(existingMappings?.operationTypes ?? {}) },
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function validateContractLogic(contract: SqlContract<SqlStorage>): void {
|
|
165
|
+
const tableNames = new Set(Object.keys(contract.storage.tables));
|
|
166
|
+
|
|
167
|
+
for (const [tableName, table] of Object.entries(contract.storage.tables)) {
|
|
168
|
+
const columnNames = new Set(Object.keys(table.columns));
|
|
169
|
+
|
|
170
|
+
if (table.primaryKey) {
|
|
171
|
+
for (const colName of table.primaryKey.columns) {
|
|
172
|
+
if (!columnNames.has(colName)) {
|
|
173
|
+
throw new Error(
|
|
174
|
+
`Table "${tableName}" primaryKey references non-existent column "${colName}"`,
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
for (const unique of table.uniques) {
|
|
181
|
+
for (const colName of unique.columns) {
|
|
182
|
+
if (!columnNames.has(colName)) {
|
|
183
|
+
throw new Error(
|
|
184
|
+
`Table "${tableName}" unique constraint references non-existent column "${colName}"`,
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
for (const index of table.indexes) {
|
|
191
|
+
for (const colName of index.columns) {
|
|
192
|
+
if (!columnNames.has(colName)) {
|
|
193
|
+
throw new Error(`Table "${tableName}" index references non-existent column "${colName}"`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
for (const fk of table.foreignKeys) {
|
|
199
|
+
for (const colName of fk.columns) {
|
|
200
|
+
if (!columnNames.has(colName)) {
|
|
201
|
+
throw new Error(
|
|
202
|
+
`Table "${tableName}" foreignKey references non-existent column "${colName}"`,
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
if (!tableNames.has(fk.references.table)) {
|
|
208
|
+
throw new Error(
|
|
209
|
+
`Table "${tableName}" foreignKey references non-existent table "${fk.references.table}"`,
|
|
210
|
+
);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const referencedTable = contract.storage.tables[
|
|
214
|
+
fk.references.table
|
|
215
|
+
] as (typeof contract.storage.tables)[string];
|
|
216
|
+
const referencedColumnNames = new Set(Object.keys(referencedTable.columns));
|
|
217
|
+
for (const colName of fk.references.columns) {
|
|
218
|
+
if (!referencedColumnNames.has(colName)) {
|
|
219
|
+
throw new Error(
|
|
220
|
+
`Table "${tableName}" foreignKey references non-existent column "${colName}" in table "${fk.references.table}"`,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
if (fk.columns.length !== fk.references.columns.length) {
|
|
226
|
+
throw new Error(
|
|
227
|
+
`Table "${tableName}" foreignKey column count (${fk.columns.length}) does not match referenced column count (${fk.references.columns.length})`,
|
|
228
|
+
);
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
function normalizeContract(contract: unknown): SqlContract<SqlStorage> {
|
|
235
|
+
if (typeof contract !== 'object' || contract === null) {
|
|
236
|
+
return contract as SqlContract<SqlStorage>;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const contractObj = contract as Record<string, unknown>;
|
|
240
|
+
|
|
241
|
+
let normalizedStorage = contractObj['storage'];
|
|
242
|
+
if (normalizedStorage && typeof normalizedStorage === 'object' && normalizedStorage !== null) {
|
|
243
|
+
const storage = normalizedStorage as Record<string, unknown>;
|
|
244
|
+
const tables = storage['tables'] as Record<string, unknown> | undefined;
|
|
245
|
+
|
|
246
|
+
if (tables) {
|
|
247
|
+
const normalizedTables: Record<string, unknown> = {};
|
|
248
|
+
for (const [tableName, table] of Object.entries(tables)) {
|
|
249
|
+
const tableObj = table as Record<string, unknown>;
|
|
250
|
+
const columns = tableObj['columns'] as Record<string, unknown> | undefined;
|
|
251
|
+
|
|
252
|
+
if (columns) {
|
|
253
|
+
const normalizedColumns: Record<string, unknown> = {};
|
|
254
|
+
for (const [columnName, column] of Object.entries(columns)) {
|
|
255
|
+
const columnObj = column as Record<string, unknown>;
|
|
256
|
+
normalizedColumns[columnName] = {
|
|
257
|
+
...columnObj,
|
|
258
|
+
nullable: columnObj['nullable'] ?? false,
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
normalizedTables[tableName] = {
|
|
263
|
+
...tableObj,
|
|
264
|
+
columns: normalizedColumns,
|
|
265
|
+
uniques: tableObj['uniques'] ?? [],
|
|
266
|
+
indexes: tableObj['indexes'] ?? [],
|
|
267
|
+
foreignKeys: tableObj['foreignKeys'] ?? [],
|
|
268
|
+
};
|
|
269
|
+
} else {
|
|
270
|
+
normalizedTables[tableName] = tableObj;
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
normalizedStorage = {
|
|
275
|
+
...storage,
|
|
276
|
+
tables: normalizedTables,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
let normalizedModels = contractObj['models'];
|
|
282
|
+
if (normalizedModels && typeof normalizedModels === 'object' && normalizedModels !== null) {
|
|
283
|
+
const models = normalizedModels as Record<string, unknown>;
|
|
284
|
+
const normalizedModelsObj: Record<string, unknown> = {};
|
|
285
|
+
for (const [modelName, model] of Object.entries(models)) {
|
|
286
|
+
const modelObj = model as Record<string, unknown>;
|
|
287
|
+
normalizedModelsObj[modelName] = {
|
|
288
|
+
...modelObj,
|
|
289
|
+
relations: modelObj['relations'] ?? {},
|
|
290
|
+
};
|
|
291
|
+
}
|
|
292
|
+
normalizedModels = normalizedModelsObj;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return {
|
|
296
|
+
...contractObj,
|
|
297
|
+
models: normalizedModels,
|
|
298
|
+
relations: contractObj['relations'] ?? {},
|
|
299
|
+
storage: normalizedStorage,
|
|
300
|
+
extensionPacks: contractObj['extensionPacks'] ?? {},
|
|
301
|
+
capabilities: contractObj['capabilities'] ?? {},
|
|
302
|
+
meta: contractObj['meta'] ?? {},
|
|
303
|
+
sources: contractObj['sources'] ?? {},
|
|
304
|
+
} as SqlContract<SqlStorage>;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function validateContract<TContract extends SqlContract<SqlStorage>>(
|
|
308
|
+
value: unknown,
|
|
309
|
+
): TContract {
|
|
310
|
+
const normalized = normalizeContract(value);
|
|
311
|
+
const structurallyValid = validateSqlContract<SqlContract<SqlStorage>>(normalized);
|
|
312
|
+
validateContractLogic(structurallyValid);
|
|
313
|
+
|
|
314
|
+
const existingMappings = (structurallyValid as { mappings?: Partial<SqlMappings> }).mappings;
|
|
315
|
+
const defaultMappings = computeDefaultMappings(
|
|
316
|
+
structurallyValid.models as Record<string, ModelDefinition>,
|
|
317
|
+
);
|
|
318
|
+
const mappings = mergeMappings(defaultMappings, existingMappings);
|
|
319
|
+
|
|
320
|
+
return {
|
|
321
|
+
...structurallyValid,
|
|
322
|
+
mappings,
|
|
323
|
+
} as TContract;
|
|
324
|
+
}
|
package/src/validators.ts
CHANGED
|
@@ -9,27 +9,64 @@ import type {
|
|
|
9
9
|
PrimaryKey,
|
|
10
10
|
SqlContract,
|
|
11
11
|
SqlStorage,
|
|
12
|
-
StorageColumn,
|
|
13
12
|
StorageTable,
|
|
14
13
|
StorageTypeInstance,
|
|
15
14
|
UniqueConstraint,
|
|
16
15
|
} from './types';
|
|
17
16
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
17
|
+
type ColumnDefaultLiteral = { readonly kind: 'literal'; readonly expression: string };
|
|
18
|
+
type ColumnDefaultFunction = { readonly kind: 'function'; readonly expression: string };
|
|
19
|
+
const literalKindSchema = type("'literal'");
|
|
20
|
+
const functionKindSchema = type("'function'");
|
|
21
|
+
const generatorKindSchema = type("'generator'");
|
|
22
|
+
const generatorIdSchema = type("'ulid' | 'nanoid' | 'uuidv7' | 'uuidv4' | 'cuid2' | 'ksuid'");
|
|
23
|
+
|
|
24
|
+
export const ColumnDefaultLiteralSchema = type.declare<ColumnDefaultLiteral>().type({
|
|
25
|
+
kind: literalKindSchema,
|
|
26
|
+
expression: 'string',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export const ColumnDefaultFunctionSchema = type.declare<ColumnDefaultFunction>().type({
|
|
30
|
+
kind: functionKindSchema,
|
|
31
|
+
expression: 'string',
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
export const ColumnDefaultSchema = ColumnDefaultLiteralSchema.or(ColumnDefaultFunctionSchema);
|
|
35
|
+
|
|
36
|
+
const ExecutionMutationDefaultValueSchema = type({
|
|
37
|
+
kind: generatorKindSchema,
|
|
38
|
+
id: generatorIdSchema,
|
|
39
|
+
'params?': 'Record<string, unknown>',
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const ExecutionMutationDefaultSchema = type({
|
|
43
|
+
ref: {
|
|
44
|
+
table: 'string',
|
|
45
|
+
column: 'string',
|
|
46
|
+
},
|
|
47
|
+
'onCreate?': ExecutionMutationDefaultValueSchema,
|
|
48
|
+
'onUpdate?': ExecutionMutationDefaultValueSchema,
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
const ExecutionSchema = type({
|
|
52
|
+
mutations: {
|
|
53
|
+
defaults: ExecutionMutationDefaultSchema.array().readonly(),
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
const StorageColumnSchema = type({
|
|
58
|
+
nativeType: 'string',
|
|
59
|
+
codecId: 'string',
|
|
60
|
+
nullable: 'boolean',
|
|
61
|
+
'typeParams?': 'Record<string, unknown>',
|
|
62
|
+
'typeRef?': 'string',
|
|
63
|
+
'default?': ColumnDefaultSchema,
|
|
64
|
+
}).narrow((col, ctx) => {
|
|
65
|
+
if (col.typeParams !== undefined && col.typeRef !== undefined) {
|
|
66
|
+
return ctx.mustBe('a column with either typeParams or typeRef, not both');
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
});
|
|
33
70
|
|
|
34
71
|
const StorageTypeInstanceSchema = type.declare<StorageTypeInstance>().type({
|
|
35
72
|
codecId: 'string',
|
|
@@ -94,7 +131,8 @@ const SqlContractSchema = type({
|
|
|
94
131
|
'schemaVersion?': "'1'",
|
|
95
132
|
target: 'string',
|
|
96
133
|
targetFamily: "'sql'",
|
|
97
|
-
|
|
134
|
+
storageHash: 'string',
|
|
135
|
+
'executionHash?': 'string',
|
|
98
136
|
'profileHash?': 'string',
|
|
99
137
|
'capabilities?': 'Record<string, Record<string, boolean>>',
|
|
100
138
|
'extensionPacks?': 'Record<string, unknown>',
|
|
@@ -102,6 +140,7 @@ const SqlContractSchema = type({
|
|
|
102
140
|
'sources?': 'Record<string, unknown>',
|
|
103
141
|
models: type({ '[string]': ModelSchema }),
|
|
104
142
|
storage: StorageSchema,
|
|
143
|
+
'execution?': ExecutionSchema,
|
|
105
144
|
});
|
|
106
145
|
|
|
107
146
|
/**
|
|
@@ -153,6 +192,10 @@ export function validateModel(value: unknown): ModelDefinition {
|
|
|
153
192
|
* @throws Error if the contract structure is invalid
|
|
154
193
|
*/
|
|
155
194
|
export function validateSqlContract<T extends SqlContract<SqlStorage>>(value: unknown): T {
|
|
195
|
+
if (typeof value !== 'object' || value === null) {
|
|
196
|
+
throw new Error('Contract structural validation failed: value must be an object');
|
|
197
|
+
}
|
|
198
|
+
|
|
156
199
|
// Check targetFamily first to provide a clear error message for unsupported target families
|
|
157
200
|
const rawValue = value as { targetFamily?: string };
|
|
158
201
|
if (rawValue.targetFamily !== undefined && rawValue.targetFamily !== 'sql') {
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"factories.d.ts","sourceRoot":"","sources":["../../src/exports/factories.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,GAAG,EACH,QAAQ,EACR,EAAE,EACF,KAAK,EACL,KAAK,EACL,EAAE,EACF,OAAO,EACP,KAAK,EACL,MAAM,GACP,MAAM,cAAc,CAAC"}
|
|
@@ -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 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"pack-types.d.ts","sourceRoot":"","sources":["../../src/exports/pack-types.ts"],"names":[],"mappings":"AAAA,YAAY,EAAE,mBAAmB,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=pack-types.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/exports/types.d.ts
DELETED
|
@@ -1,2 +0,0 @@
|
|
|
1
|
-
export type { ExtractCodecTypes, ExtractOperationTypes, ForeignKey, ForeignKeyReferences, Index, ModelDefinition, ModelField, ModelStorage, PrimaryKey, SqlContract, SqlMappings, SqlStorage, StorageColumn, StorageTable, StorageTypeInstance, UniqueConstraint, } from '../types';
|
|
2
|
-
//# sourceMappingURL=types.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/exports/types.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,iBAAiB,EACjB,qBAAqB,EACrB,UAAU,EACV,oBAAoB,EACpB,KAAK,EACL,eAAe,EACf,UAAU,EACV,YAAY,EACZ,UAAU,EACV,WAAW,EACX,WAAW,EACX,UAAU,EACV,aAAa,EACb,YAAY,EACZ,mBAAmB,EACnB,gBAAgB,GACjB,MAAM,UAAU,CAAC"}
|
package/dist/exports/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
//# sourceMappingURL=types.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"validators.d.ts","sourceRoot":"","sources":["../../src/exports/validators.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,mBAAmB,EAAE,eAAe,EAAE,MAAM,eAAe,CAAC"}
|
|
@@ -1,109 +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
|
-
"typeParams?": "Record<string, unknown>",
|
|
8
|
-
"typeRef?": "string"
|
|
9
|
-
}).narrow((col, ctx) => {
|
|
10
|
-
if (col.typeParams !== void 0 && col.typeRef !== void 0) {
|
|
11
|
-
return ctx.mustBe("a column with either typeParams or typeRef, not both");
|
|
12
|
-
}
|
|
13
|
-
return true;
|
|
14
|
-
});
|
|
15
|
-
var StorageTypeInstanceSchema = type.declare().type({
|
|
16
|
-
codecId: "string",
|
|
17
|
-
nativeType: "string",
|
|
18
|
-
typeParams: "Record<string, unknown>"
|
|
19
|
-
});
|
|
20
|
-
var PrimaryKeySchema = type.declare().type({
|
|
21
|
-
columns: type.string.array().readonly(),
|
|
22
|
-
"name?": "string"
|
|
23
|
-
});
|
|
24
|
-
var UniqueConstraintSchema = type.declare().type({
|
|
25
|
-
columns: type.string.array().readonly(),
|
|
26
|
-
"name?": "string"
|
|
27
|
-
});
|
|
28
|
-
var IndexSchema = type.declare().type({
|
|
29
|
-
columns: type.string.array().readonly(),
|
|
30
|
-
"name?": "string"
|
|
31
|
-
});
|
|
32
|
-
var ForeignKeyReferencesSchema = type.declare().type({
|
|
33
|
-
table: "string",
|
|
34
|
-
columns: type.string.array().readonly()
|
|
35
|
-
});
|
|
36
|
-
var ForeignKeySchema = type.declare().type({
|
|
37
|
-
columns: type.string.array().readonly(),
|
|
38
|
-
references: ForeignKeyReferencesSchema,
|
|
39
|
-
"name?": "string"
|
|
40
|
-
});
|
|
41
|
-
var StorageTableSchema = type.declare().type({
|
|
42
|
-
columns: type({ "[string]": StorageColumnSchema }),
|
|
43
|
-
"primaryKey?": PrimaryKeySchema,
|
|
44
|
-
uniques: UniqueConstraintSchema.array().readonly(),
|
|
45
|
-
indexes: IndexSchema.array().readonly(),
|
|
46
|
-
foreignKeys: ForeignKeySchema.array().readonly()
|
|
47
|
-
});
|
|
48
|
-
var StorageSchema = type.declare().type({
|
|
49
|
-
tables: type({ "[string]": StorageTableSchema }),
|
|
50
|
-
"types?": type({ "[string]": StorageTypeInstanceSchema })
|
|
51
|
-
});
|
|
52
|
-
var ModelFieldSchema = type.declare().type({
|
|
53
|
-
column: "string"
|
|
54
|
-
});
|
|
55
|
-
var ModelStorageSchema = type.declare().type({
|
|
56
|
-
table: "string"
|
|
57
|
-
});
|
|
58
|
-
var ModelSchema = type.declare().type({
|
|
59
|
-
storage: ModelStorageSchema,
|
|
60
|
-
fields: type({ "[string]": ModelFieldSchema }),
|
|
61
|
-
relations: type({ "[string]": "unknown" })
|
|
62
|
-
});
|
|
63
|
-
var SqlContractSchema = type({
|
|
64
|
-
"schemaVersion?": "'1'",
|
|
65
|
-
target: "string",
|
|
66
|
-
targetFamily: "'sql'",
|
|
67
|
-
coreHash: "string",
|
|
68
|
-
"profileHash?": "string",
|
|
69
|
-
"capabilities?": "Record<string, Record<string, boolean>>",
|
|
70
|
-
"extensionPacks?": "Record<string, unknown>",
|
|
71
|
-
"meta?": "Record<string, unknown>",
|
|
72
|
-
"sources?": "Record<string, unknown>",
|
|
73
|
-
models: type({ "[string]": ModelSchema }),
|
|
74
|
-
storage: StorageSchema
|
|
75
|
-
});
|
|
76
|
-
function validateStorage(value) {
|
|
77
|
-
const result = StorageSchema(value);
|
|
78
|
-
if (result instanceof type.errors) {
|
|
79
|
-
const messages = result.map((p) => p.message).join("; ");
|
|
80
|
-
throw new Error(`Storage validation failed: ${messages}`);
|
|
81
|
-
}
|
|
82
|
-
return result;
|
|
83
|
-
}
|
|
84
|
-
function validateModel(value) {
|
|
85
|
-
const result = ModelSchema(value);
|
|
86
|
-
if (result instanceof type.errors) {
|
|
87
|
-
const messages = result.map((p) => p.message).join("; ");
|
|
88
|
-
throw new Error(`Model validation failed: ${messages}`);
|
|
89
|
-
}
|
|
90
|
-
return result;
|
|
91
|
-
}
|
|
92
|
-
function validateSqlContract(value) {
|
|
93
|
-
const rawValue = value;
|
|
94
|
-
if (rawValue.targetFamily !== void 0 && rawValue.targetFamily !== "sql") {
|
|
95
|
-
throw new Error(`Unsupported target family: ${rawValue.targetFamily}`);
|
|
96
|
-
}
|
|
97
|
-
const contractResult = SqlContractSchema(value);
|
|
98
|
-
if (contractResult instanceof type.errors) {
|
|
99
|
-
const messages = contractResult.map((p) => p.message).join("; ");
|
|
100
|
-
throw new Error(`Contract structural validation failed: ${messages}`);
|
|
101
|
-
}
|
|
102
|
-
return contractResult;
|
|
103
|
-
}
|
|
104
|
-
export {
|
|
105
|
-
validateModel,
|
|
106
|
-
validateSqlContract,
|
|
107
|
-
validateStorage
|
|
108
|
-
};
|
|
109
|
-
//# sourceMappingURL=validators.js.map
|