@sedrino/db-schema 0.1.1 → 0.1.2
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/README.md +62 -6
- package/dist/cli.js +1904 -0
- package/dist/cli.js.map +1 -0
- package/dist/index.d.ts +365 -85
- package/dist/index.js +1108 -187
- package/dist/index.js.map +1 -1
- package/docs/cli.md +93 -0
- package/docs/expressions-and-transforms.md +165 -0
- package/docs/index.md +5 -2
- package/docs/migrations.md +183 -3
- package/docs/planning-and-apply.md +200 -0
- package/docs/relations.md +130 -0
- package/docs/schema-document.md +62 -0
- package/package.json +3 -2
- package/src/apply.ts +67 -0
- package/src/cli.ts +105 -7
- package/src/drizzle.ts +348 -1
- package/src/index.ts +38 -1
- package/src/migration.ts +315 -3
- package/src/operations.ts +278 -0
- package/src/planner.ts +7 -190
- package/src/project.ts +157 -1
- package/src/sql-expression.ts +123 -0
- package/src/sqlite.ts +150 -9
- package/src/transforms.ts +94 -0
- package/src/utils.ts +54 -0
package/README.md
CHANGED
|
@@ -15,8 +15,9 @@ The first version focuses on the planning layer:
|
|
|
15
15
|
- versioned schema document types
|
|
16
16
|
- migration DSL with deterministic operation recording
|
|
17
17
|
- schema materialization from migration history
|
|
18
|
-
- SQLite
|
|
18
|
+
- rebuild-aware SQLite migration emission for supported table-shape changes
|
|
19
19
|
- Drizzle source generation for a narrow SQLite + Temporal-aware subset
|
|
20
|
+
- inferred Drizzle soft-relations generation from foreign keys
|
|
20
21
|
- a libSQL-compatible apply runner with migration/state metadata tables
|
|
21
22
|
- a Bun-first CLI for planning migrations, applying them, and emitting schema artifacts
|
|
22
23
|
|
|
@@ -26,14 +27,12 @@ The first version focuses on the planning layer:
|
|
|
26
27
|
bun add @sedrino/db-schema
|
|
27
28
|
```
|
|
28
29
|
|
|
30
|
+
The CLI is Bun-first. If you want to run `sedrino-db`, make sure `bun` is available on `PATH`.
|
|
31
|
+
|
|
29
32
|
## Example
|
|
30
33
|
|
|
31
34
|
```ts
|
|
32
|
-
import {
|
|
33
|
-
compileSchemaToDrizzle,
|
|
34
|
-
createMigration,
|
|
35
|
-
planMigration,
|
|
36
|
-
} from "@sedrino/db-schema";
|
|
35
|
+
import { compileSchemaToDrizzle, createMigration, planMigration } from "@sedrino/db-schema";
|
|
37
36
|
|
|
38
37
|
const migration = createMigration(
|
|
39
38
|
{
|
|
@@ -53,17 +52,74 @@ const plan = planMigration({ migration });
|
|
|
53
52
|
const drizzleSource = compileSchemaToDrizzle(plan.nextSchema);
|
|
54
53
|
```
|
|
55
54
|
|
|
55
|
+
## Supported migration operations
|
|
56
|
+
|
|
57
|
+
- create, drop, and rename tables
|
|
58
|
+
- add, drop, rename, and alter fields
|
|
59
|
+
- add and drop indexes
|
|
60
|
+
- add and drop unique indexes
|
|
61
|
+
|
|
62
|
+
The builder also supports higher-level relationship helpers:
|
|
63
|
+
|
|
64
|
+
- `belongsTo("table", ...)` for indexed foreign keys
|
|
65
|
+
- `createJunctionTable(...)` for many-to-many join tables with composite uniqueness and inferred `through(...)` relations
|
|
66
|
+
|
|
67
|
+
For SQLite safety, field adds, drops, and supported field alterations are emitted as table rebuilds.
|
|
68
|
+
Unsafe cases still produce planner warnings and `migrate apply` will refuse to run them.
|
|
69
|
+
|
|
70
|
+
When a rebuild needs help populating data, the preferred API is higher-level transform helpers:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
import { transforms } from "@sedrino/db-schema";
|
|
74
|
+
|
|
75
|
+
m.alterTable("account", (t) => {
|
|
76
|
+
t.string("slug")
|
|
77
|
+
.required()
|
|
78
|
+
.backfill(transforms.slugFrom("name"));
|
|
79
|
+
|
|
80
|
+
t.alterField("createdAt", (f) => {
|
|
81
|
+
f.temporalInstant().using(transforms.epochMsFromIsoString("createdAt"));
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
Raw `backfillSql(...)` and `usingSql(...)` are still available as escape hatches.
|
|
87
|
+
|
|
88
|
+
Relationship helpers look like:
|
|
89
|
+
|
|
90
|
+
```ts
|
|
91
|
+
m.createTable("contact", (t) => {
|
|
92
|
+
t.id("contactId", { prefix: "ct" });
|
|
93
|
+
t.belongsTo("account", {
|
|
94
|
+
required: true,
|
|
95
|
+
onDelete: "cascade",
|
|
96
|
+
});
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
m.createJunctionTable("userGroupMembership", {
|
|
100
|
+
left: { table: "user" },
|
|
101
|
+
right: { table: "group" },
|
|
102
|
+
});
|
|
103
|
+
```
|
|
104
|
+
|
|
56
105
|
## Docs
|
|
57
106
|
|
|
58
107
|
- `docs/index.md`
|
|
59
108
|
- `docs/schema-document.md`
|
|
60
109
|
- `docs/migrations.md`
|
|
110
|
+
- `docs/planning-and-apply.md`
|
|
111
|
+
- `docs/expressions-and-transforms.md`
|
|
112
|
+
- `docs/relations.md`
|
|
113
|
+
- `docs/cli.md`
|
|
61
114
|
|
|
62
115
|
## CLI
|
|
63
116
|
|
|
64
117
|
```bash
|
|
118
|
+
sedrino-db migrate create create-account --dir db
|
|
65
119
|
sedrino-db migrate plan --dir db
|
|
66
120
|
sedrino-db migrate apply --dir db --url file:./local.db
|
|
121
|
+
sedrino-db migrate validate --dir db
|
|
122
|
+
sedrino-db migrate status --dir db --url file:./local.db
|
|
67
123
|
sedrino-db schema print --dir db
|
|
68
124
|
sedrino-db schema drizzle --dir db --out db/schema/schema.generated.ts
|
|
69
125
|
```
|