neoorm 0.9.3 → 0.9.6

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/docs/cli.md CHANGED
@@ -30,6 +30,7 @@ neoorm generate [options]
30
30
  ```
31
31
 
32
32
  Options:
33
+ - `-n, --name <name>` — migration name (e.g. `--name add_users`). Only needed when a migration is created: prompts on a TTY at that point, errors in CI. Folders are `<timestamp>_<slug>` with the slug capped at 50 chars. No-change runs (client regeneration only) and pending-apply-only `dev` runs never prompt and succeed without `--name`.
33
34
  - `--accept-data-loss` — include destructive DDL changes (column drops, type changes)
34
35
 
35
36
  ## `neoorm migrate dev`
@@ -37,9 +38,26 @@ Options:
37
38
  Apply pending migrations, then generate a new migration if the schema changed. If a pending `CREATE TABLE` targets a relation that already exists, catch the live database up to `schema.ts` and record those files as applied instead of re-running the create.
38
39
 
39
40
  ```
40
- neoorm migrate dev
41
+ neoorm migrate dev --name <name>
41
42
  ```
42
43
 
44
+ - `-n, --name <name>` — same as `generate`: only needed when `dev` creates a migration, prompts on a TTY at that point, errors in CI. Applying pending migrations never requires `--name`. Not used by `deploy`, `status`, `reset`, or `down`.
45
+
46
+ ## `neoorm migrate diff`
47
+
48
+ Preview the migration SQL `generate` / `migrate dev` would write, without writing anything.
49
+
50
+ ```
51
+ neoorm migrate diff [--accept-data-loss] [--json]
52
+ ```
53
+
54
+ Read-only: diffs `schema.ts` against `snapshot.json` only. No database connection, no files written, and `--name` is never needed (ignored if passed). Shows the exact statements that would go into `migration.sql`, so you can inspect a change before naming it.
55
+
56
+ - `--accept-data-loss` — include destructive DDL in the preview (by default destructive changes are reported as blocked, same as `generate`)
57
+ - `--json` — print `{ schemaChanged, sql, blocked, destructiveBlocked, warnings }` instead of human-readable text
58
+
59
+ Exit code is `1` when destructive changes would block the migration (mirrors `generate`), `0` otherwise. For already-written but unapplied migrations, use `migrate status`.
60
+
43
61
  ## `neoorm migrate deploy`
44
62
 
45
63
  Apply all pending migrations. Throws `migration_drift` if a pending `CREATE TABLE` would hit an existing relation.
@@ -83,6 +101,19 @@ neoorm migrate reset --force [--skip-apply]
83
101
 
84
102
  PostgreSQL: the connecting role owns the recreated schema. `PUBLIC` is not granted.
85
103
 
104
+ ## `neoorm seed`
105
+
106
+ Run a seed script against the database inside a single transaction. Any failure rolls the whole seed back.
107
+
108
+ ```
109
+ neoorm seed [--env <name>] [--file <path>]
110
+ ```
111
+
112
+ - `--env <name>` — run `seeds/<name>.ts` (next to `schema.ts`) instead of `seed.ts`
113
+ - `--file <path>` — run a specific seed file (wins over `--env` and config)
114
+
115
+ Resolution order: `--file` → config `seed.file` → `seeds/<env>.ts` → `seed.ts`. The client is compiled from `schema.ts`, so seeding works on a fresh checkout with no codegen. Write idempotent seeds (`upsert`, `findOrCreate`, `createMany` with `skipDuplicates`) — re-runs are by convention, there is no seed ledger. See [Seeding](seeding.md).
116
+
86
117
  ## `neoorm docs`
87
118
 
88
119
  Serve the NeoOrm documentation locally in your browser.
@@ -33,6 +33,8 @@ CLI commands load `.env` from the project directory before evaluating this file,
33
33
  | `generate.zod` | `boolean` | `false` | Emit Select/Create/Update Zod schemas to `out/zod.ts`. See [Zod schemas](zod.md). |
34
34
  | `generate.typebox` | `boolean` | `false` | Emit Select/Create/Update TypeBox schemas to `out/typebox.ts`. See [TypeBox schemas](typebox.md). |
35
35
  | `generate.elysia` | `boolean` | `false` | Emit Select/Create/Update Elysia `t` schemas to `out/elysia.ts`. See [Elysia schemas](elysia.md). |
36
+ | `seed.file` | `string` | unset | Default seed file, project-root relative. CLI `--file` overrides it. See [Seeding](seeding.md). |
37
+ | `seed.env` | `string` | unset | Default environment: runs `seeds/<env>.ts` next to the schema. CLI `--env` overrides it. |
36
38
 
37
39
  ### Validation emit
38
40
 
@@ -5,8 +5,9 @@
5
5
  | Command | Description |
6
6
  |---------|-------------|
7
7
  | `neoorm init` | Scaffold `neoorm.config.ts`, `schema.ts`, `.env.example` only (no codegen or migrations; run `neoorm migrate dev` after) |
8
- | `neoorm generate` | Emit manifest, typed client, models, includes, and migrations |
9
- | `neoorm migrate dev` | Apply pending migrations (or record colliding CREATE TABLE files against the live schema), then generate a new one if `schema.ts` changed |
8
+ | `neoorm generate [--name <name>]` | Emit manifest, typed client, models, includes, and migrations (`--name` only when a migration is created) |
9
+ | `neoorm migrate dev [--name <name>]` | Apply pending migrations (or record colliding CREATE TABLE files against the live schema), then generate a new one if `schema.ts` changed (`--name` only when a new migration is created) |
10
+ | `neoorm migrate diff [--accept-data-loss] [--json]` | Preview the SQL a new migration would contain — no files written, no `--name` needed |
10
11
  | `neoorm migrate deploy` | Apply pending migrations (throws `migration_drift` if a pending `CREATE TABLE` targets a relation that already exists) |
11
12
  | `neoorm migrate status` | List applied vs pending migrations |
12
13
  | `neoorm migrate down [--steps N]` | Roll back the last N applied migrations (default 1) |
@@ -22,13 +23,22 @@
22
23
  |---------|---------|
23
24
  | Schema unchanged | Snapshot hash matches — no manifest or migration changes |
24
25
  | Client regenerated | Manifest changed but no database DDL needed |
25
- | Migration created | New `migrations/<timestamp>/migration.sql` written |
26
+ | Migration created | New `migrations/<timestamp>_<name>/migration.sql` written |
26
27
  | Migration blocked | Destructive or manual changes prevented writing SQL |
27
28
 
29
+ Migration names are slugified (`Add Users` → `add_users`, capped at 50 chars) and prefixed with a timestamp so folders sort in apply order. `--name` is only needed when a migration is created: the CLI prompts on a TTY at that point (after pending migrations are applied in `dev`) and errors in non-interactive runs. Runs with no schema changes (client regeneration only) and pending-apply-only `dev` runs succeed without `--name` and never prompt. Older `*_migration` folders still apply in order.
30
+
28
31
  When migration is blocked, the CLI explains why — for example unsupported type casts (`alter_column_type_manual`), enum value changes, or destructive drops. Re-run with `--accept-data-loss` to include destructive DDL:
29
32
 
30
33
  ```bash
