hostinger-api-mcp 4.3.0 → 4.4.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/CHANGELOG.md +7 -0
- package/gemini-extension.json +1 -1
- package/package.json +1 -1
- package/server.json +2 -2
- package/skills/headless/SKILL.md +14 -7
- package/skills/headless/VERSION +3 -3
- package/skills/headless/run-log.mjs +3 -5
- package/skills/headless/steps/01-check-state/SKILL.md +16 -5
- package/skills/headless/steps/02-refine/SKILL.md +56 -29
- package/skills/headless/steps/03-provision/SKILL.md +20 -25
- package/skills/headless/steps/03-provision/pocketbase/SKILL.md +190 -0
- package/skills/headless/steps/04-content-model/SKILL.md +12 -12
- package/skills/headless/steps/04-content-model/pocketbase/SKILL.md +90 -0
- package/skills/headless/steps/04-content-model/pocketbase/auth.md +52 -0
- package/skills/headless/steps/04-content-model/pocketbase/frontend.md +130 -0
- package/skills/headless/steps/04-content-model/pocketbase/patterns.md +286 -0
- package/skills/headless/steps/04-content-model/pocketbase/rules.md +66 -0
- package/skills/headless/steps/04-content-model/pocketbase/schema.example.json +65 -0
- package/skills/headless/steps/04-content-model/pocketbase/schema.md +78 -0
- package/skills/headless/steps/04-content-model/pocketbase/scripts/pb-apply-schema.mjs +245 -0
- package/skills/headless/steps/04-content-model/pocketbase/scripts/pb-lib.mjs +120 -0
- package/skills/headless/steps/04-content-model/pocketbase/scripts/pb-verify.mjs +127 -0
- package/skills/headless/steps/04-content-model/pocketbase/troubleshooting.md +55 -0
- package/skills/headless/steps/06-frontend/SKILL.md +32 -33
- package/skills/headless/steps/07-validate/SKILL.md +8 -2
- package/skills/headless/steps/08-deploy/SKILL.md +32 -17
- package/skills/headless/steps/09-done/SKILL.md +20 -3
- package/skills/headless/wait.mjs +19 -9
- package/src/core/skills.js +114 -37
- package/src/core/skills.ts +114 -37
- package/src/servers/agency-hosting.js +1 -1
- package/src/servers/agency-hosting.ts +1 -1
- package/src/servers/agent-readiness.js +1 -1
- package/src/servers/agent-readiness.ts +1 -1
- package/src/servers/all.js +1 -1
- package/src/servers/all.ts +1 -1
- package/src/servers/billing.js +1 -1
- package/src/servers/billing.ts +1 -1
- package/src/servers/dns.js +1 -1
- package/src/servers/dns.ts +1 -1
- package/src/servers/domains.js +1 -1
- package/src/servers/domains.ts +1 -1
- package/src/servers/ecommerce.js +1 -1
- package/src/servers/ecommerce.ts +1 -1
- package/src/servers/gpu.js +1 -1
- package/src/servers/gpu.ts +1 -1
- package/src/servers/horizons.js +1 -1
- package/src/servers/horizons.ts +1 -1
- package/src/servers/hosting.js +1 -1
- package/src/servers/hosting.ts +1 -1
- package/src/servers/mail.js +1 -1
- package/src/servers/mail.ts +1 -1
- package/src/servers/reach.js +1 -1
- package/src/servers/reach.ts +1 -1
- package/src/servers/vps.js +1 -1
- package/src/servers/vps.ts +1 -1
- package/src/servers/wordpress.js +1 -1
- package/src/servers/wordpress.ts +1 -1
- package/skills/headless/steps/03-provision/webapp/SKILL.md +0 -164
|
@@ -0,0 +1,286 @@
|
|
|
1
|
+
# Recipes
|
|
2
|
+
|
|
3
|
+
Ready-made `schema.json` entries for the apps people ask for most. Copy the closest one, rename collections and fields to fit the app, and combine recipes in one `collections` array. Every recipe was checked against PocketBase 0.40.
|
|
4
|
+
|
|
5
|
+
## Contents
|
|
6
|
+
|
|
7
|
+
- Contact form, sign-up list, feedback (anonymous, owner reads)
|
|
8
|
+
- Comments on pages (anonymous or signed in)
|
|
9
|
+
- Likes, votes, polls, RSVPs (one per person)
|
|
10
|
+
- Leaderboard (scores)
|
|
11
|
+
- Private notes, to-dos, saved items (per user)
|
|
12
|
+
- Live chat or Q&A in rooms
|
|
13
|
+
- Order requests without payment
|
|
14
|
+
- Bookings with limited places
|
|
15
|
+
- Admin area inside the app
|
|
16
|
+
- Starting data (seed)
|
|
17
|
+
|
|
18
|
+
## Contact form, sign-up list, feedback
|
|
19
|
+
|
|
20
|
+
Anyone can send; only the owner reads, in the dashboard. No login.
|
|
21
|
+
|
|
22
|
+
```json
|
|
23
|
+
{
|
|
24
|
+
"collections": [
|
|
25
|
+
{
|
|
26
|
+
"name": "messages",
|
|
27
|
+
"rules": { "list": null, "view": null, "create": "@request.body.website = \"\"", "update": null, "delete": null },
|
|
28
|
+
"fields": [
|
|
29
|
+
{ "name": "name", "type": "text", "required": true, "max": 100 },
|
|
30
|
+
{ "name": "email", "type": "email", "required": true },
|
|
31
|
+
{ "name": "message", "type": "text", "required": true, "max": 3000 },
|
|
32
|
+
{ "name": "website", "type": "text", "max": 200 }
|
|
33
|
+
]
|
|
34
|
+
}
|
|
35
|
+
],
|
|
36
|
+
"rateLimits": [{ "label": "messages:create", "audience": "@guest", "maxRequests": 5, "duration": 600 }]
|
|
37
|
+
}
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
- `website` is a honeypot: render it as a visually hidden input with `tabindex="-1"` and `autocomplete="off"`. Bots fill it and the rule rejects them; people leave it empty.
|
|
41
|
+
- The rate limit allows 5 messages per 10 minutes per IP address.
|
|
42
|
+
- Because `list` is `null`, the form cannot show past entries. After `create` succeeds, show a thank-you message.
|
|
43
|
+
- PocketBase sends no email for a new message by itself. The owner reads messages in the dashboard (`/_/`, collection `messages`). Say so in the summary.
|
|
44
|
+
|
|
45
|
+
## Comments on pages
|
|
46
|
+
|
|
47
|
+
Public read. Anonymous posting with a display name; switch to signed-in authors when the app has accounts.
|
|
48
|
+
|
|
49
|
+
```json
|
|
50
|
+
{
|
|
51
|
+
"name": "comments",
|
|
52
|
+
"rules": { "list": "", "view": "", "create": "@request.body.website = \"\"", "update": null, "delete": null },
|
|
53
|
+
"fields": [
|
|
54
|
+
{ "name": "page", "type": "text", "required": true, "max": 200 },
|
|
55
|
+
{ "name": "author_name", "type": "text", "required": true, "max": 60 },
|
|
56
|
+
{ "name": "text", "type": "text", "required": true, "max": 2000 },
|
|
57
|
+
{ "name": "website", "type": "text", "max": 200 }
|
|
58
|
+
],
|
|
59
|
+
"indexes": ["CREATE INDEX idx_comments_page ON comments (page)"]
|
|
60
|
+
}
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
Add `{ "label": "comments:create", "audience": "@guest", "maxRequests": 10, "duration": 600 }` to `rateLimits`. Load with `getList(1, 50, { filter: pb.filter("page = {:p}", { p: location.pathname }), sort: "-created" })`. The owner deletes spam in the dashboard, which removes it from open pages live.
|
|
64
|
+
|
|
65
|
+
Signed-in version: replace `author_name` with `{ "name": "author", "type": "relation", "collection": "users", "required": true, "cascadeDelete": true }`, set `create` to `@request.auth.id != "" && @request.body.author = @request.auth.id` and `delete` to `author = @request.auth.id`, and load with `expand: "author"`.
|
|
66
|
+
|
|
67
|
+
## Likes, votes, polls, RSVPs
|
|
68
|
+
|
|
69
|
+
One per person through a unique index. Signed-in version:
|
|
70
|
+
|
|
71
|
+
```json
|
|
72
|
+
{
|
|
73
|
+
"name": "votes",
|
|
74
|
+
"rules": { "list": "", "view": "", "create": "@request.auth.id != \"\" && @request.body.voter = @request.auth.id", "update": null, "delete": "voter = @request.auth.id" },
|
|
75
|
+
"fields": [
|
|
76
|
+
{ "name": "poll", "type": "relation", "collection": "polls", "required": true, "cascadeDelete": true },
|
|
77
|
+
{ "name": "choice", "type": "select", "values": ["a", "b", "c"], "maxSelect": 1, "required": true },
|
|
78
|
+
{ "name": "voter", "type": "relation", "collection": "users", "required": true, "cascadeDelete": true }
|
|
79
|
+
],
|
|
80
|
+
"indexes": ["CREATE UNIQUE INDEX idx_votes_poll_voter ON votes (poll, voter)"]
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
Anonymous version: make `voter` a `text` field holding a random id the browser keeps in `localStorage` (`crypto.randomUUID()`), set `create` to `""`, `delete` to `null`, and add a rate limit. That stops accidental double votes, not someone determined to cheat; use the signed-in version when it matters.
|
|
85
|
+
|
|
86
|
+
Counting: a view collection gives totals without downloading every vote.
|
|
87
|
+
|
|
88
|
+
```json
|
|
89
|
+
{ "name": "vote_totals", "type": "view", "rules": { "list": "", "view": "" },
|
|
90
|
+
"viewQuery": "SELECT (poll || choice) AS id, poll, choice, COUNT(*) AS total FROM votes GROUP BY poll, choice" }
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
View collections send no realtime events. For live totals, subscribe to `votes` and refetch `vote_totals` when an event arrives.
|
|
94
|
+
|
|
95
|
+
## Leaderboard
|
|
96
|
+
|
|
97
|
+
One best score per signed-in player, readable by everyone, raised only upward.
|
|
98
|
+
|
|
99
|
+
```json
|
|
100
|
+
{
|
|
101
|
+
"name": "scores",
|
|
102
|
+
"rules": {
|
|
103
|
+
"list": "", "view": "",
|
|
104
|
+
"create": "@request.auth.id != \"\" && @request.body.player = @request.auth.id",
|
|
105
|
+
"update": "player = @request.auth.id && @request.body.player:changed = false && @request.body.score > score",
|
|
106
|
+
"delete": null
|
|
107
|
+
},
|
|
108
|
+
"fields": [
|
|
109
|
+
{ "name": "player", "type": "relation", "collection": "users", "required": true, "cascadeDelete": true },
|
|
110
|
+
{ "name": "player_name", "type": "text", "required": true, "max": 40 },
|
|
111
|
+
{ "name": "score", "type": "number", "required": true, "min": 0, "onlyInt": true }
|
|
112
|
+
],
|
|
113
|
+
"indexes": ["CREATE UNIQUE INDEX idx_scores_player ON scores (player)"]
|
|
114
|
+
}
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
Top 10: `getList(1, 10, { sort: "-score,created" })`. Keep a display name on the score itself, because the default `users` rules hide other users' records. Scores come from the browser, so a player can submit a fake one. Tell the user that plainly; truly cheat-proof scores need the game logic on a server.
|
|
118
|
+
|
|
119
|
+
## Private notes, to-dos, saved items
|
|
120
|
+
|
|
121
|
+
Each user sees only their own records.
|
|
122
|
+
|
|
123
|
+
```json
|
|
124
|
+
{
|
|
125
|
+
"name": "notes",
|
|
126
|
+
"rules": {
|
|
127
|
+
"list": "owner = @request.auth.id", "view": "owner = @request.auth.id",
|
|
128
|
+
"create": "@request.auth.id != \"\" && @request.body.owner = @request.auth.id",
|
|
129
|
+
"update": "owner = @request.auth.id && @request.body.owner:changed = false",
|
|
130
|
+
"delete": "owner = @request.auth.id"
|
|
131
|
+
},
|
|
132
|
+
"fields": [
|
|
133
|
+
{ "name": "owner", "type": "relation", "collection": "users", "required": true, "cascadeDelete": true },
|
|
134
|
+
{ "name": "title", "type": "text", "required": true, "max": 200 },
|
|
135
|
+
{ "name": "body", "type": "text", "max": 20000 },
|
|
136
|
+
{ "name": "done", "type": "bool" }
|
|
137
|
+
],
|
|
138
|
+
"indexes": ["CREATE INDEX idx_notes_owner ON notes (owner)"]
|
|
139
|
+
}
|
|
140
|
+
```
|
|
141
|
+
|
|
142
|
+
Create with `{ ..., owner: pb.authStore.record.id }`.
|
|
143
|
+
|
|
144
|
+
## Live chat or Q&A in rooms
|
|
145
|
+
|
|
146
|
+
Rooms are created by the host in the dashboard (or by signed-in users); anyone in an open room can post.
|
|
147
|
+
|
|
148
|
+
```json
|
|
149
|
+
{
|
|
150
|
+
"collections": [
|
|
151
|
+
{
|
|
152
|
+
"name": "rooms",
|
|
153
|
+
"rules": { "list": "", "view": "", "create": null, "update": null, "delete": null },
|
|
154
|
+
"fields": [
|
|
155
|
+
{ "name": "title", "type": "text", "required": true, "max": 80 },
|
|
156
|
+
{ "name": "open", "type": "bool" }
|
|
157
|
+
],
|
|
158
|
+
"seed": [{ "title": "General", "open": true }]
|
|
159
|
+
},
|
|
160
|
+
{
|
|
161
|
+
"name": "chat_messages",
|
|
162
|
+
"rules": {
|
|
163
|
+
"list": "room.open = true", "view": "room.open = true",
|
|
164
|
+
"create": "@request.body.room.open = true && @request.body.website = \"\"",
|
|
165
|
+
"update": null, "delete": null
|
|
166
|
+
},
|
|
167
|
+
"fields": [
|
|
168
|
+
{ "name": "room", "type": "relation", "collection": "rooms", "required": true, "cascadeDelete": true },
|
|
169
|
+
{ "name": "author_name", "type": "text", "required": true, "max": 40 },
|
|
170
|
+
{ "name": "text", "type": "text", "required": true, "max": 500 },
|
|
171
|
+
{ "name": "website", "type": "text", "max": 200 }
|
|
172
|
+
],
|
|
173
|
+
"indexes": ["CREATE INDEX idx_chat_messages_room ON chat_messages (room)"]
|
|
174
|
+
}
|
|
175
|
+
],
|
|
176
|
+
"rateLimits": [{ "label": "chat_messages:create", "audience": "@guest", "maxRequests": 30, "duration": 60 }]
|
|
177
|
+
}
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Load the last 50 (`sort: "-created"`, then reverse), then subscribe with `{ filter: pb.filter("room = {:r}", { r: roomId }) }`. Moderation is deleting the message, which removes it from every screen.
|
|
181
|
+
|
|
182
|
+
Many people on one network (a venue, a classroom) share one IP address. The default limit for the whole API on this platform is 200 requests per 5 minutes per IP (`pb-verify.mjs` prints the current limits). For an event, raise it in `rateLimits`: `{ "label": "/api", "maxRequests": 3000, "duration": 300 }`.
|
|
183
|
+
|
|
184
|
+
## Order requests without payment
|
|
185
|
+
|
|
186
|
+
Visitors send an order; the owner handles it from the dashboard. For paid checkout use the `hostinger-headless` store instead.
|
|
187
|
+
|
|
188
|
+
```json
|
|
189
|
+
{
|
|
190
|
+
"name": "orders",
|
|
191
|
+
"rules": {
|
|
192
|
+
"list": null, "view": null,
|
|
193
|
+
"create": "@request.body.status:isset = false && @request.body.website = \"\"",
|
|
194
|
+
"update": null, "delete": null
|
|
195
|
+
},
|
|
196
|
+
"fields": [
|
|
197
|
+
{ "name": "customer_name", "type": "text", "required": true, "max": 100 },
|
|
198
|
+
{ "name": "customer_email", "type": "email", "required": true },
|
|
199
|
+
{ "name": "items", "type": "json", "required": true, "maxSize": 20000 },
|
|
200
|
+
{ "name": "note", "type": "text", "max": 1000 },
|
|
201
|
+
{ "name": "status", "type": "select", "values": ["new", "confirmed", "done", "cancelled"], "maxSelect": 1 },
|
|
202
|
+
{ "name": "website", "type": "text", "max": 200 }
|
|
203
|
+
]
|
|
204
|
+
}
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Add a `orders:create` rate limit as in the contact form. Prices and totals sent by the browser can be edited by the sender, so the owner confirms the price; never treat a submitted total as paid. A `products` collection with `list: ""`, `view: ""` and every write `null` holds the catalog the owner edits in the dashboard.
|
|
208
|
+
|
|
209
|
+
## Bookings with limited places
|
|
210
|
+
|
|
211
|
+
Classes, workshops, tables or tours with a fixed number of places. Visitors book with a name and email; nobody can book a full, cancelled or past session; only the owner sees who booked. The cap is enforced by the database, so it holds even when many people book at the same moment.
|
|
212
|
+
|
|
213
|
+
```json
|
|
214
|
+
{
|
|
215
|
+
"collections": [
|
|
216
|
+
{
|
|
217
|
+
"name": "sessions",
|
|
218
|
+
"rules": { "list": "", "view": "", "create": null, "update": null, "delete": null },
|
|
219
|
+
"fields": [
|
|
220
|
+
{ "name": "title", "type": "text", "required": true, "max": 80 },
|
|
221
|
+
{ "name": "starts_at", "type": "date", "required": true },
|
|
222
|
+
{ "name": "capacity", "type": "number", "required": true, "min": 1, "max": 500, "onlyInt": true },
|
|
223
|
+
{ "name": "cancelled", "type": "bool" }
|
|
224
|
+
],
|
|
225
|
+
"indexes": ["CREATE INDEX idx_sessions_starts_at ON sessions (starts_at)"]
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
"name": "bookings",
|
|
229
|
+
"rules": {
|
|
230
|
+
"list": null, "view": null,
|
|
231
|
+
"create": "@request.body.website = \"\" && @request.body.session.cancelled = false && @request.body.session.starts_at > @now && @request.body.spot <= @request.body.session.capacity",
|
|
232
|
+
"update": null, "delete": null
|
|
233
|
+
},
|
|
234
|
+
"fields": [
|
|
235
|
+
{ "name": "session", "type": "relation", "collection": "sessions", "required": true, "cascadeDelete": true },
|
|
236
|
+
{ "name": "name", "type": "text", "required": true, "max": 80 },
|
|
237
|
+
{ "name": "email", "type": "email", "required": true },
|
|
238
|
+
{ "name": "spot", "type": "number", "required": true, "min": 1, "max": 500, "onlyInt": true },
|
|
239
|
+
{ "name": "website", "type": "text", "max": 200 }
|
|
240
|
+
],
|
|
241
|
+
"indexes": [
|
|
242
|
+
"CREATE UNIQUE INDEX idx_bookings_session_spot ON bookings (session, spot)",
|
|
243
|
+
"CREATE UNIQUE INDEX idx_bookings_session_email ON bookings (session, email)"
|
|
244
|
+
]
|
|
245
|
+
},
|
|
246
|
+
{
|
|
247
|
+
"name": "taken_spots", "type": "view",
|
|
248
|
+
"rules": { "list": "", "view": "" },
|
|
249
|
+
"viewQuery": "SELECT id, session, spot FROM bookings"
|
|
250
|
+
}
|
|
251
|
+
],
|
|
252
|
+
"rateLimits": [{ "label": "bookings:create", "audience": "@guest", "maxRequests": 20, "duration": 600 }]
|
|
253
|
+
}
|
|
254
|
+
```
|
|
255
|
+
|
|
256
|
+
- Each session has its own `capacity`. A booking holds a `spot` number, unique per session, and the create rule only accepts numbers from 1 to that session's `capacity`. When every number is taken there is none left to claim, so the database refuses the next booking. The field's `max` (500) is only an upper bound for any session.
|
|
257
|
+
- `taken_spots` shows visitors which numbers are taken, and nothing about who took them. Places left = `capacity` minus the number of rows for the session.
|
|
258
|
+
- Lowering `capacity` below the bookings already made does not remove anyone; only new bookings are limited.
|
|
259
|
+
- To book, load the session's `capacity` and its taken numbers, pick a free number from 1 to `capacity` and create the booking. If the create fails with `data.spot.code` `validation_not_unique`, someone took that number a moment earlier: reload and try another. When no number is free, the session is full. `data.email.code` `validation_not_unique` means this email already booked the session.
|
|
260
|
+
- Lowercase and trim the email before sending, so the same person cannot book twice with different capitalisation.
|
|
261
|
+
- To cancel a session, set `cancelled` to true rather than deleting it: the booking list stays, so the owner can tell the people who booked.
|
|
262
|
+
- `date` values are stored in UTC. Convert the owner's local time to UTC before saving and show times in the business's time zone (`Intl.DateTimeFormat` with `timeZone`).
|
|
263
|
+
- For an owner screen inside the app, combine this with "Admin area inside the app" and give `bookings` `list`, `view` and `delete` rules for admins.
|
|
264
|
+
|
|
265
|
+
## Admin area inside the app
|
|
266
|
+
|
|
267
|
+
Usually the dashboard (`/_/`) is the admin area and nothing needs building. When the app itself needs admin-only screens, add a locked flag to `users`:
|
|
268
|
+
|
|
269
|
+
```json
|
|
270
|
+
{
|
|
271
|
+
"name": "users", "type": "auth",
|
|
272
|
+
"rules": {
|
|
273
|
+
"list": "id = @request.auth.id", "view": "id = @request.auth.id",
|
|
274
|
+
"create": "@request.body.is_admin:isset = false",
|
|
275
|
+
"update": "id = @request.auth.id && @request.body.is_admin:isset = false",
|
|
276
|
+
"delete": "id = @request.auth.id"
|
|
277
|
+
},
|
|
278
|
+
"fields": [{ "name": "is_admin", "type": "bool" }]
|
|
279
|
+
}
|
|
280
|
+
```
|
|
281
|
+
|
|
282
|
+
The owner ticks `is_admin` on their own account in the dashboard. Rules then use `@request.auth.is_admin = true`, for example `"delete": "@request.auth.is_admin = true"`. In the frontend, `pb.authStore.record?.is_admin` decides what to show; the rules are what enforce it.
|
|
283
|
+
|
|
284
|
+
## Starting data (seed)
|
|
285
|
+
|
|
286
|
+
Any collection can carry `"seed": [ {...}, {...} ]`. `pb-apply-schema.mjs` inserts those records only when the collection is empty, so re-running never duplicates them. Seed values cannot reference other records by id; link them in the dashboard or create them from the app.
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
# API rules
|
|
2
|
+
|
|
3
|
+
Rules are PocketBase filter expressions checked on every request, including realtime events. They are the app's security: the frontend can be bypassed, the rules cannot. `patterns.md` has complete, tested rule sets per app type; this file is the syntax.
|
|
4
|
+
|
|
5
|
+
- `null`: superusers only. Use it for anything visitors must never touch.
|
|
6
|
+
- `""`: everyone, signed in or not.
|
|
7
|
+
- An expression: allowed when it is true for the record and the request.
|
|
8
|
+
|
|
9
|
+
## Building blocks
|
|
10
|
+
|
|
11
|
+
| Expression | Means |
|
|
12
|
+
| --- | --- |
|
|
13
|
+
| `@request.auth.id != ""` | Signed in |
|
|
14
|
+
| `owner = @request.auth.id` | The record belongs to the caller (list, view, update, delete) |
|
|
15
|
+
| `@request.body.owner = @request.auth.id` | On create: callers can only create records owned by themselves |
|
|
16
|
+
| `@request.body.status:isset = false` | The request may not send this field at all |
|
|
17
|
+
| `@request.body.website = ""` | The field is empty or absent (honeypot) |
|
|
18
|
+
| `@request.body.owner:changed = false` | On update: this field may not change |
|
|
19
|
+
| `@request.body.score > score` | On update: the new value must be higher than the stored one |
|
|
20
|
+
| `room.open = true` | Follow a relation of the stored record |
|
|
21
|
+
| `@request.body.room.open = true` | On create: follow the relation being submitted |
|
|
22
|
+
| `@request.body.room = question.room` | Two relations must point to the same parent |
|
|
23
|
+
| `@request.auth.is_admin = true` | A field on the signed-in user's own record |
|
|
24
|
+
| `@request.body.tags:length <= 3` | Number of items in a multi-value field (select, relation, file) |
|
|
25
|
+
| `@request.body.text:lower !~ "casino"` | Case-insensitive "does not contain" |
|
|
26
|
+
| `@collection.members.user ?= @request.auth.id && @collection.members.team ?= team` | Membership through another collection (`?=` means any match) |
|
|
27
|
+
|
|
28
|
+
Operators: `=`, `!=`, `>`, `>=`, `<`, `<=`, `~` (contains), `!~`, and `?`-prefixed versions for "any item matches". Combine with `&&`, `||` and parentheses. Strings use double quotes, escaped as `\"` inside JSON.
|
|
29
|
+
|
|
30
|
+
Not valid or not safe:
|
|
31
|
+
|
|
32
|
+
- `:length` on a text field is rejected ("invalid left operand"). Limit text with the field's `max`.
|
|
33
|
+
- A text field with `"max": 0` means the default limit (5000 characters), not zero.
|
|
34
|
+
- `@request.headers.x_token = "..."` as a password: anything the browser sends is visible to the visitor.
|
|
35
|
+
|
|
36
|
+
## How a rejected request looks
|
|
37
|
+
|
|
38
|
+
| Operation | Rule fails |
|
|
39
|
+
| --- | --- |
|
|
40
|
+
| list | 200 with only the allowed records (often an empty list) |
|
|
41
|
+
| view, update, delete | 404 "The requested resource wasn't found." |
|
|
42
|
+
| create | 400 "Failed to create record." with empty `data` |
|
|
43
|
+
| Any rule set to `null`, called by a non-superuser | 403 |
|
|
44
|
+
|
|
45
|
+
A 400 that names a field (`data.email.code`) is field validation, not a rule.
|
|
46
|
+
|
|
47
|
+
## Picking a rule set
|
|
48
|
+
|
|
49
|
+
| Data | Pattern in `patterns.md` |
|
|
50
|
+
| --- | --- |
|
|
51
|
+
| Anyone sends, owner reads (contact, sign-up list, feedback, order requests) | Contact form |
|
|
52
|
+
| Public read, anyone or signed-in users post | Comments |
|
|
53
|
+
| One per person | Likes, votes |
|
|
54
|
+
| Public read, owner writes | Leaderboard, or comments signed-in version |
|
|
55
|
+
| Private to each user | Private notes |
|
|
56
|
+
| Activity inside a space the host controls | Live chat or Q&A |
|
|
57
|
+
| Admin-only actions | Admin area |
|
|
58
|
+
| Shared by a team | `members` collection with `team` and `user`, rules with `@collection.members...` as in the table above |
|
|
59
|
+
|
|
60
|
+
## Realtime and rules
|
|
61
|
+
|
|
62
|
+
A subscriber receives an event only if it may view the record after the change. When an update makes a record invisible to someone (a `hidden` flag, `published = false`), that person gets no event and their screen keeps the old record. To take something off other people's screens, delete it. Use a hide flag only for data that is never shown live. View collections send no realtime events at all.
|
|
63
|
+
|
|
64
|
+
## Checking a rule
|
|
65
|
+
|
|
66
|
+
Run `pb-verify.mjs`, then try the forbidden action signed out and as a second user and expect the failure from the table above. The most common mistake is a `""` that silently allows everything: check each one on purpose.
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
{
|
|
2
|
+
"collections": [
|
|
3
|
+
{
|
|
4
|
+
"name": "users",
|
|
5
|
+
"type": "auth",
|
|
6
|
+
"rules": {
|
|
7
|
+
"list": "id = @request.auth.id",
|
|
8
|
+
"view": "id = @request.auth.id",
|
|
9
|
+
"create": "",
|
|
10
|
+
"update": "id = @request.auth.id",
|
|
11
|
+
"delete": "id = @request.auth.id"
|
|
12
|
+
},
|
|
13
|
+
"fields": []
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
"name": "posts",
|
|
17
|
+
"rules": {
|
|
18
|
+
"list": "published = true || author = @request.auth.id",
|
|
19
|
+
"view": "published = true || author = @request.auth.id",
|
|
20
|
+
"create": "@request.auth.id != \"\" && @request.body.author = @request.auth.id",
|
|
21
|
+
"update": "author = @request.auth.id && @request.body.author:changed = false",
|
|
22
|
+
"delete": "author = @request.auth.id"
|
|
23
|
+
},
|
|
24
|
+
"fields": [
|
|
25
|
+
{ "name": "title", "type": "text", "required": true, "min": 3, "max": 120 },
|
|
26
|
+
{ "name": "body", "type": "editor", "required": true },
|
|
27
|
+
{ "name": "cover", "type": "file", "maxSelect": 1, "maxSize": 5242880, "mimeTypes": ["image/jpeg", "image/png", "image/webp"] },
|
|
28
|
+
{ "name": "published", "type": "bool" },
|
|
29
|
+
{ "name": "author", "type": "relation", "collection": "users", "required": true, "cascadeDelete": true }
|
|
30
|
+
],
|
|
31
|
+
"indexes": ["CREATE INDEX idx_posts_author ON posts (author)"]
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
"name": "comments",
|
|
35
|
+
"rules": {
|
|
36
|
+
"list": "post.published = true",
|
|
37
|
+
"view": "post.published = true",
|
|
38
|
+
"create": "@request.auth.id != \"\" && @request.body.author = @request.auth.id && @request.body.post.published = true",
|
|
39
|
+
"update": null,
|
|
40
|
+
"delete": "author = @request.auth.id || post.author = @request.auth.id"
|
|
41
|
+
},
|
|
42
|
+
"fields": [
|
|
43
|
+
{ "name": "post", "type": "relation", "collection": "posts", "required": true, "cascadeDelete": true },
|
|
44
|
+
{ "name": "author", "type": "relation", "collection": "users", "required": true, "cascadeDelete": true },
|
|
45
|
+
{ "name": "text", "type": "text", "required": true, "min": 1, "max": 1000 }
|
|
46
|
+
],
|
|
47
|
+
"indexes": ["CREATE INDEX idx_comments_post ON comments (post)"]
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"name": "likes",
|
|
51
|
+
"rules": {
|
|
52
|
+
"list": "",
|
|
53
|
+
"view": "",
|
|
54
|
+
"create": "@request.auth.id != \"\" && @request.body.user = @request.auth.id",
|
|
55
|
+
"update": null,
|
|
56
|
+
"delete": "user = @request.auth.id"
|
|
57
|
+
},
|
|
58
|
+
"fields": [
|
|
59
|
+
{ "name": "post", "type": "relation", "collection": "posts", "required": true, "cascadeDelete": true },
|
|
60
|
+
{ "name": "user", "type": "relation", "collection": "users", "required": true, "cascadeDelete": true }
|
|
61
|
+
],
|
|
62
|
+
"indexes": ["CREATE UNIQUE INDEX idx_likes_post_user ON likes (post, user)"]
|
|
63
|
+
}
|
|
64
|
+
]
|
|
65
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
# Schema file format
|
|
2
|
+
|
|
3
|
+
`scripts/pb-apply-schema.mjs` reads `pocketbase/schema.json` and makes the backend match it. Run it with `--check` first to validate the file offline, then without to apply. It is safe to run again after every change.
|
|
4
|
+
|
|
5
|
+
```json
|
|
6
|
+
{
|
|
7
|
+
"collections": [
|
|
8
|
+
{
|
|
9
|
+
"name": "posts",
|
|
10
|
+
"type": "base",
|
|
11
|
+
"rules": { "list": "", "view": "", "create": "@request.auth.id != \"\"", "update": "author = @request.auth.id", "delete": "author = @request.auth.id" },
|
|
12
|
+
"fields": [
|
|
13
|
+
{ "name": "title", "type": "text", "required": true, "max": 120 },
|
|
14
|
+
{ "name": "author", "type": "relation", "collection": "users", "required": true, "cascadeDelete": true }
|
|
15
|
+
],
|
|
16
|
+
"indexes": ["CREATE INDEX idx_posts_author ON posts (author)"],
|
|
17
|
+
"seed": []
|
|
18
|
+
}
|
|
19
|
+
],
|
|
20
|
+
"rateLimits": [{ "label": "posts:create", "audience": "@auth", "maxRequests": 20, "duration": 600 }]
|
|
21
|
+
}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
`patterns.md` has ready-made collections; `schema.example.json` is a complete posts, comments and likes app.
|
|
25
|
+
|
|
26
|
+
## Collection keys
|
|
27
|
+
|
|
28
|
+
| Key | Meaning |
|
|
29
|
+
| --- | --- |
|
|
30
|
+
| `name` | Letters, digits, underscore. Becomes the URL segment `/api/collections/<name>/records`. |
|
|
31
|
+
| `type` | `base` (default), `auth` (people who sign in) or `view` (read-only SQL query). The built-in `users` auth collection already exists; list it to change its rules or add fields. |
|
|
32
|
+
| `rules` | `list`, `view`, `create`, `update`, `delete`, and for auth collections `auth` (who may sign in) and `manage`. Leave a key out to keep the current rule. New collections start with every rule `null`. See `rules.md`. |
|
|
33
|
+
| `fields` | Your fields. `id` is automatic; `created` and `updated` are added for you (`"timestamps": false` turns that off). |
|
|
34
|
+
| `indexes` | Plain SQL. Index names are unique across the whole database: use `idx_<collection>_<columns>`. |
|
|
35
|
+
| `viewQuery` | View collections only: a `SELECT` that returns an `id` column. Fields come from the query, so leave `fields` out. |
|
|
36
|
+
| `seed` | Records inserted only when the collection is empty. Plain values; no references to other records' ids. |
|
|
37
|
+
| `authOptions` | Auth collections only: raw PocketBase options such as `{ "passwordAuth": { "enabled": true, "identityFields": ["email"] } }`. |
|
|
38
|
+
|
|
39
|
+
## Top-level `rateLimits`
|
|
40
|
+
|
|
41
|
+
Each entry is merged into the backend's rate-limit settings by `label` and `audience`; entries not in the file stay as they are. Limits count requests per client IP.
|
|
42
|
+
|
|
43
|
+
| Key | Meaning |
|
|
44
|
+
| --- | --- |
|
|
45
|
+
| `label` | `<collection>:<action>` (`messages:create`, `users:auth`), `*:<action>` for all collections, or a path prefix such as `/api` |
|
|
46
|
+
| `audience` | `@guest` (signed out), `@auth` (signed in) or `""` (everyone, the default) |
|
|
47
|
+
| `maxRequests`, `duration` | At most `maxRequests` per `duration` seconds |
|
|
48
|
+
|
|
49
|
+
The platform default is `/api` 200 requests per 300 s and `*:auth` 20 per 300 s for guests. `pb-verify.mjs` prints the active limits.
|
|
50
|
+
|
|
51
|
+
## Field types (PocketBase 0.23 and later)
|
|
52
|
+
|
|
53
|
+
| `type` | Useful options |
|
|
54
|
+
| --- | --- |
|
|
55
|
+
| `text` | `required`, `min`, `max` (0 = default 5000), `pattern` (regex) |
|
|
56
|
+
| `editor` | Rich text (HTML). `maxSize` in bytes. Sanitize before rendering it. |
|
|
57
|
+
| `number` | `min`, `max`, `onlyInt`. `required` rejects 0. |
|
|
58
|
+
| `bool` | Defaults to false. `required` means it must be true. |
|
|
59
|
+
| `email`, `url` | `onlyDomains`, `exceptDomains` |
|
|
60
|
+
| `date` | ISO date string, `min`, `max` |
|
|
61
|
+
| `autodate` | `onCreate`, `onUpdate` (the script adds `created` and `updated`) |
|
|
62
|
+
| `select` | `values` (required array), `maxSelect` (1 = single value) |
|
|
63
|
+
| `file` | `maxSelect`, `maxSize` in bytes (default 5 MB), `mimeTypes`, `thumbs` (for example `["400x300"]`), `protected` |
|
|
64
|
+
| `relation` | `collection` (target collection name; the script resolves its id), `maxSelect` (default 1), `minSelect`, `cascadeDelete` |
|
|
65
|
+
| `json` | `maxSize` in bytes |
|
|
66
|
+
| `geoPoint` | `{ lon, lat }` |
|
|
67
|
+
|
|
68
|
+
## What the script guarantees
|
|
69
|
+
|
|
70
|
+
- `--check` catches unknown keys, field types and rule names, relations without a target, selects without values, and views without a query, before anything is sent.
|
|
71
|
+
- Base and auth collections are created first without relations, then given relations, so circular relations work. View collections are created after that, so their query can use every field.
|
|
72
|
+
- Fields and indexes that are not in the file are kept, so nothing is dropped by omission. An index with a new name but the same definition replaces the old one.
|
|
73
|
+
- Changing a field's type is refused because PocketBase would drop the column. Add a field with a new name and copy the data.
|
|
74
|
+
- Errors print PocketBase's own message, for example which rule refers to an unknown field.
|
|
75
|
+
|
|
76
|
+
## Changing the schema later
|
|
77
|
+
|
|
78
|
+
Edit `schema.json`, run `--check`, apply, then run `pb-verify.mjs`. The app keeps running: schema changes need no redeploy. Removing a field or collection deletes data, so do it in the dashboard (`/_/`) only after the user agrees.
|