zenstack 0.1.41 → 1.0.0
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.
- package/.vscode/extensions.json +7 -0
- package/.vscode/launch.json +49 -0
- package/.vscode/settings.json +4 -0
- package/README.md +1 -0
- package/package.json +8 -90
- package/packages/internal/jest.config.ts +32 -0
- package/packages/internal/package.json +42 -0
- package/packages/internal/src/constants.ts +1 -0
- package/packages/internal/src/handler/data/guard-utils.ts +7 -0
- package/packages/internal/src/handler/data/handler.ts +415 -0
- package/packages/internal/src/handler/data/query-processor.ts +504 -0
- package/packages/internal/src/handler/index.ts +1 -0
- package/packages/internal/src/handler/types.ts +20 -0
- package/packages/internal/src/index.ts +3 -0
- package/packages/internal/src/request-handler.ts +27 -0
- package/packages/internal/src/request.ts +101 -0
- package/packages/internal/src/types.ts +40 -0
- package/packages/internal/tests/query-processor.test.ts +172 -0
- package/{out/cli/tsconfig.template.json → packages/internal/tsconfig.json} +7 -3
- package/packages/runtime/auth.d.ts +1 -0
- package/packages/runtime/auth.js +3 -0
- package/packages/runtime/hooks.d.ts +10 -0
- package/packages/runtime/hooks.js +3 -0
- package/packages/runtime/index.d.ts +3 -0
- package/packages/runtime/index.js +1 -0
- package/packages/runtime/package-lock.json +512 -0
- package/packages/runtime/package.json +16 -0
- package/packages/runtime/server.d.ts +1 -0
- package/packages/runtime/server.js +3 -0
- package/packages/runtime/types.d.ts +1 -0
- package/packages/runtime/types.js +3 -0
- package/packages/schema/.eslintrc.json +13 -0
- package/packages/schema/.vscodeignore +4 -0
- package/packages/schema/asset/logo-dark.png +0 -0
- package/packages/schema/asset/logo-light.png +0 -0
- package/{bin → packages/schema/bin}/cli +0 -0
- package/packages/schema/jest.config.ts +32 -0
- package/packages/schema/langium-config.json +14 -0
- package/packages/schema/langium-quickstart.md +41 -0
- package/packages/schema/language-configuration.json +30 -0
- package/packages/schema/package.json +96 -0
- package/packages/schema/src/cli/cli-util.ts +80 -0
- package/packages/schema/src/cli/index.ts +64 -0
- package/packages/schema/src/extension.ts +76 -0
- package/packages/schema/src/generator/constants.ts +5 -0
- package/packages/schema/src/generator/index.ts +92 -0
- package/{out/generator/next-auth/index.js → packages/schema/src/generator/next-auth/index.ts} +46 -58
- package/{out → packages/schema/src}/generator/package.template.json +0 -0
- package/packages/schema/src/generator/prisma/expression-writer.ts +352 -0
- package/packages/schema/src/generator/prisma/index.ts +32 -0
- package/packages/schema/src/generator/prisma/plain-expression-builder.ts +91 -0
- package/packages/schema/src/generator/prisma/prisma-builder.ts +366 -0
- package/packages/schema/src/generator/prisma/query-gard-generator.ts +208 -0
- package/packages/schema/src/generator/prisma/schema-generator.ts +300 -0
- package/packages/schema/src/generator/react-hooks/index.ts +181 -0
- package/packages/schema/src/generator/service/index.ts +107 -0
- package/{out → packages/schema/src}/generator/tsconfig.template.json +0 -0
- package/packages/schema/src/generator/types.ts +17 -0
- package/packages/schema/src/generator/utils.ts +9 -0
- package/packages/schema/src/language-server/generated/ast.ts +603 -0
- package/{out/language-server/generated/grammar.js → packages/schema/src/language-server/generated/grammar.ts} +5 -8
- package/packages/schema/src/language-server/generated/module.ts +24 -0
- package/packages/schema/src/language-server/main.ts +12 -0
- package/{out → packages/schema/src}/language-server/stdlib.zmodel +0 -0
- package/packages/schema/src/language-server/types.ts +9 -0
- package/packages/schema/src/language-server/zmodel-index.ts +33 -0
- package/packages/schema/src/language-server/zmodel-linker.ts +409 -0
- package/packages/schema/src/language-server/zmodel-module.ts +90 -0
- package/packages/schema/src/language-server/zmodel-scope.ts +21 -0
- package/packages/schema/src/language-server/zmodel-validator.ts +35 -0
- package/packages/schema/src/language-server/zmodel.langium +186 -0
- package/packages/schema/src/utils/exec-utils.ts +5 -0
- package/packages/schema/src/utils/indent-string.ts +6 -0
- package/packages/schema/syntaxes/zmodel.json +57 -0
- package/packages/schema/syntaxes/zmodel.tmLanguage.json +57 -0
- package/packages/schema/tests/generator/expression-writer.test.ts +676 -0
- package/packages/schema/tests/generator/prisma-builder.test.ts +138 -0
- package/packages/schema/tests/schema/parser.test.ts +423 -0
- package/packages/schema/tests/schema/sample-todo.test.ts +14 -0
- package/packages/schema/tests/utils.ts +38 -0
- package/packages/schema/tsconfig.json +23 -0
- package/pnpm-workspace.yaml +3 -0
- package/samples/todo/.env +2 -0
- package/samples/todo/.eslintrc.json +3 -0
- package/samples/todo/.vscode/launch.json +11 -0
- package/samples/todo/README.md +34 -0
- package/samples/todo/components/AuthGuard.tsx +17 -0
- package/samples/todo/components/Avatar.tsx +22 -0
- package/samples/todo/components/BreadCrumb.tsx +44 -0
- package/samples/todo/components/ManageMembers.tsx +134 -0
- package/samples/todo/components/NavBar.tsx +57 -0
- package/samples/todo/components/SpaceMembers.tsx +76 -0
- package/samples/todo/components/Spaces.tsx +28 -0
- package/samples/todo/components/TimeInfo.tsx +17 -0
- package/samples/todo/components/Todo.tsx +72 -0
- package/samples/todo/components/TodoList.tsx +77 -0
- package/samples/todo/lib/context.ts +31 -0
- package/samples/todo/next.config.js +10 -0
- package/samples/todo/package-lock.json +7527 -0
- package/samples/todo/package.json +45 -0
- package/samples/todo/pages/_app.tsx +50 -0
- package/samples/todo/pages/api/auth/[...nextauth].ts +83 -0
- package/samples/todo/pages/api/zenstack/[...path].ts +16 -0
- package/samples/todo/pages/create-space.tsx +114 -0
- package/samples/todo/pages/index.tsx +32 -0
- package/samples/todo/pages/space/[slug]/[listId]/index.tsx +88 -0
- package/samples/todo/pages/space/[slug]/index.tsx +169 -0
- package/samples/todo/postcss.config.js +6 -0
- package/samples/todo/public/avatar.jpg +0 -0
- package/samples/todo/public/favicon.ico +0 -0
- package/samples/todo/public/logo.png +0 -0
- package/samples/todo/public/vercel.svg +4 -0
- package/samples/todo/styles/globals.css +7 -0
- package/samples/todo/tailwind.config.js +11 -0
- package/samples/todo/tsconfig.json +28 -0
- package/samples/todo/types/next-auth.d.ts +14 -0
- package/samples/todo/types/next.d.ts +16 -0
- package/samples/todo/zenstack/migrations/20221014084317_init/migration.sql +153 -0
- package/samples/todo/zenstack/migrations/20221020094651_upate_cli/migration.sql +23 -0
- package/samples/todo/zenstack/migrations/migration_lock.toml +3 -0
- package/samples/todo/zenstack/schema.prisma +126 -0
- package/samples/todo/zenstack/schema.zmodel +161 -0
- package/tests/integration/jest.config.ts +16 -0
- package/tests/integration/package-lock.json +1081 -0
- package/tests/integration/package.json +27 -0
- package/tests/integration/tests/operation-coverate.test.ts +563 -0
- package/tests/integration/tests/operations.zmodel +69 -0
- package/tests/integration/tests/todo-e2e.test.ts +577 -0
- package/tests/integration/tests/todo.zmodel +123 -0
- package/tests/integration/tests/tsconfig.template.json +10 -0
- package/tests/integration/tests/utils.ts +133 -0
- package/tests/integration/tsconfig.json +10 -0
- package/out/cli/cli-util.js +0 -64
- package/out/cli/cli-util.js.map +0 -1
- package/out/cli/generator.js +0 -1
- package/out/cli/generator.js.map +0 -1
- package/out/cli/index.js +0 -46
- package/out/cli/index.js.map +0 -1
- package/out/cli/package.template.json +0 -10
- package/out/extension.js +0 -81
- package/out/extension.js.map +0 -1
- package/out/generator/constants.js +0 -9
- package/out/generator/constants.js.map +0 -1
- package/out/generator/data-server/index.js +0 -1
- package/out/generator/data-server/index.js.map +0 -1
- package/out/generator/index.js +0 -98
- package/out/generator/index.js.map +0 -1
- package/out/generator/next-auth/index.js.map +0 -1
- package/out/generator/prisma/expression-writer.js +0 -287
- package/out/generator/prisma/expression-writer.js.map +0 -1
- package/out/generator/prisma/index.js +0 -38
- package/out/generator/prisma/index.js.map +0 -1
- package/out/generator/prisma/plain-expression-builder.js +0 -69
- package/out/generator/prisma/plain-expression-builder.js.map +0 -1
- package/out/generator/prisma/prisma-builder.js +0 -307
- package/out/generator/prisma/prisma-builder.js.map +0 -1
- package/out/generator/prisma/query-gard-generator.js +0 -159
- package/out/generator/prisma/query-gard-generator.js.map +0 -1
- package/out/generator/prisma/schema-generator.js +0 -201
- package/out/generator/prisma/schema-generator.js.map +0 -1
- package/out/generator/query-guard/index.js +0 -2
- package/out/generator/query-guard/index.js.map +0 -1
- package/out/generator/react-hooks/index.js +0 -179
- package/out/generator/react-hooks/index.js.map +0 -1
- package/out/generator/server/data/data-generator.js +0 -376
- package/out/generator/server/data/data-generator.js.map +0 -1
- package/out/generator/server/data/expression-writer.js +0 -287
- package/out/generator/server/data/expression-writer.js.map +0 -1
- package/out/generator/server/data/plain-expression-builder.js +0 -69
- package/out/generator/server/data/plain-expression-builder.js.map +0 -1
- package/out/generator/server/data-generator.js +0 -82
- package/out/generator/server/data-generator.js.map +0 -1
- package/out/generator/server/expression-writer.js +0 -1
- package/out/generator/server/expression-writer.js.map +0 -1
- package/out/generator/server/function/function-generator.js +0 -50
- package/out/generator/server/function/function-generator.js.map +0 -1
- package/out/generator/server/function-generator.js +0 -13
- package/out/generator/server/function-generator.js.map +0 -1
- package/out/generator/server/index.js +0 -88
- package/out/generator/server/index.js.map +0 -1
- package/out/generator/server/js-expression-builder.js +0 -1
- package/out/generator/server/js-expression-builder.js.map +0 -1
- package/out/generator/server/plain-expression-builder.js +0 -1
- package/out/generator/server/plain-expression-builder.js.map +0 -1
- package/out/generator/server/server-code-generator.js +0 -3
- package/out/generator/server/server-code-generator.js.map +0 -1
- package/out/generator/server/server-code-writer.js +0 -1
- package/out/generator/server/server-code-writer.js.map +0 -1
- package/out/generator/service/index.js +0 -133
- package/out/generator/service/index.js.map +0 -1
- package/out/generator/types.js +0 -10
- package/out/generator/types.js.map +0 -1
- package/out/generator/utils.js +0 -10
- package/out/generator/utils.js.map +0 -1
- package/out/language-server/generated/ast.js +0 -386
- package/out/language-server/generated/ast.js.map +0 -1
- package/out/language-server/generated/grammar.js.map +0 -1
- package/out/language-server/generated/module.js +0 -23
- package/out/language-server/generated/module.js.map +0 -1
- package/out/language-server/main.js +0 -12
- package/out/language-server/main.js.map +0 -1
- package/out/language-server/types.js +0 -3
- package/out/language-server/types.js.map +0 -1
- package/out/language-server/zmodel-index.js +0 -38
- package/out/language-server/zmodel-index.js.map +0 -1
- package/out/language-server/zmodel-linker.js +0 -241
- package/out/language-server/zmodel-linker.js.map +0 -1
- package/out/language-server/zmodel-module.js +0 -51
- package/out/language-server/zmodel-module.js.map +0 -1
- package/out/language-server/zmodel-scope.js +0 -30
- package/out/language-server/zmodel-scope.js.map +0 -1
- package/out/language-server/zmodel-validator.js +0 -25
- package/out/language-server/zmodel-validator.js.map +0 -1
- package/out/utils/indent-string.js +0 -9
- package/out/utils/indent-string.js.map +0 -1
|
@@ -0,0 +1,300 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AttributeArg,
|
|
3
|
+
DataModel,
|
|
4
|
+
DataModelAttribute,
|
|
5
|
+
DataModelField,
|
|
6
|
+
DataModelFieldAttribute,
|
|
7
|
+
DataSource,
|
|
8
|
+
Enum,
|
|
9
|
+
Expression,
|
|
10
|
+
InvocationExpr,
|
|
11
|
+
isArrayExpr,
|
|
12
|
+
isInvocationExpr,
|
|
13
|
+
isLiteralExpr,
|
|
14
|
+
isReferenceExpr,
|
|
15
|
+
LiteralExpr,
|
|
16
|
+
} from '@lang/generated/ast';
|
|
17
|
+
import { writeFile } from 'fs/promises';
|
|
18
|
+
import { AstNode } from 'langium';
|
|
19
|
+
import path from 'path';
|
|
20
|
+
import { GUARD_FIELD_NAME, TRANSACTION_FIELD_NAME } from '../constants';
|
|
21
|
+
import { Context, GeneratorError } from '../types';
|
|
22
|
+
import {
|
|
23
|
+
AttributeArg as PrismaAttributeArg,
|
|
24
|
+
AttributeArgValue as PrismaAttributeArgValue,
|
|
25
|
+
DataSourceUrl as PrismaDataSourceUrl,
|
|
26
|
+
FieldAttribute as PrismaFieldAttribute,
|
|
27
|
+
ModelAttribute as PrismaModelAttribute,
|
|
28
|
+
Model as PrismaDataModel,
|
|
29
|
+
FieldReference as PrismaFieldReference,
|
|
30
|
+
FieldReferenceArg as PrismaFieldReferenceArg,
|
|
31
|
+
FunctionCall as PrismaFunctionCall,
|
|
32
|
+
FunctionCallArg as PrismaFunctionCallArg,
|
|
33
|
+
PrismaModel,
|
|
34
|
+
ModelFieldType,
|
|
35
|
+
} from './prisma-builder';
|
|
36
|
+
|
|
37
|
+
const supportedProviders = ['postgresql', 'mysql', 'sqlite', 'sqlserver'];
|
|
38
|
+
const supportedAttrbutes = [
|
|
39
|
+
'id',
|
|
40
|
+
'index',
|
|
41
|
+
'relation',
|
|
42
|
+
'default',
|
|
43
|
+
'createdAt',
|
|
44
|
+
'updatedAt',
|
|
45
|
+
'unique',
|
|
46
|
+
];
|
|
47
|
+
|
|
48
|
+
export default class PrismaSchemaGenerator {
|
|
49
|
+
constructor(private readonly context: Context) {}
|
|
50
|
+
|
|
51
|
+
async generate() {
|
|
52
|
+
const { schema } = this.context;
|
|
53
|
+
const prisma = new PrismaModel();
|
|
54
|
+
|
|
55
|
+
for (const decl of schema.declarations) {
|
|
56
|
+
switch (decl.$type) {
|
|
57
|
+
case DataSource:
|
|
58
|
+
this.generateDataSource(prisma, decl as DataSource);
|
|
59
|
+
break;
|
|
60
|
+
|
|
61
|
+
case Enum:
|
|
62
|
+
this.generateEnum(prisma, decl as Enum);
|
|
63
|
+
break;
|
|
64
|
+
|
|
65
|
+
case DataModel:
|
|
66
|
+
this.generateModel(prisma, decl as DataModel);
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
this.generateGenerator(prisma);
|
|
72
|
+
|
|
73
|
+
const outFile = path.join(this.context.outDir, 'schema.prisma');
|
|
74
|
+
await writeFile(outFile, prisma.toString());
|
|
75
|
+
return outFile;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
private generateDataSource(prisma: PrismaModel, dataSource: DataSource) {
|
|
79
|
+
let provider: string | undefined = undefined;
|
|
80
|
+
let url: PrismaDataSourceUrl | undefined = undefined;
|
|
81
|
+
let shadowDatabaseUrl: PrismaDataSourceUrl | undefined = undefined;
|
|
82
|
+
|
|
83
|
+
for (const f of dataSource.fields) {
|
|
84
|
+
switch (f.name) {
|
|
85
|
+
case 'provider': {
|
|
86
|
+
if (this.isStringLiteral(f.value)) {
|
|
87
|
+
provider = f.value.value as string;
|
|
88
|
+
} else {
|
|
89
|
+
throw new GeneratorError(
|
|
90
|
+
'Datasource provider must be set to a string'
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
if (!supportedProviders.includes(provider)) {
|
|
94
|
+
throw new GeneratorError(
|
|
95
|
+
`Provider ${provider} is not supported. Supported providers: ${supportedProviders.join(
|
|
96
|
+
', '
|
|
97
|
+
)}`
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
break;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
case 'url': {
|
|
104
|
+
const r = this.extractDataSourceUrl(f.value);
|
|
105
|
+
if (!r) {
|
|
106
|
+
throw new GeneratorError(
|
|
107
|
+
'Invalid value for datasource url'
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
url = r;
|
|
111
|
+
break;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
case 'shadowDatabaseUrl': {
|
|
115
|
+
const r = this.extractDataSourceUrl(f.value);
|
|
116
|
+
if (!r) {
|
|
117
|
+
throw new GeneratorError(
|
|
118
|
+
'Invalid value for datasource url'
|
|
119
|
+
);
|
|
120
|
+
}
|
|
121
|
+
shadowDatabaseUrl = r;
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!provider) {
|
|
128
|
+
throw new GeneratorError('Datasource is missing "provider" field');
|
|
129
|
+
}
|
|
130
|
+
if (!url) {
|
|
131
|
+
throw new GeneratorError('Datasource is missing "url" field');
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
prisma.addDataSource(dataSource.name, provider, url, shadowDatabaseUrl);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
private extractDataSourceUrl(fieldValue: LiteralExpr | InvocationExpr) {
|
|
138
|
+
if (this.isStringLiteral(fieldValue)) {
|
|
139
|
+
return new PrismaDataSourceUrl(fieldValue.value as string, false);
|
|
140
|
+
} else if (
|
|
141
|
+
isInvocationExpr(fieldValue) &&
|
|
142
|
+
fieldValue.function.ref?.name === 'env' &&
|
|
143
|
+
fieldValue.args.length === 1 &&
|
|
144
|
+
this.isStringLiteral(fieldValue.args[0].value)
|
|
145
|
+
) {
|
|
146
|
+
return new PrismaDataSourceUrl(
|
|
147
|
+
fieldValue.args[0].value.value as string,
|
|
148
|
+
true
|
|
149
|
+
);
|
|
150
|
+
} else {
|
|
151
|
+
return null;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private generateGenerator(prisma: PrismaModel) {
|
|
156
|
+
prisma.addGenerator(
|
|
157
|
+
'client',
|
|
158
|
+
'prisma-client-js',
|
|
159
|
+
path.join('../', this.context.generatedCodeDir, '.prisma'),
|
|
160
|
+
['fieldReference', 'interactiveTransactions']
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private generateModel(prisma: PrismaModel, decl: DataModel) {
|
|
165
|
+
const model = prisma.addModel(decl.name);
|
|
166
|
+
for (const field of decl.fields) {
|
|
167
|
+
this.generateModelField(model, field);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// add an "zenstack_guard" field for dealing with pure auth() related conditions
|
|
171
|
+
model.addField(GUARD_FIELD_NAME, 'Boolean', [
|
|
172
|
+
new PrismaFieldAttribute('default', [
|
|
173
|
+
new PrismaAttributeArg(
|
|
174
|
+
undefined,
|
|
175
|
+
new PrismaAttributeArgValue('Boolean', true)
|
|
176
|
+
),
|
|
177
|
+
]),
|
|
178
|
+
]);
|
|
179
|
+
|
|
180
|
+
// add an "zenstack_transaction" field for tracking records created/updated with nested writes
|
|
181
|
+
model.addField(TRANSACTION_FIELD_NAME, 'String?');
|
|
182
|
+
|
|
183
|
+
for (const attr of decl.attributes.filter((attr) =>
|
|
184
|
+
supportedAttrbutes.includes(attr.decl.ref?.name!)
|
|
185
|
+
)) {
|
|
186
|
+
this.generateModelAttribute(model, attr);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
private generateModelField(model: PrismaDataModel, field: DataModelField) {
|
|
191
|
+
const type = new ModelFieldType(
|
|
192
|
+
(field.type.type || field.type.reference?.ref?.name)!,
|
|
193
|
+
field.type.array,
|
|
194
|
+
field.type.optional
|
|
195
|
+
);
|
|
196
|
+
|
|
197
|
+
const attributes = field.attributes
|
|
198
|
+
.filter((attr) => supportedAttrbutes.includes(attr.decl.ref?.name!))
|
|
199
|
+
.map((attr) => this.makeFieldAttribute(attr));
|
|
200
|
+
model.addField(field.name, type, attributes);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
private makeFieldAttribute(attr: DataModelFieldAttribute) {
|
|
204
|
+
return new PrismaFieldAttribute(
|
|
205
|
+
attr.decl.ref?.name!,
|
|
206
|
+
attr.args.map((arg) => this.makeAttributeArg(arg))
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
makeAttributeArg(arg: AttributeArg): PrismaAttributeArg {
|
|
211
|
+
return new PrismaAttributeArg(
|
|
212
|
+
arg.name,
|
|
213
|
+
this.makeAttributeArgValue(arg.value)
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
makeAttributeArgValue(node: Expression): PrismaAttributeArgValue {
|
|
218
|
+
if (isLiteralExpr(node)) {
|
|
219
|
+
switch (typeof node.value) {
|
|
220
|
+
case 'string':
|
|
221
|
+
return new PrismaAttributeArgValue('String', node.value);
|
|
222
|
+
case 'number':
|
|
223
|
+
return new PrismaAttributeArgValue('Number', node.value);
|
|
224
|
+
case 'boolean':
|
|
225
|
+
return new PrismaAttributeArgValue('Boolean', node.value);
|
|
226
|
+
default:
|
|
227
|
+
throw new GeneratorError(
|
|
228
|
+
`Unexpected literal type: ${typeof node.value}`
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
} else if (isArrayExpr(node)) {
|
|
232
|
+
return new PrismaAttributeArgValue(
|
|
233
|
+
'Array',
|
|
234
|
+
new Array(
|
|
235
|
+
...node.items.map((item) =>
|
|
236
|
+
this.makeAttributeArgValue(item)
|
|
237
|
+
)
|
|
238
|
+
)
|
|
239
|
+
);
|
|
240
|
+
} else if (isReferenceExpr(node)) {
|
|
241
|
+
return new PrismaAttributeArgValue(
|
|
242
|
+
'FieldReference',
|
|
243
|
+
new PrismaFieldReference(
|
|
244
|
+
node.target.ref?.name!,
|
|
245
|
+
node.args.map(
|
|
246
|
+
(arg) =>
|
|
247
|
+
new PrismaFieldReferenceArg(arg.name, arg.value)
|
|
248
|
+
)
|
|
249
|
+
)
|
|
250
|
+
);
|
|
251
|
+
} else if (isInvocationExpr(node)) {
|
|
252
|
+
// invocation
|
|
253
|
+
return new PrismaAttributeArgValue(
|
|
254
|
+
'FunctionCall',
|
|
255
|
+
this.makeFunctionCall(node)
|
|
256
|
+
);
|
|
257
|
+
} else {
|
|
258
|
+
throw new GeneratorError(
|
|
259
|
+
`Unsupported attribute argument expression type: ${node.$type}`
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
makeFunctionCall(node: InvocationExpr): PrismaFunctionCall {
|
|
265
|
+
return new PrismaFunctionCall(
|
|
266
|
+
node.function.ref?.name!,
|
|
267
|
+
node.args.map((arg) => {
|
|
268
|
+
if (!isLiteralExpr(arg.value)) {
|
|
269
|
+
throw new GeneratorError(
|
|
270
|
+
'Function call argument must be literal'
|
|
271
|
+
);
|
|
272
|
+
}
|
|
273
|
+
return new PrismaFunctionCallArg(arg.name, arg.value.value);
|
|
274
|
+
})
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
private generateModelAttribute(
|
|
279
|
+
model: PrismaDataModel,
|
|
280
|
+
attr: DataModelAttribute
|
|
281
|
+
) {
|
|
282
|
+
model.attributes.push(
|
|
283
|
+
new PrismaModelAttribute(
|
|
284
|
+
attr.decl.ref?.name!,
|
|
285
|
+
attr.args.map((arg) => this.makeAttributeArg(arg))
|
|
286
|
+
)
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
private generateEnum(prisma: PrismaModel, decl: Enum) {
|
|
291
|
+
prisma.addEnum(
|
|
292
|
+
decl.name,
|
|
293
|
+
decl.fields.map((f) => f.name)
|
|
294
|
+
);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
private isStringLiteral(node: AstNode): node is LiteralExpr {
|
|
298
|
+
return isLiteralExpr(node) && typeof node.value === 'string';
|
|
299
|
+
}
|
|
300
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
import { Context, Generator } from '../types';
|
|
2
|
+
import { Project } from 'ts-morph';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import { paramCase } from 'change-case';
|
|
5
|
+
import { DataModel } from '@lang/generated/ast';
|
|
6
|
+
import colors from 'colors';
|
|
7
|
+
import { extractDataModelsWithAllowRules } from '../utils';
|
|
8
|
+
import { API_ROUTE_NAME, INTERNAL_PACKAGE } from '../constants';
|
|
9
|
+
|
|
10
|
+
export default class ReactHooksGenerator implements Generator {
|
|
11
|
+
async generate(context: Context) {
|
|
12
|
+
const project = new Project();
|
|
13
|
+
|
|
14
|
+
const models = extractDataModelsWithAllowRules(context.schema);
|
|
15
|
+
|
|
16
|
+
this.generateIndex(project, context, models);
|
|
17
|
+
|
|
18
|
+
models.forEach((d) => this.generateModelHooks(project, context, d));
|
|
19
|
+
|
|
20
|
+
await project.save();
|
|
21
|
+
|
|
22
|
+
console.log(colors.blue(' ✔️ React hooks generated'));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
private generateModelHooks(
|
|
26
|
+
project: Project,
|
|
27
|
+
context: Context,
|
|
28
|
+
model: DataModel
|
|
29
|
+
) {
|
|
30
|
+
const fileName = paramCase(model.name);
|
|
31
|
+
const sf = project.createSourceFile(
|
|
32
|
+
path.join(context.generatedCodeDir, `src/hooks/${fileName}.ts`),
|
|
33
|
+
undefined,
|
|
34
|
+
{ overwrite: true }
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
sf.addImportDeclaration({
|
|
38
|
+
namedImports: [{ name: 'Prisma', alias: 'P' }, model.name],
|
|
39
|
+
isTypeOnly: true,
|
|
40
|
+
moduleSpecifier: '../../.prisma',
|
|
41
|
+
});
|
|
42
|
+
sf.addStatements(`import { request } from '${INTERNAL_PACKAGE}';`);
|
|
43
|
+
sf.addStatements(`import { type SWRResponse } from 'swr'`);
|
|
44
|
+
|
|
45
|
+
sf.addStatements(
|
|
46
|
+
`const endpoint = '/api/${API_ROUTE_NAME}/data/${model.name}';`
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
const useFuncBody = sf
|
|
50
|
+
.addFunction({
|
|
51
|
+
name: `use${model.name}`,
|
|
52
|
+
isExported: true,
|
|
53
|
+
})
|
|
54
|
+
.addBody();
|
|
55
|
+
|
|
56
|
+
useFuncBody.addStatements(['const mutate = request.getMutate();']);
|
|
57
|
+
|
|
58
|
+
// create
|
|
59
|
+
useFuncBody
|
|
60
|
+
.addFunction({
|
|
61
|
+
name: 'create',
|
|
62
|
+
isAsync: true,
|
|
63
|
+
typeParameters: [`T extends P.${model.name}CreateArgs`],
|
|
64
|
+
parameters: [
|
|
65
|
+
{ name: 'args', type: `P.${model.name}CreateArgs` },
|
|
66
|
+
],
|
|
67
|
+
})
|
|
68
|
+
.addBody()
|
|
69
|
+
.addStatements([
|
|
70
|
+
`return request.post<P.${model.name}CreateArgs, P.CheckSelect<T, ${model.name}, P.${model.name}GetPayload<T>>>(endpoint, args, mutate);`,
|
|
71
|
+
]);
|
|
72
|
+
|
|
73
|
+
// find
|
|
74
|
+
useFuncBody
|
|
75
|
+
.addFunction({
|
|
76
|
+
name: 'find',
|
|
77
|
+
typeParameters: [`T extends P.${model.name}FindManyArgs`],
|
|
78
|
+
returnType: `SWRResponse<P.CheckSelect<T, ${model.name}[], P.${model.name}GetPayload<T, keyof T>[]>, any>`,
|
|
79
|
+
parameters: [
|
|
80
|
+
{
|
|
81
|
+
name: 'args?',
|
|
82
|
+
type: `P.SelectSubset<T, P.${model.name}FindManyArgs>`,
|
|
83
|
+
},
|
|
84
|
+
],
|
|
85
|
+
})
|
|
86
|
+
.addBody()
|
|
87
|
+
.addStatements([
|
|
88
|
+
`return request.get<P.CheckSelect<T, Array<${model.name}>, Array<P.${model.name}GetPayload<T>>>>(endpoint, args);`,
|
|
89
|
+
]);
|
|
90
|
+
|
|
91
|
+
// get
|
|
92
|
+
useFuncBody
|
|
93
|
+
.addFunction({
|
|
94
|
+
name: 'get',
|
|
95
|
+
typeParameters: [
|
|
96
|
+
`T extends P.Subset<P.${model.name}FindFirstArgs, 'select' | 'include'>`,
|
|
97
|
+
],
|
|
98
|
+
returnType: `SWRResponse<P.CheckSelect<T, ${model.name}, P.${model.name}GetPayload<T, keyof T>>, any>`,
|
|
99
|
+
parameters: [
|
|
100
|
+
{
|
|
101
|
+
name: 'id',
|
|
102
|
+
type: 'String',
|
|
103
|
+
},
|
|
104
|
+
{
|
|
105
|
+
name: 'args?',
|
|
106
|
+
type: `P.SelectSubset<T, P.Subset<P.${model.name}FindFirstArgs, 'select' | 'include'>>`,
|
|
107
|
+
},
|
|
108
|
+
],
|
|
109
|
+
})
|
|
110
|
+
.addBody()
|
|
111
|
+
.addStatements([
|
|
112
|
+
`return request.get<P.CheckSelect<T, ${model.name}, P.${model.name}GetPayload<T>>>(id ? \`\${endpoint}/\${id}\`: null, args);`,
|
|
113
|
+
]);
|
|
114
|
+
|
|
115
|
+
// update
|
|
116
|
+
useFuncBody
|
|
117
|
+
.addFunction({
|
|
118
|
+
name: 'update',
|
|
119
|
+
isAsync: true,
|
|
120
|
+
typeParameters: [
|
|
121
|
+
`T extends Omit<P.${model.name}UpdateArgs, 'where'>`,
|
|
122
|
+
],
|
|
123
|
+
parameters: [
|
|
124
|
+
{ name: 'id', type: 'String' },
|
|
125
|
+
{
|
|
126
|
+
name: 'args',
|
|
127
|
+
type: `Omit<P.${model.name}UpdateArgs, 'where'>`,
|
|
128
|
+
},
|
|
129
|
+
],
|
|
130
|
+
})
|
|
131
|
+
.addBody()
|
|
132
|
+
.addStatements([
|
|
133
|
+
`return request.put<Omit<P.${model.name}UpdateArgs, 'where'>, P.CheckSelect<T, ${model.name}, P.${model.name}GetPayload<T>>>(\`\${endpoint}/\${id}\`, args, mutate);`,
|
|
134
|
+
]);
|
|
135
|
+
|
|
136
|
+
// del
|
|
137
|
+
useFuncBody
|
|
138
|
+
.addFunction({
|
|
139
|
+
name: 'del',
|
|
140
|
+
isAsync: true,
|
|
141
|
+
typeParameters: [
|
|
142
|
+
`T extends Omit<P.${model.name}DeleteArgs, 'where'>`,
|
|
143
|
+
],
|
|
144
|
+
parameters: [
|
|
145
|
+
{ name: 'id', type: 'String' },
|
|
146
|
+
{
|
|
147
|
+
name: 'args?',
|
|
148
|
+
type: `Omit<P.${model.name}DeleteArgs, 'where'>`,
|
|
149
|
+
},
|
|
150
|
+
],
|
|
151
|
+
})
|
|
152
|
+
.addBody()
|
|
153
|
+
.addStatements([
|
|
154
|
+
`return request.del<P.CheckSelect<T, ${model.name}, P.${model.name}GetPayload<T>>>(\`\${endpoint}/\${id}\`, args, mutate);`,
|
|
155
|
+
]);
|
|
156
|
+
|
|
157
|
+
useFuncBody.addStatements([
|
|
158
|
+
'return { create, find, get, update, del };',
|
|
159
|
+
]);
|
|
160
|
+
|
|
161
|
+
sf.formatText();
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
private generateIndex(
|
|
165
|
+
project: Project,
|
|
166
|
+
context: Context,
|
|
167
|
+
models: DataModel[]
|
|
168
|
+
) {
|
|
169
|
+
const sf = project.createSourceFile(
|
|
170
|
+
path.join(context.generatedCodeDir, 'src/hooks/index.ts'),
|
|
171
|
+
undefined,
|
|
172
|
+
{ overwrite: true }
|
|
173
|
+
);
|
|
174
|
+
|
|
175
|
+
sf.addStatements(
|
|
176
|
+
models.map((d) => `export * from './${paramCase(d.name)}';`)
|
|
177
|
+
);
|
|
178
|
+
|
|
179
|
+
sf.formatText();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
import { Context, Generator } from '../types';
|
|
2
|
+
import { Project, StructureKind, VariableDeclarationKind } from 'ts-morph';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
import colors from 'colors';
|
|
5
|
+
import { INTERNAL_PACKAGE } from '../constants';
|
|
6
|
+
|
|
7
|
+
export default class ServiceGenerator implements Generator {
|
|
8
|
+
async generate(context: Context) {
|
|
9
|
+
const project = new Project();
|
|
10
|
+
const sf = project.createSourceFile(
|
|
11
|
+
path.join(context.generatedCodeDir, 'src/index.ts'),
|
|
12
|
+
undefined,
|
|
13
|
+
{ overwrite: true }
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
sf.addImportDeclaration({
|
|
17
|
+
namedImports: ['PrismaClient'],
|
|
18
|
+
moduleSpecifier: '../.prisma',
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
sf.addImportDeclaration({
|
|
22
|
+
namedImports: ['Service', 'PolicyOperationKind', 'QueryContext'],
|
|
23
|
+
moduleSpecifier: INTERNAL_PACKAGE,
|
|
24
|
+
isTypeOnly: true,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const cls = sf.addClass({
|
|
28
|
+
name: 'ZenStackService',
|
|
29
|
+
isExported: true,
|
|
30
|
+
implements: ['Service<PrismaClient>'],
|
|
31
|
+
});
|
|
32
|
+
cls.addMember({
|
|
33
|
+
kind: StructureKind.Property,
|
|
34
|
+
name: 'private readonly _prisma',
|
|
35
|
+
initializer: 'new PrismaClient()',
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
cls.addGetAccessor({
|
|
39
|
+
name: 'db',
|
|
40
|
+
})
|
|
41
|
+
.addBody()
|
|
42
|
+
.setBodyText('return this._prisma;');
|
|
43
|
+
|
|
44
|
+
sf.addVariableStatement({
|
|
45
|
+
declarationKind: VariableDeclarationKind.Let,
|
|
46
|
+
declarations: [
|
|
47
|
+
{
|
|
48
|
+
name: 'guardModule',
|
|
49
|
+
type: 'any',
|
|
50
|
+
},
|
|
51
|
+
],
|
|
52
|
+
});
|
|
53
|
+
cls
|
|
54
|
+
.addMethod({
|
|
55
|
+
name: 'resolveField',
|
|
56
|
+
isAsync: true,
|
|
57
|
+
parameters: [
|
|
58
|
+
{
|
|
59
|
+
name: 'model',
|
|
60
|
+
type: 'string',
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
name: 'field',
|
|
64
|
+
type: 'string',
|
|
65
|
+
},
|
|
66
|
+
],
|
|
67
|
+
})
|
|
68
|
+
.addBody().setBodyText(`
|
|
69
|
+
if (!guardModule) {
|
|
70
|
+
guardModule = await import('./query/guard');
|
|
71
|
+
}
|
|
72
|
+
return guardModule._fieldMapping?.[model]?.[field];
|
|
73
|
+
`);
|
|
74
|
+
|
|
75
|
+
cls
|
|
76
|
+
.addMethod({
|
|
77
|
+
name: 'buildQueryGuard',
|
|
78
|
+
isAsync: true,
|
|
79
|
+
parameters: [
|
|
80
|
+
{
|
|
81
|
+
name: 'model',
|
|
82
|
+
type: 'string',
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
name: 'operation',
|
|
86
|
+
type: 'PolicyOperationKind',
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
name: 'context',
|
|
90
|
+
type: 'QueryContext',
|
|
91
|
+
},
|
|
92
|
+
],
|
|
93
|
+
})
|
|
94
|
+
.addBody().setBodyText(`
|
|
95
|
+
const module: any = await import('./query/guard');
|
|
96
|
+
const provider: (context: QueryContext) => any = module[model+ '_' + operation];
|
|
97
|
+
return provider(context);
|
|
98
|
+
`);
|
|
99
|
+
|
|
100
|
+
sf.addStatements(['export default new ZenStackService();']);
|
|
101
|
+
|
|
102
|
+
sf.formatText();
|
|
103
|
+
await project.save();
|
|
104
|
+
|
|
105
|
+
console.log(colors.blue(` ✔️ ZenStack service generated`));
|
|
106
|
+
}
|
|
107
|
+
}
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Model } from '@lang/generated/ast';
|
|
2
|
+
|
|
3
|
+
export interface Context {
|
|
4
|
+
schema: Model;
|
|
5
|
+
outDir: string;
|
|
6
|
+
generatedCodeDir: string;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export interface Generator {
|
|
10
|
+
generate(context: Context): Promise<void>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export class GeneratorError extends Error {
|
|
14
|
+
constructor(message: string) {
|
|
15
|
+
super(message);
|
|
16
|
+
}
|
|
17
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { DataModel, isDataModel, Model } from '@lang/generated/ast';
|
|
2
|
+
|
|
3
|
+
export function extractDataModelsWithAllowRules(model: Model) {
|
|
4
|
+
return model.declarations.filter(
|
|
5
|
+
(d) =>
|
|
6
|
+
isDataModel(d) &&
|
|
7
|
+
!!d.attributes.find((attr) => attr.decl.ref?.name === 'allow')
|
|
8
|
+
) as DataModel[];
|
|
9
|
+
}
|