@palbase/backend 25.1.0 → 27.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/dist/bin/palbase-backend.cjs +2401 -1039
- package/dist/bin/palbase-backend.cjs.map +1 -1
- package/dist/bin/palbase-backend.js +87 -51
- package/dist/bin/palbase-backend.js.map +1 -1
- package/dist/chunk-CGNN2PUH.js +213 -0
- package/dist/chunk-CGNN2PUH.js.map +1 -0
- package/dist/chunk-DRZFQRJI.js +370 -0
- package/dist/chunk-DRZFQRJI.js.map +1 -0
- package/dist/chunk-GYK6QYS4.js +708 -0
- package/dist/chunk-GYK6QYS4.js.map +1 -0
- package/dist/chunk-I3C4PFIW.js +930 -0
- package/dist/chunk-I3C4PFIW.js.map +1 -0
- package/dist/{chunk-VDF2T4AS.js → chunk-OO7R25AI.js} +1213 -591
- package/dist/chunk-OO7R25AI.js.map +1 -0
- package/dist/chunk-TS4U7NBD.js +885 -0
- package/dist/chunk-TS4U7NBD.js.map +1 -0
- package/dist/{chunk-YOY5DFQS.js → chunk-TWX6JTGJ.js} +76 -34
- package/dist/{chunk-YOY5DFQS.js.map → chunk-TWX6JTGJ.js.map} +1 -1
- package/dist/{chunk-35PNTIRN.js → chunk-VVMJEVQP.js} +63 -162
- package/dist/chunk-VVMJEVQP.js.map +1 -0
- package/dist/{chunk-7D4SUZUM.js → chunk-VXPNPVAG.js} +3 -1
- package/dist/db/env.cjs.map +1 -1
- package/dist/db/env.d.cts +2 -2
- package/dist/db/env.d.ts +2 -2
- package/dist/db/index.cjs +754 -344
- package/dist/db/index.cjs.map +1 -1
- package/dist/db/index.d.cts +2 -2
- package/dist/db/index.d.ts +2 -2
- package/dist/db/index.js +7 -4
- package/dist/engine/index.cjs +2339 -1006
- package/dist/engine/index.cjs.map +1 -1
- package/dist/engine/index.d.cts +6 -6
- package/dist/engine/index.d.ts +6 -6
- package/dist/engine/index.js +7 -6
- package/dist/index-BrvvxSpn.d.ts +4844 -0
- package/dist/index-Bve7BBTL.d.cts +4844 -0
- package/dist/{index-CUomTA3e.d.ts → index-NuzRCuxe.d.ts} +171 -296
- package/dist/{index-ClpDeSos.d.cts → index-VtToZmUm.d.cts} +171 -296
- package/dist/index.cjs +2694 -1169
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +165 -19
- package/dist/index.d.ts +165 -19
- package/dist/index.js +738 -477
- package/dist/index.js.map +1 -1
- package/dist/module-Dl1KFVtc.d.cts +54 -0
- package/dist/module-Dl1KFVtc.d.ts +54 -0
- package/dist/openapi/index.cjs +1330 -484
- package/dist/openapi/index.cjs.map +1 -1
- package/dist/openapi/index.d.cts +4 -2
- package/dist/openapi/index.d.ts +4 -2
- package/dist/openapi/index.js +1264 -474
- package/dist/openapi/index.js.map +1 -1
- package/dist/{registry-dZZ5JKYg.d.ts → registry-B0eyOF9x.d.ts} +1 -1
- package/dist/{registry-CC0WBQq6.d.cts → registry-Bk9_rbNd.d.cts} +1 -1
- package/dist/stack.cjs.map +1 -1
- package/dist/test/index.cjs +705 -141
- package/dist/test/index.cjs.map +1 -1
- package/dist/test/index.d.cts +30 -4
- package/dist/test/index.d.ts +30 -4
- package/dist/test/index.js +472 -124
- package/dist/test/index.js.map +1 -1
- package/docs/README.md +33 -18
- package/docs/auth.md +1 -1
- package/docs/background.md +2 -2
- package/docs/database.md +221 -50
- package/docs/endpoints.md +3 -4
- package/docs/events.md +3 -3
- package/docs/getting-started.md +1 -1
- package/docs/llms-full.txt +401 -117
- package/docs/migrations.md +2 -2
- package/docs/schema.md +19 -10
- package/docs/services.md +116 -26
- package/package.json +8 -4
- package/stager/generics.js +205 -0
- package/stager/stage.js +39 -3
- package/template/AGENTS.md +110 -72
- package/template/db/public.ts +1 -1
- package/template/{controllers → modules/health}/health.controller.ts +1 -1
- package/template/modules/health/health.module.ts +24 -0
- package/template/modules/notes/note.service.test.ts +49 -0
- package/template/modules/notes/note.service.ts +108 -0
- package/template/{controllers → modules/notes}/notes.controller.ts +17 -11
- package/template/modules/notes/notes.module.ts +37 -0
- package/template/package.json +5 -3
- package/template/scripts/test.sh +33 -0
- package/template/tsconfig.json +29 -30
- package/dist/chunk-35PNTIRN.js.map +0 -1
- package/dist/chunk-CJSKYY76.js +0 -627
- package/dist/chunk-CJSKYY76.js.map +0 -1
- package/dist/chunk-CRQKCRGF.js +0 -276
- package/dist/chunk-CRQKCRGF.js.map +0 -1
- package/dist/chunk-G4R6BTLV.js +0 -662
- package/dist/chunk-G4R6BTLV.js.map +0 -1
- package/dist/chunk-VDF2T4AS.js.map +0 -1
- package/dist/chunk-XABBC7JP.js +0 -55
- package/dist/chunk-XABBC7JP.js.map +0 -1
- package/dist/endpoint-CTEHhb7A.d.ts +0 -2386
- package/dist/endpoint-DYHMo6cC.d.cts +0 -2386
- package/dist/index-CW21M9Z3.d.ts +0 -1222
- package/dist/index-CmBK76nx.d.cts +0 -1222
- package/template/services/note.service.test.ts +0 -45
- package/template/services/note.service.ts +0 -76
- /package/dist/{chunk-7D4SUZUM.js.map → chunk-VXPNPVAG.js.map} +0 -0
- /package/template/{models/notes → modules/notes/dto}/create.ts +0 -0
package/docs/llms-full.txt
CHANGED
|
@@ -43,14 +43,16 @@ service the controllers call.
|
|
|
43
43
|
|
|
44
44
|
### The 7 rules (checklist)
|
|
45
45
|
|
|
46
|
-
1. **A controller
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
1. **A controller is exported by NAME and listed in a module.** `@Controller`
|
|
47
|
+
records the class as it decorates it, but that registration only says the
|
|
48
|
+
class EXISTS — a module's `controllers` list is what decides it should be
|
|
49
|
+
served. So the module imports it by name, which means the class is exported;
|
|
50
|
+
an unexported one is registered, owned by nothing, and refused at build with
|
|
51
|
+
`unowned class`. Nothing is default-exported here. (`export default` **is**
|
|
51
52
|
required for `jobs/`, `webhooks/`, `hooks/` and every `db/*.ts`, one class per
|
|
52
|
-
file
|
|
53
|
-
|
|
53
|
+
file — those are read off disk by name, not through a module.) What IS fatal
|
|
54
|
+
is a `@Controller` class that collected zero routes — usually
|
|
55
|
+
`experimentalDecorators` missing from `tsconfig.json`.
|
|
54
56
|
2. **Methods that call a service are `async` and return `Promise<T>`.** Services
|
|
55
57
|
`await Database`, so they return promises; a sync return type on an async body
|
|
56
58
|
is a tsc error. Annotate `: Promise<TodoSchema>`, not `: TodoSchema`, whenever
|
|
@@ -79,7 +81,7 @@ service the controllers call.
|
|
|
79
81
|
|
|
80
82
|
> **Never** emit `defineController`, `defineHandler`, `defineEndpoint`, `route.get(...)`,
|
|
81
83
|
> `req.input`, `req.params`, or `req.errors` — those are the removed legacy model
|
|
82
|
-
> and will not compile against `@palbase/backend`
|
|
84
|
+
> and will not compile against `@palbase/backend` 27.
|
|
83
85
|
|
|
84
86
|
### Complete CRUD example (copy-pasteable, compiles)
|
|
85
87
|
|
|
@@ -103,13 +105,14 @@ export type CreateTodoBody = z.infer<typeof CreateTodoBody>;
|
|
|
103
105
|
|
|
104
106
|
```ts
|
|
105
107
|
// services/todo.service.ts — plain class + singleton. The real work.
|
|
106
|
-
import { Database, NotFound } from "@palbase/backend";
|
|
108
|
+
import { Database, Injectable, NotFound } from "@palbase/backend";
|
|
107
109
|
import type { TodoSchema } from "../models/todos/shared.js";
|
|
108
110
|
|
|
109
111
|
/** The typed surface of ONE table. Naming it keeps the seam one table wide:
|
|
110
112
|
* a test fake implements five methods, not the whole `Database`. */
|
|
111
|
-
type TodosTable = typeof Database.
|
|
113
|
+
type TodosTable = typeof Database.public.todos;
|
|
112
114
|
|
|
115
|
+
@Injectable()
|
|
113
116
|
export class TodoService {
|
|
114
117
|
private readonly todos: TodosTable;
|
|
115
118
|
|
|
@@ -123,7 +126,7 @@ export class TodoService {
|
|
|
123
126
|
}
|
|
124
127
|
|
|
125
128
|
list(userId: string): Promise<TodoSchema[]> {
|
|
126
|
-
return this.todos.findMany({ user_id: userId });
|
|
129
|
+
return this.todos.findMany({ where: { user_id: userId } });
|
|
127
130
|
}
|
|
128
131
|
create(userId: string, title: string): Promise<TodoSchema> {
|
|
129
132
|
return this.todos.insert({ user_id: userId, title });
|
|
@@ -139,14 +142,26 @@ export class TodoService {
|
|
|
139
142
|
}
|
|
140
143
|
}
|
|
141
144
|
|
|
142
|
-
/** The wired instance. Controllers import THIS, never the class. It is also the
|
|
143
|
-
* ONLY supported way to hold a dependency: a controller, job, hook or webhook is
|
|
144
|
-
* constructed with no arguments, and one that declares a constructor parameter is
|
|
145
|
-
* refused with the class named — at decoration time for jobs/hooks/webhooks, and
|
|
146
|
-
* when the route table is built at boot for a controller. */
|
|
147
|
-
export const todoService = new TodoService(Database.tables.todos);
|
|
148
145
|
```
|
|
149
146
|
|
|
147
|
+
```ts
|
|
148
|
+
// todos.module.ts — ONE module says what exists, who owns it, and what it may reach.
|
|
149
|
+
import { Module, type Token } from "@palbase/backend";
|
|
150
|
+
import { TodosController } from "./controllers/todos.controller.ts";
|
|
151
|
+
import { TodoService } from "./services/todo.service.ts";
|
|
152
|
+
|
|
153
|
+
@Module({
|
|
154
|
+
controllers: [TodosController as Token],
|
|
155
|
+
providers: [TodoService as Token],
|
|
156
|
+
})
|
|
157
|
+
export class TodosModule {}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
A class no module lists does not exist: it is refused at build, by name, and it
|
|
161
|
+
never reaches the route table or the OpenAPI document. Nothing is wired by hand
|
|
162
|
+
— a controller names `TodoService` as a constructor parameter and the container
|
|
163
|
+
supplies it. See [services.md](./services.md#your-own-service-layer).
|
|
164
|
+
|
|
150
165
|
```ts
|
|
151
166
|
// controllers/todos.controller.ts — class controller. Thin: delegates to the service.
|
|
152
167
|
import { Controller, Get, Post, Delete, Body, Param, User } from "@palbase/backend";
|
|
@@ -227,7 +242,7 @@ generated client surface) changes; the verb/path do not affect it.
|
|
|
227
242
|
(stage, bundle, extract controller metadata). Exits non-zero on a decorator,
|
|
228
243
|
return-type or version-skew error, so a push that would deploy zero endpoints
|
|
229
244
|
fails on your machine instead. It also regenerates `palbase-env.d.ts` from
|
|
230
|
-
your `db/*.ts` files, which is what types `Database.
|
|
245
|
+
your `db/*.ts` files, which is what types `Database.public.*` (no import, no
|
|
231
246
|
generic) — so run it after editing the schema. There is no separate command
|
|
232
247
|
for that: `build` regenerates everything derived.
|
|
233
248
|
- `palbase push` — deploy the current backend to the selected Environment. For a
|
|
@@ -378,7 +393,7 @@ start. The loop is: edit, validate, push to a dev Environment.
|
|
|
378
393
|
type or an SDK major skew fails here rather than shipping a deploy that
|
|
379
394
|
serves zero endpoints. It is wired into a `pre-push` git hook for you. It also
|
|
380
395
|
regenerates `palbase-env.d.ts` from your `db/*.ts` files, which is what types
|
|
381
|
-
`Database.
|
|
396
|
+
`Database.public.*` (no import, no generic), so run it after editing the
|
|
382
397
|
schema — there is no separate command for that. See
|
|
383
398
|
[migrations.md](./migrations.md) for the schema side.
|
|
384
399
|
- `palbase push` deploys the current backend to the selected Environment. For a
|
|
@@ -556,10 +571,9 @@ export default class RoomsController {
|
|
|
556
571
|
|
|
557
572
|
1. **The controller does not touch the database.** It delegates to a service, as
|
|
558
573
|
above. A method here that reaches for `Database` has moved the logic into the
|
|
559
|
-
layer that is hardest to test. (
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
`export default` here is style.)
|
|
574
|
+
layer that is hardest to test. (Export the class by NAME so its module can
|
|
575
|
+
list it in `controllers`; a class no module lists is refused at build with
|
|
576
|
+
`unowned class`. Nothing here is default-exported.)
|
|
563
577
|
2. **A method that awaits a service is `async` + `Promise<T>`.** `Database`
|
|
564
578
|
returns promises, so a body that `await`s it cannot have a sync return type
|
|
565
579
|
(`: RoomSchema` on an `async` body is a `tsc` error). Both methods above are
|
|
@@ -864,7 +878,7 @@ single-use and expire; a used or expired one fails closed with a `400`.
|
|
|
864
878
|
|
|
865
879
|
- [Row-Level Security](./schema.md#row-level-security-rls) — pushing per-user
|
|
866
880
|
access rules into Postgres, where `auth.uid()` is this same verified user.
|
|
867
|
-
- [Database](./database.md) — how `Database
|
|
881
|
+
- [Database](./database.md) — how `Database.$asService()` steps outside RLS.
|
|
868
882
|
|
|
869
883
|
|
|
870
884
|
|
|
@@ -882,19 +896,19 @@ import { Database } from "@palbase/backend";
|
|
|
882
896
|
Only **middleware** still uses `ctx.db` (see [background.md](./background.md)
|
|
883
897
|
and [events.md](./events.md) for worker/job/hook/webhook examples).
|
|
884
898
|
|
|
885
|
-
## Typed by default — `Database
|
|
899
|
+
## Typed by default — `Database.<şema>.<tablo>`
|
|
886
900
|
|
|
887
|
-
When you declare a schema under `db/*.ts`, `Database.
|
|
901
|
+
When you declare a schema under `db/*.ts`, `Database.public.<name>` is typed everywhere
|
|
888
902
|
with no import and no generic. `insert` demands the right columns; rows come
|
|
889
903
|
back typed; nullable columns are `T | null`. This is the path you should use:
|
|
890
904
|
|
|
891
905
|
```ts
|
|
892
|
-
const todo = await Database.
|
|
906
|
+
const todo = await Database.public.todos.insert({ title: "buy milk" });
|
|
893
907
|
todo.id; // string ✓
|
|
894
908
|
todo.done; // boolean ✓
|
|
895
|
-
const open = await Database.
|
|
896
|
-
await Database.
|
|
897
|
-
await Database.
|
|
909
|
+
const open = await Database.public.todos.findMany({ where: { done: false } });
|
|
910
|
+
await Database.public.todos.update({ where: { id: todo.id }, set: { done: true } });
|
|
911
|
+
await Database.public.todos.delete(todo.id);
|
|
898
912
|
// todo.nope ← compile error
|
|
899
913
|
```
|
|
900
914
|
|
|
@@ -907,18 +921,18 @@ cover, the string-keyed ops are still available:
|
|
|
907
921
|
|
|
908
922
|
| Method | Returns |
|
|
909
923
|
|--------|---------|
|
|
910
|
-
| `Database
|
|
911
|
-
| `Database
|
|
912
|
-
| `Database
|
|
913
|
-
| `Database
|
|
914
|
-
| `Database
|
|
915
|
-
| `Database
|
|
916
|
-
| `Database
|
|
917
|
-
| `Database
|
|
918
|
-
| `Database
|
|
919
|
-
| `Database
|
|
920
|
-
| `Database
|
|
921
|
-
| `Database
|
|
924
|
+
| `Database.$insert(table, data)` | the inserted row (`Record<string, unknown>`) |
|
|
925
|
+
| `Database.$put(table, data, { onConflict })` | the inserted-or-updated row |
|
|
926
|
+
| `Database.$update(table, id, data)` | the updated row, or `null` if none matched |
|
|
927
|
+
| `Database.$delete(table, id)` | `void` |
|
|
928
|
+
| `Database.$updateMany(table, where, set)` | every row the filter matched, updated |
|
|
929
|
+
| `Database.$deleteMany(table, where)` | how many rows went |
|
|
930
|
+
| `Database.$findById(table, id)` | the row or `null` |
|
|
931
|
+
| `Database.$findMany(table, where?, opts?)` | matching rows (array) |
|
|
932
|
+
| `Database.$count(table, where?)` | how many rows match |
|
|
933
|
+
| `Database.$query(sql, params?)` | rows from a SQL query, in the request's own transaction |
|
|
934
|
+
| `Database.$transaction(fn)` | runs a whole transaction plan in one request |
|
|
935
|
+
| `Database.$attempt(fn)` | a savepoint: a failure inside rolls back only its own writes |
|
|
922
936
|
|
|
923
937
|
### Filters
|
|
924
938
|
|
|
@@ -926,26 +940,103 @@ A filter's keys are ANDed. Each value is either a plain value (equality) or an
|
|
|
926
940
|
operator object:
|
|
927
941
|
|
|
928
942
|
```ts
|
|
929
|
-
await Database.
|
|
930
|
-
|
|
931
|
-
|
|
932
|
-
|
|
933
|
-
|
|
943
|
+
await Database.public.entries.findMany({
|
|
944
|
+
where: {
|
|
945
|
+
household_id: hid, // equality
|
|
946
|
+
amount_kurus: { gte: 1000 }, // >=
|
|
947
|
+
category: { in: ["food", "rent"] },
|
|
948
|
+
status: { neq: "void" },
|
|
949
|
+
},
|
|
950
|
+
});
|
|
951
|
+
```
|
|
952
|
+
|
|
953
|
+
Operators: `gt` · `gte` · `lt` · `lte` · `neq` · `in` · `contains` ·
|
|
954
|
+
`icontains` · `startsWith` · `endsWith` · `isNull`, plus `OR` / `AND` / `NOT`
|
|
955
|
+
for composition. The same filter language works for `updateMany`, `deleteMany`,
|
|
956
|
+
`count` and `search` — one language, so two spellings cannot drift apart. An
|
|
957
|
+
empty `in` list means NO rows (written into the SQL as `false`), never a silent
|
|
958
|
+
full scan.
|
|
959
|
+
|
|
960
|
+
### Comparing one column with another — `col()`
|
|
961
|
+
|
|
962
|
+
The right-hand side of a comparison is normally a VALUE. `col()` puts a COLUMN
|
|
963
|
+
there, which is otherwise a reason to drop to raw SQL:
|
|
964
|
+
|
|
965
|
+
```ts
|
|
966
|
+
import { col } from "@palbase/backend";
|
|
967
|
+
|
|
968
|
+
// Invoices that are not fully paid.
|
|
969
|
+
await Database.public.invoices.findMany({ where: { total: { gt: col("amount_paid") } } });
|
|
970
|
+
```
|
|
971
|
+
|
|
972
|
+
The name is checked at COMPILE time against the row type: `col("amont_paid")`
|
|
973
|
+
does not compile. It is legal in `gt` / `gte` / `lt` / `lte` / `neq` and as a
|
|
974
|
+
bare value (which means equality); anywhere else — inside `in`, in a `set`, in
|
|
975
|
+
`orderBy` — it is refused by name.
|
|
976
|
+
|
|
977
|
+
`col()` is only ever produced by the `col()` call. A `{ "$col": "…" }` object
|
|
978
|
+
that arrives in a request body is NOT a column reference and is refused as an
|
|
979
|
+
unknown operator, so spreading untrusted input into a filter cannot turn a
|
|
980
|
+
tenant predicate into a tautology.
|
|
981
|
+
|
|
982
|
+
### Filtering through a relation — `has`
|
|
983
|
+
|
|
984
|
+
A filter on the row next to yours is the most common reason to drop to raw SQL,
|
|
985
|
+
and it is the one the schema already knows how to answer. Every foreign key you
|
|
986
|
+
declare produces a named relation on BOTH sides, and `has` filters through it:
|
|
987
|
+
|
|
988
|
+
```ts
|
|
989
|
+
// Interests that THIS user picked.
|
|
990
|
+
await Database.public.interests.findMany({
|
|
991
|
+
where: { has: { user_interests: { user_id: userId } } },
|
|
992
|
+
orderBy: { column: "sort_order", direction: "asc" },
|
|
993
|
+
});
|
|
994
|
+
```
|
|
995
|
+
|
|
996
|
+
That compiles to a correlated `IN (SELECT …)` — one statement, one round trip,
|
|
997
|
+
the RLS policies of BOTH tables still applied. The hand-written alternative was
|
|
998
|
+
a `sqlFragment` carrying a subquery, or two queries and a `Set` in JavaScript.
|
|
999
|
+
|
|
1000
|
+
The relation names come from the schema, so `has: { user_intrests: … }` is a
|
|
1001
|
+
compile error, and so is a column that the OTHER table does not have. A table
|
|
1002
|
+
that declares no foreign key at all has no `has` — the key is absent, not empty.
|
|
1003
|
+
|
|
1004
|
+
Both directions work. From the child, the relation is the parent:
|
|
1005
|
+
|
|
1006
|
+
```ts
|
|
1007
|
+
await Database.public.user_interests.findMany({
|
|
1008
|
+
where: { has: { interest: { name: { icontains: "yoga" } } } },
|
|
934
1009
|
});
|
|
935
1010
|
```
|
|
936
1011
|
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
1012
|
+
The inner filter is the SAME language: operators, `OR` / `AND` / `NOT`, `col()`,
|
|
1013
|
+
and a nested `has` one table further (three levels — the relation graph points
|
|
1014
|
+
both ways, so it is a cycle, and an unbounded type would follow it forever).
|
|
1015
|
+
|
|
1016
|
+
`has` is available wherever a filter is: `findMany`, `count`, `updateMany`,
|
|
1017
|
+
`deleteMany`, and inside `$transaction`. In a transaction the update still takes
|
|
1018
|
+
its rows in primary-key order under `FOR NO KEY UPDATE`, exactly as a plain
|
|
1019
|
+
filter does:
|
|
1020
|
+
|
|
1021
|
+
```ts
|
|
1022
|
+
await Database.$transaction((tx) => {
|
|
1023
|
+
tx.public.interests.updateWhere(
|
|
1024
|
+
{ has: { user_interests: { user_id: userId } } },
|
|
1025
|
+
{ last_seen_at: now() },
|
|
1026
|
+
);
|
|
1027
|
+
return [];
|
|
1028
|
+
});
|
|
1029
|
+
```
|
|
941
1030
|
|
|
942
1031
|
### Ordering and paging
|
|
943
1032
|
|
|
944
1033
|
```ts
|
|
945
|
-
await Database.
|
|
946
|
-
{ household_id: hid },
|
|
947
|
-
|
|
948
|
-
|
|
1034
|
+
await Database.public.entries.findMany({
|
|
1035
|
+
where: { household_id: hid },
|
|
1036
|
+
orderBy: { column: "created_at", direction: "desc" },
|
|
1037
|
+
limit: 20,
|
|
1038
|
+
offset: 40,
|
|
1039
|
+
});
|
|
949
1040
|
```
|
|
950
1041
|
|
|
951
1042
|
`orderBy.column` is checked against the row type: a mistyped column name is a
|
|
@@ -969,7 +1060,7 @@ It runs in **the request's own transaction**, not a read-only one: a write insid
|
|
|
969
1060
|
it commits with the rest of the request.
|
|
970
1061
|
|
|
971
1062
|
```ts
|
|
972
|
-
const rows = await Database
|
|
1063
|
+
const rows = await Database.$query(
|
|
973
1064
|
"SELECT c.name, count(*) FROM entries e JOIN categories c ON c.id = e.category_id" +
|
|
974
1065
|
" WHERE e.household_id = $1 GROUP BY c.name",
|
|
975
1066
|
[hid],
|
|
@@ -986,7 +1077,7 @@ Inside a transaction, `insertMany` takes an optional conflict rule — which is
|
|
|
986
1077
|
per row with a `23505` caught around each:
|
|
987
1078
|
|
|
988
1079
|
```ts
|
|
989
|
-
await Database
|
|
1080
|
+
await Database.$transaction((tx) => {
|
|
990
1081
|
tx.tables.merchants.insertMany(rows, { onConflict: ["name"], action: "ignore" });
|
|
991
1082
|
return null;
|
|
992
1083
|
});
|
|
@@ -1013,7 +1104,7 @@ carrying the constraint Postgres named:
|
|
|
1013
1104
|
import { UniqueViolation, Conflict } from "@palbase/backend";
|
|
1014
1105
|
|
|
1015
1106
|
try {
|
|
1016
|
-
return await Database.
|
|
1107
|
+
return await Database.public.users.insert({ email });
|
|
1017
1108
|
} catch (e) {
|
|
1018
1109
|
if (UniqueViolation.is(e) && e.constraint === "users_email_key") {
|
|
1019
1110
|
throw new Conflict("That email is taken", "email_taken");
|
|
@@ -1043,7 +1134,7 @@ middle, so nothing holds a database connection open while your code thinks.
|
|
|
1043
1134
|
```ts
|
|
1044
1135
|
import { Database, NotFound } from "@palbase/backend";
|
|
1045
1136
|
|
|
1046
|
-
const { orderId } = await Database
|
|
1137
|
+
const { orderId } = await Database.$transaction((tx) => {
|
|
1047
1138
|
const order = tx.tables.orders
|
|
1048
1139
|
.insert({ amount: 1000, status: "pending" })
|
|
1049
1140
|
.expectOne(new NotFound("order could not be created"));
|
|
@@ -1092,9 +1183,9 @@ does not write is not part of the transaction:
|
|
|
1092
1183
|
|
|
1093
1184
|
```ts
|
|
1094
1185
|
// Before the transaction: an ordinary value you can branch on.
|
|
1095
|
-
const overrides = await Database.
|
|
1186
|
+
const overrides = await Database.public.category_overrides.findMany({ where: { household_id } });
|
|
1096
1187
|
|
|
1097
|
-
const stmt = await Database
|
|
1188
|
+
const stmt = await Database.$transaction((tx) => { /* … */ });
|
|
1098
1189
|
```
|
|
1099
1190
|
|
|
1100
1191
|
### Writing conditions as filters
|
|
@@ -1125,19 +1216,113 @@ Three expressions may appear in the values you write:
|
|
|
1125
1216
|
|
|
1126
1217
|
| Expression | Where | Meaning |
|
|
1127
1218
|
|---|---|---|
|
|
1128
|
-
| `now()` |
|
|
1129
|
-
| `
|
|
1130
|
-
| `
|
|
1219
|
+
| `now()` | any `set` — `updateMany` and `updateWhere` | the server's clock |
|
|
1220
|
+
| `increment(n)` | any `set` | `column = column + n`, atomically |
|
|
1221
|
+
| `decrement(n)` | any `set` | `column = column - n`, atomically |
|
|
1222
|
+
|
|
1223
|
+
`inc` / `dec` are the OLD names for `increment` / `decrement`. They are the same
|
|
1224
|
+
factory (`inc === increment`), kept as deprecated aliases; write the long names.
|
|
1225
|
+
|
|
1226
|
+
The expressions work on BOTH surfaces — the direct `updateMany` and the plan's
|
|
1227
|
+
`updateWhere` — because they are one object:
|
|
1228
|
+
|
|
1229
|
+
```ts
|
|
1230
|
+
// Direct: one statement, no transaction needed.
|
|
1231
|
+
await Database.public.accounts.updateMany({
|
|
1232
|
+
where: { id: accountId },
|
|
1233
|
+
set: { balance: decrement("5.00") },
|
|
1234
|
+
});
|
|
1235
|
+
|
|
1236
|
+
// Inside a plan: the same expression, the same meaning.
|
|
1237
|
+
await Database.$transaction((tx) => {
|
|
1238
|
+
tx.tables.accounts.updateWhere({ id: accountId }, { balance: decrement("5.00") });
|
|
1239
|
+
return null;
|
|
1240
|
+
});
|
|
1241
|
+
```
|
|
1242
|
+
|
|
1243
|
+
**Give money a STRING amount.** `increment(0.1)` binds a JS number and
|
|
1244
|
+
`0.1 + 0.2` is `0.30000000000000004`; `increment("0.1")` binds the decimal and
|
|
1245
|
+
Postgres adds it exactly. Measured: `12345678901234567890` as a number becomes
|
|
1246
|
+
`12345678901234600000`, as a string it stays whole.
|
|
1247
|
+
|
|
1248
|
+
**The amount is a magnitude, not a signed number.** `decrement("-5")` is
|
|
1249
|
+
refused: a negative amount would ADD money through a call that reads as a
|
|
1250
|
+
withdrawal, and the guard names `increment()` as the way to say the other
|
|
1251
|
+
direction.
|
|
1252
|
+
|
|
1253
|
+
`increment` / `decrement` read the column's current value, which an inserted row
|
|
1254
|
+
does not have — using one in an `insert` is a compile error, and the engine
|
|
1255
|
+
refuses it by name if it arrives untyped.
|
|
1256
|
+
|
|
1257
|
+
### Zero rows is not silent success
|
|
1258
|
+
|
|
1259
|
+
`updateMany` returns EVERY row it matched, and matching nothing is an answer the
|
|
1260
|
+
caller has to read:
|
|
1261
|
+
|
|
1262
|
+
```ts
|
|
1263
|
+
const [row] = await Database.public.accounts.updateMany({
|
|
1264
|
+
where: { id: accountId, balance: { gte: amount } }, // the guard IS the filter
|
|
1265
|
+
set: { balance: decrement(amount) },
|
|
1266
|
+
});
|
|
1267
|
+
if (row === undefined) throw new Conflict("insufficient balance");
|
|
1268
|
+
```
|
|
1269
|
+
|
|
1270
|
+
The filter carries the condition, so the check and the write are ONE statement —
|
|
1271
|
+
there is no window between reading a balance and spending it. An empty array
|
|
1272
|
+
means the balance was too low, or the row does not exist, or RLS hides it from
|
|
1273
|
+
this caller. None of those is "the transfer happened".
|
|
1274
|
+
|
|
1275
|
+
### Append-only tables — a record that cannot be rewritten
|
|
1131
1276
|
|
|
1132
|
-
`
|
|
1133
|
-
|
|
1277
|
+
`appendOnly: true` on a table declaration means a row can be INSERTED and read,
|
|
1278
|
+
never updated and never deleted. A correction is a COMPENSATING ENTRY: a new row
|
|
1279
|
+
carrying the opposite sign.
|
|
1280
|
+
|
|
1281
|
+
```ts
|
|
1282
|
+
export const entries = defineTable("entries", {
|
|
1283
|
+
appendOnly: true,
|
|
1284
|
+
columns: { /* … */ },
|
|
1285
|
+
});
|
|
1286
|
+
```
|
|
1287
|
+
|
|
1288
|
+
**Three locks, because the first two are not enough on their own:**
|
|
1289
|
+
|
|
1290
|
+
| Lock | Stops | Does not stop |
|
|
1291
|
+
|---|---|---|
|
|
1292
|
+
| The TYPE | `update` / `updateMany` / `delete` / `deleteMany` / `put` / `supersede` are not on the table | untyped `$`-prefixed calls, anything off the wire |
|
|
1293
|
+
| The ENGINE | those same operations refused by name, whichever door they come through | SQL that never passes through this SDK |
|
|
1294
|
+
| The DATABASE | `REVOKE UPDATE, DELETE`, a RESTRICTIVE policy, and two triggers — `BEFORE UPDATE OR DELETE` per row and `BEFORE TRUNCATE` per statement | the table's OWNER, who can `ALTER TABLE … DISABLE TRIGGER` first (measured) — as an owner can also simply drop the table |
|
|
1295
|
+
|
|
1296
|
+
The triggers are not belt-and-braces. Measured on a live stack: the platform
|
|
1297
|
+
re-grants table privileges on every boot, which quietly undid the REVOKE, and
|
|
1298
|
+
the service role carries `BYPASSRLS`, so the policy never applied to it either.
|
|
1299
|
+
With both gone the row really was deletable. The triggers are what actually
|
|
1300
|
+
hold, and they say so:
|
|
1301
|
+
|
|
1302
|
+
```
|
|
1303
|
+
ERROR: table entries is append-only: a row cannot be updated or deleted.
|
|
1304
|
+
Write a COMPENSATING ENTRY instead (an INSERT carrying the opposite sign).
|
|
1305
|
+
```
|
|
1306
|
+
|
|
1307
|
+
`SELECT` and `INSERT` are untouched — append-only means the history is kept, not
|
|
1308
|
+
that it is unreadable, and the correction path has to stay open.
|
|
1309
|
+
|
|
1310
|
+
**TRUNCATE needs its own trigger, and finding that out took a measurement.** A
|
|
1311
|
+
row-level trigger never sees a `TRUNCATE` — Postgres truncates without touching
|
|
1312
|
+
rows — so the owner could empty the whole ledger in one statement while every
|
|
1313
|
+
other door was shut. The statement-level `BEFORE TRUNCATE` trigger closes it.
|
|
1314
|
+
|
|
1315
|
+
What is NOT closed: the table's owner can `ALTER TABLE … DISABLE TRIGGER` and
|
|
1316
|
+
then write freely. That is not a hole this can fix — the same owner can drop the
|
|
1317
|
+
table — and saying otherwise would be the kind of claim this table exists to
|
|
1318
|
+
avoid.
|
|
1134
1319
|
|
|
1135
1320
|
### Limits
|
|
1136
1321
|
|
|
1137
1322
|
A plan may carry at most 1000 operations, 5000 rows in one `insertMany`, and
|
|
1138
1323
|
8 MiB of JSON. Exceeding any of them is reported before the request is sent.
|
|
1139
1324
|
|
|
1140
|
-
## Bypassing RLS — `Database
|
|
1325
|
+
## Bypassing RLS — `Database.$asService()`
|
|
1141
1326
|
|
|
1142
1327
|
When a table has [Row-Level Security](./schema.md#row-level-security-rls)
|
|
1143
1328
|
policies, every `Database.*` call runs as the request's verified user, so the
|
|
@@ -1154,7 +1339,7 @@ nothing your code does. See
|
|
|
1154
1339
|
|
|
1155
1340
|
Sometimes you need to read or write **across all users** — an admin endpoint, a
|
|
1156
1341
|
background job that fans out notifications, a cleanup task. For that, call
|
|
1157
|
-
`Database
|
|
1342
|
+
`Database.$asService()`. It returns a sibling client that runs as
|
|
1158
1343
|
**`backend_service_role`** (the role that carries `BYPASSRLS`), exposing the
|
|
1159
1344
|
exact same surface — `tables`, the raw string ops, and `transaction`:
|
|
1160
1345
|
|
|
@@ -1162,14 +1347,14 @@ exact same surface — `tables`, the raw string ops, and `transaction`:
|
|
|
1162
1347
|
import { Database } from "@palbase/backend";
|
|
1163
1348
|
|
|
1164
1349
|
// RLS-enforced (default): only the caller's own rows.
|
|
1165
|
-
const mine = await Database.
|
|
1350
|
+
const mine = await Database.public.todos.findMany({});
|
|
1166
1351
|
|
|
1167
1352
|
// Service-role bypass: every user's rows. Explicit and greppable.
|
|
1168
|
-
const all = await Database
|
|
1169
|
-
const rows = await Database
|
|
1353
|
+
const all = await Database.$asService().tables.todos.findMany({});
|
|
1354
|
+
const rows = await Database.$asService().query("SELECT count(*) FROM todos");
|
|
1170
1355
|
|
|
1171
1356
|
// A service-role transaction (the role is fixed for the whole plan):
|
|
1172
|
-
await Database
|
|
1357
|
+
await Database.$asService().transaction((tx) => {
|
|
1173
1358
|
tx.tables.todos.updateWhere({ id }, { done: true });
|
|
1174
1359
|
return null;
|
|
1175
1360
|
});
|
|
@@ -1182,8 +1367,8 @@ Guidelines:
|
|
|
1182
1367
|
grep for in review.
|
|
1183
1368
|
- **No double-bypass / no nesting.** The sibling does not re-expose
|
|
1184
1369
|
`asService()`, and `tx` never exposes it — a plan's role is fixed for the whole
|
|
1185
|
-
transaction. Use `Database
|
|
1186
|
-
`Database
|
|
1370
|
+
transaction. Use `Database.$transaction(...)` for an authenticated one and
|
|
1371
|
+
`Database.$asService().transaction(...)` for a service-role one; you cannot mix
|
|
1187
1372
|
enforced and bypassed operations inside a single plan.
|
|
1188
1373
|
- **It is a second transaction, on a second connection.** It has to be: the
|
|
1189
1374
|
Postgres role is bound once, when the transaction opens, so a sibling sharing
|
|
@@ -1209,7 +1394,7 @@ Declare your tables under `db/`, **one file per schema**: `db/public.ts` is the
|
|
|
1209
1394
|
schema Palbase expects to find, `db/billing.ts` declares a second one. Each file
|
|
1210
1395
|
default-exports a `defineSchema("<name>", { tables })` call. That drives
|
|
1211
1396
|
[migrations](./migrations.md) (additive changes auto-apply on deploy; type
|
|
1212
|
-
changes need an explicit migration) and makes `Database.
|
|
1397
|
+
changes need an explicit migration) and makes `Database.public.*` typed
|
|
1213
1398
|
everywhere — by default, with no import and no generic.
|
|
1214
1399
|
|
|
1215
1400
|
> Coming from a single `db/schema.ts` with tables declared inline? That layout is
|
|
@@ -1616,14 +1801,14 @@ and an index is not one.
|
|
|
1616
1801
|
## Typed DB access — by default
|
|
1617
1802
|
|
|
1618
1803
|
You do **not** wire anything per endpoint. Saving a file under `db/` regenerates
|
|
1619
|
-
`palbase-env.d.ts`, which types `Database.
|
|
1804
|
+
`palbase-env.d.ts`, which types `Database.public.<name>` everywhere — no import
|
|
1620
1805
|
of the schema, no generic, no cast:
|
|
1621
1806
|
|
|
1622
1807
|
```ts
|
|
1623
1808
|
// services/room.service.ts — the layer that touches the database.
|
|
1624
1809
|
import { Database } from "@palbase/backend";
|
|
1625
1810
|
|
|
1626
|
-
type RoomsTable = typeof Database.
|
|
1811
|
+
type RoomsTable = typeof Database.public.rooms; // typed from your db/*.ts
|
|
1627
1812
|
|
|
1628
1813
|
export class RoomService {
|
|
1629
1814
|
private readonly rooms: RoomsTable;
|
|
@@ -1636,13 +1821,22 @@ export class RoomService {
|
|
|
1636
1821
|
}
|
|
1637
1822
|
}
|
|
1638
1823
|
|
|
1639
|
-
|
|
1824
|
+
```
|
|
1825
|
+
|
|
1826
|
+
```ts
|
|
1827
|
+
// rooms.module.ts
|
|
1828
|
+
import { Module, type Token } from "@palbase/backend";
|
|
1829
|
+
import { RoomsController } from "./controllers/rooms.controller.ts";
|
|
1830
|
+
import { RoomService } from "./services/room.service.ts";
|
|
1831
|
+
|
|
1832
|
+
@Module({ controllers: [RoomsController as Token], providers: [RoomService as Token] })
|
|
1833
|
+
export class RoomsModule {}
|
|
1640
1834
|
```
|
|
1641
1835
|
|
|
1642
1836
|
```ts
|
|
1643
1837
|
// controllers/rooms.controller.ts — HTTP only; no `Database` import here.
|
|
1644
1838
|
import { Controller, Post, Body, z } from "@palbase/backend";
|
|
1645
|
-
import {
|
|
1839
|
+
import { RoomService } from "../services/room.service.js";
|
|
1646
1840
|
|
|
1647
1841
|
const CreateRoomBody = z.object({ name: z.string() });
|
|
1648
1842
|
const RoomOut = z.object({ id: z.string(), name: z.string() });
|
|
@@ -1658,12 +1852,12 @@ export default class RoomsController {
|
|
|
1658
1852
|
}
|
|
1659
1853
|
```
|
|
1660
1854
|
|
|
1661
|
-
`Database.
|
|
1662
|
-
`findById(id)`, `findMany(query?)`. `Database
|
|
1855
|
+
`Database.public.<name>` exposes `insert`, `update(id, data)`, `delete(id)`,
|
|
1856
|
+
`findById(id)`, `findMany(query?)`. `Database.$transaction(fn)` yields a `tx`
|
|
1663
1857
|
whose `tx.tables.<name>` is typed from the same schema, but carries plan
|
|
1664
1858
|
operations (`insert`/`insertMany`/`updateWhere`/`deleteWhere`/`select`) rather
|
|
1665
1859
|
than awaited calls — see [database.md](./database.md#transactions). The raw
|
|
1666
|
-
string-keyed ops (`Database
|
|
1860
|
+
string-keyed ops (`Database.$insert("rooms", …)`, `Database.$query(…)`) are still
|
|
1667
1861
|
available for dynamic table names and read-only SQL.
|
|
1668
1862
|
|
|
1669
1863
|
If you want a row type explicitly, import it from the generated env module:
|
|
@@ -1784,10 +1978,10 @@ export const todos = defineTable("todos", {
|
|
|
1784
1978
|
export default defineSchema("public", { tables: [todos] });
|
|
1785
1979
|
```
|
|
1786
1980
|
|
|
1787
|
-
With this in place, `await Database.
|
|
1981
|
+
With this in place, `await Database.public.todos.findMany({})` returns only the
|
|
1788
1982
|
calling user's rows — no `WHERE owner = …` needed in the handler. To read or
|
|
1789
1983
|
write across all users (e.g. an admin job), use the explicit bypass:
|
|
1790
|
-
`Database
|
|
1984
|
+
`Database.$asService()` (see [database.md](./database.md#bypassing-rls--databaseasservice)).
|
|
1791
1985
|
|
|
1792
1986
|
### How policies are applied
|
|
1793
1987
|
|
|
@@ -1902,7 +2096,7 @@ one, `palbase push` is what moves an Environment's. Until then the declaration i
|
|
|
1902
2096
|
ahead of the tables.
|
|
1903
2097
|
|
|
1904
2098
|
The TYPES move separately again. `palbase build` regenerates `palbase-env.d.ts`
|
|
1905
|
-
from `db/*.ts`, which is what types `Database.
|
|
2099
|
+
from `db/*.ts`, which is what types `Database.public.<name>` in your
|
|
1906
2100
|
services — so after a schema edit, run it. (There is no second command for this:
|
|
1907
2101
|
`build` regenerates everything derived, because one verb to remember is one verb
|
|
1908
2102
|
to forget.) Typed and applied are independent, and knowing which one you are
|
|
@@ -1975,7 +2169,7 @@ existing data gets there.
|
|
|
1975
2169
|
Add `rls: true` + `policies: [policy(...)]` to a table in `db/*.ts`; the
|
|
1976
2170
|
generated migration emits the `ENABLE ROW LEVEL SECURITY` + `CREATE POLICY` DDL.
|
|
1977
2171
|
See [schema.md](./schema.md) for the column builders, the policy DSL, and typed
|
|
1978
|
-
`Database.
|
|
2172
|
+
`Database.public.*` access.
|
|
1979
2173
|
|
|
1980
2174
|
### Hand-writing a policy
|
|
1981
2175
|
|
|
@@ -2059,7 +2253,7 @@ await Cache.del("k");
|
|
|
2059
2253
|
// Stampede-safe read-through: concurrent callers wait on ONE execution of fn
|
|
2060
2254
|
// and share its result, however many of them there are.
|
|
2061
2255
|
const profile = await Cache.getOrSet("user:42", 300, async () => {
|
|
2062
|
-
return Database
|
|
2256
|
+
return Database.$findById("users", "42");
|
|
2063
2257
|
});
|
|
2064
2258
|
```
|
|
2065
2259
|
|
|
@@ -2167,7 +2361,7 @@ export default class CheckoutController {
|
|
|
2167
2361
|
|
|
2168
2362
|
### Overriding a flag — current user vs cross-user
|
|
2169
2363
|
|
|
2170
|
-
Writes mirror the `Database` / `Database
|
|
2364
|
+
Writes mirror the `Database` / `Database.$asService()` model:
|
|
2171
2365
|
|
|
2172
2366
|
- `Flags.setOverride(key, value)` (default) writes an override for the **current
|
|
2173
2367
|
request user** — no `userId` argument, no admin power. It errors on an
|
|
@@ -2175,7 +2369,7 @@ Writes mirror the `Database` / `Database.asService()` model:
|
|
|
2175
2369
|
- `Flags.asService()` returns the cross-user admin surface
|
|
2176
2370
|
(`setOverrideForUser`, `setOverridesForUser`, `clearOverrideForUser`,
|
|
2177
2371
|
`clearAllOverridesForUser`, `batchSetOverrides`) for writing overrides for an
|
|
2178
|
-
**arbitrary** user. Explicit and greppable, just like `Database
|
|
2372
|
+
**arbitrary** user. Explicit and greppable, just like `Database.$asService()`.
|
|
2179
2373
|
|
|
2180
2374
|
```ts
|
|
2181
2375
|
// Current request user — no userId needed:
|
|
@@ -2244,47 +2438,137 @@ A controller's job is HTTP — validate a body through a named schema, name the
|
|
|
2244
2438
|
whose, in what order is the service's. A controller that reaches for `Database`
|
|
2245
2439
|
has moved the logic into the layer that is hardest to test.
|
|
2246
2440
|
|
|
2247
|
-
**2.
|
|
2248
|
-
|
|
2249
|
-
|
|
2441
|
+
**2. A dependency arrives through the CONSTRUCTOR, and the container supplies
|
|
2442
|
+
it.** Mark the class `@Injectable()` and name what it needs as ordinary
|
|
2443
|
+
constructor parameters:
|
|
2250
2444
|
|
|
2251
2445
|
```ts
|
|
2252
2446
|
// services/note.service.ts
|
|
2253
|
-
import { Database } from "@palbase/backend";
|
|
2447
|
+
import { Database, Injectable } from "@palbase/backend";
|
|
2254
2448
|
|
|
2255
|
-
type NotesTable = typeof Database.
|
|
2449
|
+
type NotesTable = typeof Database.public.notes;
|
|
2256
2450
|
|
|
2451
|
+
@Injectable()
|
|
2257
2452
|
export class NoteService {
|
|
2258
|
-
private readonly notes: NotesTable;
|
|
2259
|
-
|
|
2260
|
-
// Assigned in the BODY. A parameter property (`constructor(private notes: …)`)
|
|
2261
|
-
// is refused by Node's type-stripping test runner, for the whole file.
|
|
2262
|
-
constructor(notes: NotesTable) {
|
|
2263
|
-
this.notes = notes;
|
|
2264
|
-
}
|
|
2453
|
+
private readonly notes: NotesTable = Database.public.notes;
|
|
2265
2454
|
|
|
2266
2455
|
list(userId: string) {
|
|
2267
|
-
return this.notes.findMany({ user_id: userId });
|
|
2456
|
+
return this.notes.findMany({ where: { user_id: userId } });
|
|
2268
2457
|
}
|
|
2269
2458
|
}
|
|
2459
|
+
```
|
|
2270
2460
|
|
|
2271
|
-
|
|
2461
|
+
```ts
|
|
2462
|
+
// services/report.service.ts — a service that depends on another service
|
|
2463
|
+
import { Injectable } from "@palbase/backend";
|
|
2464
|
+
import { NoteService } from "./note.service.ts";
|
|
2465
|
+
|
|
2466
|
+
@Injectable()
|
|
2467
|
+
export class ReportService {
|
|
2468
|
+
constructor(private readonly notes: NoteService) {}
|
|
2469
|
+
|
|
2470
|
+
async count(userId: string): Promise<number> {
|
|
2471
|
+
return (await this.notes.list(userId)).length;
|
|
2472
|
+
}
|
|
2473
|
+
}
|
|
2272
2474
|
```
|
|
2273
2475
|
|
|
2476
|
+
A controller asks the same way, and nothing wires it by hand:
|
|
2477
|
+
|
|
2274
2478
|
```ts
|
|
2275
2479
|
// controllers/notes.controller.ts
|
|
2276
|
-
import {
|
|
2480
|
+
import { Controller, Get } from "@palbase/backend";
|
|
2481
|
+
import { NoteService } from "../services/note.service.ts";
|
|
2482
|
+
|
|
2483
|
+
@Controller("/notes")
|
|
2484
|
+
export class NotesController {
|
|
2485
|
+
constructor(private readonly notes: NoteService) {}
|
|
2486
|
+
|
|
2487
|
+
@Get("") list(): Promise<Note[]> { return this.notes.list(currentUserId()); }
|
|
2488
|
+
}
|
|
2489
|
+
```
|
|
2490
|
+
|
|
2491
|
+
There is **no `inject()`, no `@Inject`, and no token registry**. A dependency is
|
|
2492
|
+
named by its parameter's TYPE and by nothing else — which is also why an
|
|
2493
|
+
`interface` cannot be one: an interface does not exist at runtime, so there is
|
|
2494
|
+
nothing to hand over. Use a class, or an `abstract class` when you want the
|
|
2495
|
+
abstraction:
|
|
2496
|
+
|
|
2497
|
+
```ts
|
|
2498
|
+
export abstract class Clock {
|
|
2499
|
+
abstract now(): number;
|
|
2500
|
+
}
|
|
2501
|
+
|
|
2502
|
+
@Injectable()
|
|
2503
|
+
export class SystemClock extends Clock {
|
|
2504
|
+
now(): number { return Date.now(); }
|
|
2505
|
+
}
|
|
2506
|
+
```
|
|
2507
|
+
|
|
2508
|
+
The abstraction is the TOKEN; the implementation is the PROVIDER. `SystemClock`
|
|
2509
|
+
goes in a module's `providers` and `Clock` goes in nobody's: the container
|
|
2510
|
+
resolves an abstraction to the single owned class that `extends` it. Listing
|
|
2511
|
+
`Clock` itself is refused — `new Clock()` succeeds in JavaScript and hands back
|
|
2512
|
+
an object missing every abstract member, and that object would be injected.
|
|
2513
|
+
|
|
2514
|
+
**3. ONE module says what exists, who owns it, and what it may reach.** A class
|
|
2515
|
+
that no module lists does not exist: it is refused at build, by name, and it
|
|
2516
|
+
never reaches the route table, the dispatcher or the OpenAPI document.
|
|
2517
|
+
|
|
2518
|
+
```ts
|
|
2519
|
+
// notes.module.ts — beside the domain it owns, not in a directory we name
|
|
2520
|
+
import { Module, type Token } from "@palbase/backend";
|
|
2521
|
+
import { NotesController } from "./controllers/notes.controller.ts";
|
|
2522
|
+
import { NoteService } from "./services/note.service.ts";
|
|
2523
|
+
import { ReportService } from "./services/report.service.ts";
|
|
2524
|
+
|
|
2525
|
+
@Module({
|
|
2526
|
+
controllers: [NotesController as Token], // the entry points this module owns
|
|
2527
|
+
providers: [NoteService as Token, ReportService as Token], // what it owns
|
|
2528
|
+
exports: [NoteService as Token], // what OTHER modules may reach
|
|
2529
|
+
imports: [], // whose exports THIS module may reach
|
|
2530
|
+
})
|
|
2531
|
+
export class NotesModule {}
|
|
2277
2532
|
```
|
|
2278
2533
|
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2534
|
+
The four lists answer four different questions, and nothing else answers them:
|
|
2535
|
+
|
|
2536
|
+
| list | question |
|
|
2537
|
+
|---|---|
|
|
2538
|
+
| `providers` | which classes does this module OWN |
|
|
2539
|
+
| `controllers` | which entry points does it own |
|
|
2540
|
+
| `exports` | which of its own classes may another module reach |
|
|
2541
|
+
| `imports` | whose exports may this module reach |
|
|
2542
|
+
|
|
2543
|
+
`ReportService` is NOT exported above, so no other module can depend on it —
|
|
2544
|
+
and the build says so by name if one tries. That is the whole boundary: a
|
|
2545
|
+
module's internals stay internal until it says otherwise.
|
|
2546
|
+
|
|
2547
|
+
There is no root module and nothing to mount one into. A small project writes
|
|
2548
|
+
one module file; a large one writes a module per domain and the boundaries are
|
|
2549
|
+
enforced at build rather than by convention.
|
|
2550
|
+
|
|
2551
|
+
**A constructor stays pure wiring.** It runs while the app is coming up, it is
|
|
2552
|
+
synchronous, and it must not do I/O. Real work at startup belongs in `onStart`,
|
|
2553
|
+
which is awaited before the first request is served — a connection opened in a
|
|
2554
|
+
constructor fails somewhere nobody is watching, and one opened in `onStart`
|
|
2555
|
+
fails at boot with the error in front of the operator.
|
|
2556
|
+
|
|
2557
|
+
Rule 2 is also what makes rule 1 pay off. A test can build the graph with a
|
|
2558
|
+
stand-in in place of any part of it, however deep:
|
|
2559
|
+
|
|
2560
|
+
```ts
|
|
2561
|
+
import { isolated } from "@palbase/backend/test";
|
|
2562
|
+
|
|
2563
|
+
const frozen = new (class extends Clock { now() { return 0; } })();
|
|
2564
|
+
const report = isolated().with(Clock, frozen).get(ReportService);
|
|
2565
|
+
```
|
|
2284
2566
|
|
|
2285
|
-
|
|
2286
|
-
|
|
2287
|
-
|
|
2567
|
+
`isolated()` rebuilds the graph with the overrides in place and touches no
|
|
2568
|
+
process-wide state, so the next test in the same file does not meet whatever
|
|
2569
|
+
this one substituted. Or construct the class yourself — `new ReportService(new
|
|
2570
|
+
NoteService())` is ordinary TypeScript, and the container is not required for it
|
|
2571
|
+
to work.
|
|
2288
2572
|
|
|
2289
2573
|
```ts
|
|
2290
2574
|
// services/note.service.test.ts — `npm test`, no database
|
|
@@ -2438,8 +2722,8 @@ import { Database, Job, Log, type JobMeta } from "@palbase/backend";
|
|
|
2438
2722
|
@Job({ schedule: "0 3 * * *", timeout: 120 }) // schedule: standard cron; timeout: optional, seconds
|
|
2439
2723
|
export default class CleanupJob {
|
|
2440
2724
|
async run(meta: JobMeta) {
|
|
2441
|
-
const expired = await Database
|
|
2442
|
-
for (const s of expired) await Database
|
|
2725
|
+
const expired = await Database.$findMany("sessions", { expired: true });
|
|
2726
|
+
for (const s of expired) await Database.$delete("sessions", s.id as string);
|
|
2443
2727
|
Log.info(`cleaned ${expired.length} sessions in ${meta.environmentId}`);
|
|
2444
2728
|
}
|
|
2445
2729
|
}
|
|
@@ -2471,7 +2755,7 @@ import { auth, Database, Log } from "@palbase/backend";
|
|
|
2471
2755
|
|
|
2472
2756
|
export const onUserCreated = auth.onUserCreated(async (event, meta) => {
|
|
2473
2757
|
Log.info(`new user: ${event.user.email}`);
|
|
2474
|
-
await Database
|
|
2758
|
+
await Database.$insert("profiles", {
|
|
2475
2759
|
user_id: event.user.id,
|
|
2476
2760
|
email: event.user.email,
|
|
2477
2761
|
});
|
|
@@ -2504,13 +2788,13 @@ import { Database, Log, On, Webhook, type WebhookMeta } from "@palbase/backend";
|
|
|
2504
2788
|
export default class StripeWebhook {
|
|
2505
2789
|
@On("checkout.session.completed")
|
|
2506
2790
|
async checkoutCompleted(event: unknown, meta: WebhookMeta) {
|
|
2507
|
-
await Database
|
|
2791
|
+
await Database.$insert("orders", { status: "paid", data: event });
|
|
2508
2792
|
}
|
|
2509
2793
|
|
|
2510
2794
|
@On("payment_intent.payment_failed")
|
|
2511
2795
|
async paymentFailed(event: unknown, meta: WebhookMeta) {
|
|
2512
2796
|
Log.error("payment failed");
|
|
2513
|
-
await Database
|
|
2797
|
+
await Database.$insert("payment_failures", { data: event });
|
|
2514
2798
|
}
|
|
2515
2799
|
}
|
|
2516
2800
|
```
|