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
2 changes: 1 addition & 1 deletion packages/schema/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"publisher": "zenstack",
"displayName": "ZenStack Language Tools",
"description": "ZenStack is a toolkit that simplifies full-stack development",
"version": "0.1.54",
"version": "0.1.55",
"author": {
"name": "ZenStack Team"
},
Expand Down
28 changes: 18 additions & 10 deletions packages/schema/src/generator/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ export default class ServiceGenerator implements Generator {
isTypeOnly: true,
});

sf.addVariableStatement({
declarationKind: VariableDeclarationKind.Let,
declarations: [
{
name: 'guardModule',
type: 'any',
},
],
});

const cls = sf.addClass({
name: 'ZenStackService',
isExported: true,
Expand All @@ -44,15 +54,6 @@ export default class ServiceGenerator implements Generator {
.addBody()
.setBodyText('return this._prisma;');

sf.addVariableStatement({
declarationKind: VariableDeclarationKind.Let,
declarations: [
{
name: 'guardModule',
type: 'any',
},
],
});
cls
.addMethod({
name: 'resolveField',
Expand Down Expand Up @@ -100,7 +101,14 @@ export default class ServiceGenerator implements Generator {
return provider(context);
`);

sf.addStatements(['export default new ZenStackService();']);
// Recommended by Prisma for Next.js
// https://www.prisma.io/docs/guides/database/troubleshooting-orm/help-articles/nextjs-prisma-client-dev-practices#problem
sf.addStatements([
'declare global { var zenstackService: ZenStackService | undefined}',
'const service = global.zenstackService || new ZenStackService();',
'export default service;',
`if (process.env.NODE_ENV !== 'production') global.zenstackService = service;`,
]);

sf.formatText();
await project.save();
Expand Down
14 changes: 7 additions & 7 deletions samples/todo/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.