@palbase/backend 2.0.0 → 3.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 (51) hide show
  1. package/README.md +24 -0
  2. package/dist/{chunk-4J3F32SH.js → chunk-B7EUJP5W.js} +38 -9
  3. package/dist/chunk-B7EUJP5W.js.map +1 -0
  4. package/dist/{chunk-L36JLUPO.js → chunk-PHAFZGHN.js} +43 -46
  5. package/dist/chunk-PHAFZGHN.js.map +1 -0
  6. package/dist/db/env.cjs +19 -0
  7. package/dist/db/env.cjs.map +1 -0
  8. package/dist/db/env.d.cts +45 -0
  9. package/dist/db/env.d.ts +45 -0
  10. package/dist/db/env.js +1 -0
  11. package/dist/db/env.js.map +1 -0
  12. package/dist/db/index.cjs +28 -231
  13. package/dist/db/index.cjs.map +1 -1
  14. package/dist/db/index.d.cts +4 -20
  15. package/dist/db/index.d.ts +4 -20
  16. package/dist/db/index.js +3 -233
  17. package/dist/db/index.js.map +1 -1
  18. package/dist/{endpoint-Djk5L6G2.d.ts → endpoint-DJ98tQd6.d.cts} +30 -68
  19. package/dist/{endpoint-BlcY2xNA.d.cts → endpoint-DJ98tQd6.d.ts} +30 -68
  20. package/dist/index-CXUs9iTQ.d.ts +294 -0
  21. package/dist/index-CZAwpQE1.d.cts +294 -0
  22. package/dist/index.cjs +229 -61
  23. package/dist/index.cjs.map +1 -1
  24. package/dist/index.d.cts +398 -154
  25. package/dist/index.d.ts +398 -154
  26. package/dist/index.js +147 -12
  27. package/dist/index.js.map +1 -1
  28. package/dist/test/index.cjs +41 -1
  29. package/dist/test/index.cjs.map +1 -1
  30. package/dist/test/index.d.cts +1 -2
  31. package/dist/test/index.d.ts +1 -2
  32. package/dist/test/index.js +1 -1
  33. package/docs/README.md +72 -0
  34. package/docs/background.md +62 -0
  35. package/docs/database.md +73 -0
  36. package/docs/endpoints.md +134 -0
  37. package/docs/errors.md +47 -0
  38. package/docs/events.md +67 -0
  39. package/docs/getting-started.md +65 -0
  40. package/docs/llms-full.txt +1038 -0
  41. package/docs/llms.txt +18 -0
  42. package/docs/migrations.md +98 -0
  43. package/docs/resources.md +94 -0
  44. package/docs/routing.md +67 -0
  45. package/docs/schema.md +92 -0
  46. package/docs/services.md +104 -0
  47. package/package.json +15 -3
  48. package/dist/chunk-4J3F32SH.js.map +0 -1
  49. package/dist/chunk-L36JLUPO.js.map +0 -1
  50. package/dist/schema-BqfEhIC0.d.cts +0 -133
  51. package/dist/schema-BqfEhIC0.d.ts +0 -133
