@palbase/backend 3.0.0 → 5.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.
Files changed (41) hide show
  1. package/dist/{chunk-B7EUJP5W.js → chunk-2N4YNN6F.js} +113 -3
  2. package/dist/chunk-2N4YNN6F.js.map +1 -0
  3. package/dist/{chunk-PHAFZGHN.js → chunk-WUQO76NW.js} +26 -19
  4. package/dist/chunk-WUQO76NW.js.map +1 -0
  5. package/dist/db/index.cjs +117 -2
  6. package/dist/db/index.cjs.map +1 -1
  7. package/dist/db/index.d.cts +2 -2
  8. package/dist/db/index.d.ts +2 -2
  9. package/dist/db/index.js +11 -1
  10. package/dist/{endpoint-DJ98tQd6.d.cts → endpoint-BEHjfvFH.d.cts} +99 -57
  11. package/dist/{endpoint-DJ98tQd6.d.ts → endpoint-BEHjfvFH.d.ts} +99 -57
  12. package/dist/index-BTVdhfsb.d.ts +469 -0
  13. package/dist/index-mr3Co63T.d.cts +469 -0
  14. package/dist/index.cjs +356 -42
  15. package/dist/index.cjs.map +1 -1
  16. package/dist/index.d.cts +80 -219
  17. package/dist/index.d.ts +80 -219
  18. package/dist/index.js +203 -21
  19. package/dist/index.js.map +1 -1
  20. package/dist/test/index.cjs +34 -19
  21. package/dist/test/index.cjs.map +1 -1
  22. package/dist/test/index.d.cts +1 -1
  23. package/dist/test/index.d.ts +1 -1
  24. package/dist/test/index.js +10 -2
  25. package/dist/test/index.js.map +1 -1
  26. package/docs/README.md +212 -14
  27. package/docs/database.md +40 -0
  28. package/docs/endpoints.md +110 -92
  29. package/docs/errors.md +37 -30
  30. package/docs/getting-started.md +64 -26
  31. package/docs/llms-full.txt +724 -312
  32. package/docs/llms.txt +1 -1
  33. package/docs/migrations.md +75 -73
  34. package/docs/routing.md +39 -45
  35. package/docs/schema.md +135 -23
  36. package/docs/services.md +13 -10
  37. package/package.json +2 -2
  38. package/dist/chunk-B7EUJP5W.js.map +0 -1
  39. package/dist/chunk-PHAFZGHN.js.map +0 -1
  40. package/dist/index-CXUs9iTQ.d.ts +0 -294
  41. package/dist/index-CZAwpQE1.d.cts +0 -294
@@ -3,7 +3,7 @@
3
3
  There is no CLI init command. A starter project is created for you when your
4
4
  Palbase project is provisioned. You then edit the files locally and deploy them.
5
5
 
6
- ## package.json
6
+ ## package.json + tsconfig.json
7
7
 
8
8
  Your project depends on the SDK and uses the Palbase CLI for the dev loop:
9
9
 
@@ -20,46 +20,84 @@ Your project depends on the SDK and uses the Palbase CLI for the dev loop:
20
20
  }
21
21
  ```
22
22
 
23
+ The controllers use **decorators**, so the `tsconfig.json` must set
24
+ `experimentalDecorators: true` (the scaffold ships it):
25
+
26
+ ```json
27
+ {
28
+ "compilerOptions": {
29
+ "target": "ES2022",
30
+ "module": "ESNext",
31
+ "moduleResolution": "Bundler",
32
+ "strict": true,
33
+ "experimentalDecorators": true,
34
+ "noEmit": true
35
+ },
36
+ "include": ["controllers/**/*.ts", "models/**/*.ts", "services/**/*.ts", "db/**/*.ts", "*.d.ts"]
37
+ }
38
+ ```
39
+
23
40
  ## Local dev loop
24
41
 
25
42
  - `palbase serve` — run your backend locally with hot reload. It runs your
26
- `controllers/` locally but proxies `Database`/`ctx.*` to the **deployed**
27
- branch, so the branch must already be deployed (serve tells you to push first
28
- if it isn't). See [migrations.md](./migrations.md) for the schema/migration
29
- side of this.
43
+ `controllers/` locally but proxies `Database` and the service singletons to
44
+ the **deployed** branch, so the branch must already be deployed (serve tells
45
+ you to push first if it isn't). On startup it also runs `gen-types`. See
46
+ [migrations.md](./migrations.md) for the schema/migration side of this.
47
+ - `palbase gen-types` — regenerate `palbase-env.d.ts` from `db/schema.ts` so
48
+ `Database.tables.*` is typed (no import, no generic). Run it standalone after
49
+ editing the schema, or rely on `palbase serve` running it for you.
30
50
  - **Deploy is GitHub-native** — there is no `palbase push`. Commit and
31
51
  `git push` to your project's repo; the push triggers a deploy of the backend
32
52
  runtime. Push a **branch** to deploy that branch instead of `main`.
33
53
 
34
54
  ## Your first endpoint
35
55
 
36
- A handler is one endpoint unit; a controller maps method+path to it. Create
37
- `handlers/hello.ts`:
56
+ An endpoint is a method on a class controller. Declare the schemas in `models/`:
38
57
 
39
58
  ```ts
