@prisma-next/emitter 0.5.0-dev.2 → 0.5.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.
@@ -1,219 +0,0 @@
1
- import type { TypesImportSpec } from '@prisma-next/framework-components/emission';
2
- import { timeouts } from '@prisma-next/test-utils';
3
- import { describe, expect, it } from 'vitest';
4
- import type { EmitStackInput } from '../src/exports';
5
- import { emit } from '../src/exports';
6
- import { createMockSpi } from './mock-spi';
7
- import { createTestContract } from './utils';
8
-
9
- const mockSqlHook = createMockSpi();
10
-
11
- describe('emitter integration', () => {
12
- it(
13
- 'emits complete contract from IR to artifacts',
14
- async () => {
15
- const ir = createTestContract({
16
- models: {
17
- User: {
18
- storage: {
19
- table: 'user',
20
- fields: {
21
- id: { column: 'id' },
22
- email: { column: 'email' },
23
- },
24
- },
25
- fields: {
26
- id: { type: { kind: 'scalar', codecId: 'pg/int4@1' }, nullable: false },
27
- email: { type: { kind: 'scalar', codecId: 'pg/text@1' }, nullable: false },
28
- },
29
- relations: {},
30
- },
31
- },
32
- storage: {
33
- tables: {
34
- user: {
35
- columns: {
36
- id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
37
- email: { codecId: 'pg/text@1', nativeType: 'text', nullable: false },
38
- },
39
- primaryKey: { columns: ['id'] },
40
- uniques: [],
41
- indexes: [],
42
- foreignKeys: [],
43
- },
44
- },
45
- },
46
- extensionPacks: {
47
- postgres: {
48
- version: '0.0.1',
49
- },
50
- pg: {},
51
- },
52
- });
53
-
54
- const codecTypeImports: TypesImportSpec[] = [];
55
- const operationTypeImports: TypesImportSpec[] = [];
56
- const extensionIds = ['postgres', 'pg'];
57
- const options: EmitStackInput = {
58
- codecTypeImports,
59
- operationTypeImports,
60
- extensionIds,
61
- };
62
-
63
- const result = await emit(ir, options, mockSqlHook);
64
-
65
- expect(result.storageHash).toMatch(/^sha256:[a-f0-9]{64}$/);
66
- expect(result.contractDts).toContain('export type Contract');
67
- expect(result.contractDts).toContain('CodecTypes');
68
- expect(result.contractDts).toContain('LaneCodecTypes');
69
-
70
- const contractJson = JSON.parse(result.contractJson);
71
- expect(contractJson).toMatchObject({
72
- schemaVersion: '1',
73
- targetFamily: 'sql',
74
- target: 'postgres',
75
- profileHash: expect.stringMatching(/^sha256:/),
76
- roots: {},
77
- storage: {
78
- storageHash: result.storageHash,
79
- tables: {
80
- user: expect.anything(),
81
- },
82
- },
83
- });
84
- },
85
- timeouts.typeScriptCompilation,
86
- );
87
-
88
- it(
89
- 'produces stable hashes for identical input',
90
- async () => {
91
- const ir = createTestContract({
92
- models: {
93
- User: {
94
- storage: {
95
- table: 'user',
96
- fields: {
97
- id: { column: 'id' },
98
- },
99
- },
100
- fields: {
101
- id: { type: { kind: 'scalar', codecId: 'pg/int4@1' }, nullable: false },
102
- },
103
- relations: {},
104
- },
105
- },
106
- storage: {
107
- tables: {
108
- user: {
109
- columns: {
110
- id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
111
- },
112
- primaryKey: { columns: ['id'] },
113
- uniques: [],
114
- indexes: [],
115
- foreignKeys: [],
116
- },
117
- },
118
- },
119
- extensionPacks: {
120
- postgres: {
121
- version: '0.0.1',
122
- },
123
- pg: {},
124
- },
125
- });
126
-
127
- const codecTypeImports: TypesImportSpec[] = [];
128
- const operationTypeImports: TypesImportSpec[] = [];
129
- const extensionIds = ['postgres', 'pg'];
130
- const options: EmitStackInput = {
131
- codecTypeImports,
132
- operationTypeImports,
133
- extensionIds,
134
- };
135
-
136
- const result1 = await emit(ir, options, mockSqlHook);
137
- const result2 = await emit(ir, options, mockSqlHook);
138
-
139
- expect(result1.storageHash).toBe(result2.storageHash);
140
- expect(result1.contractDts).toBe(result2.contractDts);
141
- expect(result1.contractJson).toBe(result2.contractJson);
142
- },
143
- timeouts.typeScriptCompilation,
144
- );
145
-
146
- it(
147
- 'round-trip: IR → JSON → parse JSON → compare',
148
- async () => {
149
- const ir = createTestContract({
150
- models: {
151
- User: {
152
- storage: {
153
- table: 'user',
154
- fields: {
155
- id: { column: 'id' },
156
- email: { column: 'email' },
157
- },
158
- },
159
- fields: {
160
- id: { type: { kind: 'scalar', codecId: 'pg/int4@1' }, nullable: false },
161
- email: { type: { kind: 'scalar', codecId: 'pg/text@1' }, nullable: false },
162
- },
163
- relations: {},
164
- },
165
- },
166
- storage: {
167
- tables: {
168
- user: {
169
- columns: {
170
- id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
171
- email: { codecId: 'pg/text@1', nativeType: 'text', nullable: false },
172
- },
173
- primaryKey: { columns: ['id'] },
174
- uniques: [],
175
- indexes: [],
176
- foreignKeys: [],
177
- },
178
- },
179
- },
180
- extensionPacks: {
181
- postgres: {
182
- version: '0.0.1',
183
- },
184
- pg: {},
185
- },
186
- });
187
-
188
- const codecTypeImports: TypesImportSpec[] = [];
189
- const operationTypeImports: TypesImportSpec[] = [];
190
- const extensionIds = ['postgres', 'pg'];
191
- const options: EmitStackInput = {
192
- codecTypeImports,
193
- operationTypeImports,
194
- extensionIds,
195
- };
196
-
197
- const result1 = await emit(ir, options, mockSqlHook);
198
- const contractJson1 = JSON.parse(result1.contractJson) as Record<string, unknown>;
199
-
200
- const ir2 = createTestContract({
201
- targetFamily: contractJson1['targetFamily'] as string,
202
- target: contractJson1['target'] as string,
203
- roots: contractJson1['roots'] as Record<string, string>,
204
- models: contractJson1['models'] as Record<string, unknown>,
205
- storage: contractJson1['storage'] as Record<string, unknown>,
206
- extensionPacks: contractJson1['extensionPacks'] as Record<string, unknown>,
207
- capabilities:
208
- (contractJson1['capabilities'] as Record<string, Record<string, boolean>>) || {},
209
- meta: (contractJson1['meta'] as Record<string, unknown>) || {},
210
- });
211
-
212
- const result2 = await emit(ir2, options, mockSqlHook);
213
-
214
- expect(result1.contractJson).toBe(result2.contractJson);
215
- expect(result1.storageHash).toBe(result2.storageHash);
216
- },
217
- timeouts.typeScriptCompilation,
218
- );
219
- });
@@ -1,296 +0,0 @@
1
- import type { TypesImportSpec } from '@prisma-next/framework-components/emission';
2
- import { timeouts } from '@prisma-next/test-utils';
3
- import { describe, expect, it } from 'vitest';
4
- import type { EmitStackInput } from '../src/exports';
5
- import { emit } from '../src/exports';
6
- import { createMockSpi } from './mock-spi';
7
- import { createTestContract } from './utils';
8
-
9
- const mockSqlHook = createMockSpi();
10
-
11
- describe('emitter round-trip', () => {
12
- it(
13
- 'round-trip with minimal IR',
14
- async () => {
15
- const ir = createTestContract({
16
- storage: {
17
- tables: {
18
- user: {
19
- columns: {
20
- id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
21
- },
22
- primaryKey: { columns: ['id'] },
23
- uniques: [],
24
- indexes: [],
25
- foreignKeys: [],
26
- },
27
- },
28
- },
29
- extensionPacks: {
30
- postgres: { version: '0.0.1' },
31
- pg: {},
32
- },
33
- });
34
-
35
- const codecTypeImports: TypesImportSpec[] = [];
36
- const operationTypeImports: TypesImportSpec[] = [];
37
- const extensionIds = ['postgres', 'pg'];
38
- const options: EmitStackInput = {
39
- codecTypeImports,
40
- operationTypeImports,
41
- extensionIds,
42
- };
43
-
44
- const result1 = await emit(ir, options, mockSqlHook);
45
- const contractJson1 = JSON.parse(result1.contractJson) as Record<string, unknown>;
46
-
47
- const ir2 = createTestContract({
48
- targetFamily: contractJson1['targetFamily'] as string,
49
- target: contractJson1['target'] as string,
50
- roots: contractJson1['roots'] as Record<string, string>,
51
- models: contractJson1['models'] as Record<string, unknown>,
52
- storage: contractJson1['storage'] as Record<string, unknown>,
53
- extensionPacks: contractJson1['extensionPacks'] as Record<string, unknown>,
54
- capabilities:
55
- (contractJson1['capabilities'] as Record<string, Record<string, boolean>>) || {},
56
- meta: (contractJson1['meta'] as Record<string, unknown>) || {},
57
- });
58
-
59
- const result2 = await emit(ir2, options, mockSqlHook);
60
-
61
- expect(result1.contractJson).toBe(result2.contractJson);
62
- expect(result1.storageHash).toBe(result2.storageHash);
63
- },
64
- timeouts.typeScriptCompilation,
65
- );
66
-
67
- it(
68
- 'round-trip with complex IR',
69
- async () => {
70
- const ir = createTestContract({
71
- models: {
72
- User: {
73
- storage: {
74
- table: 'user',
75
- fields: {
76
- id: { column: 'id' },
77
- email: { column: 'email' },
78
- name: { column: 'name' },
79
- },
80
- },
81
- fields: {
82
- id: { type: { kind: 'scalar', codecId: 'pg/int4@1' }, nullable: false },
83
- email: { type: { kind: 'scalar', codecId: 'pg/text@1' }, nullable: false },
84
- name: { type: { kind: 'scalar', codecId: 'pg/text@1' }, nullable: true },
85
- },
86
- relations: {},
87
- },
88
- Post: {
89
- storage: {
90
- table: 'post',
91
- fields: {
92
- id: { column: 'id' },
93
- title: { column: 'title' },
94
- userId: { column: 'user_id' },
95
- },
96
- },
97
- fields: {
98
- id: { type: { kind: 'scalar', codecId: 'pg/int4@1' }, nullable: false },
99
- title: { type: { kind: 'scalar', codecId: 'pg/text@1' }, nullable: false },
100
- userId: { type: { kind: 'scalar', codecId: 'pg/int4@1' }, nullable: false },
101
- },
102
- relations: {},
103
- },
104
- },
105
- storage: {
106
- tables: {
107
- user: {
108
- columns: {
109
- id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
110
- email: { codecId: 'pg/text@1', nativeType: 'text', nullable: false },
111
- name: { codecId: 'pg/text@1', nativeType: 'text', nullable: true },
112
- },
113
- primaryKey: { columns: ['id'] },
114
- uniques: [{ columns: ['email'], name: 'user_email_key' }],
115
- indexes: [{ columns: ['name'], name: 'user_name_idx' }],
116
- foreignKeys: [],
117
- },
118
- post: {
119
- columns: {
120
- id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
121
- title: { codecId: 'pg/text@1', nativeType: 'text', nullable: false },
122
- user_id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
123
- },
124
- primaryKey: { columns: ['id'] },
125
- uniques: [],
126
- indexes: [],
127
- foreignKeys: [
128
- {
129
- columns: ['user_id'],
130
- references: { table: 'user', columns: ['id'] },
131
- name: 'post_user_id_fkey',
132
- },
133
- ],
134
- },
135
- },
136
- },
137
- extensionPacks: {
138
- postgres: { version: '0.0.1' },
139
- },
140
- });
141
-
142
- const codecTypeImports: TypesImportSpec[] = [];
143
- const operationTypeImports: TypesImportSpec[] = [];
144
- const extensionIds = ['postgres'];
145
- const options: EmitStackInput = {
146
- codecTypeImports,
147
- operationTypeImports,
148
- extensionIds,
149
- };
150
-
151
- const result1 = await emit(ir, options, mockSqlHook);
152
- const contractJson1 = JSON.parse(result1.contractJson) as Record<string, unknown>;
153
-
154
- const ir2 = createTestContract({
155
- targetFamily: contractJson1['targetFamily'] as string,
156
- target: contractJson1['target'] as string,
157
- roots: contractJson1['roots'] as Record<string, string>,
158
- models: contractJson1['models'] as Record<string, unknown>,
159
- storage: contractJson1['storage'] as Record<string, unknown>,
160
- extensionPacks: contractJson1['extensionPacks'] as Record<string, unknown>,
161
- capabilities:
162
- (contractJson1['capabilities'] as Record<string, Record<string, boolean>>) || {},
163
- meta: (contractJson1['meta'] as Record<string, unknown>) || {},
164
- });
165
-
166
- const result2 = await emit(ir2, options, mockSqlHook);
167
-
168
- expect(result1.contractJson).toBe(result2.contractJson);
169
- expect(result1.storageHash).toBe(result2.storageHash);
170
- },
171
- timeouts.typeScriptCompilation,
172
- );
173
-
174
- it('round-trip with nullable fields', async () => {
175
- const ir = createTestContract({
176
- storage: {
177
- tables: {
178
- user: {
179
- columns: {
180
- id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
181
- email: { codecId: 'pg/text@1', nativeType: 'text', nullable: true },
182
- name: { codecId: 'pg/text@1', nativeType: 'text', nullable: false },
183
- },
184
- primaryKey: { columns: ['id'] },
185
- uniques: [],
186
- indexes: [],
187
- foreignKeys: [],
188
- },
189
- },
190
- },
191
- extensionPacks: {
192
- postgres: { version: '0.0.1' },
193
- pg: {},
194
- },
195
- });
196
-
197
- const codecTypeImports: TypesImportSpec[] = [];
198
- const operationTypeImports: TypesImportSpec[] = [];
199
- const extensionIds = ['postgres', 'pg'];
200
- const options: EmitStackInput = {
201
- codecTypeImports,
202
- operationTypeImports,
203
- extensionIds,
204
- };
205
-
206
- const result1 = await emit(ir, options, mockSqlHook);
207
- const contractJson1 = JSON.parse(result1.contractJson) as Record<string, unknown>;
208
-
209
- const ir2 = createTestContract({
210
- targetFamily: contractJson1['targetFamily'] as string,
211
- target: contractJson1['target'] as string,
212
- roots: contractJson1['roots'] as Record<string, string>,
213
- models: contractJson1['models'] as Record<string, unknown>,
214
- storage: contractJson1['storage'] as Record<string, unknown>,
215
- extensionPacks: contractJson1['extensionPacks'] as Record<string, unknown>,
216
- capabilities:
217
- (contractJson1['capabilities'] as Record<string, Record<string, boolean>>) || {},
218
- meta: (contractJson1['meta'] as Record<string, unknown>) || {},
219
- });
220
-
221
- const result2 = await emit(ir2, options, mockSqlHook);
222
-
223
- expect(result1.contractJson).toBe(result2.contractJson);
224
- expect(result1.storageHash).toBe(result2.storageHash);
225
-
226
- const parsed2 = JSON.parse(result2.contractJson) as Record<string, unknown>;
227
- const storage = parsed2['storage'] as Record<string, unknown>;
228
- const tables = storage['tables'] as Record<string, unknown>;
229
- const user = tables['user'] as Record<string, unknown>;
230
- const columns = user['columns'] as Record<string, unknown>;
231
- const id = columns['id'] as Record<string, unknown>;
232
- const email = columns['email'] as Record<string, unknown>;
233
- const name = columns['name'] as Record<string, unknown>;
234
- expect(id['nullable']).toBe(false);
235
- expect(email['nullable']).toBe(true);
236
- expect(name['nullable']).toBe(false);
237
- });
238
-
239
- it('round-trip with capabilities', async () => {
240
- const ir = createTestContract({
241
- storage: {
242
- tables: {
243
- user: {
244
- columns: {
245
- id: { codecId: 'pg/int4@1', nativeType: 'int4', nullable: false },
246
- },
247
- primaryKey: { columns: ['id'] },
248
- uniques: [],
249
- indexes: [],
250
- foreignKeys: [],
251
- },
252
- },
253
- },
254
- extensionPacks: {
255
- postgres: { version: '0.0.1' },
256
- pg: {},
257
- },
258
- capabilities: {
259
- postgres: {
260
- jsonAgg: true,
261
- lateral: true,
262
- },
263
- },
264
- });
265
-
266
- const codecTypeImports: TypesImportSpec[] = [];
267
- const operationTypeImports: TypesImportSpec[] = [];
268
- const extensionIds = ['postgres', 'pg'];
269
- const options: EmitStackInput = {
270
- codecTypeImports,
271
- operationTypeImports,
272
- extensionIds,
273
- };
274
-
275
- const result1 = await emit(ir, options, mockSqlHook);
276
- const contractJson1 = JSON.parse(result1.contractJson) as Record<string, unknown>;
277
-
278
- const ir2 = createTestContract({
279
- targetFamily: contractJson1['targetFamily'] as string,
280
- target: contractJson1['target'] as string,
281
- roots: contractJson1['roots'] as Record<string, string>,
282
- models: contractJson1['models'] as Record<string, unknown>,
283
- storage: contractJson1['storage'] as Record<string, unknown>,
284
- extensionPacks: contractJson1['extensionPacks'] as Record<string, unknown>,
285
- capabilities:
286
- (contractJson1['capabilities'] as Record<string, Record<string, boolean>>) || {},
287
- meta: (contractJson1['meta'] as Record<string, unknown>) || {},
288
- });
289
-
290
- const result2 = await emit(ir2, options, mockSqlHook);
291
-
292
- expect(result1.contractJson).toBe(result2.contractJson);
293
- expect(result1.storageHash).toBe(result2.storageHash);
294
- expect(result1.profileHash).toBe(result2.profileHash);
295
- });
296
- });