@prisma-next/core-control-plane 0.3.0-dev.2 → 0.3.0-dev.20
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 +29 -0
- package/dist/{chunk-U5RYT6PT.js → chunk-YT6YGR3N.js} +17 -6
- package/dist/chunk-YT6YGR3N.js.map +1 -0
- package/dist/config-types.d.ts +79 -0
- package/dist/config-types.d.ts.map +1 -0
- package/dist/config-validation.d.ts +10 -0
- package/dist/config-validation.d.ts.map +1 -0
- package/dist/emission/canonicalization.d.ts +6 -0
- package/dist/emission/canonicalization.d.ts.map +1 -0
- package/dist/emission/emit.d.ts +5 -0
- package/dist/emission/emit.d.ts.map +1 -0
- package/dist/emission/hashing.d.ts +17 -0
- package/dist/emission/hashing.d.ts.map +1 -0
- package/dist/emission/types.d.ts +16 -0
- package/dist/emission/types.d.ts.map +1 -0
- package/dist/errors.d.ts +188 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/exports/config-types.d.ts +3 -70
- package/dist/exports/config-types.d.ts.map +1 -0
- package/dist/exports/config-types.js.map +1 -1
- package/dist/exports/config-validation.d.ts +2 -18
- package/dist/exports/config-validation.d.ts.map +1 -0
- package/dist/exports/config-validation.js +1 -1
- package/dist/exports/emission.d.ts +5 -42
- package/dist/exports/emission.d.ts.map +1 -0
- package/dist/exports/emission.js +2 -2
- package/dist/exports/emission.js.map +1 -1
- package/dist/exports/errors.d.ts +3 -184
- package/dist/exports/errors.d.ts.map +1 -0
- package/dist/exports/errors.js +3 -3
- package/dist/exports/schema-view.d.ts +2 -87
- package/dist/exports/schema-view.d.ts.map +1 -0
- package/dist/exports/stack.d.ts +2 -0
- package/dist/exports/stack.d.ts.map +1 -0
- package/dist/exports/stack.js +13 -0
- package/dist/exports/stack.js.map +1 -0
- package/dist/exports/types.d.ts +2 -589
- package/dist/exports/types.d.ts.map +1 -0
- package/dist/migrations.d.ts +190 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/schema-view.d.ts +86 -0
- package/dist/schema-view.d.ts.map +1 -0
- package/dist/stack.d.ts +25 -0
- package/dist/stack.d.ts.map +1 -0
- package/dist/types.d.ts +416 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +17 -13
- package/src/config-types.ts +174 -0
- package/src/config-validation.ts +270 -0
- package/src/emission/canonicalization.ts +253 -0
- package/src/emission/emit.ts +118 -0
- package/src/emission/hashing.ts +57 -0
- package/src/emission/types.ts +17 -0
- package/src/errors.ts +445 -0
- package/src/exports/config-types.ts +5 -0
- package/src/exports/config-validation.ts +1 -0
- package/src/exports/emission.ts +6 -0
- package/src/exports/errors.ts +22 -0
- package/src/exports/schema-view.ts +1 -0
- package/src/exports/stack.ts +1 -0
- package/src/exports/types.ts +38 -0
- package/src/migrations.ts +247 -0
- package/src/schema-view.ts +95 -0
- package/src/stack.ts +38 -0
- package/src/types.ts +530 -0
- package/dist/chunk-U5RYT6PT.js.map +0 -1
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import type { PrismaNextConfig } from './config-types';
|
|
2
|
+
import { errorConfigValidation } from './errors';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Validates that the config has the required structure.
|
|
6
|
+
* This is pure validation logic with no file I/O or CLI awareness.
|
|
7
|
+
*
|
|
8
|
+
* @param config - Config object to validate
|
|
9
|
+
* @throws CliStructuredError if config structure is invalid
|
|
10
|
+
*/
|
|
11
|
+
export function validateConfig(config: unknown): asserts config is PrismaNextConfig {
|
|
12
|
+
if (!config || typeof config !== 'object') {
|
|
13
|
+
throw errorConfigValidation('object', {
|
|
14
|
+
why: 'Config must be an object',
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const configObj = config as Record<string, unknown>;
|
|
19
|
+
|
|
20
|
+
if (!configObj['family']) {
|
|
21
|
+
throw errorConfigValidation('family');
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
if (!configObj['target']) {
|
|
25
|
+
throw errorConfigValidation('target');
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (!configObj['adapter']) {
|
|
29
|
+
throw errorConfigValidation('adapter');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Validate family descriptor
|
|
33
|
+
const family = configObj['family'] as Record<string, unknown>;
|
|
34
|
+
if (family['kind'] !== 'family') {
|
|
35
|
+
throw errorConfigValidation('family.kind', {
|
|
36
|
+
why: 'Config.family must have kind: "family"',
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (typeof family['familyId'] !== 'string') {
|
|
40
|
+
throw errorConfigValidation('family.familyId', {
|
|
41
|
+
why: 'Config.family must have familyId: string',
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (typeof family['version'] !== 'string') {
|
|
45
|
+
throw errorConfigValidation('family.version', {
|
|
46
|
+
why: 'Config.family must have version: string',
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
if (!family['hook'] || typeof family['hook'] !== 'object') {
|
|
50
|
+
throw errorConfigValidation('family.hook', {
|
|
51
|
+
why: 'Config.family must have hook: TargetFamilyHook',
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
if (typeof family['create'] !== 'function') {
|
|
55
|
+
throw errorConfigValidation('family.create', {
|
|
56
|
+
why: 'Config.family must have create: function',
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const familyId = family['familyId'] as string;
|
|
61
|
+
|
|
62
|
+
// Validate target descriptor
|
|
63
|
+
const target = configObj['target'] as Record<string, unknown>;
|
|
64
|
+
if (target['kind'] !== 'target') {
|
|
65
|
+
throw errorConfigValidation('target.kind', {
|
|
66
|
+
why: 'Config.target must have kind: "target"',
|
|
67
|
+
});
|
|
68
|
+
}
|
|
69
|
+
if (typeof target['id'] !== 'string') {
|
|
70
|
+
throw errorConfigValidation('target.id', {
|
|
71
|
+
why: 'Config.target must have id: string',
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
if (typeof target['familyId'] !== 'string') {
|
|
75
|
+
throw errorConfigValidation('target.familyId', {
|
|
76
|
+
why: 'Config.target must have familyId: string',
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (typeof target['version'] !== 'string') {
|
|
80
|
+
throw errorConfigValidation('target.version', {
|
|
81
|
+
why: 'Config.target must have version: string',
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (target['familyId'] !== familyId) {
|
|
85
|
+
throw errorConfigValidation('target.familyId', {
|
|
86
|
+
why: `Config.target.familyId must match Config.family.familyId (expected: ${familyId}, got: ${target['familyId']})`,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
if (typeof target['targetId'] !== 'string') {
|
|
90
|
+
throw errorConfigValidation('target.targetId', {
|
|
91
|
+
why: 'Config.target must have targetId: string',
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
if (typeof target['create'] !== 'function') {
|
|
95
|
+
throw errorConfigValidation('target.create', {
|
|
96
|
+
why: 'Config.target must have create: function',
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const expectedTargetId = target['targetId'] as string;
|
|
100
|
+
|
|
101
|
+
// Validate adapter descriptor
|
|
102
|
+
const adapter = configObj['adapter'] as Record<string, unknown>;
|
|
103
|
+
if (adapter['kind'] !== 'adapter') {
|
|
104
|
+
throw errorConfigValidation('adapter.kind', {
|
|
105
|
+
why: 'Config.adapter must have kind: "adapter"',
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
if (typeof adapter['id'] !== 'string') {
|
|
109
|
+
throw errorConfigValidation('adapter.id', {
|
|
110
|
+
why: 'Config.adapter must have id: string',
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
if (typeof adapter['familyId'] !== 'string') {
|
|
114
|
+
throw errorConfigValidation('adapter.familyId', {
|
|
115
|
+
why: 'Config.adapter must have familyId: string',
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
if (typeof adapter['version'] !== 'string') {
|
|
119
|
+
throw errorConfigValidation('adapter.version', {
|
|
120
|
+
why: 'Config.adapter must have version: string',
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
if (adapter['familyId'] !== familyId) {
|
|
124
|
+
throw errorConfigValidation('adapter.familyId', {
|
|
125
|
+
why: `Config.adapter.familyId must match Config.family.familyId (expected: ${familyId}, got: ${adapter['familyId']})`,
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
if (typeof adapter['targetId'] !== 'string') {
|
|
129
|
+
throw errorConfigValidation('adapter.targetId', {
|
|
130
|
+
why: 'Config.adapter must have targetId: string',
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (adapter['targetId'] !== expectedTargetId) {
|
|
134
|
+
throw errorConfigValidation('adapter.targetId', {
|
|
135
|
+
why: `Config.adapter.targetId must match Config.target.targetId (expected: ${expectedTargetId}, got: ${adapter['targetId']})`,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
if (typeof adapter['create'] !== 'function') {
|
|
139
|
+
throw errorConfigValidation('adapter.create', {
|
|
140
|
+
why: 'Config.adapter must have create: function',
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Validate extensions array if present
|
|
145
|
+
if (configObj['extensions'] !== undefined) {
|
|
146
|
+
if (!Array.isArray(configObj['extensions'])) {
|
|
147
|
+
throw errorConfigValidation('extensions', {
|
|
148
|
+
why: 'Config.extensions must be an array',
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
for (const ext of configObj['extensions']) {
|
|
152
|
+
if (!ext || typeof ext !== 'object') {
|
|
153
|
+
throw errorConfigValidation('extensions[]', {
|
|
154
|
+
why: 'Config.extensions must contain ExtensionDescriptor objects',
|
|
155
|
+
});
|
|
156
|
+
}
|
|
157
|
+
const extObj = ext as Record<string, unknown>;
|
|
158
|
+
if (extObj['kind'] !== 'extension') {
|
|
159
|
+
throw errorConfigValidation('extensions[].kind', {
|
|
160
|
+
why: 'Config.extensions items must have kind: "extension"',
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
if (typeof extObj['id'] !== 'string') {
|
|
164
|
+
throw errorConfigValidation('extensions[].id', {
|
|
165
|
+
why: 'Config.extensions items must have id: string',
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
if (typeof extObj['familyId'] !== 'string') {
|
|
169
|
+
throw errorConfigValidation('extensions[].familyId', {
|
|
170
|
+
why: 'Config.extensions items must have familyId: string',
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
if (typeof extObj['version'] !== 'string') {
|
|
174
|
+
throw errorConfigValidation('extensions[].version', {
|
|
175
|
+
why: 'Config.extensions items must have version: string',
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
if (extObj['familyId'] !== familyId) {
|
|
179
|
+
throw errorConfigValidation('extensions[].familyId', {
|
|
180
|
+
why: `Config.extensions[].familyId must match Config.family.familyId (expected: ${familyId}, got: ${extObj['familyId']})`,
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
if (typeof extObj['targetId'] !== 'string') {
|
|
184
|
+
throw errorConfigValidation('extensions[].targetId', {
|
|
185
|
+
why: 'Config.extensions items must have targetId: string',
|
|
186
|
+
});
|
|
187
|
+
}
|
|
188
|
+
if (extObj['targetId'] !== expectedTargetId) {
|
|
189
|
+
throw errorConfigValidation('extensions[].targetId', {
|
|
190
|
+
why: `Config.extensions[].targetId must match Config.target.targetId (expected: ${expectedTargetId}, got: ${extObj['targetId']})`,
|
|
191
|
+
});
|
|
192
|
+
}
|
|
193
|
+
if (typeof extObj['create'] !== 'function') {
|
|
194
|
+
throw errorConfigValidation('extensions[].create', {
|
|
195
|
+
why: 'Config.extensions items must have create: function',
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
// Validate driver descriptor if present
|
|
202
|
+
if (configObj['driver'] !== undefined) {
|
|
203
|
+
const driver = configObj['driver'] as Record<string, unknown>;
|
|
204
|
+
if (driver['kind'] !== 'driver') {
|
|
205
|
+
throw errorConfigValidation('driver.kind', {
|
|
206
|
+
why: 'Config.driver must have kind: "driver"',
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
if (typeof driver['id'] !== 'string') {
|
|
210
|
+
throw errorConfigValidation('driver.id', {
|
|
211
|
+
why: 'Config.driver must have id: string',
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
if (typeof driver['version'] !== 'string') {
|
|
215
|
+
throw errorConfigValidation('driver.version', {
|
|
216
|
+
why: 'Config.driver must have version: string',
|
|
217
|
+
});
|
|
218
|
+
}
|
|
219
|
+
if (typeof driver['familyId'] !== 'string') {
|
|
220
|
+
throw errorConfigValidation('driver.familyId', {
|
|
221
|
+
why: 'Config.driver must have familyId: string',
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
if (driver['familyId'] !== familyId) {
|
|
225
|
+
throw errorConfigValidation('driver.familyId', {
|
|
226
|
+
why: `Config.driver.familyId must match Config.family.familyId (expected: ${familyId}, got: ${driver['familyId']})`,
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
if (typeof driver['targetId'] !== 'string') {
|
|
230
|
+
throw errorConfigValidation('driver.targetId', {
|
|
231
|
+
why: 'Config.driver must have targetId: string',
|
|
232
|
+
});
|
|
233
|
+
}
|
|
234
|
+
if (driver['targetId'] !== expectedTargetId) {
|
|
235
|
+
throw errorConfigValidation('driver.targetId', {
|
|
236
|
+
why: `Config.driver.targetId must match Config.target.targetId (expected: ${expectedTargetId}, got: ${driver['targetId']})`,
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
if (typeof driver['create'] !== 'function') {
|
|
240
|
+
throw errorConfigValidation('driver.create', {
|
|
241
|
+
why: 'Config.driver must have create: function',
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
// Validate contract config if present (structure validation - defineConfig() handles normalization)
|
|
247
|
+
if (configObj['contract'] !== undefined) {
|
|
248
|
+
const contract = configObj['contract'] as Record<string, unknown>;
|
|
249
|
+
if (!contract || typeof contract !== 'object') {
|
|
250
|
+
throw errorConfigValidation('contract', {
|
|
251
|
+
why: 'Config.contract must be an object',
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
if (!('source' in contract)) {
|
|
255
|
+
throw errorConfigValidation('contract.source', {
|
|
256
|
+
why: 'Config.contract.source is required when contract is provided',
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
if (contract['output'] !== undefined && typeof contract['output'] !== 'string') {
|
|
260
|
+
throw errorConfigValidation('contract.output', {
|
|
261
|
+
why: 'Config.contract.output must be a string when provided',
|
|
262
|
+
});
|
|
263
|
+
}
|
|
264
|
+
if (contract['types'] !== undefined && typeof contract['types'] !== 'string') {
|
|
265
|
+
throw errorConfigValidation('contract.types', {
|
|
266
|
+
why: 'Config.contract.types must be a string when provided',
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import type { ContractIR } from '@prisma-next/contract/ir';
|
|
2
|
+
import { isArrayEqual } from '@prisma-next/utils/array-equal';
|
|
3
|
+
|
|
4
|
+
type NormalizedContract = {
|
|
5
|
+
schemaVersion: string;
|
|
6
|
+
targetFamily: string;
|
|
7
|
+
target: string;
|
|
8
|
+
coreHash?: string;
|
|
9
|
+
profileHash?: string;
|
|
10
|
+
models: Record<string, unknown>;
|
|
11
|
+
relations: Record<string, unknown>;
|
|
12
|
+
storage: Record<string, unknown>;
|
|
13
|
+
extensionPacks: Record<string, unknown>;
|
|
14
|
+
capabilities: Record<string, Record<string, boolean>>;
|
|
15
|
+
meta: Record<string, unknown>;
|
|
16
|
+
sources: Record<string, unknown>;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const TOP_LEVEL_ORDER = [
|
|
20
|
+
'schemaVersion',
|
|
21
|
+
'canonicalVersion',
|
|
22
|
+
'targetFamily',
|
|
23
|
+
'target',
|
|
24
|
+
'coreHash',
|
|
25
|
+
'profileHash',
|
|
26
|
+
'models',
|
|
27
|
+
'storage',
|
|
28
|
+
'capabilities',
|
|
29
|
+
'extensionPacks',
|
|
30
|
+
'meta',
|
|
31
|
+
'sources',
|
|
32
|
+
] as const;
|
|
33
|
+
|
|
34
|
+
function isDefaultValue(value: unknown): boolean {
|
|
35
|
+
if (value === false) return true;
|
|
36
|
+
if (value === null) return false;
|
|
37
|
+
if (Array.isArray(value) && value.length === 0) return true;
|
|
38
|
+
if (typeof value === 'object' && value !== null) {
|
|
39
|
+
const keys = Object.keys(value);
|
|
40
|
+
return keys.length === 0;
|
|
41
|
+
}
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function omitDefaults(obj: unknown, path: readonly string[]): unknown {
|
|
46
|
+
if (obj === null || typeof obj !== 'object') {
|
|
47
|
+
return obj;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
if (Array.isArray(obj)) {
|
|
51
|
+
return obj.map((item) => omitDefaults(item, path));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const result: Record<string, unknown> = {};
|
|
55
|
+
|
|
56
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
57
|
+
const currentPath = [...path, key];
|
|
58
|
+
|
|
59
|
+
// Exclude metadata fields from canonicalization
|
|
60
|
+
if (key === '_generated') {
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
if (key === 'nullable' && value === false) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
if (key === 'generated' && value === false) {
|
|
69
|
+
continue;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (isDefaultValue(value)) {
|
|
73
|
+
const isRequiredModels = isArrayEqual(currentPath, ['models']);
|
|
74
|
+
const isRequiredTables = isArrayEqual(currentPath, ['storage', 'tables']);
|
|
75
|
+
const isRequiredRelations = isArrayEqual(currentPath, ['relations']);
|
|
76
|
+
const isRequiredExtensionPacks = isArrayEqual(currentPath, ['extensionPacks']);
|
|
77
|
+
const isRequiredCapabilities = isArrayEqual(currentPath, ['capabilities']);
|
|
78
|
+
const isRequiredMeta = isArrayEqual(currentPath, ['meta']);
|
|
79
|
+
const isRequiredSources = isArrayEqual(currentPath, ['sources']);
|
|
80
|
+
const isExtensionNamespace = currentPath.length === 2 && currentPath[0] === 'extensionPacks';
|
|
81
|
+
const isModelRelations =
|
|
82
|
+
currentPath.length === 3 &&
|
|
83
|
+
isArrayEqual([currentPath[0], currentPath[2]], ['models', 'relations']);
|
|
84
|
+
const isTableUniques =
|
|
85
|
+
currentPath.length === 4 &&
|
|
86
|
+
isArrayEqual(
|
|
87
|
+
[currentPath[0], currentPath[1], currentPath[3]],
|
|
88
|
+
['storage', 'tables', 'uniques'],
|
|
89
|
+
);
|
|
90
|
+
const isTableIndexes =
|
|
91
|
+
currentPath.length === 4 &&
|
|
92
|
+
isArrayEqual(
|
|
93
|
+
[currentPath[0], currentPath[1], currentPath[3]],
|
|
94
|
+
['storage', 'tables', 'indexes'],
|
|
95
|
+
);
|
|
96
|
+
const isTableForeignKeys =
|
|
97
|
+
currentPath.length === 4 &&
|
|
98
|
+
isArrayEqual(
|
|
99
|
+
[currentPath[0], currentPath[1], currentPath[3]],
|
|
100
|
+
['storage', 'tables', 'foreignKeys'],
|
|
101
|
+
);
|
|
102
|
+
|
|
103
|
+
if (
|
|
104
|
+
!isRequiredModels &&
|
|
105
|
+
!isRequiredTables &&
|
|
106
|
+
!isRequiredRelations &&
|
|
107
|
+
!isRequiredExtensionPacks &&
|
|
108
|
+
!isRequiredCapabilities &&
|
|
109
|
+
!isRequiredMeta &&
|
|
110
|
+
!isRequiredSources &&
|
|
111
|
+
!isExtensionNamespace &&
|
|
112
|
+
!isModelRelations &&
|
|
113
|
+
!isTableUniques &&
|
|
114
|
+
!isTableIndexes &&
|
|
115
|
+
!isTableForeignKeys
|
|
116
|
+
) {
|
|
117
|
+
continue;
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
result[key] = omitDefaults(value, currentPath);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return result;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
function sortObjectKeys(obj: unknown): unknown {
|
|
128
|
+
if (obj === null || typeof obj !== 'object') {
|
|
129
|
+
return obj;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
if (Array.isArray(obj)) {
|
|
133
|
+
return obj.map((item) => sortObjectKeys(item));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const sorted: Record<string, unknown> = {};
|
|
137
|
+
const keys = Object.keys(obj).sort();
|
|
138
|
+
for (const key of keys) {
|
|
139
|
+
sorted[key] = sortObjectKeys((obj as Record<string, unknown>)[key]);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
return sorted;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
type StorageObject = {
|
|
146
|
+
tables?: Record<string, unknown>;
|
|
147
|
+
[key: string]: unknown;
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
type TableObject = {
|
|
151
|
+
indexes?: unknown[];
|
|
152
|
+
uniques?: unknown[];
|
|
153
|
+
[key: string]: unknown;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
function sortIndexesAndUniques(storage: unknown): unknown {
|
|
157
|
+
if (!storage || typeof storage !== 'object') {
|
|
158
|
+
return storage;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const storageObj = storage as StorageObject;
|
|
162
|
+
if (!storageObj.tables || typeof storageObj.tables !== 'object') {
|
|
163
|
+
return storage;
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const tables = storageObj.tables;
|
|
167
|
+
const result: StorageObject = { ...storageObj };
|
|
168
|
+
|
|
169
|
+
result.tables = {};
|
|
170
|
+
// Sort table names to ensure deterministic ordering
|
|
171
|
+
const sortedTableNames = Object.keys(tables).sort();
|
|
172
|
+
for (const tableName of sortedTableNames) {
|
|
173
|
+
const table = tables[tableName];
|
|
174
|
+
if (!table || typeof table !== 'object') {
|
|
175
|
+
result.tables[tableName] = table;
|
|
176
|
+
continue;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const tableObj = table as TableObject;
|
|
180
|
+
const sortedTable: TableObject = { ...tableObj };
|
|
181
|
+
|
|
182
|
+
if (Array.isArray(tableObj.indexes)) {
|
|
183
|
+
sortedTable.indexes = [...tableObj.indexes].sort((a, b) => {
|
|
184
|
+
const nameA = (a as { name?: string })?.name || '';
|
|
185
|
+
const nameB = (b as { name?: string })?.name || '';
|
|
186
|
+
return nameA.localeCompare(nameB);
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (Array.isArray(tableObj.uniques)) {
|
|
191
|
+
sortedTable.uniques = [...tableObj.uniques].sort((a, b) => {
|
|
192
|
+
const nameA = (a as { name?: string })?.name || '';
|
|
193
|
+
const nameB = (b as { name?: string })?.name || '';
|
|
194
|
+
return nameA.localeCompare(nameB);
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
result.tables[tableName] = sortedTable;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
return result;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
function orderTopLevel(obj: Record<string, unknown>): Record<string, unknown> {
|
|
205
|
+
const ordered: Record<string, unknown> = {};
|
|
206
|
+
const remaining = new Set(Object.keys(obj));
|
|
207
|
+
|
|
208
|
+
for (const key of TOP_LEVEL_ORDER) {
|
|
209
|
+
if (remaining.has(key)) {
|
|
210
|
+
ordered[key] = obj[key];
|
|
211
|
+
remaining.delete(key);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
for (const key of Array.from(remaining).sort()) {
|
|
216
|
+
ordered[key] = obj[key];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
return ordered;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export function canonicalizeContract(
|
|
223
|
+
ir: ContractIR & { coreHash?: string; profileHash?: string },
|
|
224
|
+
): string {
|
|
225
|
+
const normalized: NormalizedContract = {
|
|
226
|
+
schemaVersion: ir.schemaVersion,
|
|
227
|
+
targetFamily: ir.targetFamily,
|
|
228
|
+
target: ir.target,
|
|
229
|
+
models: ir.models,
|
|
230
|
+
relations: ir.relations,
|
|
231
|
+
storage: ir.storage,
|
|
232
|
+
extensionPacks: ir.extensionPacks,
|
|
233
|
+
capabilities: ir.capabilities,
|
|
234
|
+
meta: ir.meta,
|
|
235
|
+
sources: ir.sources,
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
if (ir.coreHash !== undefined) {
|
|
239
|
+
normalized.coreHash = ir.coreHash;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
if (ir.profileHash !== undefined) {
|
|
243
|
+
normalized.profileHash = ir.profileHash;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const withDefaultsOmitted = omitDefaults(normalized, []) as NormalizedContract;
|
|
247
|
+
const withSortedIndexes = sortIndexesAndUniques(withDefaultsOmitted.storage);
|
|
248
|
+
const withSortedStorage = { ...withDefaultsOmitted, storage: withSortedIndexes };
|
|
249
|
+
const withSortedKeys = sortObjectKeys(withSortedStorage) as Record<string, unknown>;
|
|
250
|
+
const withOrderedTopLevel = orderTopLevel(withSortedKeys);
|
|
251
|
+
|
|
252
|
+
return JSON.stringify(withOrderedTopLevel, null, 2);
|
|
253
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { ContractIR } from '@prisma-next/contract/ir';
|
|
2
|
+
import type { TargetFamilyHook, ValidationContext } from '@prisma-next/contract/types';
|
|
3
|
+
import { format } from 'prettier';
|
|
4
|
+
import { canonicalizeContract } from './canonicalization';
|
|
5
|
+
import { computeCoreHash, computeProfileHash } from './hashing';
|
|
6
|
+
import type { EmitOptions, EmitResult } from './types';
|
|
7
|
+
|
|
8
|
+
function validateCoreStructure(ir: ContractIR): void {
|
|
9
|
+
if (!ir.targetFamily) {
|
|
10
|
+
throw new Error('ContractIR must have targetFamily');
|
|
11
|
+
}
|
|
12
|
+
if (!ir.target) {
|
|
13
|
+
throw new Error('ContractIR must have target');
|
|
14
|
+
}
|
|
15
|
+
if (!ir.schemaVersion) {
|
|
16
|
+
throw new Error('ContractIR must have schemaVersion');
|
|
17
|
+
}
|
|
18
|
+
if (!ir.models || typeof ir.models !== 'object') {
|
|
19
|
+
throw new Error('ContractIR must have models');
|
|
20
|
+
}
|
|
21
|
+
if (!ir.storage || typeof ir.storage !== 'object') {
|
|
22
|
+
throw new Error('ContractIR must have storage');
|
|
23
|
+
}
|
|
24
|
+
if (!ir.relations || typeof ir.relations !== 'object') {
|
|
25
|
+
throw new Error('ContractIR must have relations');
|
|
26
|
+
}
|
|
27
|
+
if (!ir.extensionPacks || typeof ir.extensionPacks !== 'object') {
|
|
28
|
+
throw new Error('ContractIR must have extensionPacks');
|
|
29
|
+
}
|
|
30
|
+
if (!ir.capabilities || typeof ir.capabilities !== 'object') {
|
|
31
|
+
throw new Error('ContractIR must have capabilities');
|
|
32
|
+
}
|
|
33
|
+
if (!ir.meta || typeof ir.meta !== 'object') {
|
|
34
|
+
throw new Error('ContractIR must have meta');
|
|
35
|
+
}
|
|
36
|
+
if (!ir.sources || typeof ir.sources !== 'object') {
|
|
37
|
+
throw new Error('ContractIR must have sources');
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function emit(
|
|
42
|
+
ir: ContractIR,
|
|
43
|
+
options: EmitOptions,
|
|
44
|
+
targetFamily: TargetFamilyHook,
|
|
45
|
+
): Promise<EmitResult> {
|
|
46
|
+
const { operationRegistry, codecTypeImports, operationTypeImports, extensionIds } = options;
|
|
47
|
+
|
|
48
|
+
validateCoreStructure(ir);
|
|
49
|
+
|
|
50
|
+
const ctx: ValidationContext = {
|
|
51
|
+
...(operationRegistry ? { operationRegistry } : {}),
|
|
52
|
+
...(codecTypeImports ? { codecTypeImports } : {}),
|
|
53
|
+
...(operationTypeImports ? { operationTypeImports } : {}),
|
|
54
|
+
...(extensionIds ? { extensionIds } : {}),
|
|
55
|
+
};
|
|
56
|
+
targetFamily.validateTypes(ir, ctx);
|
|
57
|
+
|
|
58
|
+
targetFamily.validateStructure(ir);
|
|
59
|
+
|
|
60
|
+
const contractJson = {
|
|
61
|
+
schemaVersion: ir.schemaVersion,
|
|
62
|
+
targetFamily: ir.targetFamily,
|
|
63
|
+
target: ir.target,
|
|
64
|
+
models: ir.models,
|
|
65
|
+
relations: ir.relations,
|
|
66
|
+
storage: ir.storage,
|
|
67
|
+
extensionPacks: ir.extensionPacks,
|
|
68
|
+
capabilities: ir.capabilities,
|
|
69
|
+
meta: ir.meta,
|
|
70
|
+
sources: ir.sources,
|
|
71
|
+
} as const;
|
|
72
|
+
|
|
73
|
+
const coreHash = computeCoreHash(contractJson);
|
|
74
|
+
const profileHash = computeProfileHash(contractJson);
|
|
75
|
+
|
|
76
|
+
const contractWithHashes: ContractIR & { coreHash?: string; profileHash?: string } = {
|
|
77
|
+
...ir,
|
|
78
|
+
schemaVersion: contractJson.schemaVersion,
|
|
79
|
+
coreHash,
|
|
80
|
+
profileHash,
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
// Add _generated metadata to indicate this is a generated artifact
|
|
84
|
+
// This ensures consistency between CLI emit and programmatic emit
|
|
85
|
+
// Always add/update _generated with standard content for consistency
|
|
86
|
+
const contractJsonObj = JSON.parse(canonicalizeContract(contractWithHashes)) as Record<
|
|
87
|
+
string,
|
|
88
|
+
unknown
|
|
89
|
+
>;
|
|
90
|
+
const contractJsonWithMeta = {
|
|
91
|
+
...contractJsonObj,
|
|
92
|
+
_generated: {
|
|
93
|
+
warning: '⚠️ GENERATED FILE - DO NOT EDIT',
|
|
94
|
+
message: 'This file is automatically generated by "prisma-next contract emit".',
|
|
95
|
+
regenerate: 'To regenerate, run: prisma-next contract emit',
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
const contractJsonString = JSON.stringify(contractJsonWithMeta, null, 2);
|
|
99
|
+
|
|
100
|
+
const contractDtsRaw = targetFamily.generateContractTypes(
|
|
101
|
+
ir,
|
|
102
|
+
codecTypeImports ?? [],
|
|
103
|
+
operationTypeImports ?? [],
|
|
104
|
+
);
|
|
105
|
+
const contractDts = await format(contractDtsRaw, {
|
|
106
|
+
parser: 'typescript',
|
|
107
|
+
singleQuote: true,
|
|
108
|
+
semi: true,
|
|
109
|
+
printWidth: 100,
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
contractJson: contractJsonString,
|
|
114
|
+
contractDts,
|
|
115
|
+
coreHash,
|
|
116
|
+
profileHash,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
import { createHash } from 'node:crypto';
|
|
2
|
+
import type { ContractIR } from '@prisma-next/contract/ir';
|
|
3
|
+
import { canonicalizeContract } from './canonicalization';
|
|
4
|
+
|
|
5
|
+
type ContractInput = {
|
|
6
|
+
schemaVersion: string;
|
|
7
|
+
targetFamily: string;
|
|
8
|
+
target: string;
|
|
9
|
+
models: Record<string, unknown>;
|
|
10
|
+
relations: Record<string, unknown>;
|
|
11
|
+
storage: Record<string, unknown>;
|
|
12
|
+
extensionPacks: Record<string, unknown>;
|
|
13
|
+
sources: Record<string, unknown>;
|
|
14
|
+
capabilities: Record<string, Record<string, boolean>>;
|
|
15
|
+
meta: Record<string, unknown>;
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function computeHash(content: string): string {
|
|
20
|
+
const hash = createHash('sha256');
|
|
21
|
+
hash.update(content);
|
|
22
|
+
return `sha256:${hash.digest('hex')}`;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export function computeCoreHash(contract: ContractInput): string {
|
|
26
|
+
const coreContract: ContractIR = {
|
|
27
|
+
schemaVersion: contract.schemaVersion,
|
|
28
|
+
targetFamily: contract.targetFamily,
|
|
29
|
+
target: contract.target,
|
|
30
|
+
models: contract.models,
|
|
31
|
+
relations: contract.relations,
|
|
32
|
+
storage: contract.storage,
|
|
33
|
+
extensionPacks: contract.extensionPacks,
|
|
34
|
+
sources: contract.sources,
|
|
35
|
+
capabilities: contract.capabilities,
|
|
36
|
+
meta: contract.meta,
|
|
37
|
+
};
|
|
38
|
+
const canonical = canonicalizeContract(coreContract);
|
|
39
|
+
return computeHash(canonical);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function computeProfileHash(contract: ContractInput): string {
|
|
43
|
+
const profileContract: ContractIR = {
|
|
44
|
+
schemaVersion: contract.schemaVersion,
|
|
45
|
+
targetFamily: contract.targetFamily,
|
|
46
|
+
target: contract.target,
|
|
47
|
+
models: {},
|
|
48
|
+
relations: {},
|
|
49
|
+
storage: {},
|
|
50
|
+
extensionPacks: {},
|
|
51
|
+
capabilities: contract.capabilities,
|
|
52
|
+
meta: {},
|
|
53
|
+
sources: {},
|
|
54
|
+
};
|
|
55
|
+
const canonical = canonicalizeContract(profileContract);
|
|
56
|
+
return computeHash(canonical);
|
|
57
|
+
}
|