package/docs/events.md ADDED
@@ -0,0 +1,67 @@
1
+ # Hooks & Webhooks
2
+
3
+ Like workers/jobs, hooks and webhooks use the **singleton model** — the same
4
+ imported service singletons as endpoints (`import { Database, Log } from
5
+ "@palbase/backend"`). They do **not** receive a `req`. A second `meta` argument
6
+ carries the non-service data (`env`, `projectId`, `environmentId`; webhooks also
7
+ get `requestId`).
8
+
9
+ ## Hooks (platform events)
10
+
11
+ React to auth, storage, and document events. Files live under `hooks/`. Builders
12
+ are imported from `@palbase/backend`: `auth`, `storage`, `documents`.
13
+
14
+ ```ts
15
+ // hooks/auth.ts
16
+ import { auth, Database, Log } from "@palbase/backend";
17
+
18
+ export const onUserCreated = auth.onUserCreated(async (event, meta) => {
19
+ Log.info(`new user: ${event.user.email}`);
20
+ await Database.insert("profiles", {
21
+ user_id: event.user.id,
22
+ email: event.user.email,
23
+ });
24
+ });
25
+
26
+ export const onSignIn = auth.onSignIn(async (event, meta) => {
27
+ Log.info(`sign in: ${event.user.email} via ${event.provider}`);
28
+ });
29
+ ```
30
+
31
+ `meta` shape: `{ env, projectId, environmentId }`. Branch env vars are in
32
+ `meta.env`; services come from the imported singletons.
33
+
34
+ Available hook builders: `auth.onUserCreated`, `auth.onSignIn`, `auth.onSignOut`,
35
+ `auth.onPasswordReset`, `storage.onFileUploaded`, `storage.onFileDeleted`,
36
+ `documents.onDocumentCreated`, `documents.onDocumentUpdated`,
37
+ `documents.onDocumentDeleted`.
38
+
39
+ ## Webhooks (inbound provider events)
40
+
41
+ Receive and verify webhooks from third-party providers. Files live under
42
+ `webhooks/`.
43
+
44
+ ```ts
45
+ // webhooks/stripe.ts
46
+ import { defineWebhook, Database, Log } from "@palbase/backend";
47
+
48
+ export default defineWebhook({
49
+ provider: "stripe",
50
+ secret: { env: "STRIPE_WEBHOOK_SECRET" }, // signing secret resolved from env
51
+ events: {
52
+ "checkout.session.completed": async (event, meta) => {
53
+ await Database.insert("orders", { status: "paid", data: event });
54
+ },
55
+ "payment_intent.payment_failed": async (event, meta) => {
56
+ Log.error("payment failed");
57
+ await Database.insert("payment_failures", { data: event });
58
+ },
59
+ },
60
+ });
61
+ ```
62
+
63
+ The signing secret is resolved by the runtime from `secret: { env: "NAME" }`;
64
+ your handlers access branch env vars via `meta.env`. The runtime verifies the
65
+ signature before dispatching to your event handlers.
66
+
67
+ `meta` shape: `{ env, requestId, projectId, environmentId }`.
@@ -0,0 +1,65 @@
1
+ # Getting started
2
+
3
+ There is no CLI init command. A starter project is created for you when your
4
+ Palbase project is provisioned. You then edit the files locally and deploy them.
5
+
6
+ ## package.json
7
+
8
+ Your project depends on the SDK and uses the Palbase CLI for the dev loop:
9
+
10
+ ```json
11
+ {
12
+ "name": "my-backend",
13
+ "private": true,
14
+ "scripts": {
15
+ "dev": "palbase serve",
16
+ "typecheck": "tsc --noEmit"
17
+ },
18
+ "dependencies": { "@palbase/backend": "latest" },
19
+ "devDependencies": { "@types/node": "^22", "typescript": "^5" }
20
+ }
21
+ ```
22
+
23
+ ## Local dev loop
24
+
25
+ - `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.
30
+ - **Deploy is GitHub-native** — there is no `palbase push`. Commit and
31
+ `git push` to your project's repo; the push triggers a deploy of the backend
32
+ runtime. Push a **branch** to deploy that branch instead of `main`.
33
+
34
+ ## Your first endpoint
35
+
36
+ A handler is one endpoint unit; a controller maps method+path to it. Create
37
+ `handlers/hello.ts`:
38
+
39
+ ```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
+ });
50
+ ```
51
+
52
+ Then mount it in `controllers/hello.controller.ts`:
53
+
54
+ ```ts
55
+ import { defineController, route } from "@palbase/backend";
56
+ import hello from "../handlers/hello.js";
57
+
58
+ export default defineController("/hello", {
59
+ hello: route.get("/", hello),
60
+ });
61
+ ```
62
+
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.