31
- neoorm generate --accept-data-loss
34
+ neoorm generate --name add_users --accept-data-loss
35
+ ```
36
+
37
+ To inspect the SQL before naming the migration, preview it first (read-only, needs no `--name`):
38
+
39
+ ```bash
40
+ neoorm migrate diff
41
+ neoorm migrate diff --json
32
42
  ```
33
43
 
34
44
  ## Existing tables vs pending CREATE TABLE
@@ -0,0 +1,67 @@
1
+ # Seeding
2
+
3
+ `neoorm seed` runs a seed script against the database inside a single transaction. Any failure rolls the whole seed back, so failed runs are safe to retry.
4
+
5
+ ```
6
+ neoorm seed [--env <name>] [--file <path>]
7
+ ```
8
+
9
+ ## Seed files
10
+
11
+ By convention the seed lives next to the schema as `seed.ts` and exports a `seed(db)` function. `db` is a transaction-scoped client, so every write in the file is atomic:
12
+
13
+ ```ts
14
+ // seed.ts
15
+ import type { TransactionClient } from "neoorm";
16
+ import { schema } from "./schema.js";
17
+
18
+ export async function seed(db: TransactionClient<typeof schema._tables>) {
19
+ await db.users.upsert({
20
+ where: { email: "admin@example.com" },
21
+ create: { email: "admin@example.com", name: "Admin" },
22
+ update: {},
23
+ });
24
+ await db.tags.createMany({
25
+ data: [
26
+ { slug: "news", name: "News" },
27
+ { slug: "guides", name: "Guides" },
28
+ ],
29
+ skipDuplicates: true,
30
+ });
31
+ }
32
+ ```
33
+
34
+ Annotating with `TransactionClient<typeof schema._tables>` gives fully typed repositories — the same types as the generated client. A default-exported function works too, and `SeedContext` from `neoorm` is available for manifest-agnostic helpers.
35
+
36
+ ## Environments
37
+
38
+ `--env <name>` runs `seeds/<name>.ts` instead of `seed.ts`:
39
+
40
+ ```bash
41
+ neoorm seed --env dev
42
+ neoorm seed --file ./seeds/manual.ts
43
+ ```
44
+
45
+ `--file` wins over `--env`, and both win over config (see below).
46
+
47
+ ## Idempotency
48
+
49
+ Seeds are re-run by convention — there is no seed ledger. Write seeds so running them twice is safe: `upsert`, `findOrCreate`, and `createMany({ skipDuplicates: true })` are the building blocks. The transaction guarantees all-or-nothing per run, not across runs.
50
+
51
+ ## Configuration
52
+
53
+ Pin defaults in `neoorm.config.ts` (CLI flags override them):
54
+
55
+ ```ts
56
+ export default defineConfig({
57
+ // ...
58
+ seed: { env: "dev", file: "./seeds/base.ts" },
59
+ });
60
+ ```
61
+
62
+ | Key | Type | Description |
63
+ |-----|------|-------------|
64
+ | `seed.file` | `string` | Default seed file, project-root relative |
65
+ | `seed.env` | `string` | Default environment, runs `seeds/<env>.ts` next to the schema |
66
+
67
+ See [Configuration](configuration.md) for the full config reference.