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,504 @@
|
|
|
1
|
+
import deepcopy from 'deepcopy';
|
|
2
|
+
import { TRANSACTION_FIELD_NAME } from '../../constants';
|
|
3
|
+
import {
|
|
4
|
+
PolicyOperationKind,
|
|
5
|
+
QueryContext,
|
|
6
|
+
ServerErrorCode,
|
|
7
|
+
Service,
|
|
8
|
+
} from '../../types';
|
|
9
|
+
import { RequestHandlerError } from '../types';
|
|
10
|
+
import { and } from './guard-utils';
|
|
11
|
+
|
|
12
|
+
export class QueryProcessor {
|
|
13
|
+
constructor(private readonly service: Service) {}
|
|
14
|
+
|
|
15
|
+
async processQueryArgsForWrite(
|
|
16
|
+
model: string,
|
|
17
|
+
args: any,
|
|
18
|
+
operation: PolicyOperationKind,
|
|
19
|
+
context: QueryContext,
|
|
20
|
+
transactionId: string
|
|
21
|
+
) {
|
|
22
|
+
const preWriteGuard = args ? deepcopy(args) : {};
|
|
23
|
+
delete preWriteGuard.data;
|
|
24
|
+
delete preWriteGuard.include;
|
|
25
|
+
delete preWriteGuard.select;
|
|
26
|
+
|
|
27
|
+
if (operation === 'update') {
|
|
28
|
+
preWriteGuard.select = { id: true };
|
|
29
|
+
await this.injectSelectForToOneRelation(
|
|
30
|
+
model,
|
|
31
|
+
preWriteGuard.select,
|
|
32
|
+
args.data,
|
|
33
|
+
operation
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
await this.processQueryArgs(
|
|
38
|
+
model,
|
|
39
|
+
preWriteGuard,
|
|
40
|
+
operation,
|
|
41
|
+
context,
|
|
42
|
+
true
|
|
43
|
+
);
|
|
44
|
+
|
|
45
|
+
const writeArgs = args ? deepcopy(args) : {};
|
|
46
|
+
delete writeArgs.include;
|
|
47
|
+
delete writeArgs.select;
|
|
48
|
+
|
|
49
|
+
const includedModels = new Set<string>([model]);
|
|
50
|
+
await this.collectModelsInNestedWrites(
|
|
51
|
+
model,
|
|
52
|
+
writeArgs.data,
|
|
53
|
+
operation,
|
|
54
|
+
context,
|
|
55
|
+
includedModels,
|
|
56
|
+
transactionId
|
|
57
|
+
);
|
|
58
|
+
|
|
59
|
+
return { preWriteGuard, writeArgs, includedModels };
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async injectSelectForToOneRelation(
|
|
63
|
+
model: string,
|
|
64
|
+
select: any,
|
|
65
|
+
updateData: any,
|
|
66
|
+
operation: string
|
|
67
|
+
) {
|
|
68
|
+
if (!updateData) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
for (const [k, v] of Object.entries(updateData)) {
|
|
72
|
+
const fieldInfo = await this.service.resolveField(model, k);
|
|
73
|
+
if (fieldInfo) {
|
|
74
|
+
if (fieldInfo.isArray) {
|
|
75
|
+
select[k] = { select: { ...select?.[k]?.select } };
|
|
76
|
+
await this.injectSelectForToOneRelation(
|
|
77
|
+
fieldInfo.type,
|
|
78
|
+
select[k].select,
|
|
79
|
+
(v as any)?.update,
|
|
80
|
+
operation
|
|
81
|
+
);
|
|
82
|
+
if (Object.keys(select[k].select).length === 0) {
|
|
83
|
+
delete select[k].select;
|
|
84
|
+
}
|
|
85
|
+
} else {
|
|
86
|
+
select[k] = {
|
|
87
|
+
select: { ...select?.[k]?.select, id: true },
|
|
88
|
+
};
|
|
89
|
+
await this.injectSelectForToOneRelation(
|
|
90
|
+
fieldInfo.type,
|
|
91
|
+
select[k].select,
|
|
92
|
+
v,
|
|
93
|
+
operation
|
|
94
|
+
);
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
private async collectModelsInNestedWrites(
|
|
101
|
+
model: string,
|
|
102
|
+
data: any,
|
|
103
|
+
operation: PolicyOperationKind,
|
|
104
|
+
context: QueryContext,
|
|
105
|
+
includedModels: Set<string>,
|
|
106
|
+
transactionId: string
|
|
107
|
+
) {
|
|
108
|
+
if (!data) {
|
|
109
|
+
return;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const arr = Array.isArray(data) ? data : [data];
|
|
113
|
+
|
|
114
|
+
for (const item of arr) {
|
|
115
|
+
item[TRANSACTION_FIELD_NAME] = transactionId + ':' + operation;
|
|
116
|
+
|
|
117
|
+
for (const [k, v] of Object.entries<any>(item)) {
|
|
118
|
+
if (!v) {
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
const fieldInfo = await this.service.resolveField(model, k);
|
|
122
|
+
if (fieldInfo) {
|
|
123
|
+
includedModels.add(fieldInfo.type);
|
|
124
|
+
|
|
125
|
+
for (const [op, payload] of Array.from(
|
|
126
|
+
Object.entries<any>(v)
|
|
127
|
+
)) {
|
|
128
|
+
if (!payload) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
switch (op) {
|
|
132
|
+
case 'create':
|
|
133
|
+
await this.collectModelsInNestedWrites(
|
|
134
|
+
fieldInfo.type,
|
|
135
|
+
payload,
|
|
136
|
+
'create',
|
|
137
|
+
context,
|
|
138
|
+
includedModels,
|
|
139
|
+
transactionId
|
|
140
|
+
);
|
|
141
|
+
break;
|
|
142
|
+
|
|
143
|
+
case 'connectOrCreate':
|
|
144
|
+
if (payload.create) {
|
|
145
|
+
await this.collectModelsInNestedWrites(
|
|
146
|
+
fieldInfo.type,
|
|
147
|
+
payload.create,
|
|
148
|
+
'create',
|
|
149
|
+
context,
|
|
150
|
+
includedModels,
|
|
151
|
+
transactionId
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
break;
|
|
155
|
+
|
|
156
|
+
case 'upsert':
|
|
157
|
+
if (payload.update) {
|
|
158
|
+
await this.collectModelsInNestedWrites(
|
|
159
|
+
fieldInfo.type,
|
|
160
|
+
payload.update,
|
|
161
|
+
'update',
|
|
162
|
+
context,
|
|
163
|
+
includedModels,
|
|
164
|
+
transactionId
|
|
165
|
+
);
|
|
166
|
+
}
|
|
167
|
+
if (payload.update) {
|
|
168
|
+
await this.collectModelsInNestedWrites(
|
|
169
|
+
fieldInfo.type,
|
|
170
|
+
payload.create,
|
|
171
|
+
'create',
|
|
172
|
+
context,
|
|
173
|
+
includedModels,
|
|
174
|
+
transactionId
|
|
175
|
+
);
|
|
176
|
+
}
|
|
177
|
+
break;
|
|
178
|
+
|
|
179
|
+
case 'createMany':
|
|
180
|
+
if (
|
|
181
|
+
payload.data &&
|
|
182
|
+
typeof payload.data[Symbol.iterator] ===
|
|
183
|
+
'function'
|
|
184
|
+
) {
|
|
185
|
+
for (const item of payload.data) {
|
|
186
|
+
await this.collectModelsInNestedWrites(
|
|
187
|
+
fieldInfo.type,
|
|
188
|
+
item,
|
|
189
|
+
'create',
|
|
190
|
+
context,
|
|
191
|
+
includedModels,
|
|
192
|
+
transactionId
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
break;
|
|
196
|
+
}
|
|
197
|
+
break;
|
|
198
|
+
|
|
199
|
+
case 'update':
|
|
200
|
+
if (fieldInfo.isArray) {
|
|
201
|
+
const guard =
|
|
202
|
+
await this.service.buildQueryGuard(
|
|
203
|
+
fieldInfo.type,
|
|
204
|
+
'update',
|
|
205
|
+
context
|
|
206
|
+
);
|
|
207
|
+
|
|
208
|
+
if (
|
|
209
|
+
guard &&
|
|
210
|
+
Object.keys(guard).length > 0
|
|
211
|
+
) {
|
|
212
|
+
payload.where = and(
|
|
213
|
+
payload.where,
|
|
214
|
+
guard
|
|
215
|
+
);
|
|
216
|
+
v.updateMany = payload;
|
|
217
|
+
delete v.update;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
// to-many updates, data is in 'data' field
|
|
221
|
+
await this.collectModelsInNestedWrites(
|
|
222
|
+
fieldInfo.type,
|
|
223
|
+
payload.data,
|
|
224
|
+
'update',
|
|
225
|
+
context,
|
|
226
|
+
includedModels,
|
|
227
|
+
transactionId
|
|
228
|
+
);
|
|
229
|
+
} else {
|
|
230
|
+
// to-one update, payload is data
|
|
231
|
+
await this.collectModelsInNestedWrites(
|
|
232
|
+
fieldInfo.type,
|
|
233
|
+
payload,
|
|
234
|
+
'update',
|
|
235
|
+
context,
|
|
236
|
+
includedModels,
|
|
237
|
+
transactionId
|
|
238
|
+
);
|
|
239
|
+
}
|
|
240
|
+
break;
|
|
241
|
+
|
|
242
|
+
case 'updateMany': {
|
|
243
|
+
const guard =
|
|
244
|
+
await this.service.buildQueryGuard(
|
|
245
|
+
fieldInfo.type,
|
|
246
|
+
'update',
|
|
247
|
+
context
|
|
248
|
+
);
|
|
249
|
+
|
|
250
|
+
if (guard && Object.keys(guard).length > 0) {
|
|
251
|
+
payload.where = and(payload.where, guard);
|
|
252
|
+
v.updateMany = payload;
|
|
253
|
+
}
|
|
254
|
+
await this.collectModelsInNestedWrites(
|
|
255
|
+
fieldInfo.type,
|
|
256
|
+
payload,
|
|
257
|
+
'update',
|
|
258
|
+
context,
|
|
259
|
+
includedModels,
|
|
260
|
+
transactionId
|
|
261
|
+
);
|
|
262
|
+
break;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
case 'delete':
|
|
266
|
+
case 'deleteMany': {
|
|
267
|
+
if (fieldInfo.isArray) {
|
|
268
|
+
v[
|
|
269
|
+
op === 'delete'
|
|
270
|
+
? 'update'
|
|
271
|
+
: 'updateMany'
|
|
272
|
+
] = {
|
|
273
|
+
where: payload,
|
|
274
|
+
data: {
|
|
275
|
+
[TRANSACTION_FIELD_NAME]:
|
|
276
|
+
transactionId + ':delete',
|
|
277
|
+
},
|
|
278
|
+
};
|
|
279
|
+
} else {
|
|
280
|
+
v[
|
|
281
|
+
op === 'delete'
|
|
282
|
+
? 'update'
|
|
283
|
+
: 'updateMany'
|
|
284
|
+
] = {
|
|
285
|
+
[TRANSACTION_FIELD_NAME]:
|
|
286
|
+
transactionId + ':delete',
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
delete v[op];
|
|
290
|
+
break;
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
case 'connect':
|
|
294
|
+
// noop
|
|
295
|
+
break;
|
|
296
|
+
|
|
297
|
+
default:
|
|
298
|
+
throw new RequestHandlerError(
|
|
299
|
+
ServerErrorCode.INVALID_REQUEST_PARAMS,
|
|
300
|
+
`Unsupported nested operation '${op}'`
|
|
301
|
+
);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
async processQueryArgs(
|
|
310
|
+
model: string,
|
|
311
|
+
args: any,
|
|
312
|
+
operation: PolicyOperationKind,
|
|
313
|
+
context: QueryContext,
|
|
314
|
+
injectWhere: boolean = true
|
|
315
|
+
) {
|
|
316
|
+
const r = args ? deepcopy(args) : {};
|
|
317
|
+
|
|
318
|
+
if (injectWhere) {
|
|
319
|
+
const guard = await this.service.buildQueryGuard(
|
|
320
|
+
model,
|
|
321
|
+
operation,
|
|
322
|
+
context
|
|
323
|
+
);
|
|
324
|
+
if (guard) {
|
|
325
|
+
if (!r.where) {
|
|
326
|
+
r.where = guard;
|
|
327
|
+
} else {
|
|
328
|
+
r.where = {
|
|
329
|
+
AND: [guard, r.where],
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (r.include || r.select) {
|
|
336
|
+
if (r.include && r.select) {
|
|
337
|
+
throw new RequestHandlerError(
|
|
338
|
+
ServerErrorCode.INVALID_REQUEST_PARAMS,
|
|
339
|
+
'Passing both "include" and "select" at the same level of query is not supported'
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// "include" and "select" are mutually exclusive
|
|
344
|
+
const selector = r.include ? 'include' : 'select';
|
|
345
|
+
for (const [field, value] of Object.entries(r[selector])) {
|
|
346
|
+
const fieldInfo = await this.service.resolveField(model, field);
|
|
347
|
+
if (fieldInfo) {
|
|
348
|
+
if (fieldInfo.isArray) {
|
|
349
|
+
// note that Prisma only allows to attach filter for "to-many" relation
|
|
350
|
+
// query, so we need to handle "to-one" filter separately in post-processing
|
|
351
|
+
const fieldGuard = await this.processQueryArgs(
|
|
352
|
+
fieldInfo.type,
|
|
353
|
+
value === true ? {} : value,
|
|
354
|
+
operation,
|
|
355
|
+
context
|
|
356
|
+
);
|
|
357
|
+
r[selector][field] = fieldGuard;
|
|
358
|
+
} else {
|
|
359
|
+
// make sure "id" field is included so that we can do post-process filtering
|
|
360
|
+
if (selector === 'select') {
|
|
361
|
+
r[selector].id = true;
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
return r;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
private async getToOneFieldInfo(
|
|
372
|
+
model: string,
|
|
373
|
+
fieldName: string,
|
|
374
|
+
fieldValue: any
|
|
375
|
+
) {
|
|
376
|
+
if (
|
|
377
|
+
!fieldValue ||
|
|
378
|
+
Array.isArray(fieldValue) ||
|
|
379
|
+
typeof fieldValue !== 'object'
|
|
380
|
+
) {
|
|
381
|
+
return null;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const fieldInfo = await this.service.resolveField(model, fieldName);
|
|
385
|
+
if (!fieldInfo || fieldInfo.isArray) {
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return fieldInfo;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
private async collectRelationFields(
|
|
393
|
+
model: string,
|
|
394
|
+
data: any,
|
|
395
|
+
map: Map<string, string[]>
|
|
396
|
+
) {
|
|
397
|
+
for (const [fieldName, fieldValue] of Object.entries(data)) {
|
|
398
|
+
const val: any = fieldValue;
|
|
399
|
+
const fieldInfo = await this.getToOneFieldInfo(
|
|
400
|
+
model,
|
|
401
|
+
fieldName,
|
|
402
|
+
fieldValue
|
|
403
|
+
);
|
|
404
|
+
if (!fieldInfo) {
|
|
405
|
+
continue;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
if (!map.has(fieldInfo.type)) {
|
|
409
|
+
map.set(fieldInfo.type, []);
|
|
410
|
+
}
|
|
411
|
+
map.get(fieldInfo.type)!.push(val.id);
|
|
412
|
+
|
|
413
|
+
// recurse into field value
|
|
414
|
+
this.collectRelationFields(fieldInfo.type, val, map);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
private async checkIdsAgainstPolicy(
|
|
419
|
+
relationFieldMap: Map<string, string[]>,
|
|
420
|
+
operation: PolicyOperationKind,
|
|
421
|
+
context: QueryContext
|
|
422
|
+
) {
|
|
423
|
+
const promises = Array.from(relationFieldMap.entries()).map(
|
|
424
|
+
async ([model, ids]) => {
|
|
425
|
+
const args = {
|
|
426
|
+
select: { id: true },
|
|
427
|
+
where: {
|
|
428
|
+
id: { in: ids },
|
|
429
|
+
},
|
|
430
|
+
};
|
|
431
|
+
|
|
432
|
+
const processedArgs = await this.processQueryArgs(
|
|
433
|
+
model,
|
|
434
|
+
args,
|
|
435
|
+
operation,
|
|
436
|
+
context,
|
|
437
|
+
true
|
|
438
|
+
);
|
|
439
|
+
|
|
440
|
+
const checkedIds: Array<{ id: string }> = await this.service.db[
|
|
441
|
+
model
|
|
442
|
+
].findMany(processedArgs);
|
|
443
|
+
return [model, checkedIds.map((r) => r.id)] as [
|
|
444
|
+
string,
|
|
445
|
+
string[]
|
|
446
|
+
];
|
|
447
|
+
}
|
|
448
|
+
);
|
|
449
|
+
return new Map<string, string[]>(await Promise.all(promises));
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
private async sanitizeData(
|
|
453
|
+
model: string,
|
|
454
|
+
data: any,
|
|
455
|
+
validatedIds: Map<string, string[]>
|
|
456
|
+
): Promise<boolean> {
|
|
457
|
+
let deleted = false;
|
|
458
|
+
for (const [fieldName, fieldValue] of Object.entries(data)) {
|
|
459
|
+
const fieldInfo = await this.getToOneFieldInfo(
|
|
460
|
+
model,
|
|
461
|
+
fieldName,
|
|
462
|
+
fieldValue
|
|
463
|
+
);
|
|
464
|
+
if (!fieldInfo) {
|
|
465
|
+
continue;
|
|
466
|
+
}
|
|
467
|
+
const fv = fieldValue as { id: string };
|
|
468
|
+
const valIds = validatedIds.get(fieldInfo.type);
|
|
469
|
+
|
|
470
|
+
if (!valIds || !valIds.includes(fv.id)) {
|
|
471
|
+
console.log(
|
|
472
|
+
`Deleting field ${fieldName} from ${model}#${data.id}, because field value #${fv.id} failed policy check`
|
|
473
|
+
);
|
|
474
|
+
delete data[fieldName];
|
|
475
|
+
deleted = true;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
const r = await this.sanitizeData(
|
|
479
|
+
fieldInfo.type,
|
|
480
|
+
fieldValue,
|
|
481
|
+
validatedIds
|
|
482
|
+
);
|
|
483
|
+
deleted = deleted || r;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
return deleted;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
async postProcess(
|
|
490
|
+
model: string,
|
|
491
|
+
data: any,
|
|
492
|
+
operation: PolicyOperationKind,
|
|
493
|
+
context: QueryContext
|
|
494
|
+
) {
|
|
495
|
+
const relationFieldMap = new Map<string, string[]>();
|
|
496
|
+
await this.collectRelationFields(model, data, relationFieldMap);
|
|
497
|
+
const validatedIds = await this.checkIdsAgainstPolicy(
|
|
498
|
+
relationFieldMap,
|
|
499
|
+
operation,
|
|
500
|
+
context
|
|
501
|
+
);
|
|
502
|
+
return this.sanitizeData(model, data, validatedIds);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { default as DataHandler } from './data/handler';
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { NextApiRequest, NextApiResponse } from 'next';
|
|
2
|
+
import { ServerErrorCode } from '../types';
|
|
3
|
+
|
|
4
|
+
export interface RequestHandler {
|
|
5
|
+
handle(
|
|
6
|
+
req: NextApiRequest,
|
|
7
|
+
res: NextApiResponse,
|
|
8
|
+
path: string[]
|
|
9
|
+
): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export class RequestHandlerError extends Error {
|
|
13
|
+
constructor(public readonly code: ServerErrorCode, message: string) {
|
|
14
|
+
super(message);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
toString() {
|
|
18
|
+
return `Request handler error: ${this.code}, ${this.message}`;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { NextApiRequest, NextApiResponse } from 'next';
|
|
2
|
+
import { DataHandler } from './handler';
|
|
3
|
+
import { AuthUser, Service } from './types';
|
|
4
|
+
|
|
5
|
+
export type RequestHandlerOptions = {
|
|
6
|
+
getServerUser: (
|
|
7
|
+
req: NextApiRequest,
|
|
8
|
+
res: NextApiResponse
|
|
9
|
+
) => Promise<AuthUser | undefined>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export function requestHandler<DbClient>(
|
|
13
|
+
service: Service<DbClient>,
|
|
14
|
+
options: RequestHandlerOptions
|
|
15
|
+
) {
|
|
16
|
+
const dataHandler = new DataHandler<DbClient>(service, options);
|
|
17
|
+
return async (req: NextApiRequest, res: NextApiResponse) => {
|
|
18
|
+
const [route, ...rest] = req.query.path as string[];
|
|
19
|
+
switch (route) {
|
|
20
|
+
case 'data':
|
|
21
|
+
return dataHandler.handle(req, res, rest);
|
|
22
|
+
|
|
23
|
+
default:
|
|
24
|
+
res.status(404).json({ error: 'Unknown route: ' + route });
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
}
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import useSWR, { useSWRConfig } from 'swr';
|
|
2
|
+
import type { MutatorCallback, MutatorOptions } from 'swr/dist/types';
|
|
3
|
+
|
|
4
|
+
const fetcher = async (url: string, options?: RequestInit) => {
|
|
5
|
+
const res = await fetch(url, options);
|
|
6
|
+
if (!res.ok) {
|
|
7
|
+
const error: Error & { info?: any; status?: number } = new Error(
|
|
8
|
+
'An error occurred while fetching the data.'
|
|
9
|
+
);
|
|
10
|
+
error.info = await res.json();
|
|
11
|
+
error.status = res.status;
|
|
12
|
+
throw error;
|
|
13
|
+
}
|
|
14
|
+
return res.json();
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function makeUrl(url: string, args: unknown) {
|
|
18
|
+
return args ? url + `?q=${encodeURIComponent(JSON.stringify(args))}` : url;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function get<Data, Error = any>(url: string | null, args?: unknown) {
|
|
22
|
+
return useSWR<Data, Error>(url && makeUrl(url, args), fetcher);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export async function post<Data, Result>(
|
|
26
|
+
url: string,
|
|
27
|
+
data: Data,
|
|
28
|
+
mutate: Mutator
|
|
29
|
+
) {
|
|
30
|
+
const r: Result = await fetcher(url, {
|
|
31
|
+
method: 'POST',
|
|
32
|
+
headers: {
|
|
33
|
+
'content-type': 'application/json',
|
|
34
|
+
},
|
|
35
|
+
body: JSON.stringify(data),
|
|
36
|
+
});
|
|
37
|
+
mutate(url, true);
|
|
38
|
+
return r;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export async function put<Data, Result>(
|
|
42
|
+
url: string,
|
|
43
|
+
data: Data,
|
|
44
|
+
mutate: Mutator
|
|
45
|
+
) {
|
|
46
|
+
const r: Result = await fetcher(url, {
|
|
47
|
+
method: 'PUT',
|
|
48
|
+
headers: {
|
|
49
|
+
'content-type': 'application/json',
|
|
50
|
+
},
|
|
51
|
+
body: JSON.stringify(data),
|
|
52
|
+
});
|
|
53
|
+
mutate(url, true);
|
|
54
|
+
return r;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
export async function del<Result>(url: string, args: unknown, mutate: Mutator) {
|
|
58
|
+
const reqUrl = makeUrl(url, args);
|
|
59
|
+
const r: Result = await fetcher(reqUrl, {
|
|
60
|
+
method: 'DELETE',
|
|
61
|
+
});
|
|
62
|
+
const path = url.split('/');
|
|
63
|
+
path.pop();
|
|
64
|
+
mutate(path.join('/'), true);
|
|
65
|
+
return r;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
type Mutator = (
|
|
69
|
+
key: string,
|
|
70
|
+
prefix: boolean,
|
|
71
|
+
data?: any | Promise<any> | MutatorCallback,
|
|
72
|
+
opts?: boolean | MutatorOptions
|
|
73
|
+
) => Promise<any[]>;
|
|
74
|
+
|
|
75
|
+
export function getMutate(): Mutator {
|
|
76
|
+
// https://swr.vercel.app/docs/advanced/cache#mutate-multiple-keys-from-regex
|
|
77
|
+
const { cache, mutate } = useSWRConfig();
|
|
78
|
+
return (
|
|
79
|
+
key: string,
|
|
80
|
+
prefix: boolean,
|
|
81
|
+
data?: any | Promise<any> | MutatorCallback,
|
|
82
|
+
opts?: boolean | MutatorOptions
|
|
83
|
+
) => {
|
|
84
|
+
if (!prefix) {
|
|
85
|
+
return mutate(key, data, opts);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
if (!(cache instanceof Map)) {
|
|
89
|
+
throw new Error(
|
|
90
|
+
'mutate requires the cache provider to be a Map instance'
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const keys = Array.from(cache.keys()).filter(
|
|
95
|
+
(k) => typeof k === 'string' && k.startsWith(key)
|
|
96
|
+
) as string[];
|
|
97
|
+
console.log('Mutating keys:', JSON.stringify(keys));
|
|
98
|
+
const mutations = keys.map((key) => mutate(key, data, opts));
|
|
99
|
+
return Promise.all(mutations);
|
|
100
|
+
};
|
|
101
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export interface DbOperations {
|
|
2
|
+
findMany(args: any): Promise<any[]>;
|
|
3
|
+
findFirst(args: any): Promise<any>;
|
|
4
|
+
create(args: any): Promise<any>;
|
|
5
|
+
update(args: any): Promise<any>;
|
|
6
|
+
delete(args: any): Promise<any>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export type PolicyKind = 'allow' | 'deny';
|
|
10
|
+
|
|
11
|
+
export type PolicyOperationKind = 'create' | 'update' | 'read' | 'delete';
|
|
12
|
+
|
|
13
|
+
export type AuthUser = { id: string } & Record<string, any>;
|
|
14
|
+
|
|
15
|
+
export type QueryContext = {
|
|
16
|
+
user?: AuthUser;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type FieldInfo = { type: string; isArray: boolean };
|
|
20
|
+
|
|
21
|
+
export interface Service<DbClient = any> {
|
|
22
|
+
get db(): DbClient;
|
|
23
|
+
|
|
24
|
+
resolveField(model: string, field: string): Promise<FieldInfo | undefined>;
|
|
25
|
+
|
|
26
|
+
buildQueryGuard(
|
|
27
|
+
model: string,
|
|
28
|
+
operation: PolicyOperationKind,
|
|
29
|
+
context: QueryContext
|
|
30
|
+
): any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export enum ServerErrorCode {
|
|
34
|
+
ENTITY_NOT_FOUND = 'ENTITY_NOT_FOUND',
|
|
35
|
+
INVALID_REQUEST_PARAMS = 'INVALID_REQUEST_PARAMS',
|
|
36
|
+
DENIED_BY_POLICY = 'DENIED_BY_POLICY',
|
|
37
|
+
UNIQUE_CONSTRAINT_VIOLATION = 'UNIQUE_CONSTRAINT_VIOLATION',
|
|
38
|
+
REFERENCE_CONSTRAINT_VIOLATION = 'REFERENCE_CONSTRAINT_VIOLATION',
|
|
39
|
+
UNKNOWN = 'UNKNOWN',
|
|
40
|
+
}
|