very-express 0.6.13-alpha → 0.6.15
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/.github/prompt/plan-dataIsolation.md +67 -0
- package/dist/index.js +276 -276
- package/dist/templates/jsonSchema/Session.json +4 -3
- package/dist/templates/jsonSchema/User.json +8 -7
- package/dist/templates/jsonSchema/UserAuthProfiles.json +4 -3
- package/dist/templates/jsonSchemaRBAC/UserRole.json +4 -6
- package/package.json +1 -1
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
## Plan: Data Isolation Code Generation
|
|
2
|
+
|
|
3
|
+
Config in `vex.config.json` maps schema → FK column → JWT source field. Controller generator reads matching rule and injects isolation logic into all 5 route operations. Column must have `x-foreignKey`.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
### Phase 1: Types & Config
|
|
8
|
+
|
|
9
|
+
**Step 1** — Add `DataIsolationRule` interface to [src/types/types.ts](src/types/types.ts):
|
|
10
|
+
```
|
|
11
|
+
{ schema: string, column: string, source: string }
|
|
12
|
+
```
|
|
13
|
+
- `schema` → documentName to isolate
|
|
14
|
+
- `column` → FK column on that schema (must have `x-foreignKey`)
|
|
15
|
+
- `source` → field name from `req.user` (e.g. `"_id"`, `"tenantId"`)
|
|
16
|
+
|
|
17
|
+
**Step 2** — Add `dataIsolation?: DataIsolationRule[]` to `compilerOptions` in same file.
|
|
18
|
+
|
|
19
|
+
**Step 3** — Add validation in `src/utils/configChecker.ts`:
|
|
20
|
+
- each rule's `schema` must match a loaded documentName
|
|
21
|
+
- each rule's `column` must exist on that schema **with** `x-foreignKey`
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
### Phase 2: Generator Pass-through
|
|
26
|
+
|
|
27
|
+
**Step 4** — In [src/generators/controller/controllers.generator.ts](src/generators/controller/controllers.generator.ts): look up matching `DataIsolationRule` from `compilerOptions.dataIsolation` for current schema, pass as `isolationRule?` to `controllerTemplate()`.
|
|
28
|
+
|
|
29
|
+
**Step 5** — Extend `controllerTemplate()` options type to accept `isolationRule?: DataIsolationRule`.
|
|
30
|
+
|
|
31
|
+
---
|
|
32
|
+
|
|
33
|
+
### Phase 3: Template Code Generation
|
|
34
|
+
|
|
35
|
+
**Step 6** — When `isolationRule` present, add `"Request"` to tsoa decorator imports and `import express from "express"`.
|
|
36
|
+
|
|
37
|
+
**Step 7** — Inject private method into generated class:
|
|
38
|
+
```typescript
|
|
39
|
+
private buildIsolationFilter(request: express.Request): Partial<Post> {
|
|
40
|
+
const user = (request as any).user;
|
|
41
|
+
if (!user) throw new VexResErr(401);
|
|
42
|
+
return { userId: user['_id'] }; // values baked in at generation time
|
|
43
|
+
}
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
**Step 8** — Inject `@Request() request: express.Request` param and isolation into all 5 routes:
|
|
47
|
+
| Route | Change |
|
|
48
|
+
|---|---|
|
|
49
|
+
| `getList` | Spread `buildIsolationFilter(request)` into `body.filter` before `repo.find()` |
|
|
50
|
+
| `get` | Merge into `findOne({ _id: id, ...isolationFilter })` |
|
|
51
|
+
| `post` | Auto-set `body[column] = user[source]` before `repo.create()` |
|
|
52
|
+
| `put / patch` | Pre-verify with `findOne({ _id: id, ...isolationFilter })` before mutate |
|
|
53
|
+
| `delete` | Pre-verify with `findOne({ _id: id, ...isolationFilter })` before delete |
|
|
54
|
+
|
|
55
|
+
---
|
|
56
|
+
|
|
57
|
+
### Relevant Files
|
|
58
|
+
- [src/types/types.ts](src/types/types.ts) — new interface + `compilerOptions` field
|
|
59
|
+
- [src/utils/configChecker.ts](src/utils/configChecker.ts) — startup validation
|
|
60
|
+
- [src/generators/controller/controllers.generator.ts](src/generators/controller/controllers.generator.ts) — rule lookup
|
|
61
|
+
- [src/generators/controller/controller.template.ts](src/generators/controller/controller.template.ts) — all template changes
|
|
62
|
+
|
|
63
|
+
### Verification
|
|
64
|
+
1. Add to `vex.config.json`: `"dataIsolation": [{ "schema": "Post", "column": "userId", "source": "_id" }]`
|
|
65
|
+
2. Run `npm run dev`
|
|
66
|
+
3. Inspect generated `PostController.gen.ts` — confirm `buildIsolationFilter()` in class body and `@Request()` param in all routes
|
|
67
|
+
4. Check a non-isolated schema (e.g. `User`) — controller unchanged
|