Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions packages/schema/src/plugins/enhancer/enhance/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -824,12 +824,21 @@ export type Enhanced<Client> =
};

const replacePrismaJson = (source: string, field: DataModelField) => {
return source.replace(
new RegExp(`(${field.name}\\??\\s*):[^\\n]+`),
`$1: ${field.type.reference!.$refText}${field.type.array ? '[]' : ''}${
field.type.optional ? ' | null' : ''
}`
);
let replaceValue = `$1: ${field.type.reference!.$refText}`;
if (field.type.array) {
replaceValue += '[]';
}
if (field.type.optional) {
replaceValue += ' | null';
}

// Check if the field in the source is optional (has a `?`)
const isOptionalInSource = new RegExp(`(${field.name}\\?\\s*):`).test(source);
if (isOptionalInSource) {
replaceValue += ' | $Types.Skip';
}

return source.replace(new RegExp(`(${field.name}\\??\\s*):[^\\n]+`), replaceValue);
};

// fix "$[Model]Payload" type
Expand Down
62 changes: 62 additions & 0 deletions tests/integration/tests/enhancements/json/crud.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -429,4 +429,66 @@ describe('Json field CRUD', () => {

await expect(post.content.content[0].content[0].text).toBe('hello');
});

it('works with Prisma.skip', async () => {
const params = await loadSchema(
`
type Profile {
foo Int
bar String
}

model User {
id Int @id @default(autoincrement())
name String
profile Profile @json
@@allow('all', true)
}
`,
{
provider: 'postgresql',
dbUrl,
compile: true,
extraSourceFiles: [
{
name: 'main.ts',
content: `
import { enhance } from '.zenstack/enhance';
import { Prisma, PrismaClient } from '@prisma/client';
const prisma = new PrismaClient();
const db = enhance(prisma);

async function main() {
// @ts-expect-error Non optional JSON fields should not be skippable in the create call
db.user.create({ data: { name: 'test', profile: Prisma.skip } });

const u = await db.user.create({ data: { name: 'test', profile: { foo: 18, bar: 'test' } } });
await db.user.update({ where: { id: u.id }, data: { profile: Prisma.skip } });
}
`,
},
],
}
);

prisma = params.prisma;
const skip = params.prismaModule.Prisma.skip;
const db = params.enhance();

const user = await db.user.create({ data: { name: 'test', profile: { foo: 18, bar: 'test' } } });

await expect(
db.user.update({
where: { id: user.id },
data: { profile: skip },
})
).resolves.toMatchObject({
id: user.id,
name: 'test',
profile: {
foo: 18,
bar: 'test',
},
});
});
});
Loading