very-express 0.8.3 → 0.8.4
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/AGENTS.md +1 -1
- package/dist/index.js +548 -373
- package/dist/templates/_middlewares/Authentication.ts +49 -32
- package/dist/templates/_routes/LoginUI.ts +49 -0
- package/dist/templates/_services/MongooseRepositoryAdapter.ts +67 -5
- package/dist/templates/_services/TypeOrmRepositoryAdapter.ts +70 -4
- package/dist/templates/_services/oauth/OAuthProfileMap.ts +25 -6
- package/dist/templates/_types/auth.ts +15 -0
- package/dist/templates/_types/vex/VexRepository.ts +21 -0
- package/dist/templates/jsonSchema/User.json +8 -1
- package/dist/templates/root/public/js/deleteaccount.js +80 -0
- package/docs/architecture/authAndRouting.md +8 -1
- package/docs/features/accountDeletion.md +150 -0
- package/docs/features/auditFields.md +0 -30
- package/docs/vexJsonSchema.md +11 -1
- package/package.json +1 -1
- package/vex.config.json +3 -1
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
# Account deletion (tombstone)
|
|
2
|
+
|
|
3
|
+
Self-service account deletion for generated apps: `POST /api/auth/delete-account` plus a
|
|
4
|
+
`/delete_account` page. The account is **soft-deleted** — the row survives so business tables can
|
|
5
|
+
keep their required foreign keys — and its credentials and personal data are erased.
|
|
6
|
+
|
|
7
|
+
This covers the identity domain only. Business rows, uploaded objects and third-party identities
|
|
8
|
+
(Firebase, etc.) belong to the application. See the Renomaster spec for the full table-by-table
|
|
9
|
+
split.
|
|
10
|
+
|
|
11
|
+
## Configuration
|
|
12
|
+
|
|
13
|
+
```json
|
|
14
|
+
{
|
|
15
|
+
"auth": { "deleteAccount": true },
|
|
16
|
+
"app": { "showSoftDeleted": false }
|
|
17
|
+
}
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
| Option | Default | Meaning |
|
|
21
|
+
|---|---|---|
|
|
22
|
+
| `auth.deleteAccount` | `true` | Emit the endpoint, the service and the page. Also requires auth to be enabled — a deletion endpoint that cannot authenticate a caller could only delete the wrong account. |
|
|
23
|
+
| `app.showSoftDeleted` | `false` | Lifts the adapter's soft-delete filter app-wide, making tombstones visible (and writable) again. Loud on every generation run. |
|
|
24
|
+
|
|
25
|
+
Both are read with `??`, so an explicit `false` is never swallowed. There is no `deleteAccountPlaceholder`:
|
|
26
|
+
the tombstone label is the constant `"Deleted user"`.
|
|
27
|
+
|
|
28
|
+
## Declaring soft delete
|
|
29
|
+
|
|
30
|
+
Tag one boolean property with `x-vexData: "softDelete"`:
|
|
31
|
+
|
|
32
|
+
```jsonc
|
|
33
|
+
"properties": {
|
|
34
|
+
"deleted": {
|
|
35
|
+
"type": "boolean",
|
|
36
|
+
"default": false,
|
|
37
|
+
"x-vexData": "softDelete",
|
|
38
|
+
"description": "Framework-managed; never client-writable."
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
"required": ["deleted"]
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
The framework then owns that field:
|
|
45
|
+
|
|
46
|
+
- **Not client-writable.** `collectRequestManagedFields()` omits it, so it never appears in a
|
|
47
|
+
generated request body (`PayloadUser`), and the OpenAPI schema stops advertising it. A client
|
|
48
|
+
cannot soft-delete, resurrect or pre-tombstone a row through the CRUD API.
|
|
49
|
+
- **Hidden by default.** The adapters add `marker IS NOT TRUE` (`$ne: true` on Mongo) to every read
|
|
50
|
+
and write filter, so a tombstoned row is unreachable without any call-site change.
|
|
51
|
+
- **Owned writes.** `softDelete(id, data)` writes the marker through the normal update-phase
|
|
52
|
+
machinery, so `updatedAt` / `updatedBy` record who deleted the row and when. No `deletedAt` /
|
|
53
|
+
`deletedBy` columns are added.
|
|
54
|
+
|
|
55
|
+
Validation (generation-time, fails loud):
|
|
56
|
+
|
|
57
|
+
- at most **one** marker per document;
|
|
58
|
+
- it must be `"type": "boolean"`, `"default": false` and **required**.
|
|
59
|
+
|
|
60
|
+
The required/default rules are load-bearing. The hide-filter is `marker IS NOT TRUE`, so a nullable
|
|
61
|
+
column would drop every row written before it existed. `required` + `default: false` produce a
|
|
62
|
+
`boolean NOT NULL DEFAULT false` column.
|
|
63
|
+
|
|
64
|
+
## Query semantics
|
|
65
|
+
|
|
66
|
+
`app.showSoftDeleted` is a **generation-time constant** baked into `VexFieldRegistry.gen.ts`, so no
|
|
67
|
+
request can flip it. There is deliberately no soft-delete key in `Filter<T>`: `Filter` is the shape
|
|
68
|
+
of every HTTP `filter` body, so a filter-level escape hatch would be a client-side bypass.
|
|
69
|
+
|
|
70
|
+
Internal callers use dedicated methods instead:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
softDelete(id, data?) // write the marker + redaction in one update
|
|
74
|
+
findOneWithDeleted(filter, join, select) // skip ONLY the soft-delete term; ownership still applies
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
The hide term is **not** applied to relation loading, which is why the marker is a plain column and
|
|
78
|
+
not TypeORM's `@DeleteDateColumn` — the decorator nulls relation loads, and retained conversations
|
|
79
|
+
must still resolve their author to show "Deleted user".
|
|
80
|
+
|
|
81
|
+
> Enabling `app.showSoftDeleted` is a blunt instrument: it applies to every entity, including ones
|
|
82
|
+
> added later, and it removes the write protection that keeps a tombstone's audit fields frozen.
|
|
83
|
+
|
|
84
|
+
## The token guard
|
|
85
|
+
|
|
86
|
+
`Authentication.middleware` verifies the JWT signature and nothing else, so a deleted user's
|
|
87
|
+
unexpired access token would keep working for the rest of its lifetime. The generated
|
|
88
|
+
`AccountStateGuard` closes that gap.
|
|
89
|
+
|
|
90
|
+
It reads the marker **value** rather than relying on the row being hidden, so the answer holds under
|
|
91
|
+
`app.showSoftDeleted` — a visibility-based check would call every tombstone alive. It uses
|
|
92
|
+
`findOneWithDeleted` so the ownership filter still applies, and treats a missing marker as live
|
|
93
|
+
(`!== true`), matching the adapters.
|
|
94
|
+
|
|
95
|
+
Every other auth path is already fail-closed: refresh and the OAuth exchange look the user up
|
|
96
|
+
through a soft-filtered read, local login compares an email that has been wiped to NULL, and the
|
|
97
|
+
OAuth callback cannot re-bind because the profile rows are gone.
|
|
98
|
+
|
|
99
|
+
The guard is emitted as a no-op when the identity document declares no marker, so the middleware's
|
|
100
|
+
import always resolves and projects without soft delete pay nothing.
|
|
101
|
+
|
|
102
|
+
The exported middleware is **synchronous** on purpose. Both call sites are express chains, and
|
|
103
|
+
Express 4 does not catch a rejected promise — an `async` handler would turn every failure into an
|
|
104
|
+
unhandled rejection. The async work lives in `handle()` and every outcome is routed to `next()`.
|
|
105
|
+
|
|
106
|
+
## Deletion flow
|
|
107
|
+
|
|
108
|
+
`AccountDeletionService.deleteSelf()` takes the identity from `UserContext` only; no id is ever
|
|
109
|
+
accepted from a path or body, so one account cannot delete another. It is idempotent: a repeat call
|
|
110
|
+
returns `alreadyDeleted` without re-stamping the row.
|
|
111
|
+
|
|
112
|
+
Order is load-bearing:
|
|
113
|
+
|
|
114
|
+
1. **Credentials first** — auth profiles, roles, sessions. Tombstoning first would leave the OAuth
|
|
115
|
+
path able to resolve the account through `provider` / `oauthId`, i.e. a deleted user could log
|
|
116
|
+
back into their own tombstone.
|
|
117
|
+
2. **Tombstone last** — `softDelete(userId, TOMBSTONE)`. A mid-flight failure therefore leaves a
|
|
118
|
+
live account that can simply be retried.
|
|
119
|
+
|
|
120
|
+
The tombstone redacts the standard personal fields: `email` → NULL, `name` → `"Deleted user"`,
|
|
121
|
+
`locale` / `profileErrors` → NULL, `active` → false. `email` is NULL rather than `""` because `User`
|
|
122
|
+
declares a unique index on it — a second deletion would collide on the empty string, and the same
|
|
123
|
+
address must be able to register a fresh account immediately.
|
|
124
|
+
|
|
125
|
+
The payload is derived from the project's own schema, so absent or renamed fields are never emitted
|
|
126
|
+
and audit / identity fields are skipped.
|
|
127
|
+
|
|
128
|
+
## The page
|
|
129
|
+
|
|
130
|
+
`GET /delete_account`, served from `LoginUI` rather than as a static file — `express.static("public")`
|
|
131
|
+
would expose `public/delete_account.html` at `/delete_account.html`, not at the route the store
|
|
132
|
+
listing points people at.
|
|
133
|
+
|
|
134
|
+
`LoginUIConfig.deleteAccount` is **optional and defaults to true**, because an app's `server.ts` is
|
|
135
|
+
generated once and is not overwritten on later runs; a required field would stop such a project from
|
|
136
|
+
compiling.
|
|
137
|
+
|
|
138
|
+
The copy is explicit that this is a tombstone, not an erasure. The client clears its stored tokens on
|
|
139
|
+
success: the account is gone the moment the tombstone is written, so keeping a token the server now
|
|
140
|
+
rejects would only produce confusing 401s.
|
|
141
|
+
|
|
142
|
+
## Integration notes
|
|
143
|
+
|
|
144
|
+
- **Migration.** Adding the marker adds a column. `SQL_SYNCHRONIZE=true` covers development;
|
|
145
|
+
production needs `ALTER TABLE "user" ADD COLUMN "deleted" boolean NOT NULL DEFAULT false`.
|
|
146
|
+
- **`allowOverwrite: false`.** A project that pinned `UserModel.gen.ts` / `User.gen.ts` must add the
|
|
147
|
+
column by hand, or clear the `.vex/meta.json` entry for those files.
|
|
148
|
+
- **Per-request cost.** The guard costs one narrow indexed read per authenticated request.
|
|
149
|
+
- **Third-party identity.** vex cannot delete a Firebase account. Do it from the app, and capture the
|
|
150
|
+
provider uid *before* calling the service.
|
|
@@ -237,36 +237,6 @@ Validation runs once, after every schema is loaded (`validateAuditFields()` in
|
|
|
237
237
|
`src/preprocess/auditFields.ts`, called from the pipeline), because R1/R2 are cross-document. All problems
|
|
238
238
|
are collected and reported together; `log.error` exits the generator, so a contradiction never reaches runtime.
|
|
239
239
|
|
|
240
|
-
## 7. Backward compatibility
|
|
241
|
-
|
|
242
|
-
- Schema with no reserved keyword and no tag → nothing changes; generation output is identical.
|
|
243
|
-
- Schema with a literal `default` → unchanged.
|
|
244
|
-
- Existing generated apps: re-run `vex`; entities gain `nullable` audit columns if they were declared.
|
|
245
|
-
- Existing rows keep `createdBy = NULL`. This feature cannot backfill; a downstream app that needs
|
|
246
|
-
ownership on legacy rows must migrate data itself.
|
|
247
|
-
- Tokens issued before the upgrade still work (the `?? _id` fallback on refresh).
|
|
248
|
-
- The prototype branch's lowercase `x-format` values (`primary`, `uuid`, …) are **rejected** — this design
|
|
249
|
-
keeps the current capitalized values (`Primary`, `PrimaryUUID`, `UUID`, `UnixTimestamp`) and only adds
|
|
250
|
-
`Timestamp`.
|
|
251
|
-
|
|
252
|
-
### Breaking since v0.8.2
|
|
253
|
-
|
|
254
|
-
Three changes downstream apps must absorb when they re-run `vex`:
|
|
255
|
-
|
|
256
|
-
1. **The context module was renamed.** `DataIsolationContext.gen.ts` is gone; `UserContext.gen.ts` replaces
|
|
257
|
-
it. `cleanupStaleFiles()` removes the old file on the next run, so anything importing it by name breaks.
|
|
258
|
-
2. **A create without an identity now throws** where it previously wrote NULL. This affects
|
|
259
|
-
`onCreateUserId` only, and only a path that reaches `create()` with no authenticated request context
|
|
260
|
-
(seed scripts, background jobs, or a controller that does not mount `Authentication.middleware`).
|
|
261
|
-
3. **Data isolation becomes effective for the first time.** It used to be mounted only on entities declaring
|
|
262
|
-
`dataIsolation`, which in practice meant it rarely ran at all. It now has a context on every
|
|
263
|
-
authenticating controller. Ownership still wins over the caller's filter, so an endpoint that queries
|
|
264
|
-
*another* user's row by id will silently act on the caller's own row instead. Audit those endpoints per
|
|
265
|
-
app.
|
|
266
|
-
|
|
267
|
-
Downstream apps no longer need the `dataIsolation` config merely to get the ALS context mounted — that
|
|
268
|
-
coupling is gone. Use `dataIsolation` only when ownership filtering is actually wanted.
|
|
269
|
-
|
|
270
240
|
## 8. Mongoose
|
|
271
241
|
|
|
272
242
|
Implemented in parallel with TypeORM — same registry, same strip/inject phases, same values
|
package/docs/vexJsonSchema.md
CHANGED
|
@@ -77,6 +77,16 @@ Each `.json` file defines one REST resource. Filename must match `x-documentConf
|
|
|
77
77
|
| --- | --- |
|
|
78
78
|
| `role` | Marks this field as the RBAC role field. Required for role-based access control to work. |
|
|
79
79
|
| `userId` | Marks the field that holds the identity value. Declare it **exactly once**, on the account schema (`User._id`) — every `onXXUserId` audit field is filled from it. |
|
|
80
|
+
| `softDelete` | Marks the boolean field carrying this document's soft-delete state. **At most one per document.** The repository then hides rows whose marker is set, and the field is removed from generated request bodies. Requires `{ "type": "boolean", "default": false }` and must be `required`, which yields a `boolean NOT NULL DEFAULT false` column. Full reference: [`docs/features/accountDeletion.md`](features/accountDeletion.md). |
|
|
81
|
+
|
|
82
|
+
```jsonc
|
|
83
|
+
"deleted": { "type": "boolean", "default": false, "x-vexData": "softDelete" }
|
|
84
|
+
// and list it in the root "required" array
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
A nullable marker would break the hide-filter (`marker IS NOT TRUE` drops NULL rows), so generation
|
|
88
|
+
rejects it. There is no separate `deletedAt` / `deletedBy`: the existing `updatedAt` / `updatedBy`
|
|
89
|
+
audit fields record who deleted the row and when.
|
|
80
90
|
|
|
81
91
|
---
|
|
82
92
|
|
|
@@ -105,7 +115,7 @@ Works for both `sql` and `mongo` targets. Full reference:
|
|
|
105
115
|
|
|
106
116
|
Generation fails early on a contradiction: an unknown `x-vexData` value, an `onXXUserId` field whose
|
|
107
117
|
column type differs from the tagged identity field, a mismatched timestamp keyword, a missing identity
|
|
108
|
-
field,
|
|
118
|
+
field, an audit field marked `required`, or a malformed soft-delete marker.
|
|
109
119
|
|
|
110
120
|
Declaring a keyword also removes the field from the **request body**: the generator emits
|
|
111
121
|
`export type Payload{Doc} = Omit<{Doc}, "createdAt" | … >;` into `src/system/_types/{Doc}.gen.ts` and the
|
package/package.json
CHANGED
package/vex.config.json
CHANGED
|
@@ -9,11 +9,13 @@
|
|
|
9
9
|
"useUserSchema": true,
|
|
10
10
|
"useObjectID": true,
|
|
11
11
|
"allowApiCreateUpdate_id": false,
|
|
12
|
-
"useStatefulRedisAuth": false
|
|
12
|
+
"useStatefulRedisAuth": false,
|
|
13
|
+
"showSoftDeleted": false
|
|
13
14
|
},
|
|
14
15
|
"auth": {
|
|
15
16
|
"localAuth": true,
|
|
16
17
|
"useHttpOnlyCookieToken": true,
|
|
18
|
+
"deleteAccount": true,
|
|
17
19
|
"oauthProviders": {
|
|
18
20
|
"google": false,
|
|
19
21
|
"github": false
|