peta-migrate 0.1.0 → 0.1.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/README.md +131 -0
- package/package.json +2 -2
package/README.md
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
# peta-migrate
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/peta-migrate)
|
|
4
|
+
[](https://www.typescriptlang.org)
|
|
5
|
+
[](LICENSE)
|
|
6
|
+
|
|
7
|
+
Standalone migration runner and generator for [peta-orm](https://www.npmjs.com/package/peta-orm). Run, roll back, and generate database migrations with a clean programmatic API and CLI.
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bun add peta-migrate
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Requires `kysely` as a peer dependency.
|
|
14
|
+
|
|
15
|
+
---
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
### Programmatic
|
|
20
|
+
|
|
21
|
+
```ts
|
|
22
|
+
import { Kysely, BunSqliteDialect } from "kysely-bun-sqlite"
|
|
23
|
+
import { Database } from "bun:sqlite"
|
|
24
|
+
import { createMigrationRunner, createMigrationGenerator } from "peta-migrate"
|
|
25
|
+
|
|
26
|
+
const db = new Kysely({ dialect: new BunSqliteDialect({ database: new Database("my-app.db") }) })
|
|
27
|
+
const runner = createMigrationRunner(db)
|
|
28
|
+
|
|
29
|
+
await runner.ensureTable() // create tracking table
|
|
30
|
+
|
|
31
|
+
await runner.up([
|
|
32
|
+
{
|
|
33
|
+
name: "001_create_users",
|
|
34
|
+
up: async (k) => {
|
|
35
|
+
await k.schema
|
|
36
|
+
.createTable("users")
|
|
37
|
+
.addColumn("id", "integer", (c) => c.autoIncrement().primaryKey())
|
|
38
|
+
.addColumn("name", "varchar(255)", (c) => c.notNull())
|
|
39
|
+
.execute()
|
|
40
|
+
},
|
|
41
|
+
down: async (k) => {
|
|
42
|
+
await k.schema.dropTable("users").execute()
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
])
|
|
46
|
+
|
|
47
|
+
// Check status
|
|
48
|
+
const completed = await runner.getCompleted() // MigrationRecord[]
|
|
49
|
+
const status = await runner.status() // { completed: [...], pending: [...] }
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### CLI
|
|
53
|
+
|
|
54
|
+
```bash
|
|
55
|
+
bun x peta migrate:init # Create migrations directory and tracking table
|
|
56
|
+
bun x peta migrate:generate # Generate initial migration from models
|
|
57
|
+
bun x peta migrate:up # Run pending migrations
|
|
58
|
+
bun x peta migrate:status # Show migration status
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
---
|
|
62
|
+
|
|
63
|
+
## API
|
|
64
|
+
|
|
65
|
+
### `createMigrationRunner(kysely)`
|
|
66
|
+
|
|
67
|
+
Creates a runner that manages migration execution.
|
|
68
|
+
|
|
69
|
+
| Method | Description |
|
|
70
|
+
|--------|-------------|
|
|
71
|
+
| `ensureTable()` | Create the migrations tracking table |
|
|
72
|
+
| `up(migrations)` | Apply pending migrations in order |
|
|
73
|
+
| `down()` | Roll back the last batch of migrations |
|
|
74
|
+
| `getCompleted()` | Return list of completed migration records |
|
|
75
|
+
| `status()` | Return `{ completed, pending }` with both lists |
|
|
76
|
+
|
|
77
|
+
### `createMigrationGenerator()`
|
|
78
|
+
|
|
79
|
+
Creates a generator that produces migration code from model definitions.
|
|
80
|
+
|
|
81
|
+
| Method | Description |
|
|
82
|
+
|--------|-------------|
|
|
83
|
+
| `generateInitialMigration(models)` | Generate a create-table migration from registered models |
|
|
84
|
+
|
|
85
|
+
### Configuration
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { defineConfig } from "peta-migrate"
|
|
89
|
+
|
|
90
|
+
const config = defineConfig({
|
|
91
|
+
migrationsDir: "./migrations",
|
|
92
|
+
models: ["./src/models/*.ts"],
|
|
93
|
+
getKysely: () => db,
|
|
94
|
+
})
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
| Option | Type | Description |
|
|
98
|
+
|--------|------|-------------|
|
|
99
|
+
| `migrationsDir` | `string` | Directory to store migration files |
|
|
100
|
+
| `models` | `string[]` \| `string` | Glob patterns for model files |
|
|
101
|
+
| `getKysely` | `() => Kysely` | Function returning a Kysely instance |
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
105
|
+
## Types
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
interface MigrationFile {
|
|
109
|
+
name: string
|
|
110
|
+
up: (db: Kysely<unknown>) => Promise<void>
|
|
111
|
+
down: (db: Kysely<unknown>) => Promise<void>
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface MigrationRecord {
|
|
115
|
+
name: string
|
|
116
|
+
appliedAt: string
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
interface MigrationStatus {
|
|
120
|
+
completed: MigrationRecord[]
|
|
121
|
+
pending: MigrationFile[]
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
---
|
|
126
|
+
|
|
127
|
+
## Related packages
|
|
128
|
+
|
|
129
|
+
- [peta-orm](../orm) — ORM with models, relations, hooks, soft deletes
|
|
130
|
+
- [peta-auth](../auth) — Encrypted cookie sessions, JWT, OAuth
|
|
131
|
+
- [peta-docs](../docs) — OpenAPI 3.1 spec generation + Scalar UI
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "peta-migrate",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "0.1.
|
|
4
|
+
"version": "0.1.1",
|
|
5
5
|
"description": "Migration tools for peta-orm",
|
|
6
6
|
"license": "MIT",
|
|
7
7
|
"repository": {
|
|
@@ -31,7 +31,7 @@
|
|
|
31
31
|
"ora": "^9.4.0"
|
|
32
32
|
},
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"kysely": "^0.
|
|
34
|
+
"kysely": "^0.29.2",
|
|
35
35
|
"typescript": "^6.0.0"
|
|
36
36
|
},
|
|
37
37
|
"devDependencies": {
|