Skip to content

Commit

Permalink
fix(json): support recursive definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 committed Nov 16, 2024
1 parent 68a0eb3 commit 26292dc
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions packages/sdk/src/code-gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export function createProject(options?: CompilerOptions) {
strict: true,
skipLibCheck: true,
noEmitOnError: true,
noImplicitAny: false,
...options,
},
});
Expand Down
45 changes: 45 additions & 0 deletions tests/integration/tests/enhancements/json/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -344,4 +344,49 @@ describe('Json field CRUD', () => {
expect(u2.profile.ownerId).toBe(2);
expect(u2.profile.nested.userId).toBe(3);
});

it('works with recursive types', async () => {
const params = await loadSchema(
`
type Content {
type String
content Content[]?
text String?
}
model Post {
id Int @id @default(autoincrement())
content Content @json
@@allow('all', true)
}
`,
{
provider: 'postgresql',
dbUrl,
}
);

prisma = params.prisma;
const db = params.enhance();
const post = await db.post.create({
data: {
content: {
type: 'text',
content: [
{
type: 'text',
content: [
{
type: 'text',
text: 'hello',
},
],
},
],
},
},
});

await expect(post.content.content[0].content[0].text).toBe('hello');
});
});
53 changes: 53 additions & 0 deletions tests/integration/tests/enhancements/json/typing.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,59 @@ async function main() {
dateTime: new Date(),
json: { a: 1 },
}
}
`,
},
],
}
);
});

it('supports recursive definition', async () => {
await loadSchema(
`
type Content {
type String
content Content[]?
text String?
}
model Post {
id Int @id @default(autoincrement())
content Content @json
}
`,
{
provider: 'postgresql',
pushDb: false,
compile: true,
extraSourceFiles: [
{
name: 'main.ts',
content: `
import type { Content } from '.zenstack/models';
import { enhance } from '.zenstack/enhance';
import { PrismaClient } from '@prisma/client';
async function main() {
const content: Content = {
type: 'text',
content: [
{
type: 'text',
content: [
{
type: 'text',
text: 'hello',
},
],
},
],
}
const db = enhance(new PrismaClient());
const post = await db.post.create({ data: { content } });
console.log(post.content.content?.[0].content?.[0].text);
}
`,
},
Expand Down

0 comments on commit 26292dc

Please sign in to comment.