@prisma-next/emitter 0.3.0-dev.13 → 0.3.0-dev.130
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/LICENSE +201 -0
- package/README.md +13 -6
- package/dist/domain-type-generation.d.mts +18 -0
- package/dist/domain-type-generation.d.mts.map +1 -0
- package/dist/domain-type-generation.mjs +74 -0
- package/dist/domain-type-generation.mjs.map +1 -0
- package/dist/exports/index.d.mts +4 -0
- package/dist/exports/index.mjs +4 -0
- package/dist/test/{utils.d.ts → utils.d.mts} +10 -5
- package/dist/test/utils.d.mts.map +1 -0
- package/dist/test/utils.mjs +59 -0
- package/dist/test/utils.mjs.map +1 -0
- package/package.json +24 -14
- package/src/domain-type-generation.ts +126 -0
- package/src/exports/index.ts +10 -0
- package/test/canonicalization.test.ts +188 -5
- package/test/domain-type-generation.test.ts +271 -0
- package/test/emitter.integration.test.ts +65 -61
- package/test/emitter.roundtrip.test.ts +4 -4
- package/test/emitter.test.ts +119 -33
- package/test/factories.test.ts +10 -10
- package/test/hashing.test.ts +5 -5
- package/test/utils.ts +9 -9
- package/dist/exports/index.js +0 -6
- package/dist/exports/index.js.map +0 -1
- package/dist/src/exports/index.d.ts +0 -4
- package/dist/src/exports/index.d.ts.map +0 -1
- package/dist/src/target-family.d.ts +0 -2
- package/dist/src/target-family.d.ts.map +0 -1
- package/dist/test/utils.d.ts.map +0 -1
- package/dist/test/utils.js +0 -78
- package/dist/test/utils.js.map +0 -1
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import type { TypesImportSpec } from '@prisma-next/contract/types';
|
|
2
|
+
|
|
3
|
+
export function serializeValue(value: unknown): string {
|
|
4
|
+
if (value === null) {
|
|
5
|
+
return 'null';
|
|
6
|
+
}
|
|
7
|
+
if (value === undefined) {
|
|
8
|
+
return 'undefined';
|
|
9
|
+
}
|
|
10
|
+
if (typeof value === 'string') {
|
|
11
|
+
const escaped = value.replace(/\\/g, '\\\\').replace(/'/g, "\\'");
|
|
12
|
+
return `'${escaped}'`;
|
|
13
|
+
}
|
|
14
|
+
if (typeof value === 'number' || typeof value === 'boolean') {
|
|
15
|
+
return String(value);
|
|
16
|
+
}
|
|
17
|
+
if (typeof value === 'bigint') {
|
|
18
|
+
return `${value}n`;
|
|
19
|
+
}
|
|
20
|
+
if (Array.isArray(value)) {
|
|
21
|
+
const items = value.map((v) => serializeValue(v)).join(', ');
|
|
22
|
+
return `readonly [${items}]`;
|
|
23
|
+
}
|
|
24
|
+
if (typeof value === 'object') {
|
|
25
|
+
const entries: string[] = [];
|
|
26
|
+
for (const [k, v] of Object.entries(value)) {
|
|
27
|
+
entries.push(`readonly ${serializeObjectKey(k)}: ${serializeValue(v)}`);
|
|
28
|
+
}
|
|
29
|
+
return `{ ${entries.join('; ')} }`;
|
|
30
|
+
}
|
|
31
|
+
return 'unknown';
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function serializeObjectKey(key: string): string {
|
|
35
|
+
if (/^[$A-Z_a-z][$\w]*$/.test(key)) {
|
|
36
|
+
return key;
|
|
37
|
+
}
|
|
38
|
+
return serializeValue(key);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function generateRootsType(roots: Record<string, string> | undefined): string {
|
|
42
|
+
if (!roots || Object.keys(roots).length === 0) {
|
|
43
|
+
return 'Record<string, string>';
|
|
44
|
+
}
|
|
45
|
+
const entries = Object.entries(roots)
|
|
46
|
+
.map(([key, value]) => `readonly ${serializeObjectKey(key)}: ${serializeValue(value)}`)
|
|
47
|
+
.join('; ');
|
|
48
|
+
return `{ ${entries} }`;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function generateModelRelationsType(relations: Record<string, unknown>): string {
|
|
52
|
+
const relationEntries: string[] = [];
|
|
53
|
+
|
|
54
|
+
for (const [relName, rel] of Object.entries(relations)) {
|
|
55
|
+
if (typeof rel !== 'object' || rel === null) continue;
|
|
56
|
+
const relObj = rel as Record<string, unknown>;
|
|
57
|
+
const parts: string[] = [];
|
|
58
|
+
|
|
59
|
+
if (relObj['to']) parts.push(`readonly to: ${serializeValue(relObj['to'])}`);
|
|
60
|
+
if (relObj['cardinality'])
|
|
61
|
+
parts.push(`readonly cardinality: ${serializeValue(relObj['cardinality'])}`);
|
|
62
|
+
|
|
63
|
+
const on = relObj['on'] as { localFields?: string[]; targetFields?: string[] } | undefined;
|
|
64
|
+
if (on?.localFields && on.targetFields) {
|
|
65
|
+
const localFields = on.localFields.map((f) => serializeValue(f)).join(', ');
|
|
66
|
+
const targetFields = on.targetFields.map((f) => serializeValue(f)).join(', ');
|
|
67
|
+
parts.push(
|
|
68
|
+
`readonly on: { readonly localFields: readonly [${localFields}]; readonly targetFields: readonly [${targetFields}] }`,
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (parts.length > 0) {
|
|
73
|
+
relationEntries.push(`readonly ${relName}: { ${parts.join('; ')} }`);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (relationEntries.length === 0) {
|
|
78
|
+
return 'Record<string, never>';
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return `{ ${relationEntries.join('; ')} }`;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export function deduplicateImports(imports: TypesImportSpec[]): TypesImportSpec[] {
|
|
85
|
+
const seenKeys = new Set<string>();
|
|
86
|
+
const result: TypesImportSpec[] = [];
|
|
87
|
+
for (const imp of imports) {
|
|
88
|
+
const key = `${imp.package}::${imp.named}`;
|
|
89
|
+
if (!seenKeys.has(key)) {
|
|
90
|
+
seenKeys.add(key);
|
|
91
|
+
result.push(imp);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
export function generateImportLines(imports: TypesImportSpec[]): string[] {
|
|
98
|
+
return imports.map((imp) => {
|
|
99
|
+
const importClause = imp.named === imp.alias ? imp.named : `${imp.named} as ${imp.alias}`;
|
|
100
|
+
return `import type { ${importClause} } from '${imp.package}';`;
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export function generateCodecTypeIntersection(
|
|
105
|
+
imports: ReadonlyArray<TypesImportSpec>,
|
|
106
|
+
named: string,
|
|
107
|
+
): string {
|
|
108
|
+
const aliases = imports.filter((imp) => imp.named === named).map((imp) => imp.alias);
|
|
109
|
+
return aliases.join(' & ') || 'Record<string, never>';
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export function generateHashTypeAliases(hashes: {
|
|
113
|
+
readonly storageHash: string;
|
|
114
|
+
readonly executionHash?: string;
|
|
115
|
+
readonly profileHash: string;
|
|
116
|
+
}): string {
|
|
117
|
+
const executionHashType = hashes.executionHash
|
|
118
|
+
? `ExecutionHashBase<'${hashes.executionHash}'>`
|
|
119
|
+
: 'ExecutionHashBase<string>';
|
|
120
|
+
|
|
121
|
+
return [
|
|
122
|
+
`export type StorageHash = StorageHashBase<'${hashes.storageHash}'>;`,
|
|
123
|
+
`export type ExecutionHash = ${executionHashType};`,
|
|
124
|
+
`export type ProfileHash = ProfileHashBase<'${hashes.profileHash}'>;`,
|
|
125
|
+
].join('\n');
|
|
126
|
+
}
|
package/src/exports/index.ts
CHANGED
|
@@ -7,3 +7,13 @@ export type {
|
|
|
7
7
|
export type { EmitOptions, EmitResult } from '@prisma-next/core-control-plane/emission';
|
|
8
8
|
// Re-export emit function and types from core-control-plane
|
|
9
9
|
export { emit } from '@prisma-next/core-control-plane/emission';
|
|
10
|
+
export {
|
|
11
|
+
deduplicateImports,
|
|
12
|
+
generateCodecTypeIntersection,
|
|
13
|
+
generateHashTypeAliases,
|
|
14
|
+
generateImportLines,
|
|
15
|
+
generateModelRelationsType,
|
|
16
|
+
generateRootsType,
|
|
17
|
+
serializeObjectKey,
|
|
18
|
+
serializeValue,
|
|
19
|
+
} from '../domain-type-generation';
|
|
@@ -6,7 +6,7 @@ describe('canonicalization', () => {
|
|
|
6
6
|
it('orders top-level sections correctly', () => {
|
|
7
7
|
const ir = createContractIR({
|
|
8
8
|
capabilities: { postgres: { jsonAgg: true } },
|
|
9
|
-
meta: {
|
|
9
|
+
meta: { emitterVersion: 'test' },
|
|
10
10
|
});
|
|
11
11
|
|
|
12
12
|
const result = canonicalizeContract(ir);
|
|
@@ -17,6 +17,7 @@ describe('canonicalization', () => {
|
|
|
17
17
|
const targetFamilyIndex = keys.indexOf('targetFamily');
|
|
18
18
|
const targetIndex = keys.indexOf('target');
|
|
19
19
|
const modelsIndex = keys.indexOf('models');
|
|
20
|
+
const relationsIndex = keys.indexOf('relations');
|
|
20
21
|
const storageIndex = keys.indexOf('storage');
|
|
21
22
|
const capabilitiesIndex = keys.indexOf('capabilities');
|
|
22
23
|
const metaIndex = keys.indexOf('meta');
|
|
@@ -24,6 +25,8 @@ describe('canonicalization', () => {
|
|
|
24
25
|
expect(schemaVersionIndex).toBeLessThan(targetFamilyIndex);
|
|
25
26
|
expect(targetFamilyIndex).toBeLessThan(targetIndex);
|
|
26
27
|
expect(targetIndex).toBeLessThan(modelsIndex);
|
|
28
|
+
expect(modelsIndex).toBeLessThan(relationsIndex);
|
|
29
|
+
expect(relationsIndex).toBeLessThan(storageIndex);
|
|
27
30
|
expect(modelsIndex).toBeLessThan(storageIndex);
|
|
28
31
|
expect(storageIndex).toBeLessThan(capabilitiesIndex);
|
|
29
32
|
expect(capabilitiesIndex).toBeLessThan(metaIndex);
|
|
@@ -55,6 +58,73 @@ describe('canonicalization', () => {
|
|
|
55
58
|
expect(email['nullable']).toBe(true);
|
|
56
59
|
});
|
|
57
60
|
|
|
61
|
+
it.each([
|
|
62
|
+
{ nullable: false },
|
|
63
|
+
{ nullable: undefined },
|
|
64
|
+
])('omits nullable:false for columns with defaults (nullable=$nullable)', ({ nullable }) => {
|
|
65
|
+
const ir = createContractIR({
|
|
66
|
+
storage: {
|
|
67
|
+
tables: {
|
|
68
|
+
user: {
|
|
69
|
+
columns: {
|
|
70
|
+
created_at: {
|
|
71
|
+
codecId: 'pg/timestamptz@1',
|
|
72
|
+
nativeType: 'timestamptz',
|
|
73
|
+
nullable,
|
|
74
|
+
default: { kind: 'function', expression: 'now()' },
|
|
75
|
+
},
|
|
76
|
+
updated_at: {
|
|
77
|
+
codecId: 'pg/timestamptz@1',
|
|
78
|
+
nativeType: 'timestamptz',
|
|
79
|
+
nullable: true,
|
|
80
|
+
},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
},
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
const result = canonicalizeContract(ir);
|
|
88
|
+
const parsed = JSON.parse(result) as Record<string, unknown>;
|
|
89
|
+
const storage = parsed['storage'] as Record<string, unknown>;
|
|
90
|
+
const tables = storage['tables'] as Record<string, unknown>;
|
|
91
|
+
const user = tables['user'] as Record<string, unknown>;
|
|
92
|
+
const columns = user['columns'] as Record<string, unknown>;
|
|
93
|
+
const createdAt = columns['created_at'] as Record<string, unknown>;
|
|
94
|
+
const updatedAt = columns['updated_at'] as Record<string, unknown>;
|
|
95
|
+
expect(createdAt['nullable']).toBeUndefined();
|
|
96
|
+
expect(updatedAt['nullable']).toBe(true);
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it('preserves nullable:true for columns with defaults', () => {
|
|
100
|
+
const ir = createContractIR({
|
|
101
|
+
storage: {
|
|
102
|
+
tables: {
|
|
103
|
+
user: {
|
|
104
|
+
columns: {
|
|
105
|
+
bio: {
|
|
106
|
+
codecId: 'pg/text@1',
|
|
107
|
+
nativeType: 'text',
|
|
108
|
+
nullable: true,
|
|
109
|
+
default: { kind: 'literal', value: '' },
|
|
110
|
+
},
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
},
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
const result = canonicalizeContract(ir);
|
|
118
|
+
const parsed = JSON.parse(result) as Record<string, unknown>;
|
|
119
|
+
const storage = parsed['storage'] as Record<string, unknown>;
|
|
120
|
+
const tables = storage['tables'] as Record<string, unknown>;
|
|
121
|
+
const user = tables['user'] as Record<string, unknown>;
|
|
122
|
+
const columns = user['columns'] as Record<string, unknown>;
|
|
123
|
+
const bio = columns['bio'] as Record<string, unknown>;
|
|
124
|
+
expect(bio['nullable']).toBe(true);
|
|
125
|
+
expect(bio['default']).toEqual({ kind: 'literal', value: '' });
|
|
126
|
+
});
|
|
127
|
+
|
|
58
128
|
it('omits empty arrays and objects except required ones', () => {
|
|
59
129
|
const ir = createContractIR();
|
|
60
130
|
|
|
@@ -66,14 +136,12 @@ describe('canonicalization', () => {
|
|
|
66
136
|
tables: expect.anything(),
|
|
67
137
|
},
|
|
68
138
|
});
|
|
69
|
-
// Required top-level fields (capabilities, extensionPacks, meta, relations
|
|
70
|
-
// because they are required by ContractIR and needed for round-trip tests
|
|
139
|
+
// Required top-level fields (capabilities, extensionPacks, meta, relations) are preserved even when empty.
|
|
71
140
|
expect(parsed).toMatchObject({
|
|
72
141
|
capabilities: expect.anything(),
|
|
73
142
|
extensionPacks: expect.anything(),
|
|
74
143
|
meta: expect.anything(),
|
|
75
144
|
relations: expect.anything(),
|
|
76
|
-
sources: expect.anything(),
|
|
77
145
|
});
|
|
78
146
|
});
|
|
79
147
|
|
|
@@ -117,7 +185,7 @@ describe('canonicalization', () => {
|
|
|
117
185
|
expect(result1).not.toBe(result2);
|
|
118
186
|
});
|
|
119
187
|
|
|
120
|
-
it('sorts
|
|
188
|
+
it('sorts indexes by canonical name', () => {
|
|
121
189
|
const ir = createContractIR({
|
|
122
190
|
storage: {
|
|
123
191
|
tables: {
|
|
@@ -144,6 +212,60 @@ describe('canonicalization', () => {
|
|
|
144
212
|
expect(indexNames).toEqual(['user_email_idx', 'user_name_idx']);
|
|
145
213
|
});
|
|
146
214
|
|
|
215
|
+
it('sorts uniques by canonical name', () => {
|
|
216
|
+
const ir = createContractIR({
|
|
217
|
+
storage: {
|
|
218
|
+
tables: {
|
|
219
|
+
user: {
|
|
220
|
+
columns: {
|
|
221
|
+
id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
|
|
222
|
+
email: { codecId: 'pg/text@1', nativeType: 'text', nullable: false },
|
|
223
|
+
username: { codecId: 'pg/text@1', nativeType: 'text', nullable: false },
|
|
224
|
+
},
|
|
225
|
+
uniques: [
|
|
226
|
+
{ columns: ['username'], name: 'user_username_key' },
|
|
227
|
+
{ columns: ['email'], name: 'user_email_key' },
|
|
228
|
+
],
|
|
229
|
+
},
|
|
230
|
+
},
|
|
231
|
+
},
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
const result = canonicalizeContract(ir);
|
|
235
|
+
const parsed = JSON.parse(result) as Record<string, unknown>;
|
|
236
|
+
const storage = parsed['storage'] as Record<string, unknown>;
|
|
237
|
+
const tables = storage['tables'] as Record<string, unknown>;
|
|
238
|
+
const user = tables['user'] as Record<string, unknown>;
|
|
239
|
+
const uniques = user['uniques'] as Array<{ name: string }>;
|
|
240
|
+
const uniqueNames = uniques.map((u) => u.name);
|
|
241
|
+
expect(uniqueNames).toEqual(['user_email_key', 'user_username_key']);
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
it('preserves column order in composite unique constraints', () => {
|
|
245
|
+
const ir = createContractIR({
|
|
246
|
+
storage: {
|
|
247
|
+
tables: {
|
|
248
|
+
user: {
|
|
249
|
+
columns: {
|
|
250
|
+
id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
|
|
251
|
+
first_name: { codecId: 'pg/text@1', nativeType: 'text', nullable: false },
|
|
252
|
+
last_name: { codecId: 'pg/text@1', nativeType: 'text', nullable: false },
|
|
253
|
+
},
|
|
254
|
+
uniques: [{ columns: ['last_name', 'first_name'], name: 'user_name_key' }],
|
|
255
|
+
},
|
|
256
|
+
},
|
|
257
|
+
},
|
|
258
|
+
});
|
|
259
|
+
|
|
260
|
+
const result = canonicalizeContract(ir);
|
|
261
|
+
const parsed = JSON.parse(result) as Record<string, unknown>;
|
|
262
|
+
const storage = parsed['storage'] as Record<string, unknown>;
|
|
263
|
+
const tables = storage['tables'] as Record<string, unknown>;
|
|
264
|
+
const user = tables['user'] as Record<string, unknown>;
|
|
265
|
+
const uniques = user['uniques'] as Array<{ columns: string[] }>;
|
|
266
|
+
expect(uniques[0]!.columns).toEqual(['last_name', 'first_name']);
|
|
267
|
+
});
|
|
268
|
+
|
|
147
269
|
it('sorts nested object keys lexicographically', () => {
|
|
148
270
|
const ir = createContractIR({
|
|
149
271
|
storage: {
|
|
@@ -169,6 +291,67 @@ describe('canonicalization', () => {
|
|
|
169
291
|
expect(columnKeys).toEqual(['a_field', 'm_field', 'z_field']);
|
|
170
292
|
});
|
|
171
293
|
|
|
294
|
+
describe('Mongo storage.collections preservation', () => {
|
|
295
|
+
it('preserves empty storage.collections container', () => {
|
|
296
|
+
const ir = createContractIR({
|
|
297
|
+
targetFamily: 'mongo',
|
|
298
|
+
target: 'mongo',
|
|
299
|
+
storage: { collections: {} },
|
|
300
|
+
});
|
|
301
|
+
|
|
302
|
+
const result = canonicalizeContract(ir);
|
|
303
|
+
const parsed = JSON.parse(result) as Record<string, unknown>;
|
|
304
|
+
const storage = parsed['storage'] as Record<string, unknown>;
|
|
305
|
+
expect(storage['collections']).toEqual({});
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
it('preserves collection entries with empty payloads', () => {
|
|
309
|
+
const ir = createContractIR({
|
|
310
|
+
targetFamily: 'mongo',
|
|
311
|
+
target: 'mongo',
|
|
312
|
+
storage: { collections: { users: {}, posts: {} } },
|
|
313
|
+
});
|
|
314
|
+
|
|
315
|
+
const result = canonicalizeContract(ir);
|
|
316
|
+
const parsed = JSON.parse(result) as Record<string, unknown>;
|
|
317
|
+
const storage = parsed['storage'] as Record<string, unknown>;
|
|
318
|
+
const collections = storage['collections'] as Record<string, unknown>;
|
|
319
|
+
expect(collections['users']).toEqual({});
|
|
320
|
+
expect(collections['posts']).toEqual({});
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
it('sorts collection names lexicographically', () => {
|
|
324
|
+
const ir = createContractIR({
|
|
325
|
+
targetFamily: 'mongo',
|
|
326
|
+
target: 'mongo',
|
|
327
|
+
storage: { collections: { zebras: {}, apples: {}, mangoes: {} } },
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const result = canonicalizeContract(ir);
|
|
331
|
+
const parsed = JSON.parse(result) as Record<string, unknown>;
|
|
332
|
+
const storage = parsed['storage'] as Record<string, unknown>;
|
|
333
|
+
const collections = storage['collections'] as Record<string, unknown>;
|
|
334
|
+
expect(Object.keys(collections)).toEqual(['apples', 'mangoes', 'zebras']);
|
|
335
|
+
});
|
|
336
|
+
|
|
337
|
+
it('produces different hashes when collections differ', () => {
|
|
338
|
+
const ir1 = createContractIR({
|
|
339
|
+
targetFamily: 'mongo',
|
|
340
|
+
target: 'mongo',
|
|
341
|
+
storage: { collections: { users: {} } },
|
|
342
|
+
});
|
|
343
|
+
const ir2 = createContractIR({
|
|
344
|
+
targetFamily: 'mongo',
|
|
345
|
+
target: 'mongo',
|
|
346
|
+
storage: { collections: { users: {}, posts: {} } },
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
const result1 = canonicalizeContract(ir1);
|
|
350
|
+
const result2 = canonicalizeContract(ir2);
|
|
351
|
+
expect(result1).not.toBe(result2);
|
|
352
|
+
});
|
|
353
|
+
});
|
|
354
|
+
|
|
172
355
|
it('sorts extension namespaces lexicographically', () => {
|
|
173
356
|
const ir = createContractIR({
|
|
174
357
|
extensionPacks: {
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
import type { TypesImportSpec } from '@prisma-next/contract/types';
|
|
2
|
+
import { describe, expect, it } from 'vitest';
|
|
3
|
+
import {
|
|
4
|
+
deduplicateImports,
|
|
5
|
+
generateCodecTypeIntersection,
|
|
6
|
+
generateHashTypeAliases,
|
|
7
|
+
generateImportLines,
|
|
8
|
+
generateModelRelationsType,
|
|
9
|
+
generateRootsType,
|
|
10
|
+
serializeObjectKey,
|
|
11
|
+
serializeValue,
|
|
12
|
+
} from '../src/domain-type-generation';
|
|
13
|
+
|
|
14
|
+
describe('serializeValue', () => {
|
|
15
|
+
it('serializes null', () => {
|
|
16
|
+
expect(serializeValue(null)).toBe('null');
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('serializes undefined', () => {
|
|
20
|
+
expect(serializeValue(undefined)).toBe('undefined');
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('serializes strings with single quotes', () => {
|
|
24
|
+
expect(serializeValue('hello')).toBe("'hello'");
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('escapes backslashes and single quotes in strings', () => {
|
|
28
|
+
expect(serializeValue("it's")).toBe("'it\\'s'");
|
|
29
|
+
expect(serializeValue('back\\slash')).toBe("'back\\\\slash'");
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('serializes numbers', () => {
|
|
33
|
+
expect(serializeValue(42)).toBe('42');
|
|
34
|
+
expect(serializeValue(3.14)).toBe('3.14');
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('serializes booleans', () => {
|
|
38
|
+
expect(serializeValue(true)).toBe('true');
|
|
39
|
+
expect(serializeValue(false)).toBe('false');
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it('serializes bigints', () => {
|
|
43
|
+
expect(serializeValue(BigInt(123))).toBe('123n');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('serializes arrays as readonly tuples', () => {
|
|
47
|
+
expect(serializeValue(['a', 'b'])).toBe("readonly ['a', 'b']");
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
it('serializes objects with readonly properties', () => {
|
|
51
|
+
expect(serializeValue({ key: 'val' })).toBe("{ readonly key: 'val' }");
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it('serializes nested objects', () => {
|
|
55
|
+
const result = serializeValue({ a: { b: 1 } });
|
|
56
|
+
expect(result).toBe('{ readonly a: { readonly b: 1 } }');
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
it('returns unknown for unsupported types', () => {
|
|
60
|
+
expect(serializeValue(Symbol('test'))).toBe('unknown');
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
describe('serializeObjectKey', () => {
|
|
65
|
+
it('passes through valid identifiers', () => {
|
|
66
|
+
expect(serializeObjectKey('foo')).toBe('foo');
|
|
67
|
+
expect(serializeObjectKey('_bar')).toBe('_bar');
|
|
68
|
+
expect(serializeObjectKey('$baz')).toBe('$baz');
|
|
69
|
+
expect(serializeObjectKey('camelCase')).toBe('camelCase');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('quotes keys with special characters', () => {
|
|
73
|
+
expect(serializeObjectKey('has space')).toBe("'has space'");
|
|
74
|
+
expect(serializeObjectKey('has-dash')).toBe("'has-dash'");
|
|
75
|
+
expect(serializeObjectKey('ns/name@1')).toBe("'ns/name@1'");
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
describe('generateRootsType', () => {
|
|
80
|
+
it('returns Record<string, string> for undefined roots', () => {
|
|
81
|
+
expect(generateRootsType(undefined)).toBe('Record<string, string>');
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it('returns Record<string, string> for empty roots', () => {
|
|
85
|
+
expect(generateRootsType({})).toBe('Record<string, string>');
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
it('generates literal object type for roots', () => {
|
|
89
|
+
const result = generateRootsType({ users: 'User', posts: 'Post' });
|
|
90
|
+
expect(result).toContain("readonly users: 'User'");
|
|
91
|
+
expect(result).toContain("readonly posts: 'Post'");
|
|
92
|
+
});
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
describe('generateModelRelationsType', () => {
|
|
96
|
+
it('returns empty object for empty relations', () => {
|
|
97
|
+
expect(generateModelRelationsType({})).toBe('Record<string, never>');
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
it('generates relation with to and cardinality', () => {
|
|
101
|
+
const result = generateModelRelationsType({
|
|
102
|
+
posts: { to: 'Post', cardinality: '1:N' },
|
|
103
|
+
});
|
|
104
|
+
expect(result).toContain("readonly to: 'Post'");
|
|
105
|
+
expect(result).toContain("readonly cardinality: '1:N'");
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
it('generates relation with on (localFields/targetFields)', () => {
|
|
109
|
+
const result = generateModelRelationsType({
|
|
110
|
+
author: {
|
|
111
|
+
to: 'User',
|
|
112
|
+
cardinality: 'N:1',
|
|
113
|
+
on: { localFields: ['authorId'], targetFields: ['_id'] },
|
|
114
|
+
},
|
|
115
|
+
});
|
|
116
|
+
expect(result).toContain("readonly to: 'User'");
|
|
117
|
+
expect(result).toContain("readonly cardinality: 'N:1'");
|
|
118
|
+
expect(result).toContain("readonly localFields: readonly ['authorId']");
|
|
119
|
+
expect(result).toContain("readonly targetFields: readonly ['_id']");
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('skips non-object relations', () => {
|
|
123
|
+
const result = generateModelRelationsType({
|
|
124
|
+
bad: 'not an object' as unknown as Record<string, unknown>,
|
|
125
|
+
});
|
|
126
|
+
expect(result).toBe('Record<string, never>');
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
it('generates multiple relations', () => {
|
|
130
|
+
const result = generateModelRelationsType({
|
|
131
|
+
author: { to: 'User', cardinality: 'N:1' },
|
|
132
|
+
comments: { to: 'Comment', cardinality: '1:N' },
|
|
133
|
+
});
|
|
134
|
+
expect(result).toContain('readonly author:');
|
|
135
|
+
expect(result).toContain('readonly comments:');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('omits to when missing from relation', () => {
|
|
139
|
+
const result = generateModelRelationsType({
|
|
140
|
+
rel: { cardinality: '1:N' },
|
|
141
|
+
});
|
|
142
|
+
expect(result).toContain("readonly cardinality: '1:N'");
|
|
143
|
+
expect(result).not.toContain('readonly to:');
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
it('omits cardinality when missing from relation', () => {
|
|
147
|
+
const result = generateModelRelationsType({
|
|
148
|
+
rel: { to: 'Post' },
|
|
149
|
+
});
|
|
150
|
+
expect(result).toContain("readonly to: 'Post'");
|
|
151
|
+
expect(result).not.toContain('readonly cardinality:');
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
it('skips relation object with no recognized properties', () => {
|
|
155
|
+
const result = generateModelRelationsType({
|
|
156
|
+
empty: { unknown: true },
|
|
157
|
+
});
|
|
158
|
+
expect(result).toBe('Record<string, never>');
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe('deduplicateImports', () => {
|
|
163
|
+
it('returns empty array for empty input', () => {
|
|
164
|
+
expect(deduplicateImports([])).toEqual([]);
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
it('keeps unique imports', () => {
|
|
168
|
+
const imports: TypesImportSpec[] = [
|
|
169
|
+
{ package: 'pkg-a', named: 'CodecTypes', alias: 'A' },
|
|
170
|
+
{ package: 'pkg-b', named: 'CodecTypes', alias: 'B' },
|
|
171
|
+
];
|
|
172
|
+
expect(deduplicateImports(imports)).toHaveLength(2);
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
it('deduplicates by package+named (first wins)', () => {
|
|
176
|
+
const imports: TypesImportSpec[] = [
|
|
177
|
+
{ package: 'pkg-a', named: 'CodecTypes', alias: 'First' },
|
|
178
|
+
{ package: 'pkg-a', named: 'CodecTypes', alias: 'Second' },
|
|
179
|
+
];
|
|
180
|
+
const result = deduplicateImports(imports);
|
|
181
|
+
expect(result).toHaveLength(1);
|
|
182
|
+
expect(result[0]!.alias).toBe('First');
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('preserves insertion order', () => {
|
|
186
|
+
const imports: TypesImportSpec[] = [
|
|
187
|
+
{ package: 'pkg-b', named: 'X', alias: 'X' },
|
|
188
|
+
{ package: 'pkg-a', named: 'Y', alias: 'Y' },
|
|
189
|
+
];
|
|
190
|
+
const result = deduplicateImports(imports);
|
|
191
|
+
expect(result[0]!.package).toBe('pkg-b');
|
|
192
|
+
expect(result[1]!.package).toBe('pkg-a');
|
|
193
|
+
});
|
|
194
|
+
});
|
|
195
|
+
|
|
196
|
+
describe('generateImportLines', () => {
|
|
197
|
+
it('generates import with alias', () => {
|
|
198
|
+
const imports: TypesImportSpec[] = [
|
|
199
|
+
{ package: '@prisma-next/adapter', named: 'CodecTypes', alias: 'PgCodecTypes' },
|
|
200
|
+
];
|
|
201
|
+
const lines = generateImportLines(imports);
|
|
202
|
+
expect(lines).toEqual([
|
|
203
|
+
"import type { CodecTypes as PgCodecTypes } from '@prisma-next/adapter';",
|
|
204
|
+
]);
|
|
205
|
+
});
|
|
206
|
+
|
|
207
|
+
it('simplifies import when named === alias', () => {
|
|
208
|
+
const imports: TypesImportSpec[] = [
|
|
209
|
+
{ package: '@prisma-next/adapter', named: 'Vector', alias: 'Vector' },
|
|
210
|
+
];
|
|
211
|
+
const lines = generateImportLines(imports);
|
|
212
|
+
expect(lines).toEqual(["import type { Vector } from '@prisma-next/adapter';"]);
|
|
213
|
+
});
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
describe('generateCodecTypeIntersection', () => {
|
|
217
|
+
it('returns Record<string, never> when no matching imports', () => {
|
|
218
|
+
expect(generateCodecTypeIntersection([], 'CodecTypes')).toBe('Record<string, never>');
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
it('returns single alias when one match', () => {
|
|
222
|
+
const imports: TypesImportSpec[] = [
|
|
223
|
+
{ package: 'pkg', named: 'CodecTypes', alias: 'PgCodecTypes' },
|
|
224
|
+
];
|
|
225
|
+
expect(generateCodecTypeIntersection(imports, 'CodecTypes')).toBe('PgCodecTypes');
|
|
226
|
+
});
|
|
227
|
+
|
|
228
|
+
it('returns intersection when multiple matches', () => {
|
|
229
|
+
const imports: TypesImportSpec[] = [
|
|
230
|
+
{ package: 'pkg-a', named: 'CodecTypes', alias: 'A' },
|
|
231
|
+
{ package: 'pkg-b', named: 'CodecTypes', alias: 'B' },
|
|
232
|
+
];
|
|
233
|
+
expect(generateCodecTypeIntersection(imports, 'CodecTypes')).toBe('A & B');
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
it('filters by named parameter', () => {
|
|
237
|
+
const imports: TypesImportSpec[] = [
|
|
238
|
+
{ package: 'pkg', named: 'CodecTypes', alias: 'CT' },
|
|
239
|
+
{ package: 'pkg', named: 'OperationTypes', alias: 'OT' },
|
|
240
|
+
];
|
|
241
|
+
expect(generateCodecTypeIntersection(imports, 'OperationTypes')).toBe('OT');
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
describe('generateHashTypeAliases', () => {
|
|
246
|
+
it('generates storage and profile hash aliases', () => {
|
|
247
|
+
const result = generateHashTypeAliases({
|
|
248
|
+
storageHash: 'sha256:abc123',
|
|
249
|
+
profileHash: 'sha256:def456',
|
|
250
|
+
});
|
|
251
|
+
expect(result).toContain("StorageHashBase<'sha256:abc123'>");
|
|
252
|
+
expect(result).toContain("ProfileHashBase<'sha256:def456'>");
|
|
253
|
+
});
|
|
254
|
+
|
|
255
|
+
it('generates concrete execution hash when provided', () => {
|
|
256
|
+
const result = generateHashTypeAliases({
|
|
257
|
+
storageHash: 'sha256:abc',
|
|
258
|
+
executionHash: 'sha256:exec',
|
|
259
|
+
profileHash: 'sha256:prof',
|
|
260
|
+
});
|
|
261
|
+
expect(result).toContain("ExecutionHashBase<'sha256:exec'>");
|
|
262
|
+
});
|
|
263
|
+
|
|
264
|
+
it('generates generic execution hash when not provided', () => {
|
|
265
|
+
const result = generateHashTypeAliases({
|
|
266
|
+
storageHash: 'sha256:abc',
|
|
267
|
+
profileHash: 'sha256:prof',
|
|
268
|
+
});
|
|
269
|
+
expect(result).toContain('ExecutionHashBase<string>');
|
|
270
|
+
});
|
|
271
|
+
});
|