create-knit 0.4.1 → 0.6.1
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/index.mjs +14 -6
- package/package.json +1 -1
- package/template/base/.claude/skills/knit-add-model/SKILL.md +1 -1
- package/template/base/.claude/skills/knit-add-permission/SKILL.md +26 -0
- package/template/base/.claude/skills/knit-add-task/SKILL.md +25 -0
- package/template/base/.claude/skills/knit-add-translation/SKILL.md +22 -0
- package/template/base/.claude/skills/knit-deploy/SKILL.md +27 -0
- package/template/base/.claude/skills/knit-write-e2e-test/SKILL.md +26 -0
- package/template/base/AGENTS.md +20 -5
- package/template/base/CLAUDE.md +1 -1
- package/template/base/apps/web/.env.example +21 -2
- package/template/base/docker/Dockerfile +1 -1
- package/template/base/docker/docker-compose.prod.yml +1 -1
- package/template/base/llms.txt +5 -1
- package/template/versions.json +1 -1
package/index.mjs
CHANGED
|
@@ -17,6 +17,9 @@ const versions = JSON.parse(readFileSync(join(template, 'versions.json'), 'utf8'
|
|
|
17
17
|
const color = (code, text) => (process.stdout.isTTY && !process.env.NO_COLOR ? `\x1b[${code}m${text}\x1b[0m` : text)
|
|
18
18
|
const bold = text => color('1', text)
|
|
19
19
|
const dim = text => color('2', text)
|
|
20
|
+
// A coded error (KNIT_E2101…); this script runs before @knit-framework/core is installed.
|
|
21
|
+
const fail = (code, message) => Object.assign(new Error(message), { code })
|
|
22
|
+
const errorDocs = code => `https://3ig.github.io/knit/docs/errors/${code}`
|
|
20
23
|
|
|
21
24
|
function usage() {
|
|
22
25
|
return `Usage: pnpm create knit [dir] [--examples | --no-examples] [--no-install] [--no-git]
|
|
@@ -40,9 +43,9 @@ function parseArgs(argv) {
|
|
|
40
43
|
console.log(usage())
|
|
41
44
|
process.exit(0)
|
|
42
45
|
}
|
|
43
|
-
else if (arg.startsWith('-')) throw
|
|
46
|
+
else if (arg.startsWith('-')) throw fail('KNIT_E2101', `Unknown option ${arg}\n\n${usage()}`)
|
|
44
47
|
else if (!opts.dir) opts.dir = arg
|
|
45
|
-
else throw
|
|
48
|
+
else throw fail('KNIT_E2101', `Unexpected argument ${arg}\n\n${usage()}`)
|
|
46
49
|
}
|
|
47
50
|
return opts
|
|
48
51
|
}
|
|
@@ -60,6 +63,7 @@ function localPackages() {
|
|
|
60
63
|
const files = readdirSync(dir)
|
|
61
64
|
const find = prefix => {
|
|
62
65
|
const file = files.find(f => f.startsWith(prefix) && f.endsWith('.tgz'))
|
|
66
|
+
// internal: KNIT_CREATE_TARBALLS is only set by Knit's own tests
|
|
63
67
|
if (!file) throw new Error(`KNIT_CREATE_TARBALLS: no ${prefix}*.tgz in ${dir}`)
|
|
64
68
|
return `file:${resolve(dir, file)}`
|
|
65
69
|
}
|
|
@@ -111,7 +115,7 @@ function writeProject(target, name, examples) {
|
|
|
111
115
|
vitest: versions.vitest,
|
|
112
116
|
},
|
|
113
117
|
// Project settings, read by the `knit` CLI and the core layer.
|
|
114
|
-
knit: { app: 'apps/web', layersDir: 'layers', migrationsDir: 'server/database/migrations' },
|
|
118
|
+
knit: { app: 'apps/web', layersDir: 'layers', migrationsDir: 'server/database/migrations', template: versions.knit },
|
|
115
119
|
}))
|
|
116
120
|
|
|
117
121
|
writeFileSync(join(target, 'pnpm-workspace.yaml'), [
|
|
@@ -125,6 +129,8 @@ function writeProject(target, name, examples) {
|
|
|
125
129
|
" '@parcel/watcher': true",
|
|
126
130
|
" '@tailwindcss/oxide': true",
|
|
127
131
|
' unrs-resolver: true',
|
|
132
|
+
' # Pulled in by OpenTelemetry\'s exporter (docs/observability.md); its script only prints a notice.',
|
|
133
|
+
' protobufjs: false',
|
|
128
134
|
...(local ? ['', '# Local @knit-framework packages (KNIT_CREATE_TARBALLS)', 'overrides:', ` '@knit-framework/core': '${local.core}'`, ` '@knit-framework/cli': '${local.cli}'`] : []),
|
|
129
135
|
'',
|
|
130
136
|
].join('\n'))
|
|
@@ -240,7 +246,7 @@ async function main() {
|
|
|
240
246
|
try {
|
|
241
247
|
const dir = opts.dir ?? await ask(rl, 'Project folder?', 'my-knit-app')
|
|
242
248
|
const target = resolve(dir)
|
|
243
|
-
if (existsSync(target) && readdirSync(target).length) throw
|
|
249
|
+
if (existsSync(target) && readdirSync(target).length) throw fail('KNIT_E2102', `${dir} already exists and is not empty.`)
|
|
244
250
|
const examples = opts.examples ?? (rl ? !/^n/i.test(await ask(rl, 'Include the example apps (blog, dashboard)?', 'Y/n')) : true)
|
|
245
251
|
rl?.close()
|
|
246
252
|
|
|
@@ -257,8 +263,8 @@ async function main() {
|
|
|
257
263
|
if (opts.git) initGit(target)
|
|
258
264
|
return
|
|
259
265
|
}
|
|
260
|
-
if (!run('pnpm', ['install'], target)) throw
|
|
261
|
-
if (!run('pnpm', ['manage', 'makemigrations', '--name', 'initial'], target)) throw
|
|
266
|
+
if (!run('pnpm', ['install'], target)) throw fail('KNIT_E2010', 'pnpm install failed (see above).')
|
|
267
|
+
if (!run('pnpm', ['manage', 'makemigrations', '--name', 'initial'], target)) throw fail('KNIT_E2103', 'Creating the initial migration failed (see above).')
|
|
262
268
|
}
|
|
263
269
|
|
|
264
270
|
// Last, so the first commit holds the lockfile and the initial migration too.
|
|
@@ -277,5 +283,7 @@ async function main() {
|
|
|
277
283
|
|
|
278
284
|
main().catch((error) => {
|
|
279
285
|
console.error(`\n${color('31', 'Error:')} ${error instanceof Error ? error.message : String(error)}`)
|
|
286
|
+
const code = error?.code
|
|
287
|
+
if (typeof code === 'string' && /^KNIT_E\d{4}$/.test(code)) console.error(dim(`See ${code} · ${errorDocs(code)}`))
|
|
280
288
|
process.exit(1)
|
|
281
289
|
})
|
package/package.json
CHANGED
|
@@ -21,7 +21,7 @@ description: Add or change a model (Drizzle table) in a Knit app, end to end - s
|
|
|
21
21
|
- Then run `pnpm manage makemigrations --name <slug>`.
|
|
22
22
|
- If it says drizzle-kit must ask about a rename, split the change: add the new column, migrate, then drop the old one.
|
|
23
23
|
5. **Apply it** with `pnpm manage migrate`. This works while `pnpm dev` runs, and `pnpm dev` also applies migrations when it starts.
|
|
24
|
-
6. **Admin:** register the table in `layers/<app>/admin.ts` with `registerAdminModel(table, { listDisplay, searchFields, listFilter, ordering, readonlyFields })`, using field *keys* (camelCase JS names).
|
|
24
|
+
6. **Admin:** register the table in `layers/<app>/admin.ts` with `registerAdminModel(table, { listDisplay, searchFields, listFilter, ordering, readonlyFields })`, using field *keys* (camelCase JS names). A running `pnpm dev` picks up a new or changed `admin.ts` without a restart.
|
|
25
25
|
7. **Verify.**
|
|
26
26
|
- `pnpm -s manage check --json` (MCP: `knit_check`) has no errors. It catches unknown admin fields, missing prefixes and clashes.
|
|
27
27
|
- `pnpm typecheck` and `pnpm lint` pass.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: knit-add-permission
|
|
3
|
+
description: Add a custom permission to a Knit app, enforce it on the server and grant it to a group or user - customPermissions, requirePermission, resource rules, seeds and a denied-request test. Use when asked to restrict who can do something, add a role-like capability ("only editors can publish"), or grant permissions in a Knit project.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Add a permission and grant it
|
|
7
|
+
|
|
8
|
+
1. **Look first.**
|
|
9
|
+
- `knit_describe` with `section: "models"` lists each model's permissions (`<label>.view_<model>`, `add_`, `change_`, `delete_`).
|
|
10
|
+
- `knit_describe` with `section: "resources"` shows which rule guards each API action.
|
|
11
|
+
- The four model permissions may already be enough: use them before inventing a new one.
|
|
12
|
+
2. **Declare a custom permission** in `layers/<app>/admin.ts`: `registerAdminModel(table, { customPermissions: ['publish_post'] })`. Its full name is `<label>.publish_post`, the label being the app label (`knit.app` in the layer's `nuxt.config.ts`).
|
|
13
|
+
3. **Enforce it on the server.** The UI check `useAuth().can(perm)` only hides buttons.
|
|
14
|
+
- Server route: `await requirePermission(event, 'blog.publish_post')` in a route under `layers/<app>/server/api/` (auto-imported there).
|
|
15
|
+
- Resource: put the name in the action's rule, e.g. `permissions: { update: 'blog.publish_post' }` or `[isOwner('authorId'), 'blog.change_post']` (any rule allows).
|
|
16
|
+
4. **Create the permission rows.** The server creates them when it starts. A running `pnpm dev` reloads on the `admin.ts` change; restart it if the permission is still unknown. Granting before that fails with `KNIT_E1903` (`knit_docs { "topic": "errors/KNIT_E1903" }`).
|
|
17
|
+
5. **Grant it.**
|
|
18
|
+
- By hand: `/admin/auth/groups/:id` (a matrix per group) or `/admin/auth/users/:id`.
|
|
19
|
+
- In code (a seed or command): `permissionIds(db, [...])`, then `setGroupPermissions` and `addUserToGroup` from `@knit-framework/core/permissions`. The full seed is in the auth guide, "Grant a permission in code". Run it with `pnpm manage seed` (stop `pnpm dev` first on PGlite).
|
|
20
|
+
6. **Verify.**
|
|
21
|
+
- `pnpm -s manage check --json` (MCP: `knit_check`) has no errors; `pnpm typecheck` passes.
|
|
22
|
+
- `knit_resource` shows the new rule on the resource's endpoints.
|
|
23
|
+
- An e2e test with one allowed request and one denied: 403 for a signed-in user without the permission, 401 when signed out. Follow the `knit-write-e2e-test` skill.
|
|
24
|
+
- Sign in as a user in the group and as one outside it, and try the action.
|
|
25
|
+
|
|
26
|
+
Guide: `knit_docs { "topic": "auth" }` (or `node_modules/@knit-framework/core/docs/auth.md`), section "Groups & permissions".
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: knit-add-task
|
|
3
|
+
description: Add a background or scheduled task to a Knit app - generate it, write run(), enqueue it, schedule it with cron, run it by hand and test it. Use when asked for background work, a job, a queue, a cron job, or "do X every night" in a Knit project.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Add a background or scheduled task
|
|
7
|
+
|
|
8
|
+
1. **Look first.**
|
|
9
|
+
- `knit_describe` with `section: "tasks"` (or `pnpm -s manage runtask --json`) lists the existing tasks and schedules.
|
|
10
|
+
- Note the backend: `NUXT_KNIT_TASKS_BACKEND` is `immediate` (default, runs in-process) or `database` (queued in a table, with retries).
|
|
11
|
+
2. **Generate the file.** `pnpm manage generate task <app> <name>` writes `layers/<app>/server/tasks/<name>.ts` with task `<label>.<name>`. A running `pnpm dev` picks it up.
|
|
12
|
+
3. **Write `run(payload, ctx)`.**
|
|
13
|
+
- Task files also run outside the server: no auto-imports. Import tables from `../database/schema` and use `ctx.db`.
|
|
14
|
+
- Write through `useModel(table, { db: ctx.db })` from `@knit-framework/core/models` so validators and signals run.
|
|
15
|
+
- Return something JSON-serialisable; throw to fail (the database backend retries up to `maxAttempts`).
|
|
16
|
+
- Scheduled work: add `schedule: '0 3 * * *'` (cron). It runs once per due time, however many processes run.
|
|
17
|
+
4. **Enqueue it** from server code: `await enqueue('<label>.<name>', payload)` (auto-imported, checked against the task's payload type). Inside another task, use `ctx.enqueue(taskObject, payload)`.
|
|
18
|
+
5. **Run it by hand.** `pnpm -s manage runtask <label>.<name> --payload '{"note":"hi"}' --json` prints `{ ok, result }` or `{ ok: false, error }`. On PGlite, stop `pnpm dev` first.
|
|
19
|
+
6. **Test it.** Add `layers/<app>/test/tasks.test.ts` with `createTestDatabase` and `configureTasks()` before `enqueue()`, passing the task object, not its name. Copy the sample from the tasks guide, "Test a task" (it explains `KNIT_E1701`).
|
|
20
|
+
7. **Verify.**
|
|
21
|
+
- `pnpm manage test --unit` and `pnpm typecheck` pass; `pnpm -s manage check --json` (MCP: `knit_check`) has no errors.
|
|
22
|
+
- `knit_describe` with `section: "tasks"` lists the task (and its schedule).
|
|
23
|
+
- With the database backend, the run appears in the queue table with status `succeeded` (the tasks guide, "Check that a task ran").
|
|
24
|
+
|
|
25
|
+
Guide: `knit_docs { "topic": "tasks" }` (or `node_modules/@knit-framework/core/docs/tasks.md`).
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: knit-add-translation
|
|
3
|
+
description: Add or translate UI strings in a Knit app - t() in pages, layer locale files in every language, model and field labels for the admin, and a test that keeps the languages in step. Use when asked to translate a page or the admin, add a language, or add user-facing text in a Knit project.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Add or translate UI strings
|
|
7
|
+
|
|
8
|
+
1. **Look first.**
|
|
9
|
+
- List `layers/<app>/i18n/locales/`: `pnpm manage startapp` writes `en.json`, `de.json` and `es.json` with the same keys.
|
|
10
|
+
- `knit_describe` with `section: "models"` gives each model's admin slug, the key used for model labels.
|
|
11
|
+
2. **Use `t()` for every string** in pages and components: `const { t } = useI18n()`, then `t('shop.cart.title')`. Put your own keys under your app's namespace, never under `knit.*`. Plurals: `t('shop.cart.items', { count: n(items.length) }, items.length)`.
|
|
12
|
+
3. **Admin labels** go under `knit.models.<slug>` (`label`, `labelPlural`, `fields.<field>`, `help.<field>`) and `knit.groups.<group key>`. English already comes from `admin.ts`; add other languages there.
|
|
13
|
+
4. **Add the key to every locale file of the layer**, in the same place. A missing key silently falls back to English.
|
|
14
|
+
- A literal `{`, `}`, `@` or `|` breaks the whole language: write `{'@'}` for `@`.
|
|
15
|
+
- Pass sentence parts as named placeholders (`{model}`); don't glue fragments or lowercase in code.
|
|
16
|
+
5. **A new language** is a new file, e.g. `layers/<app>/i18n/locales/fr.json`. Restart `pnpm dev` after adding a file; that is the only change that needs a restart.
|
|
17
|
+
6. **Verify.**
|
|
18
|
+
- Add the key-parity test from the i18n guide ("Keep an app's languages in step") to `layers/<app>/test/locales.test.ts` if the layer has none, then run `pnpm manage test --unit`.
|
|
19
|
+
- `pnpm typecheck` passes.
|
|
20
|
+
- Open the page and the admin, switch the language in the user menu or with `<KnitLocaleSelect />`, and check that no English is left.
|
|
21
|
+
|
|
22
|
+
Guide: `knit_docs { "topic": "i18n" }` (or `node_modules/@knit-framework/core/docs/i18n.md`).
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: knit-deploy
|
|
3
|
+
description: Deploy a Knit app to production - pick a host, set secrets, database, migrations, first admin, email and tasks, then verify with check --deploy and the health check. Use when asked to deploy, host, go live, ship to production, or prepare a Knit project for a server, Docker, Fly.io, Railway, Render, Vercel or Netlify.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Deploy to production
|
|
7
|
+
|
|
8
|
+
1. **Look first.**
|
|
9
|
+
- `knit_docs { "topic": "deployment" }` has the host table and the checklist this skill follows.
|
|
10
|
+
- `knit_describe` with `section: "settings"` shows the current settings; `knit_describe` with `section: "tasks"` and `section: "channels"` tell you whether you need a worker and WebSockets.
|
|
11
|
+
2. **Pick the host.** Long-running Node with a persistent disk works best (Docker, a VPS, Fly.io, Railway, Render). Vercel and Netlify have no WebSockets, worker or scheduled tasks. Read the host's page, e.g. `knit_docs { "topic": "deployment/fly" }`.
|
|
12
|
+
3. **Make sure the build is clean.** `pnpm manage makemigrations --check` (no forgotten migration), `pnpm typecheck`, `pnpm manage test`, then `pnpm build`.
|
|
13
|
+
4. **Set the production environment**, step by step from the guide's checklist:
|
|
14
|
+
- `NUXT_BETTER_AUTH_SECRET` (`openssl rand -base64 32`) and `NUXT_BETTER_AUTH_URL=https://your.domain`.
|
|
15
|
+
- Database: the embedded one for a single process with a persistent disk; otherwise `DATABASE_URL` (Postgres).
|
|
16
|
+
- Migrations: `NUXT_KNIT_AUTO_MIGRATE=always`, or `pnpm manage migrate` as a release step.
|
|
17
|
+
- `NUXT_MAIL_URL` and `NUXT_MAIL_FROM`; `NUXT_KNIT_TASKS_BACKEND=database` if there are tasks.
|
|
18
|
+
- Behind a TLS proxy: `NUXT_KNIT_SECURITY_TRUST_PROXY=true` and `NUXT_BETTER_AUTH_TRUST_PROXY=true`.
|
|
19
|
+
- Never commit secrets: set them in the host's settings, not in `apps/web/.env`.
|
|
20
|
+
5. **Check before going live.** With those variables set, `pnpm -s manage check --deploy --json` (MCP: `knit_check` with `deploy: true`) has no errors. An error code has a page: `knit_docs { "topic": "errors/<code>" }`.
|
|
21
|
+
6. **Deploy**, then create the first admin (the setup wizard is off in production): `KNIT_SUPERUSER_PASSWORD=… pnpm manage createsuperuser --no-input --email you@example.com`, where the CLI can reach the production database.
|
|
22
|
+
7. **Verify.**
|
|
23
|
+
- The host's health check points at `/api/_knit/health`, and `curl https://your.domain/api/_knit/health` returns `{ "ok": true }`.
|
|
24
|
+
- Sign in at `/admin`; request a password reset to see that email is delivered.
|
|
25
|
+
- With tasks: enqueue one and see it succeed in the logs.
|
|
26
|
+
|
|
27
|
+
Guide: `knit_docs { "topic": "deployment" }` (or `node_modules/@knit-framework/core/docs/deployment/index.md`); settings in `knit_docs { "topic": "configuration" }`.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
---
|
|
2
|
+
name: knit-write-e2e-test
|
|
3
|
+
description: Write an end-to-end test for a Knit app that boots the real server and calls it over HTTP with createTestClient - first admin, sign-in, sign-up, allowed and denied requests. Use when asked to test an API, a permission, a page response or a whole flow over HTTP in a Knit project.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Write an end-to-end test
|
|
7
|
+
|
|
8
|
+
1. **Look first.**
|
|
9
|
+
- `knit_routes` (or `pnpm -s manage routes --json`) lists the pages, API routes and resource endpoints to call.
|
|
10
|
+
- `knit_resource` with the slug gives the permission rule of each action: test one allowed and one denied request per rule.
|
|
11
|
+
- Logic without HTTP (a model, a task) belongs in a faster unit test: `pnpm manage generate test <app> <name> --table <export>`.
|
|
12
|
+
2. **Create `apps/web/test/e2e/<name>.test.ts`.** Copy the sample from the testing guide, "End-to-end tests". It is self-contained:
|
|
13
|
+
- Set `NUXT_KNIT_DATA_DIR=memory://`, `NUXT_KNIT_AUTO_MIGRATE=always`, `NUXT_KNIT_ALLOW_SETUP=always` and a random `NUXT_BETTER_AUTH_SECRET` before `setup()`.
|
|
14
|
+
- `await setup({ rootDir: fileURLToPath(new URL('../..', import.meta.url)), server: true, browser: false })` from `@nuxt/test-utils/e2e`.
|
|
15
|
+
3. **Make the users in `beforeAll`** (the server starts in `setup()`'s own hook):
|
|
16
|
+
- One `createTestClient(url('/'))` from `@knit-framework/core/testing` per user; each keeps its own cookies and sends the CSRF-safe `Origin`.
|
|
17
|
+
- First admin: `POST /api/_knit/setup` with `{ name, email, password }` (201), then `signIn(email, password)`. It is a superuser.
|
|
18
|
+
- Ordinary user: `POST /api/auth/sign-up/email` (200) also signs the client in.
|
|
19
|
+
4. **Write the cases.** Responses are `{ status, data }` and never throw.
|
|
20
|
+
- Allowed: 200 or 201 and the returned data.
|
|
21
|
+
- Denied: 403 signed in, 401 signed out. Validation errors: 422 with `data.data.issues`.
|
|
22
|
+
- Tests in one file share the server and its data: create what each test needs, or order them.
|
|
23
|
+
5. **Your schema changed?** Run `pnpm manage makemigrations --name <slug>` first: the server applies the migration files, not the schema.
|
|
24
|
+
6. **Verify.** `pnpm manage test --e2e` passes (it builds the app; the first run is slow), and `pnpm typecheck` passes. Break the rule on purpose once (e.g. allow the denied action) and check the test fails.
|
|
25
|
+
|
|
26
|
+
Guide: `knit_docs { "topic": "testing" }` (or `node_modules/@knit-framework/core/docs/testing.md`).
|
package/template/base/AGENTS.md
CHANGED
|
@@ -12,10 +12,13 @@ Don't read files to find out what exists. Ask Knit:
|
|
|
12
12
|
- `knit_describe`: apps, models with columns, foreign keys and `with:` relations, admin, resources and their permission rules, tasks, channels, routes, commands, migrations. Pass `section` to get one part.
|
|
13
13
|
- `knit_model`, `knit_resource`: one model; one resource with its endpoints and permission rules.
|
|
14
14
|
- `knit_routes`, `knit_openapi`: pages, API routes and resource endpoints; the resources' OpenAPI document.
|
|
15
|
-
- `knit_check`: run after changes.
|
|
15
|
+
- `knit_check`: run after changes. `knit_doctor`: when the environment seems broken (Node, packages, database, ports), then every check.
|
|
16
|
+
- `knit_reference`: the exact signature, import path, options and example of any Knit function, type, component, setting or command, e.g. `{ "symbol": "defineResource" }`. Use it instead of reading Knit's source.
|
|
16
17
|
- `knit_migrations`, `knit_migration_plan`, `knit_pending_migration_sql`, `knit_sqlmigrate`.
|
|
17
18
|
- `knit_docs`: the guides, e.g. `{ "topic": "models" }`. Also as MCP resources: `knit://docs/{topic}`, `knit://describe`, `knit://agents`.
|
|
18
|
-
|
|
19
|
+
- **Error codes:** an error with a code (`KNIT_E1203`) has a page that says why and how to fix it: read it with `knit_docs { "topic": "errors/KNIT_E1203" }` before changing code.
|
|
20
|
+
- With `pnpm dev` running: `knit_errors` (recent server errors with hints and codes), `knit_outbox` (sent emails, e.g. a reset link), `knit_tasks` (task runs and the queue).
|
|
21
|
+
- Only when the server runs with `--allow-writes`: `knit_startapp`, `knit_generate`, `knit_makemigrations`, `knit_migrate`. Each takes `dryRun: true` to see what it would do first.
|
|
19
22
|
- **CLI:** `pnpm -s manage describe --json` (the same document; `--section <name>` for one part), `routes --json`, `openapi`, `check --json`, `showmigrations --json`, `runtask --json`. Commands that change things take `--json` too (`startapp`, `generate`, `makemigrations`, `migrate`, …). `-s` keeps pnpm's banner out of the JSON.
|
|
20
23
|
- **Library docs:** the `nuxt`, `nuxt-ui` and `better-auth` MCP servers. Use them instead of guessing APIs.
|
|
21
24
|
|
|
@@ -49,10 +52,22 @@ Don't edit `node_modules/@knit-framework/*`: override instead (a page, component
|
|
|
49
52
|
- **UI strings** go through `t()`; a literal `{`, `}`, `@` or `|` in a locale message breaks the whole language.
|
|
50
53
|
- **Logging:** `useLogger('scope')` in server code, never `console.*`.
|
|
51
54
|
|
|
52
|
-
## 4.
|
|
55
|
+
## 4. Recipes
|
|
56
|
+
|
|
57
|
+
Step-by-step procedures for common jobs. Claude Code loads them as skills; any other agent can read and follow the file:
|
|
58
|
+
|
|
59
|
+
- Add or change a model: [`.claude/skills/knit-add-model/SKILL.md`](.claude/skills/knit-add-model/SKILL.md)
|
|
60
|
+
- Add a public API and forms: [`.claude/skills/knit-add-api/SKILL.md`](.claude/skills/knit-add-api/SKILL.md)
|
|
61
|
+
- Add a permission and grant it: [`.claude/skills/knit-add-permission/SKILL.md`](.claude/skills/knit-add-permission/SKILL.md)
|
|
62
|
+
- Add or translate UI strings: [`.claude/skills/knit-add-translation/SKILL.md`](.claude/skills/knit-add-translation/SKILL.md)
|
|
63
|
+
- Add a background or scheduled task: [`.claude/skills/knit-add-task/SKILL.md`](.claude/skills/knit-add-task/SKILL.md)
|
|
64
|
+
- Write an end-to-end test: [`.claude/skills/knit-write-e2e-test/SKILL.md`](.claude/skills/knit-write-e2e-test/SKILL.md)
|
|
65
|
+
- Deploy to production: [`.claude/skills/knit-deploy/SKILL.md`](.claude/skills/knit-deploy/SKILL.md)
|
|
66
|
+
|
|
67
|
+
## 5. Change → verify loop
|
|
53
68
|
|
|
54
69
|
```bash
|
|
55
|
-
pnpm -s manage check --json # schema, admin field names, layers, migrations (or MCP: knit_check)
|
|
70
|
+
pnpm -s manage check --json # schema, admin field names, settings, layers, migrations (or MCP: knit_check)
|
|
56
71
|
pnpm manage makemigrations --name <slug> # after a schema change; --dry-run to preview
|
|
57
72
|
pnpm manage migrate # works while pnpm dev runs; pnpm dev also applies on start
|
|
58
73
|
pnpm typecheck # must pass
|
|
@@ -68,7 +83,7 @@ pnpm manage test --unit # fast; pnpm test runs e2e too (builds
|
|
|
68
83
|
- **Tests:** `createTestDatabase`, `defineFactory`, `createTestClient` from `@knit-framework/core/testing`; app tests in `layers/<app>/test/*.test.ts`. See `node_modules/@knit-framework/core/docs/testing.md`.
|
|
69
84
|
- **Admin or API changes:** sign in at `/admin` and try list, create (including a validation error), edit and delete.
|
|
70
85
|
|
|
71
|
-
##
|
|
86
|
+
## 6. Gotchas
|
|
72
87
|
|
|
73
88
|
- **Relational queries need `relations()`.** `with: { author: true }` fails without a relation named `author`.
|
|
74
89
|
- **Resources** must be *exported*. `_choices` for a foreign key goes through the target table's resource (no resource, no choices).
|
package/template/base/CLAUDE.md
CHANGED
|
@@ -6,4 +6,4 @@ Project MCP servers (`.mcp.json`):
|
|
|
6
6
|
- `knit`: this project's models, resources, routes, OpenAPI, checks, migrations and Knit's guides. Start with `knit_describe`. It only reads unless started with `--allow-writes`.
|
|
7
7
|
- `nuxt`, `nuxt-ui`, `better-auth`: library APIs; use them instead of guessing.
|
|
8
8
|
|
|
9
|
-
Skills in `.claude/skills/`: `knit-add-model`, `knit-add-api`. New files: `pnpm manage startapp <app>` and `pnpm manage generate <kind> <app> <name>`.
|
|
9
|
+
Skills in `.claude/skills/`: `knit-add-model`, `knit-add-api`, `knit-add-permission`, `knit-add-translation`, `knit-add-task`, `knit-write-e2e-test`, `knit-deploy`. New files: `pnpm manage startapp <app>` and `pnpm manage generate <kind> <app> <name>`.
|
|
@@ -9,10 +9,21 @@ NUXT_BETTER_AUTH_URL=
|
|
|
9
9
|
|
|
10
10
|
# Use a Postgres server instead of the embedded PGlite database (app + `pnpm manage`).
|
|
11
11
|
# DATABASE_URL=postgres://user:password@localhost:5432/knit
|
|
12
|
+
# When DATABASE_URL goes through a transaction pooler (PgBouncer, a provider's pooled URL): the direct
|
|
13
|
+
# connection, for migrations and the realtime listener (docs/postgres.md)
|
|
14
|
+
# DATABASE_DIRECT_URL=postgres://user:password@db.internal:5432/knit
|
|
15
|
+
# Postgres pool per server process, idle timeout (s), statement timeout (ms, 0 = none), TLS (require | no-verify)
|
|
16
|
+
# NUXT_KNIT_DATABASE_POOL_MAX=10
|
|
17
|
+
# NUXT_KNIT_DATABASE_IDLE_TIMEOUT=10
|
|
18
|
+
# NUXT_KNIT_DATABASE_STATEMENT_TIMEOUT=0
|
|
19
|
+
# NUXT_KNIT_DATABASE_SSL=
|
|
12
20
|
|
|
13
21
|
# PGlite data directory (relative to apps/web, or to the folder holding a deployed .output). Default: .data/pglite
|
|
14
22
|
# NUXT_KNIT_DATA_DIR=.data/pglite
|
|
15
23
|
|
|
24
|
+
# Read migrations from this folder at startup instead of the copy bundled into the build
|
|
25
|
+
# NUXT_KNIT_MIGRATIONS_DIR=server/database/migrations
|
|
26
|
+
|
|
16
27
|
# Apply pending migrations at startup: dev (default) | always | never
|
|
17
28
|
# NUXT_KNIT_AUTO_MIGRATE=dev
|
|
18
29
|
|
|
@@ -38,10 +49,10 @@ NUXT_BETTER_AUTH_URL=
|
|
|
38
49
|
# Default sender address
|
|
39
50
|
# NUXT_MAIL_FROM=Knit <webmaster@localhost>
|
|
40
51
|
|
|
41
|
-
# Used by `pnpm manage createsuperuser --
|
|
52
|
+
# Used by `pnpm manage createsuperuser --no-input`
|
|
42
53
|
# KNIT_SUPERUSER_PASSWORD=
|
|
43
54
|
|
|
44
|
-
# Used by `pnpm manage changepassword <email> --
|
|
55
|
+
# Used by `pnpm manage changepassword <email> --no-input`
|
|
45
56
|
# KNIT_NEW_PASSWORD=
|
|
46
57
|
# ── Logging (LogLayer) ─────────────────────────────────────────────────────────
|
|
47
58
|
# trace | debug | info | warn | error | fatal (default: debug in dev, info in production)
|
|
@@ -53,6 +64,12 @@ NUXT_BETTER_AUTH_URL=
|
|
|
53
64
|
# Extra keys to redact, comma-separated (passwords, tokens, cookies… are always redacted)
|
|
54
65
|
# NUXT_KNIT_LOG_REDACT=ssn,card.number
|
|
55
66
|
|
|
67
|
+
# ── Telemetry (OpenTelemetry traces, docs/observability.md) ─────────────────────
|
|
68
|
+
# Needs: pnpm add -w @opentelemetry/api @opentelemetry/sdk-node @opentelemetry/exporter-trace-otlp-http
|
|
69
|
+
# NUXT_KNIT_TELEMETRY_ENABLED=false
|
|
70
|
+
# NUXT_KNIT_TELEMETRY_SERVICE_NAME=knit
|
|
71
|
+
# OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:4318
|
|
72
|
+
|
|
56
73
|
# ── Site ────────────────────────────────────────────────────────────────────────
|
|
57
74
|
# Public base URL for sitemaps and feeds (default: NUXT_BETTER_AUTH_URL, then the request)
|
|
58
75
|
# NUXT_KNIT_SITE_URL=https://example.com
|
|
@@ -88,6 +105,8 @@ NUXT_BETTER_AUTH_URL=
|
|
|
88
105
|
# NUXT_KNIT_TASKS_POLL_INTERVAL=1000
|
|
89
106
|
# NUXT_KNIT_TASKS_MAX_ATTEMPTS=3
|
|
90
107
|
# NUXT_KNIT_TASKS_RETRY_DELAY=10
|
|
108
|
+
# Seconds after which a running task counts as interrupted (its worker died) and is queued again
|
|
109
|
+
# NUXT_KNIT_TASKS_STALE_AFTER=3600
|
|
91
110
|
|
|
92
111
|
# ── Realtime (SSE + WebSocket channels) ─────────────────────────────────────────
|
|
93
112
|
# memory (default, one process) | postgres (LISTEN/NOTIFY on DATABASE_URL: several
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
# docker build -f docker/Dockerfile -t knit . # the app (default target: runtime)
|
|
6
6
|
# docker build -f docker/Dockerfile -t knit-tools --target tools . # one-off `pnpm manage …` commands
|
|
7
7
|
#
|
|
8
|
-
# See docs/deployment
|
|
8
|
+
# See docs/deployment/.
|
|
9
9
|
|
|
10
10
|
ARG NODE_VERSION=24
|
|
11
11
|
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
# cp docker/.env.production.example docker/.env.production # then fill in the secrets
|
|
4
4
|
# docker compose -f docker/docker-compose.prod.yml --env-file docker/.env.production up -d --build
|
|
5
5
|
#
|
|
6
|
-
# See docs/deployment
|
|
6
|
+
# See docs/deployment/. Put a TLS-terminating proxy (Caddy, nginx, Traefik…) in front.
|
|
7
7
|
|
|
8
8
|
name: knit-prod
|
|
9
9
|
|
package/template/base/llms.txt
CHANGED
|
@@ -26,16 +26,20 @@ Agents working in a Knit project: read AGENTS.md, and use the `knit` MCP server
|
|
|
26
26
|
- [Files & uploads](node_modules/@knit-framework/core/docs/files.md): `fileColumn`, uploads, disk/S3 storage
|
|
27
27
|
- [Cache, messages & humanize](node_modules/@knit-framework/core/docs/utilities.md): `cached()`, flash messages, formatting helpers
|
|
28
28
|
- [Sitemaps & feeds](node_modules/@knit-framework/core/docs/sitemaps-and-feeds.md): `/sitemap.xml`, RSS and Atom
|
|
29
|
+
- [Observability](node_modules/@knit-framework/core/docs/observability.md): OpenTelemetry traces (NUXT_KNIT_TELEMETRY_ENABLED), what each span contains, `withSpan`
|
|
29
30
|
- [Logging](node_modules/@knit-framework/core/docs/logging.md): LogLayer: pretty dev output, JSON in production, request logs, redaction
|
|
30
31
|
- [Security](node_modules/@knit-framework/core/docs/security.md): CSRF, security headers, CSP
|
|
31
32
|
- [Testing](node_modules/@knit-framework/core/docs/testing.md): Test databases, factories, the HTTP client, `pnpm test`
|
|
32
33
|
- [`manage` CLI](node_modules/@knit-framework/core/docs/cli.md): Every command, `startapp` and `generate`, JSON output for scripts and agents, seeds, custom commands
|
|
33
34
|
- [Configuration](node_modules/@knit-framework/core/docs/configuration.md): Every environment variable and setting
|
|
34
|
-
- [Deployment](node_modules/@knit-framework/core/docs/deployment.md):
|
|
35
|
+
- [Deployment](node_modules/@knit-framework/core/docs/deployment/index.md): production checklist, what Knit needs from a host, and one page per host (Docker, VPS with systemd and Caddy, Fly.io, Railway, Render; Vercel and Netlify with limits; Cloudflare not supported)
|
|
36
|
+
- [Postgres in production](node_modules/@knit-framework/core/docs/postgres.md): moving off PGlite, DATABASE_DIRECT_URL and poolers, zero-downtime migrations, backups, several instances
|
|
35
37
|
- [Upgrading](node_modules/@knit-framework/core/docs/upgrading.md): updating the @knit-framework packages, migrating Knit's tables, removing the examples
|
|
36
38
|
- [How Knit works](node_modules/@knit-framework/core/docs/architecture.md): Discovery, generated modules, request lifecycle
|
|
37
39
|
- [Working with AI agents](node_modules/@knit-framework/core/docs/ai-agents.md): `AGENTS.md`, the `knit` MCP server (tools, resources, write tools), `describe`, `routes`, `openapi`, skills
|
|
38
40
|
- [Troubleshooting](node_modules/@knit-framework/core/docs/troubleshooting.md): Common errors and fixes
|
|
41
|
+
- [API reference](node_modules/@knit-framework/core/docs/reference/index.md): every public function, type, component, setting and command with signatures and examples, generated from the source; `node_modules/@knit-framework/core/docs/reference/reference.json` has the same as data
|
|
42
|
+
- [Error codes](node_modules/@knit-framework/core/docs/errors/index.md): every `KNIT_E…` code with its cause and fix; read one as `node_modules/@knit-framework/core/docs/errors/<code>.md`
|
|
39
43
|
- [Roadmap](node_modules/@knit-framework/core/docs/roadmap.md): what's proposed next, and why
|
|
40
44
|
- [Coming from Django](node_modules/@knit-framework/core/docs/coming-from-django.md): every Django concept and its Knit equivalent
|
|
41
45
|
|
package/template/versions.json
CHANGED