Skip to content

Commit c7f333d

Browse files
authored
fix(json): support recursive definitions (#1865)
1 parent 68a0eb3 commit c7f333d

File tree

3 files changed

+99
-0
lines changed

3 files changed

+99
-0
lines changed

packages/sdk/src/code-gen.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ export function createProject(options?: CompilerOptions) {
1515
strict: true,
1616
skipLibCheck: true,
1717
noEmitOnError: true,
18+
noImplicitAny: false,
1819
...options,
1920
},
2021
});

tests/integration/tests/enhancements/json/crud.test.ts

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,4 +344,49 @@ describe('Json field CRUD', () => {
344344
expect(u2.profile.ownerId).toBe(2);
345345
expect(u2.profile.nested.userId).toBe(3);
346346
});
347+
348+
it('works with recursive types', async () => {
349+
const params = await loadSchema(
350+
`
351+
type Content {
352+
type String
353+
content Content[]?
354+
text String?
355+
}
356+
357+
model Post {
358+
id Int @id @default(autoincrement())
359+
content Content @json
360+
@@allow('all', true)
361+
}
362+
`,
363+
{
364+
provider: 'postgresql',
365+
dbUrl,
366+
}
367+
);
368+
369+
prisma = params.prisma;
370+
const db = params.enhance();
371+
const post = await db.post.create({
372+
data: {
373+
content: {
374+
type: 'text',
375+
content: [
376+
{
377+
type: 'text',
378+
content: [
379+
{
380+
type: 'text',
381+
text: 'hello',
382+
},
383+
],
384+
},
385+
],
386+
},
387+
},
388+
});
389+
390+
await expect(post.content.content[0].content[0].text).toBe('hello');
391+
});
347392
});

tests/integration/tests/enhancements/json/typing.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,59 @@ async function main() {
325325
dateTime: new Date(),
326326
json: { a: 1 },
327327
}
328+
}
329+
`,
330+
},
331+
],
332+
}
333+
);
334+
});
335+
336+
it('supports recursive definition', async () => {
337+
await loadSchema(
338+
`
339+
type Content {
340+
type String
341+
content Content[]?
342+
text String?
343+
}
344+
345+
model Post {
346+
id Int @id @default(autoincrement())
347+
content Content @json
348+
}
349+
`,
350+
{
351+
provider: 'postgresql',
352+
pushDb: false,
353+
compile: true,
354+
extraSourceFiles: [
355+
{
356+
name: 'main.ts',
357+
content: `
358+
import type { Content } from '.zenstack/models';
359+
import { enhance } from '.zenstack/enhance';
360+
import { PrismaClient } from '@prisma/client';
361+
362+
async function main() {
363+
const content: Content = {
364+
type: 'text',
365+
content: [
366+
{
367+
type: 'text',
368+
content: [
369+
{
370+
type: 'text',
371+
text: 'hello',
372+
},
373+
],
374+
},
375+
],
376+
}
377+
378+
const db = enhance(new PrismaClient());
379+
const post = await db.post.create({ data: { content } });
380+
console.log(post.content.content?.[0].content?.[0].text);
328381
}
329382
`,
330383
},

0 commit comments

Comments
 (0)