40
- import { defineHandler, z } from "@palbase/backend";
41
-
42
- export default defineHandler({
43
- input: z.object({ name: z.string().optional() }),
44
- output: z.object({ message: z.string(), user: z.string().nullable() }),
45
- handler: async (req) => ({
46
- message: `hello, ${req.input.name ?? "world"}!`,
47
- user: req.user?.id ?? null,
48
- }),
49
- });
59
+ // models/hello/greet.ts
60
+ import { z } from "@palbase/backend";
61
+
62
+ export const GreetQuery = z.object({ name: z.string().optional() });
63
+ export type GreetQuery = z.infer<typeof GreetQuery>;
64
+
65
+ export const HelloResponse = z.object({ message: z.string(), user: z.string().nullable() });
66
+ export type HelloResponse = z.infer<typeof HelloResponse>;
50
67
  ```
51
68
 
52
- Then mount it in `controllers/hello.controller.ts`:
69
+ Then write the controller in `controllers/hello.controller.ts`:
53
70
 
54
71
  ```ts
55
- import { defineController, route } from "@palbase/backend";
56
- import hello from "../handlers/hello.js";
72
+ import { Controller, Get, Query, OptionalUser } from "@palbase/backend";
73
+ import type { UserT } from "@palbase/backend";
74
+ import { GreetQuery } from "../models/hello/greet.js";
75
+ import type { HelloResponse } from "../models/hello/greet.js"; // the return TYPE names the 200 schema
57
76
 
58
- export default defineController("/hello", {
59
- hello: route.get("/", hello),
60
- });
77
+ @Controller("/hello", { auth: false })
78
+ export default class HelloController {
79
+ @Get("")
80
+ greet(@Query(GreetQuery) q: GreetQuery, @OptionalUser() user: UserT | null): HelloResponse {
81
+ return { message: `hello, ${q.name ?? "world"}!`, user: user?.id ?? null };
82
+ }
83
+ }
61
84
  ```
62
85
 
63
- This is served at `GET /hello`. The Zod `input` schema validates the request and
64
- flows into `req.input`; the `output` schema validates your return value. See
65
- [routing.md](./routing.md) for the handler + controller model.
86
+ This is served at `GET /hello`. The `@Query` schema validates the query string;
87
+ the method's RETURN TYPE (`: HelloResponse`) names the response schema codegen
88
+ + the runtime read it to validate and describe the 200 response. There is no
89
+ `@Returns` decorator; a body route with no named return type is a build error.
90
+
91
+ Two things every controller file MUST have:
92
+
93
+ - **A default export of the `@Controller` class.** Above it is `export default class
94
+ HelloController` (inline); the equivalent trailing form is `export class
95
+ HelloController {…}` then `export default HelloController;`. Without a default
96
+ export the deploy fails ("not a @Controller").
97
+ - **`async` + `Promise<T>` once the method awaits a service.** `greet` above is
98
+ synchronous (pure), so it returns `HelloResponse` directly. The moment a method
99
+ calls a service that awaits `Database`, make it `async` and return
100
+ `Promise<HelloResponse>`.
101
+
102
+ See [routing.md](./routing.md) and [endpoints.md](./endpoints.md) for the full
103
+ class-controller model.