Reference
newt add context
Adds a context to the backend, with its own database and its own files.
newt add context adds a context: a part of your app with its own database, such as tasks or chat. It opens the same kind of database and file storage as the rest of the backend.
newt add context <name> [options]Arguments
Section titled “Arguments”| Argument | Meaning |
|---|---|
name |
The context’s name, as in tasks or web-session |
Options
Section titled “Options”| Option | Default | Meaning |
|---|---|---|
--backend <folder> |
the one you are in | The backend, in a monorepo with several |
It also takes the global flags.
Name rules
Section titled “Name rules”- Lowercase letters and digits, words joined by
-, starting with a letter:tasks,web-session. - Not
api: its app would be served at/api, where the routes are. - With Postgres or MySQL, the database name is
<backend>__<context>; with S3, the bucket is<backend>--<context>. Each must fit in 63 characters.
From web-session, newt makes WebSession (class names), webSession (variables) and WEB_SESSION (environment variables).
What it writes
Section titled “What it writes”newt add context chat created src/contexts/chat/chat.context.ts created src/contexts/chat/chat-modules.ts created src/contexts/chat/migrations/.gitkeep skipped src/auth.ts (already exists) updated .env updated .env.example updated src/contexts.ts
Next steps: bun run devsrc/contexts.tsgets a line that opens the context.src/auth.tsis written only if it is missing..envand.env.exampleget the context’s variables, by the backend’s database and files:
| Backend uses | Variable added | Value |
|---|---|---|
sqlite |
CHAT_DATABASE |
data/databases/chat/chat.sqlite |
postgres |
CHAT_DATABASE_URL |
postgres://postgres:postgres@localhost:5432/<backend>__chat |
mysql |
CHAT_DATABASE_URL |
mysql://root:root@localhost:3306/<backend>__chat |
local files |
CHAT_FILES |
data/files/chat |
s3 files |
CHAT_S3_BUCKET |
<backend>--chat |
memory adds nothing. Values already in .env are kept.
Next steps
Section titled “Next steps”- With Postgres or MySQL: create the context’s database in your server, as
<CONTEXT>_DATABASE_URLsays. - With S3: set
<CONTEXT>_S3_BUCKETin.envto its bucket. bun run dev
Errors
Section titled “Errors”- A bad name:
'<name>' cannot name a context: lowercase words joined by -. api: refused.- A database or bucket name over 63 characters: refused; give the context a shorter name.
src/stores.tsopens no database or storage newt knows: refused, since newt cannot tell what the context should open.- No backend found: see Overview.
See Contexts and modules.