@somewhere-cloud/cli 0.38.155 → 0.38.157
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 +8 -1
- package/dist/commands/docs.js +1 -1
- package/dist/lib/bundle.js +286 -188
- package/dist/lib/bundle.js.map +3 -3
- package/npm-shrinkwrap.json +2 -2
- package/package.json +2 -3
- package/dist/docs/adopt.md +0 -39
- package/dist/docs/analytics.md +0 -161
- package/dist/docs/api-reference.md +0 -339
- package/dist/docs/api-surface.md +0 -2067
- package/dist/docs/app-mcp.md +0 -162
- package/dist/docs/architecture-patterns.md +0 -334
- package/dist/docs/auth-client.md +0 -531
- package/dist/docs/billing.md +0 -76
- package/dist/docs/browser.md +0 -639
- package/dist/docs/calendar.md +0 -298
- package/dist/docs/changes.md +0 -323
- package/dist/docs/cli.md +0 -407
- package/dist/docs/common-mistakes.md +0 -199
- package/dist/docs/connect-agent.md +0 -72
- package/dist/docs/cors.md +0 -114
- package/dist/docs/cron.md +0 -167
- package/dist/docs/database-engine.md +0 -167
- package/dist/docs/declared-data.md +0 -1656
- package/dist/docs/declared-files.md +0 -475
- package/dist/docs/deploy-intelligence.md +0 -188
- package/dist/docs/deploy.md +0 -733
- package/dist/docs/design-system.md +0 -273
- package/dist/docs/dev-environments.md +0 -496
- package/dist/docs/domain-ownership.md +0 -31
- package/dist/docs/domains.md +0 -370
- package/dist/docs/feedback.md +0 -88
- package/dist/docs/file-tools.md +0 -116
- package/dist/docs/functions.md +0 -489
- package/dist/docs/getting-started.md +0 -238
- package/dist/docs/github.md +0 -95
- package/dist/docs/google-calendar.md +0 -212
- package/dist/docs/groups.md +0 -836
- package/dist/docs/guarantees.md +0 -58
- package/dist/docs/handled-for-you.md +0 -224
- package/dist/docs/inspect.md +0 -171
- package/dist/docs/limits.md +0 -68
- package/dist/docs/live-data.md +0 -197
- package/dist/docs/live.md +0 -92
- package/dist/docs/local-dev.md +0 -30
- package/dist/docs/migration-supabase.md +0 -58
- package/dist/docs/payments.md +0 -1102
- package/dist/docs/portability.md +0 -194
- package/dist/docs/postgres.md +0 -301
- package/dist/docs/pricing-comparison.md +0 -42
- package/dist/docs/pricing.md +0 -39
- package/dist/docs/project-groups.md +0 -107
- package/dist/docs/projects.md +0 -380
- package/dist/docs/recipe-ai-agent.md +0 -112
- package/dist/docs/recipe-ai-features.md +0 -227
- package/dist/docs/recipe-ai-receipts.md +0 -783
- package/dist/docs/recipe-background-jobs.md +0 -369
- package/dist/docs/recipe-booking.md +0 -348
- package/dist/docs/recipe-csv-import.md +0 -390
- package/dist/docs/recipe-ecommerce.md +0 -127
- package/dist/docs/recipe-email.md +0 -282
- package/dist/docs/recipe-escalation.md +0 -133
- package/dist/docs/recipe-file-uploads.md +0 -159
- package/dist/docs/recipe-import-app.md +0 -110
- package/dist/docs/recipe-login.md +0 -457
- package/dist/docs/recipe-multi-chat.md +0 -198
- package/dist/docs/recipe-paid-api.md +0 -161
- package/dist/docs/recipe-pdfs.md +0 -205
- package/dist/docs/recipe-push-notifications.md +0 -107
- package/dist/docs/recipe-seo.md +0 -200
- package/dist/docs/recipe-signed-in-app.md +0 -330
- package/dist/docs/render.md +0 -99
- package/dist/docs/rooms.md +0 -554
- package/dist/docs/sdk.md +0 -287
- package/dist/docs/sdks.md +0 -24
- package/dist/docs/search.md +0 -199
- package/dist/docs/security-model.md +0 -168
- package/dist/docs/setup.md +0 -212
- package/dist/docs/smoke.md +0 -102
- package/dist/docs/speed.md +0 -298
- package/dist/docs/sql-compatibility.md +0 -96
- package/dist/docs/sw.agent.md +0 -248
- package/dist/docs/sw.ai.md +0 -1291
- package/dist/docs/sw.auth.md +0 -1811
- package/dist/docs/sw.billing.md +0 -171
- package/dist/docs/sw.db.md +0 -2128
- package/dist/docs/sw.email.md +0 -661
- package/dist/docs/sw.endpoint.md +0 -187
- package/dist/docs/sw.env.md +0 -303
- package/dist/docs/sw.fetch.md +0 -115
- package/dist/docs/sw.fs.md +0 -424
- package/dist/docs/sw.groups.md +0 -836
- package/dist/docs/sw.image.md +0 -69
- package/dist/docs/sw.jobs.md +0 -520
- package/dist/docs/sw.logs.md +0 -61
- package/dist/docs/sw.mail.md +0 -106
- package/dist/docs/sw.notifications.md +0 -78
- package/dist/docs/sw.push.md +0 -161
- package/dist/docs/sw.rateLimit.md +0 -67
- package/dist/docs/sw.web.md +0 -128
- package/dist/docs/tasks.md +0 -269
- package/dist/docs/test.md +0 -220
- package/dist/docs/tool-reference-account.md +0 -7
- package/dist/docs/tool-reference-ai.md +0 -121
- package/dist/docs/tool-reference-analytics.md +0 -37
- package/dist/docs/tool-reference-api.md +0 -19
- package/dist/docs/tool-reference-artifacts.md +0 -35
- package/dist/docs/tool-reference-auth.md +0 -163
- package/dist/docs/tool-reference-browser.md +0 -114
- package/dist/docs/tool-reference-code.md +0 -26
- package/dist/docs/tool-reference-connect.md +0 -21
- package/dist/docs/tool-reference-cron.md +0 -48
- package/dist/docs/tool-reference-db.md +0 -282
- package/dist/docs/tool-reference-domains.md +0 -107
- package/dist/docs/tool-reference-email.md +0 -55
- package/dist/docs/tool-reference-env.md +0 -19
- package/dist/docs/tool-reference-feedback.md +0 -56
- package/dist/docs/tool-reference-fs.md +0 -265
- package/dist/docs/tool-reference-github.md +0 -57
- package/dist/docs/tool-reference-groups.md +0 -85
- package/dist/docs/tool-reference-help.md +0 -29
- package/dist/docs/tool-reference-jobs.md +0 -39
- package/dist/docs/tool-reference-live.md +0 -48
- package/dist/docs/tool-reference-logs.md +0 -16
- package/dist/docs/tool-reference-payments.md +0 -44
- package/dist/docs/tool-reference-project.md +0 -706
- package/dist/docs/tool-reference-push.md +0 -78
- package/dist/docs/tool-reference-rate-limit.md +0 -18
- package/dist/docs/tool-reference-search.md +0 -68
- package/dist/docs/tool-reference-security.md +0 -42
- package/dist/docs/tool-reference-smoke.md +0 -17
- package/dist/docs/tool-reference-stock-photos.md +0 -20
- package/dist/docs/tool-reference-tasks.md +0 -73
- package/dist/docs/tool-reference-telegram.md +0 -32
- package/dist/docs/tool-reference-usage.md +0 -9
- package/dist/docs/tool-reference-web.md +0 -71
- package/dist/docs/tool-reference-webhooks.md +0 -9
- package/dist/docs/troubleshooting.md +0 -411
- package/dist/docs/typed-functions.md +0 -94
- package/dist/docs/verify-before-deploy.md +0 -67
- package/dist/docs/vs-supabase.md +0 -75
- package/dist/docs/vs-vercel.md +0 -103
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
# Connect an agent, claim an app, disconnect
|
|
2
|
-
|
|
3
|
-
A person lets an agent work on their Somewhere account with a **connect code**,
|
|
4
|
-
keeps an app an agent made without an account by **claiming** it, and ends an
|
|
5
|
-
agent's access by **disconnecting** it. Issuing, claiming and disconnecting are
|
|
6
|
-
done by the signed-in person; the agent only redeems.
|
|
7
|
-
|
|
8
|
-
## The person's own sign-in
|
|
9
|
-
|
|
10
|
-
A person signs in to Somewhere in the browser: `POST /v1/auth/platform-signup`,
|
|
11
|
-
`/platform-login` or `/platform-magic-link`, then `GET /v1/auth/platform-me` and
|
|
12
|
-
`POST /v1/auth/platform-logout`. These set an HttpOnly cookie session for the
|
|
13
|
-
dashboard; they are for the person, not for agents. An agent works with a
|
|
14
|
-
connect code (below) or with `somewhere login` on the person's machine, which the
|
|
15
|
-
person approves. A step that only the person may take answers a key or an agent
|
|
16
|
-
with `403 PERSON_APPROVAL_REQUIRED`.
|
|
17
|
-
|
|
18
|
-
## Connect
|
|
19
|
-
|
|
20
|
-
1. The person issues a code while signed in: the dashboard's **Copy prompt for
|
|
21
|
-
your agent**, or `POST /v1/auth/connect-codes` from their signed-in session.
|
|
22
|
-
A code starts with `swcc_`, works once, and expires after 10 minutes.
|
|
23
|
-
2. The agent redeems it, with no other credential:
|
|
24
|
-
|
|
25
|
-
```bash
|
|
26
|
-
curl -s https://api.somewhere.cloud/v1/auth/connect-codes/redeem \
|
|
27
|
-
-H 'Content-Type: application/json' -d '{"connect_code":"swcc_..."}'
|
|
28
|
-
# → { "data": { "key": "smt_...", "expires_at": ..., "projects_remaining": 3 } }
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
Somewhere's own apps (the Guide) skip the copy and paste: the person presses
|
|
32
|
-
Allow on a Somewhere page, which issues the same kind of code and returns it to
|
|
33
|
-
that app's fixed address. That page is not open to other apps. Inside the dashboard, the Guide signs in as the
|
|
34
|
-
person without any page at all: the dashboard hands it a one-time sign-in for the
|
|
35
|
-
person's own account (the same authority as their `somewhere login`, ended from
|
|
36
|
-
Devices and sessions). Only Somewhere's own listed apps can be handed a sign-in.
|
|
37
|
-
|
|
38
|
-
Keep the returned key on your server. Never put it, or the code, in a model
|
|
39
|
-
conversation or browser code.
|
|
40
|
-
|
|
41
|
-
### What the key may do
|
|
42
|
-
|
|
43
|
-
It may create a few projects (`projects_remaining`), deploy them by id
|
|
44
|
-
(`POST /v1/deploy { project_id, ... }`, including `template`), run their SQL,
|
|
45
|
-
read their logs, and read each of them (`GET /v1/projects/{id}` and its
|
|
46
|
-
`/overview`). It cannot see or touch the person's other apps, account, billing,
|
|
47
|
-
domains or secrets. Deploy and SQL pause on a project once its owner adds
|
|
48
|
-
private configuration to it. An unused key expires after 30 days.
|
|
49
|
-
|
|
50
|
-
Deploying files to a live project needs `base_release_id`: keep the
|
|
51
|
-
`release_id` your last deploy returned and pass it on the next one. If someone
|
|
52
|
-
else changed the app in between, the deploy is refused instead of overwriting
|
|
53
|
-
their work.
|
|
54
|
-
|
|
55
|
-
## Disconnect
|
|
56
|
-
|
|
57
|
-
The person lists their codes with `GET /v1/auth/connect-codes` (statuses
|
|
58
|
-
`available`, `connected`, `disconnected`, `revoked`, `expired`) and ends one with
|
|
59
|
-
`DELETE /v1/auth/connect-codes/{id}`: an unused code is revoked, and a used code
|
|
60
|
-
disconnects the agent it connected (`{ "disconnected": true }`). The dashboard
|
|
61
|
-
does the same. The agent's next call is refused with `401`; tell the person in
|
|
62
|
-
plain words that it is no longer connected.
|
|
63
|
-
|
|
64
|
-
## Claim an app made without an account
|
|
65
|
-
|
|
66
|
-
A deploy made without an account answers with a `claim_url`. Give that link to
|
|
67
|
-
the person: they open it signed in to their account and the app moves to it. The
|
|
68
|
-
agent can also ask the platform to email the link with the temporary deploy's
|
|
69
|
-
own key: `POST /v1/auth/claim { "email": "..." }` (`202 accepted`). Claiming
|
|
70
|
-
itself is always the person's step on the claim page.
|
|
71
|
-
|
|
72
|
-
Related topics: `projects`, `deploy`, `security-model`.
|
package/dist/docs/cors.md
DELETED
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
# CORS — which browser origins may call your project's functions
|
|
2
|
-
|
|
3
|
-
Read this when a browser call to your API returns
|
|
4
|
-
`403 {"ok":false,"error":"ORIGIN_NOT_ALLOWED"}`, or before you point a
|
|
5
|
-
frontend hosted elsewhere at a somewhere.cloud project.
|
|
6
|
-
|
|
7
|
-
The same origin policy applies to every project. There is no opt-out. The policy is enforced when the request reaches the project, so
|
|
8
|
-
existing apps do not need a new deploy for it.
|
|
9
|
-
|
|
10
|
-
## Your own origins are allowed by construction — nothing to configure
|
|
11
|
-
|
|
12
|
-
While the platform is serving your project on a host, that host's origin is
|
|
13
|
-
your project's own origin. There is no row to write and nothing to turn on:
|
|
14
|
-
|
|
15
|
-
- your **production** project URL, `https://<subdomain>.somewhere.site`
|
|
16
|
-
- every **verified custom domain** attached to the project
|
|
17
|
-
- your **private preview** URL
|
|
18
|
-
|
|
19
|
-
A brand-new project has an empty allowlist and its own frontend still works.
|
|
20
|
-
If you are calling your API from a page the platform served for that same
|
|
21
|
-
project, the allowlist is not your problem — read "Diagnose a 403" below.
|
|
22
|
-
|
|
23
|
-
The one nuance: a preview origin is admitted while the preview host is being
|
|
24
|
-
served, and a production origin while production is being served. It is not a
|
|
25
|
-
cross-environment grant, so a page on your production host calling your
|
|
26
|
-
preview host is a third-party call and needs the allowlist.
|
|
27
|
-
|
|
28
|
-
## The allowlist is for THIRD-PARTY origins only
|
|
29
|
-
|
|
30
|
-
Configure an origin when the page making the call is **not** served by this
|
|
31
|
-
project — a frontend you host elsewhere, a marketing site on another
|
|
32
|
-
domain, a local dev server on `http://localhost:5173`, or another
|
|
33
|
-
somewhere.cloud project.
|
|
34
|
-
|
|
35
|
-
Manage the list with `somewhere project allowed-origins`. Your own origins
|
|
36
|
-
are never listed.
|
|
37
|
-
|
|
38
|
-
```bash
|
|
39
|
-
# Read the current list first — set replaces it wholesale.
|
|
40
|
-
somewhere project allowed-origins list --project my-app
|
|
41
|
-
|
|
42
|
-
# Replace the FULL list. Include everything you want kept.
|
|
43
|
-
somewhere project allowed-origins set https://app.example.com http://localhost:5173 --project my-app
|
|
44
|
-
|
|
45
|
-
# Allow no other origins; the project's own addresses stay trusted.
|
|
46
|
-
somewhere project allowed-origins set --clear --project my-app
|
|
47
|
-
```
|
|
48
|
-
|
|
49
|
-
`--project` defaults to the linked project.
|
|
50
|
-
|
|
51
|
-
The response returns the configured `allowed_origins`. There is no
|
|
52
|
-
`cors_mode` setting or response field; sending that obsolete selector to a
|
|
53
|
-
project update or to `project_allowed_origins_set` is rejected.
|
|
54
|
-
|
|
55
|
-
Over MCP the same tools are `project_allowed_origins_get` and
|
|
56
|
-
`project_allowed_origins_set`. `project_id` accepts the UUID, subdomain or slug; use the resolved
|
|
57
|
-
`project_id` the response echoes in follow-up calls.
|
|
58
|
-
|
|
59
|
-
Rules the platform enforces on each entry:
|
|
60
|
-
|
|
61
|
-
- An **exact origin**: scheme + host + optional port. No path, query,
|
|
62
|
-
fragment, credentials, or wildcards — `https://app.example.com/` and
|
|
63
|
-
`https://*.example.com` are both rejected with `VALIDATION_ERROR`.
|
|
64
|
-
- `https` only, except `http://localhost` and `http://127.0.0.1`.
|
|
65
|
-
- Matching is **byte-exact**. A different subdomain, a different port, and a
|
|
66
|
-
trailing slash are each a different origin and must be listed on their own.
|
|
67
|
-
- At most 50 origins per project.
|
|
68
|
-
- Reading the list is open to any project member; changing it is the project
|
|
69
|
-
**owner or a platform admin** only, and every change is audited.
|
|
70
|
-
|
|
71
|
-
## What is refused, and what reaches your function
|
|
72
|
-
|
|
73
|
-
The check covers requests to your functions (`/api/*`), the managed data
|
|
74
|
-
and files endpoints (`/__sw/data`, `/__sw/files`) and live list connections
|
|
75
|
-
(`/__sw/live/subscribe`). It is about
|
|
76
|
-
**credentials**, not about blocking traffic:
|
|
77
|
-
|
|
78
|
-
| Request | Outcome |
|
|
79
|
-
|---|---|
|
|
80
|
-
| No `Origin` header (server-to-server, `curl`, a mobile app) | Reaches your function. CORS is a browser mechanism; a non-browser caller is unaffected. A `POST`/`PUT`/`PATCH`/`DELETE` authenticated by a session cookie gets `400 ORIGIN_REQUIRED` when your function reads its user: send the app's `Origin`, or use `Authorization: Bearer`. |
|
|
81
|
-
| Approved origin (own or configured) | Reaches your function. The platform adds the CORS response headers, including `Access-Control-Allow-Credentials`. |
|
|
82
|
-
| Approved origin, `OPTIONS` preflight | Answered by the platform with `204` and those headers. You never write preflight boilerplate. |
|
|
83
|
-
| Unapproved origin, **no** `Cookie` and **no** `Authorization`, method `GET`/`HEAD`/`POST` | Reaches your function, with **no** platform CORS headers. Whether the browser may read the result is decided by the headers your own handler sets. |
|
|
84
|
-
| Unapproved origin carrying `Cookie` or `Authorization` | Refused with `403 {"ok":false,"error":"ORIGIN_NOT_ALLOWED"}` before your code runs. |
|
|
85
|
-
| Unapproved origin, preflight advertising a method other than `GET`/`HEAD`/`POST`, or `Authorization`/`Cookie` in `Access-Control-Request-Headers` | Same `403`. The preflight is announcing a credentialed follow-up. |
|
|
86
|
-
|
|
87
|
-
So a deliberately public, credentialless API stays possible: set your own
|
|
88
|
-
`Access-Control-Allow-Origin: *` (without `Access-Control-Allow-Credentials`)
|
|
89
|
-
in the handler's response. When your handler sets its own
|
|
90
|
-
`Access-Control-Allow-Origin`, that header wins and the platform layers
|
|
91
|
-
nothing on top.
|
|
92
|
-
|
|
93
|
-
## Diagnose a 403 ORIGIN_NOT_ALLOWED
|
|
94
|
-
|
|
95
|
-
1. Read the exact `Origin` header the browser sent — the DevTools Network
|
|
96
|
-
tab shows it on the failing request. That string, byte for byte, is what
|
|
97
|
-
has to match.
|
|
98
|
-
2. Is it a host the platform serves for **this** project (production URL,
|
|
99
|
-
verified custom domain, this project's preview)? Then it is already
|
|
100
|
-
allowed, and the failure is something else: a stale bookmark on an old
|
|
101
|
-
subdomain, a call aimed at a different project, or a custom domain that is
|
|
102
|
-
attached but not yet verified. Check with
|
|
103
|
-
`somewhere call project_get '{"project_id":"my-app"}'` and
|
|
104
|
-
`somewhere call domain_list '{"project_id":"my-app"}'`.
|
|
105
|
-
3. Otherwise it is a third-party origin: add it with
|
|
106
|
-
`project_allowed_origins_set`, sending the full list.
|
|
107
|
-
4. Not from a browser at all? Then `ORIGIN_NOT_ALLOWED` means something is
|
|
108
|
-
forwarding a foreign `Origin` header along with a cookie or bearer token.
|
|
109
|
-
With `Authorization: Bearer`, drop the `Origin` header — bearer calls are
|
|
110
|
-
not origin-checked. With a session cookie, a write still needs an
|
|
111
|
-
`Origin` (`400 ORIGIN_REQUIRED` without one): send this project's own
|
|
112
|
-
origin, or switch to `Authorization: Bearer` (`docs({ topic: 'auth-client' })`).
|
|
113
|
-
|
|
114
|
-
Related: `security-model`, `auth-client`, `troubleshooting`.
|
package/dist/docs/cron.md
DELETED
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
# Cron — Scheduled Tasks
|
|
2
|
-
|
|
3
|
-
A schedule calls one of your deployed functions on a cron expression, such as
|
|
4
|
-
every hour or every day at 09:00 in a time zone you choose. The handler is an
|
|
5
|
-
ordinary function in `api/`, signed by the platform, so there's no scheduler
|
|
6
|
-
or worker for you to run. After creating a schedule, test it now with
|
|
7
|
-
`somewhere cron run <id or name> --wait`; do not wait for its scheduled time.
|
|
8
|
-
|
|
9
|
-
Schedules run on every plan. The plan sets the schedule count and minimum
|
|
10
|
-
interval; `cron_list({ project_id })` returns both in `policy` as
|
|
11
|
-
`min_interval_minutes` and `max_per_project`. A schedule that exceeds
|
|
12
|
-
them gets `CRON_SCHEDULE_TOO_FREQUENT` or `CRON_QUOTA_EXCEEDED`.
|
|
13
|
-
|
|
14
|
-
## Recipe — hourly cleanup of stale rows
|
|
15
|
-
|
|
16
|
-
Delete unpaid submissions older than a day, every hour. It takes two parts: a
|
|
17
|
-
deployed, signed handler and a registered schedule.
|
|
18
|
-
|
|
19
|
-
```ts
|
|
20
|
-
// db/schema.ts
|
|
21
|
-
import { id, owner, schema, table, text, timestamp } from 'somewhere/db';
|
|
22
|
-
|
|
23
|
-
export default schema({
|
|
24
|
-
submissions: table(
|
|
25
|
-
{ id: id(), status: text(), created_at: timestamp({ default: 'now' }) },
|
|
26
|
-
{ scope: owner() },
|
|
27
|
-
),
|
|
28
|
-
});
|
|
29
|
-
```
|
|
30
|
-
|
|
31
|
-
```ts
|
|
32
|
-
// api/jobs/cleanup.ts — deployed with the app; runs only when the scheduler calls it
|
|
33
|
-
export default sw.endpoint({
|
|
34
|
-
auth: 'none', // The platform invocation signature authorizes this route.
|
|
35
|
-
handler: async ({ request: req }, sw) => {
|
|
36
|
-
if (!(await sw.jobs.verifyInvocation(req))) return new Response('forbidden', { status: 403 });
|
|
37
|
-
const cutoff = new Date(Date.now() - 24 * 60 * 60 * 1000).toISOString();
|
|
38
|
-
const r = await sw.db.server.remove('submissions', { where: { status: 'unpaid', created_at: { lt: cutoff } } });
|
|
39
|
-
return { removed: r.changes };
|
|
40
|
-
},
|
|
41
|
-
});
|
|
42
|
-
```
|
|
43
|
-
|
|
44
|
-
```bash
|
|
45
|
-
somewhere deploy # the handler must be live first
|
|
46
|
-
somewhere cron create "0 * * * *" /api/jobs/cleanup --project <project> --name cleanup
|
|
47
|
-
somewhere cron run cleanup --project <project> --wait # run once now and wait for the result (id or name)
|
|
48
|
-
```
|
|
49
|
-
|
|
50
|
-
- **Verify first.** `sw.jobs.verifyInvocation(req)` is true only for a call
|
|
51
|
-
the platform signed for this project. Anyone else calling the URL gets 403
|
|
52
|
-
before the handler touches data.
|
|
53
|
-
- **Make it safe to repeat.** A run can be delivered more than once. A delete
|
|
54
|
-
by condition, like this one, gives the same result the second time.
|
|
55
|
-
|
|
56
|
-
## Create a schedule
|
|
57
|
-
cron_create({
|
|
58
|
-
project_id: "my-app",
|
|
59
|
-
schedule: "0 9 * * *", // 5-field cron expression
|
|
60
|
-
timezone: "America/Los_Angeles", // optional; defaults to UTC
|
|
61
|
-
handler: "/api/jobs/daily-digest",
|
|
62
|
-
payload: { segment: "daily" } // optional
|
|
63
|
-
})
|
|
64
|
-
|
|
65
|
-
Schedules use UTC unless you pass an IANA `timezone`. With a time zone, the
|
|
66
|
-
schedule follows the local wall clock across daylight-saving changes: 09:00
|
|
67
|
-
in Los Angeles fires at 16:00Z in September (PDT) and 17:00Z in December
|
|
68
|
-
(PST). The CLI takes the same option as `--timezone`.
|
|
69
|
-
|
|
70
|
-
## Run once now
|
|
71
|
-
|
|
72
|
-
Use `cron_run({ cron_id })` (CLI: `somewhere cron run <id or name> --wait`)
|
|
73
|
-
to test the deployed handler immediately. It uses the same path as a
|
|
74
|
-
scheduled fire, leaves the schedule unchanged, and records the run with
|
|
75
|
-
`trigger: "manual"`. Read the run history with `job_get({ cron_id })`.
|
|
76
|
-
|
|
77
|
-
## Scheduled tasks and previews
|
|
78
|
-
|
|
79
|
-
Nothing runs on a schedule in a preview. To test a scheduled task there, run
|
|
80
|
-
it once on demand:
|
|
81
|
-
|
|
82
|
-
- `somewhere cron run <name> --preview` runs that production task's handler
|
|
83
|
-
path and payload once in the folder's open preview (MCP: `cron_run` with
|
|
84
|
-
`preview_session_id` and `preview_id`). The production schedule is only
|
|
85
|
-
read: its last run, next run and failure streak never change.
|
|
86
|
-
- `somewhere preview run-job <handler-path> [--payload <json>]` runs any
|
|
87
|
-
handler path once in the preview, as one signed job delivery: a schedule is
|
|
88
|
-
only a handler path plus a payload.
|
|
89
|
-
|
|
90
|
-
Either way the run is a preview job: the preview's code, data, files, users
|
|
91
|
-
and mail, one attempt, at most 270 seconds, no user (as a scheduled run
|
|
92
|
-
has none). Preview code cannot create or change a schedule: `sw.cron` refuses
|
|
93
|
-
there with `PREVIEW_CRON_UNAVAILABLE`. Automatic preview schedules were
|
|
94
|
-
removed: their routes answer 410 `PREVIEW_SCHEDULES_REMOVED`.
|
|
95
|
-
|
|
96
|
-
## Observe a scheduled fire
|
|
97
|
-
|
|
98
|
-
To check automatic delivery, read `somewhere cron runs <id or name> --json`
|
|
99
|
-
(CLI `--limit` is 1–100, default 20). It shows the schedule and recent jobs.
|
|
100
|
-
Record the next scheduled occurrence, then read the history again after that
|
|
101
|
-
slot. Match a job with `trigger: "scheduled"` and `cron_scheduled_at` equal
|
|
102
|
-
to that occurrence. Read its `status`, `started_at`, `completed_at` and
|
|
103
|
-
result to distinguish dispatch from a completed handler. The same history is
|
|
104
|
-
available through `job_get({ cron_id, limit: 20 })`.
|
|
105
|
-
|
|
106
|
-
A manual run exercises the signed job path immediately; it does not establish
|
|
107
|
-
that the timer delivered an occurrence. Its `trigger` remains `"manual"`
|
|
108
|
-
and `cron_scheduled_at` is null. Keep the two checks separate in your test
|
|
109
|
-
results: handler exercised now, automatic scheduled delivery observed later.
|
|
110
|
-
|
|
111
|
-
The scheduler checks due schedules once a minute. Dispatch and job execution
|
|
112
|
-
can start later than the nominal slot, including delays of a few minutes;
|
|
113
|
-
there is no exact-second start guarantee. An overdue label says the schedule
|
|
114
|
-
has not advanced yet. Check the matching run history before concluding that
|
|
115
|
-
the handler ran or failed. Recent history is bounded, so a missing row in that
|
|
116
|
-
page alone does not establish that an older occurrence never ran.
|
|
117
|
-
|
|
118
|
-
## Handler
|
|
119
|
-
A handler is a deployed function, written and deployed like any other
|
|
120
|
-
(`functions`), so deploy it before you create the schedule. The platform
|
|
121
|
-
POSTs the schedule's `payload` when it fires.
|
|
122
|
-
|
|
123
|
-
Verify each call with `sw.jobs.verifyInvocation(req)`, the same check job
|
|
124
|
-
and queue handlers use. The platform signs every invocation; a header or a
|
|
125
|
-
shared secret of your own is not a substitute. A run may repeat, so make the
|
|
126
|
-
handler idempotent.
|
|
127
|
-
|
|
128
|
-
## Schedule format
|
|
129
|
-
minute hour day-of-month month day-of-week
|
|
130
|
-
0 9 * * * → every day at 09:00 in the selected timezone
|
|
131
|
-
*/15 * * * * → every 15 minutes
|
|
132
|
-
0 0 1 * * → first of every month
|
|
133
|
-
|
|
134
|
-
The finest granularity is one minute: a 5-field expression has no seconds
|
|
135
|
-
field. For updates faster than that, push as data arrives (`live-data`).
|
|
136
|
-
|
|
137
|
-
## When a schedule is the wrong tool
|
|
138
|
-
|
|
139
|
-
| You want | Use |
|
|
140
|
-
|---|---|
|
|
141
|
-
| Work in response to a user action | The request itself, or `sw.jobs.create` from it (`recipe-background-jobs`) |
|
|
142
|
-
| One piece of work at a later time, such as a reminder in two hours | `sw.jobs.create({ handler, payload, run_at })`; `run_at` can be any future time |
|
|
143
|
-
| Each user picks their own time | One schedule every few minutes that reads due rows from your table; schedules per project are capped by plan |
|
|
144
|
-
| A retry after a failure | Nothing extra: a failed job run is retried |
|
|
145
|
-
| A page that shows changes as they happen | A live view (`live`), not a polling schedule |
|
|
146
|
-
|
|
147
|
-
## Minimum interval
|
|
148
|
-
|
|
149
|
-
Your plan sets the smallest gap allowed between two fires, and how many
|
|
150
|
-
schedules one project may hold. Read both from `cron_list({ project_id })` →
|
|
151
|
-
`policy.min_interval_minutes` and `policy.max_per_project`; `null` means the
|
|
152
|
-
plan sets no limit.
|
|
153
|
-
|
|
154
|
-
- A schedule tighter than `min_interval_minutes` is refused when you create or
|
|
155
|
-
edit it, with `CRON_SCHEDULE_TOO_FREQUENT` naming both intervals. The check
|
|
156
|
-
measures the schedule's real cadence, so a tight gap inside a
|
|
157
|
-
sparse-looking expression is caught too.
|
|
158
|
-
- One more schedule than `max_per_project` is refused with
|
|
159
|
-
`CRON_QUOTA_EXCEEDED` (429).
|
|
160
|
-
- The interval is checked when a schedule is created or its timing is edited.
|
|
161
|
-
Editing the handler, payload or name, or resuming a paused schedule, leaves
|
|
162
|
-
the cadence as it is.
|
|
163
|
-
|
|
164
|
-
## Missed fires
|
|
165
|
-
A fire time that passes while the platform is unavailable is not replayed.
|
|
166
|
-
Design schedules to tolerate an occasional skipped run: the cleanup above
|
|
167
|
-
catches up on the next hour.
|
|
@@ -1,167 +0,0 @@
|
|
|
1
|
-
# Database — SQL support, capabilities, and limits
|
|
2
|
-
|
|
3
|
-
Each project has its own SQL database. Declare each table in `db/schema.ts`
|
|
4
|
-
together with who may reach its rows; `somewhere deploy` creates the table and
|
|
5
|
-
the platform applies that rule on every structured call:
|
|
6
|
-
|
|
7
|
-
- `owner()` — each signed-in user reads and changes only their own rows.
|
|
8
|
-
- `shared()` — any signed-in user reads every row and changes only the rows
|
|
9
|
-
they wrote.
|
|
10
|
-
- `serverOnly()` — use it from server functions; the generated browser
|
|
11
|
-
client has no access. Authorize the caller in your handler.
|
|
12
|
-
|
|
13
|
-
In the browser, import the generated `data` client from `somewhere:data`; it
|
|
14
|
-
has exactly the operations you granted. Inside a function, use `sw.db.from`,
|
|
15
|
-
`insert`, `update` and `remove`. Deploy, sign in with two accounts, and each
|
|
16
|
-
sees only its own `owner()` rows.
|
|
17
|
-
|
|
18
|
-
Next: `declared-data` for the schema and the generated client, `sw.db` for
|
|
19
|
-
server queries and transactions, `sql-compatibility` for the SQL dialect, and
|
|
20
|
-
`postgres` to attach a PostgreSQL database you own.
|
|
21
|
-
|
|
22
|
-
## Short answer
|
|
23
|
-
|
|
24
|
-
A fully managed relational database, one database per project. It supports
|
|
25
|
-
foreign keys, joins, indexes, transactions, triggers, JSON operators,
|
|
26
|
-
full-text search, and math functions.
|
|
27
|
-
|
|
28
|
-
## Built on Cloudflare D1
|
|
29
|
-
|
|
30
|
-
Each app's rows live in its own Cloudflare D1 database. Your functions run
|
|
31
|
-
on Cloudflare Workers.
|
|
32
|
-
|
|
33
|
-
- **Regional read replicas shorten read paths.** New project databases are
|
|
34
|
-
created with D1 read replication on. A deployed function's reads go through a
|
|
35
|
-
D1 session, so a replica closer to the request can answer them instead of
|
|
36
|
-
the primary.
|
|
37
|
-
- **Writes use one primary.** Every write goes to the database's single
|
|
38
|
-
primary, in the region chosen when the project was created.
|
|
39
|
-
- **Read-your-writes travels with the session bookmark.** Within one request,
|
|
40
|
-
a read after a write sees that write. Across requests, it holds while the
|
|
41
|
-
client carries the session bookmark the platform returns; a request without
|
|
42
|
-
it may be answered by a replica that is briefly behind.
|
|
43
|
-
- Developer tools that query the database directly (`db_query`, the CLI's
|
|
44
|
-
SQL commands) read the primary.
|
|
45
|
-
|
|
46
|
-
Replica regions and status:
|
|
47
|
-
https://developers.cloudflare.com/d1/best-practices/read-replication/
|
|
48
|
-
|
|
49
|
-
## How storage grows
|
|
50
|
-
|
|
51
|
-
Managed data grows with your app. Somewhere handles storage as it grows.
|
|
52
|
-
Database storage is measured across your account under your plan (see the
|
|
53
|
-
storage section of `sw.db`). File contents, platform logs, analytics
|
|
54
|
-
events, sign-in credentials and search vectors already live in separate stores
|
|
55
|
-
and do not count toward it. App rows and indexes do, along with internal data
|
|
56
|
-
such as persisted AI conversations and file-search text. Keep large values in
|
|
57
|
-
`sw.fs`.
|
|
58
|
-
|
|
59
|
-
Growth handling covers data read and written through the managed API. Very
|
|
60
|
-
large tables queried with raw SQL stay in one database; use the managed API
|
|
61
|
-
for datasets that need to grow.
|
|
62
|
-
|
|
63
|
-
## Starting a new app? Start here
|
|
64
|
-
|
|
65
|
-
Start with the managed project database. It comes with the parts of a data
|
|
66
|
-
layer you would otherwise build and maintain yourself:
|
|
67
|
-
|
|
68
|
-
- **Row permissions are declared once and enforced automatically.** A table
|
|
69
|
-
declares who may reach it, and every structured call carries that rule into
|
|
70
|
-
the statement — so the authorization plumbing that usually repeats in every
|
|
71
|
-
handler is written in one place and applied for you.
|
|
72
|
-
- **A typed browser client, generated from that declaration.** The operations
|
|
73
|
-
you granted exist and are typed; the ones you did not are absent.
|
|
74
|
-
- **The schema is context an agent can use.** `db/schema.ts` is a file it
|
|
75
|
-
reads and changes, so the thing building your app already knows the shape of
|
|
76
|
-
your data — no separate migration tool, no drift between what the code
|
|
77
|
-
believes and what the database holds.
|
|
78
|
-
- **Nothing to operate.** No connection string to hold, no pool to size, no
|
|
79
|
-
migration runner, no second bill.
|
|
80
|
-
|
|
81
|
-
Its SQL surface is narrower than PostgreSQL's; the next section covers
|
|
82
|
-
when to attach a database you own.
|
|
83
|
-
|
|
84
|
-
## When PostgreSQL fits
|
|
85
|
-
|
|
86
|
-
Attach one PostgreSQL database per project when you already have Postgres data,
|
|
87
|
-
need a specific extension or type, or rely on existing Postgres tools.
|
|
88
|
-
Use the managed project database for the app data you want the platform to
|
|
89
|
-
scope automatically. An app can use both.
|
|
90
|
-
|
|
91
|
-
The attachment connects to Neon on every plan, including Free. You own the
|
|
92
|
-
account, bill, region, credentials, migrations, authorization and recovery.
|
|
93
|
-
Queries run through the provider's driver; declared row permissions and the
|
|
94
|
-
generated browser client apply to the managed database, not those queries.
|
|
95
|
-
See `postgres` for connecting and authorizing access.
|
|
96
|
-
|
|
97
|
-
Neon references: https://neon.com/docs/reference/compatibility and
|
|
98
|
-
https://neon.com/docs/serverless/serverless-driver
|
|
99
|
-
|
|
100
|
-
## What you get vs Postgres
|
|
101
|
-
|
|
102
|
-
Supported syntax and capabilities:
|
|
103
|
-
- Standard SQL: SELECT / INSERT / UPDATE / DELETE / JOIN / GROUP BY / HAVING / window functions
|
|
104
|
-
- Constraints: PRIMARY KEY, FOREIGN KEY (with ON DELETE/UPDATE), UNIQUE, NOT NULL, CHECK
|
|
105
|
-
- Indexes (B-tree), partial indexes, expression indexes
|
|
106
|
-
- Triggers (BEFORE / AFTER / INSTEAD OF)
|
|
107
|
-
- Transactions with full ACID, savepoints
|
|
108
|
-
- JSON columns + JSON operators (`json_extract`, `->`, `->>`)
|
|
109
|
-
- Full-text search
|
|
110
|
-
- Common Table Expressions, recursive CTEs
|
|
111
|
-
- `RETURNING` on INSERT, UPDATE and DELETE
|
|
112
|
-
|
|
113
|
-
Differences to account for in SQL:
|
|
114
|
-
- No `ENUM` types — use CHECK constraints + TEXT.
|
|
115
|
-
- No `UUID` type — use TEXT, generate via `crypto.randomUUID()` in your function.
|
|
116
|
-
- No stored procedures / PL/pgSQL — write logic in your server function.
|
|
117
|
-
- No materialized views — cache results in a regular table refreshed via cron.
|
|
118
|
-
- Ordinary writes are direct. The per-project database engine schedules writes
|
|
119
|
-
one at a time; there is no additional platform write queue. Concurrent reads
|
|
120
|
-
may overlap, but placement and database load still affect latency.
|
|
121
|
-
- Default-engine semantics apply. Division by zero returns `NULL`,
|
|
122
|
-
ascending order places `NULL` first by default, and `LIKE` is
|
|
123
|
-
case-insensitive for ASCII unless the query makes its intent explicit.
|
|
124
|
-
|
|
125
|
-
## What the platform compensates for
|
|
126
|
-
|
|
127
|
-
- **Schema as source** — declare tables in `db/schema.ts` and deploy; the
|
|
128
|
-
platform creates them and arms per-user scoping in the same step, and the
|
|
129
|
-
file stays the only schema writer for those tables. Hand-managed tables keep
|
|
130
|
-
`db_migrate`; their access rules come from `db/schema.ts` too. See the two-worlds section in
|
|
131
|
-
docs({ topic: 'sw.db' }).
|
|
132
|
-
- **Vector search** — `sw.search.*` gives you embeddings + similarity queries over your tables without bolting on a separate vector database.
|
|
133
|
-
- **Database change events** — the developer-side `db_webhook_set/get/delete`
|
|
134
|
-
tools register a webhook on INSERT/UPDATE/DELETE. Cleaner than LISTEN/NOTIFY
|
|
135
|
-
for typical app use.
|
|
136
|
-
- **Per-user row scoping** — the structured builder
|
|
137
|
-
`sw.db.from/count/insert/update/remove` on a declared user-owned table injects the
|
|
138
|
-
owner filter from the request's verified user automatically. Explicit
|
|
139
|
-
`sw.db.server` reads bypass that scope only when selected by the function.
|
|
140
|
-
- **Atomic batches** — `sw.db.tx` and `sw.db.server.tx` commit related
|
|
141
|
-
declared operations together; ordinary single writes remain direct.
|
|
142
|
-
- **Database storage** — read your plan’s account total in the
|
|
143
|
-
[live pricing catalog](https://api.somewhere.cloud/v1/pricing). It is summed
|
|
144
|
-
across your projects.
|
|
145
|
-
This is not a combined cap on database, files, logs
|
|
146
|
-
and analytics: those primitives use separate stores and limits. Internal
|
|
147
|
-
conversation history and file-search text indexes can still occupy project
|
|
148
|
-
database space; see the size boundaries in docs({ topic: 'sw.db' }). Storage
|
|
149
|
-
is enforced; reads are unmetered. The live values are returned by
|
|
150
|
-
`GET /v1/pricing`.
|
|
151
|
-
|
|
152
|
-
## Choosing for your workload
|
|
153
|
-
|
|
154
|
-
Use the managed database for relational app data: accounts, content, orders
|
|
155
|
-
and messages, with the row rules and typed client declared in your project.
|
|
156
|
-
Keep request-path queries bounded and batch related writes. Storage is
|
|
157
|
-
measured in GB under your plan; see the database storage notes above.
|
|
158
|
-
|
|
159
|
-
For large analytical scans, evaluate a warehouse. For Postgres-specific
|
|
160
|
-
extensions or existing SQL and tooling, use an attached database. Measure
|
|
161
|
-
your own query shapes and write concurrency when choosing between them.
|
|
162
|
-
|
|
163
|
-
Developer-side `db_dump` gives you a SQL file when another database better
|
|
164
|
-
fits the workload. It restores directly into the source database engine;
|
|
165
|
-
Postgres requires the two-step conversion in `portability`.
|
|
166
|
-
|
|
167
|
-
Related: `sw.db`, `portability`, `security-model`.
|