@prisma-next/core-control-plane 0.3.0-dev.4 → 0.3.0-dev.6

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/dist/config-types.d.ts +68 -0
  2. package/dist/config-types.d.ts.map +1 -0
  3. package/dist/config-validation.d.ts +10 -0
  4. package/dist/config-validation.d.ts.map +1 -0
  5. package/dist/emission/canonicalization.d.ts +6 -0
  6. package/dist/emission/canonicalization.d.ts.map +1 -0
  7. package/dist/emission/emit.d.ts +5 -0
  8. package/dist/emission/emit.d.ts.map +1 -0
  9. package/dist/emission/hashing.d.ts +17 -0
  10. package/dist/emission/hashing.d.ts.map +1 -0
  11. package/dist/emission/types.d.ts +16 -0
  12. package/dist/emission/types.d.ts.map +1 -0
  13. package/dist/errors.d.ts +183 -0
  14. package/dist/errors.d.ts.map +1 -0
  15. package/dist/exports/config-types.d.ts +3 -70
  16. package/dist/exports/config-types.d.ts.map +1 -0
  17. package/dist/exports/config-validation.d.ts +2 -18
  18. package/dist/exports/config-validation.d.ts.map +1 -0
  19. package/dist/exports/emission.d.ts +5 -42
  20. package/dist/exports/emission.d.ts.map +1 -0
  21. package/dist/exports/errors.d.ts +3 -184
  22. package/dist/exports/errors.d.ts.map +1 -0
  23. package/dist/exports/schema-view.d.ts +2 -87
  24. package/dist/exports/schema-view.d.ts.map +1 -0
  25. package/dist/exports/types.d.ts +2 -589
  26. package/dist/exports/types.d.ts.map +1 -0
  27. package/dist/migrations.d.ts +190 -0
  28. package/dist/migrations.d.ts.map +1 -0
  29. package/dist/schema-view.d.ts +86 -0
  30. package/dist/schema-view.d.ts.map +1 -0
  31. package/dist/types.d.ts +400 -0
  32. package/dist/types.d.ts.map +1 -0
  33. package/package.json +7 -6
  34. package/src/config-types.ts +157 -0
  35. package/src/config-validation.ts +270 -0
  36. package/src/emission/canonicalization.ts +253 -0
  37. package/src/emission/emit.ts +118 -0
  38. package/src/emission/hashing.ts +57 -0
  39. package/src/emission/types.ts +17 -0
  40. package/src/errors.ts +426 -0
  41. package/src/exports/config-types.ts +5 -0
  42. package/src/exports/config-validation.ts +1 -0
  43. package/src/exports/emission.ts +6 -0
  44. package/src/exports/errors.ts +22 -0
  45. package/src/exports/schema-view.ts +1 -0
  46. package/src/exports/types.ts +37 -0
  47. package/src/migrations.ts +247 -0
  48. package/src/schema-view.ts +95 -0
  49. package/src/types.ts +512 -0
@@ -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 emit".',
95
+ regenerate: 'To regenerate, run: prisma-next 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
+ }
@@ -0,0 +1,17 @@
1
+ import type { TypesImportSpec } from '@prisma-next/contract/types';
2
+ import type { OperationRegistry } from '@prisma-next/operations';
3
+
4
+ export interface EmitOptions {
5
+ readonly outputDir: string;
6
+ readonly operationRegistry?: OperationRegistry;
7
+ readonly codecTypeImports?: ReadonlyArray<TypesImportSpec>;
8
+ readonly operationTypeImports?: ReadonlyArray<TypesImportSpec>;
9
+ readonly extensionIds?: ReadonlyArray<string>;
10
+ }
11
+
12
+ export interface EmitResult {
13
+ readonly contractJson: string;
14
+ readonly contractDts: string;
15
+ readonly coreHash: string;
16
+ readonly profileHash: string;
17
+ }