@cossackframework/database 0.8.1 → 0.8.3
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 +6 -4
- package/dist/cossack/index.d.ts +1 -0
- package/dist/cossack/index.d.ts.map +1 -1
- package/dist/cossack/index.js +18 -18
- package/dist/cossack/index.js.map +1 -1
- package/dist/migration/runner.d.ts +1 -0
- package/dist/migration/runner.d.ts.map +1 -1
- package/dist/migration/runner.js +14 -12
- package/dist/migration/runner.js.map +1 -1
- package/dist/runtime/cloudflare.d.ts +17 -19
- package/dist/runtime/cloudflare.d.ts.map +1 -1
- package/dist/runtime/cloudflare.js +20 -17
- package/dist/runtime/cloudflare.js.map +1 -1
- package/dist/runtime/deno.d.ts +12 -0
- package/dist/runtime/deno.d.ts.map +1 -1
- package/dist/runtime/deno.js +17 -0
- package/dist/runtime/deno.js.map +1 -1
- package/dist/runtime/node.d.ts +1 -1
- package/dist/runtime/node.d.ts.map +1 -1
- package/dist/runtime/node.js +2 -23
- package/dist/runtime/node.js.map +1 -1
- package/dist/runtime/turso.d.ts +33 -0
- package/dist/runtime/turso.d.ts.map +1 -0
- package/dist/runtime/turso.js +54 -0
- package/dist/runtime/turso.js.map +1 -0
- package/dist/schema/introspection.d.ts.map +1 -1
- package/dist/schema/introspection.js +213 -15
- package/dist/schema/introspection.js.map +1 -1
- package/dist/sql/client.js +4 -3
- package/dist/sql/client.js.map +1 -1
- package/dist/tooling/format-error.d.ts +3 -0
- package/dist/tooling/format-error.d.ts.map +1 -0
- package/dist/tooling/format-error.js +22 -0
- package/dist/tooling/format-error.js.map +1 -0
- package/dist/tooling/index.d.ts.map +1 -1
- package/dist/tooling/index.js +2 -1
- package/dist/tooling/index.js.map +1 -1
- package/docs/advanced.md +290 -0
- package/docs/comparison.md +188 -0
- package/docs/installation.md +140 -0
- package/docs/introduction.md +126 -0
- package/docs/migrations.md +232 -0
- package/docs/models.md +241 -0
- package/docs/queries.md +261 -0
- package/docs/raw-queries.md +183 -0
- package/docs/relationships.md +243 -0
- package/docs/runtimes.md +426 -0
- package/docs/seeders.md +198 -0
- package/package.json +11 -6
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Installation
|
|
3
|
+
description: Install Cossack ORM, configure TypeScript decorators, and create an ORM instance.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Installation
|
|
7
|
+
|
|
8
|
+
## Requirements
|
|
9
|
+
|
|
10
|
+
Cossack ORM is ESM-only and targets:
|
|
11
|
+
|
|
12
|
+
- Node.js 22 or newer
|
|
13
|
+
- A current stable Bun release
|
|
14
|
+
- A current stable Deno release
|
|
15
|
+
- Modern Cloudflare Workers compatibility dates
|
|
16
|
+
- TypeScript with decorator metadata enabled
|
|
17
|
+
|
|
18
|
+
Install the ORM and decorator metadata support:
|
|
19
|
+
|
|
20
|
+
```sh
|
|
21
|
+
pnpm add @cossackframework/database reflect-metadata
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Equivalent npm, Yarn, and Bun commands work as well.
|
|
25
|
+
|
|
26
|
+
```sh
|
|
27
|
+
npm install @cossackframework/database reflect-metadata
|
|
28
|
+
yarn add @cossackframework/database reflect-metadata
|
|
29
|
+
bun add @cossackframework/database reflect-metadata
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Import `reflect-metadata` once before decorated models are evaluated:
|
|
33
|
+
|
|
34
|
+
```ts
|
|
35
|
+
import "reflect-metadata";
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## TypeScript configuration
|
|
39
|
+
|
|
40
|
+
Cossack applications use legacy TypeScript decorators. Add these options to
|
|
41
|
+
`tsconfig.json`:
|
|
42
|
+
|
|
43
|
+
```json
|
|
44
|
+
{
|
|
45
|
+
"compilerOptions": {
|
|
46
|
+
"target": "ES2022",
|
|
47
|
+
"module": "NodeNext",
|
|
48
|
+
"moduleResolution": "NodeNext",
|
|
49
|
+
"experimentalDecorators": true,
|
|
50
|
+
"emitDecoratorMetadata": true,
|
|
51
|
+
"useDefineForClassFields": false
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
Because the package is ESM-only, set `"type": "module"` in `package.json` and
|
|
57
|
+
use `.js` extensions in relative TypeScript imports when using `NodeNext`.
|
|
58
|
+
|
|
59
|
+
## Choose a runtime adapter
|
|
60
|
+
|
|
61
|
+
The root package does not eagerly bundle database drivers. Import the adapter
|
|
62
|
+
for the runtime where the application executes:
|
|
63
|
+
|
|
64
|
+
| Runtime | Import | Available adapters |
|
|
65
|
+
| --- | --- | --- |
|
|
66
|
+
| Node.js | `@cossackframework/database/node` | `nodeSQLite`, `betterSQLite`, `postgres`, `mysql`, `turso` |
|
|
67
|
+
| Deno | `@cossackframework/database/deno` | `deno`, `denoSQLite`, `postgres`, `mysql`, `turso` |
|
|
68
|
+
| Bun | `@cossackframework/database/bun` | `bun` for SQLite, PostgreSQL, or MySQL |
|
|
69
|
+
| Cloudflare Workers | `@cossackframework/database/cloudflare` | `d1`, `hyperdrivePostgres`, `hyperdriveMySQL`, `turso` |
|
|
70
|
+
|
|
71
|
+
Some Node and Workers adapters require an optional peer driver:
|
|
72
|
+
|
|
73
|
+
```sh
|
|
74
|
+
# Install only what the application uses.
|
|
75
|
+
pnpm add pg
|
|
76
|
+
pnpm add mysql2
|
|
77
|
+
pnpm add @tursodatabase/database # embedded SQLite / Desktop
|
|
78
|
+
pnpm add @tursodatabase/serverless # remote Turso / edge runtimes
|
|
79
|
+
pnpm add better-sqlite3
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
Node 22's built-in SQLite adapter, Cloudflare D1, and Bun's native SQL client do
|
|
83
|
+
not require one of these packages.
|
|
84
|
+
|
|
85
|
+
## Create an ORM
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
import { createORM } from "@cossackframework/database";
|
|
89
|
+
import { nodeSQLite } from "@cossackframework/database/node";
|
|
90
|
+
import { entities } from "./models/index.js";
|
|
91
|
+
|
|
92
|
+
export const orm = createORM({
|
|
93
|
+
adapter: await nodeSQLite({ filename: "app.db" }),
|
|
94
|
+
entities,
|
|
95
|
+
});
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
Every relation target must appear in `entities`. Metadata is finalized when the
|
|
99
|
+
ORM is created, so duplicate names, missing primary keys, unresolved relations,
|
|
100
|
+
and ambiguous column types fail during startup rather than during the first
|
|
101
|
+
query.
|
|
102
|
+
|
|
103
|
+
## Create `orm.config.ts`
|
|
104
|
+
|
|
105
|
+
The CLI and Studio use one configuration file:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { defineConfig } from "@cossackframework/database";
|
|
109
|
+
import { nodeSQLite } from "@cossackframework/database/node";
|
|
110
|
+
import { entities } from "./src/models/index.js";
|
|
111
|
+
import { migrations } from "./migrations/index.js";
|
|
112
|
+
import { seeders } from "./seeders/index.js";
|
|
113
|
+
|
|
114
|
+
export default defineConfig({
|
|
115
|
+
entities,
|
|
116
|
+
migrations,
|
|
117
|
+
seeds: seeders,
|
|
118
|
+
adapter: () => nodeSQLite({ filename: "app.db" }),
|
|
119
|
+
});
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
An adapter factory is recommended in configuration files. It delays opening the
|
|
123
|
+
database until a command needs it.
|
|
124
|
+
|
|
125
|
+
## Verify the setup
|
|
126
|
+
|
|
127
|
+
```sh
|
|
128
|
+
cossack-orm schema check
|
|
129
|
+
cossack-orm migration status
|
|
130
|
+
cossack-orm seed list
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Pass a non-default config location with `--config`:
|
|
134
|
+
|
|
135
|
+
```sh
|
|
136
|
+
cossack-orm schema check --config ./config/orm.config.ts
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
Next, define application data in [Models](./models.md) and choose the correct
|
|
140
|
+
adapter details in [Runtimes](./runtimes.md).
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Introduction
|
|
3
|
+
description: Learn the design, core concepts, and first steps of Cossack ORM.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Introduction
|
|
7
|
+
|
|
8
|
+
Cossack ORM is an ESM-only, serverless-first SQL ORM for Node.js, Bun,
|
|
9
|
+
Cloudflare Workers, and Deno. It combines decorated models, Active Record
|
|
10
|
+
methods, a typed query builder, safe SQL fragments, migrations, introspection,
|
|
11
|
+
and explicit runtime adapters.
|
|
12
|
+
|
|
13
|
+
The package is deliberately small in shape:
|
|
14
|
+
|
|
15
|
+
- Models are ordinary classes that extend `BaseEntity`.
|
|
16
|
+
- Every application registers its complete entity list with `createORM()`.
|
|
17
|
+
- Static Active Record methods resolve through the current request or job scope.
|
|
18
|
+
- Dialects compile SQL while adapters execute it.
|
|
19
|
+
- Relations are loaded explicitly and batched.
|
|
20
|
+
- Migrations are generated and reviewed; startup never mutates the schema.
|
|
21
|
+
|
|
22
|
+
## A first model and query
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import "reflect-metadata";
|
|
26
|
+
import {
|
|
27
|
+
BaseEntity,
|
|
28
|
+
Column,
|
|
29
|
+
Entity,
|
|
30
|
+
PrimaryGeneratedColumn,
|
|
31
|
+
createORM,
|
|
32
|
+
} from "@cossackframework/database";
|
|
33
|
+
import { nodeSQLite } from "@cossackframework/database/node";
|
|
34
|
+
|
|
35
|
+
@Entity()
|
|
36
|
+
class User extends BaseEntity {
|
|
37
|
+
@PrimaryGeneratedColumn()
|
|
38
|
+
declare id: number;
|
|
39
|
+
|
|
40
|
+
@Column({ unique: true })
|
|
41
|
+
declare email: string;
|
|
42
|
+
|
|
43
|
+
@Column()
|
|
44
|
+
declare name: string;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const orm = createORM({
|
|
48
|
+
adapter: await nodeSQLite({ filename: "app.db" }),
|
|
49
|
+
entities: [User],
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
await orm.run(async () => {
|
|
54
|
+
await User.create({
|
|
55
|
+
email: "ada@example.com",
|
|
56
|
+
name: "Ada Lovelace",
|
|
57
|
+
}).save();
|
|
58
|
+
|
|
59
|
+
const users = await User.find({
|
|
60
|
+
order: { name: "asc" },
|
|
61
|
+
});
|
|
62
|
+
});
|
|
63
|
+
} finally {
|
|
64
|
+
await orm.close();
|
|
65
|
+
}
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
`orm.run()` establishes the current ORM for static methods such as
|
|
69
|
+
`User.find()`, `entity.save()`, and the global `sql` tag. Put one scope around a
|
|
70
|
+
request, queue job, scheduled task, or command:not around each query.
|
|
71
|
+
|
|
72
|
+
Framework middleware normally installs this scope automatically. For example,
|
|
73
|
+
the Cossack/Hono middleware wraps the request once:
|
|
74
|
+
|
|
75
|
+
```ts
|
|
76
|
+
import { ormMiddleware } from "@cossackframework/database/cossack";
|
|
77
|
+
|
|
78
|
+
app.use("*", ormMiddleware(orm));
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
Handlers below that middleware can call `User.find()` directly.
|
|
82
|
+
|
|
83
|
+
## Explicit model access
|
|
84
|
+
|
|
85
|
+
Code that already owns an ORM instance can query through a model manager without
|
|
86
|
+
an Active Record scope:
|
|
87
|
+
|
|
88
|
+
```ts
|
|
89
|
+
const users = await orm.model(User).find({
|
|
90
|
+
order: { name: "asc" },
|
|
91
|
+
});
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
This form is useful for framework integrations and simple read-only scripts.
|
|
95
|
+
Use `orm.run()` when you want static Active Record methods, instance persistence,
|
|
96
|
+
the global `sql` tag, or automatic transaction rebinding.
|
|
97
|
+
|
|
98
|
+
## What `orm.run()` does
|
|
99
|
+
|
|
100
|
+
An ORM scope identifies the correct ORM and transaction client across
|
|
101
|
+
asynchronous calls. It does not, by itself, promise to reserve a database
|
|
102
|
+
connection. Pooling adapters generally acquire connections as needed.
|
|
103
|
+
`orm.transaction()` and `orm.reserve()` explicitly pin the appropriate client
|
|
104
|
+
when the adapter supports those capabilities.
|
|
105
|
+
|
|
106
|
+
This distinction matters when an application has multiple databases, concurrent
|
|
107
|
+
requests, request-specific Workers bindings, or nested transactions. Cossack ORM
|
|
108
|
+
throws an actionable `ScopeError` instead of silently guessing a global
|
|
109
|
+
connection.
|
|
110
|
+
|
|
111
|
+
## Design boundaries
|
|
112
|
+
|
|
113
|
+
Cossack ORM v1 does not provide a repository/Data Mapper layer, automatic schema
|
|
114
|
+
synchronization, a legacy query-builder compatibility API, NoSQL or GraphQL integration, or
|
|
115
|
+
query-result caching. Database-specific work remains possible through safe SQL
|
|
116
|
+
fragments and public adapter contracts.
|
|
117
|
+
|
|
118
|
+
## Continue reading
|
|
119
|
+
|
|
120
|
+
- [Installation](./installation.md)
|
|
121
|
+
- [Models](./models.md)
|
|
122
|
+
- [Queries](./queries.md)
|
|
123
|
+
- [Relationships](./relationships.md)
|
|
124
|
+
- [Raw queries](./raw-queries.md)
|
|
125
|
+
- [Comparison](./comparison.md)
|
|
126
|
+
- [Complete SQLite starter](../examples/sqlite-starter/README.md)
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Migrations
|
|
3
|
+
description: Generate, review, apply, revert, baseline, and inspect deterministic database migrations.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Migrations
|
|
7
|
+
|
|
8
|
+
Migrations are explicit, versioned changes to a database schema. Cossack ORM
|
|
9
|
+
never synchronizes models to the database during application startup.
|
|
10
|
+
|
|
11
|
+
The normal workflow is:
|
|
12
|
+
|
|
13
|
+
1. Change decorated models.
|
|
14
|
+
2. Generate a migration from the committed model snapshot diff.
|
|
15
|
+
3. Review its generated forward and inverse SQL.
|
|
16
|
+
4. Apply it in development and CI.
|
|
17
|
+
5. Commit the model and migration together.
|
|
18
|
+
|
|
19
|
+
## Configuration
|
|
20
|
+
|
|
21
|
+
Register migrations in `orm.config.ts`:
|
|
22
|
+
|
|
23
|
+
```ts
|
|
24
|
+
import { defineConfig } from "@cossackframework/database";
|
|
25
|
+
import { nodeSQLite } from "@cossackframework/database/node";
|
|
26
|
+
import { entities } from "./src/models/index.js";
|
|
27
|
+
import { migrations } from "./migrations/index.js";
|
|
28
|
+
|
|
29
|
+
export default defineConfig({
|
|
30
|
+
adapter: () => nodeSQLite({ filename: "app.db" }),
|
|
31
|
+
entities,
|
|
32
|
+
migrations,
|
|
33
|
+
migrationDirectory: "./migrations",
|
|
34
|
+
});
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
Keep their configured names stable and unique:
|
|
38
|
+
|
|
39
|
+
```ts
|
|
40
|
+
import createUsers from "./0001_create_users.js";
|
|
41
|
+
import createPosts from "./0002_create_posts.js";
|
|
42
|
+
|
|
43
|
+
export const migrations = [
|
|
44
|
+
createUsers,
|
|
45
|
+
createPosts,
|
|
46
|
+
] as const;
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Generate a migration
|
|
50
|
+
|
|
51
|
+
Existing projects first record their current decorated models once:
|
|
52
|
+
|
|
53
|
+
```sh
|
|
54
|
+
cossack-orm migration snapshot
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
This writes `.cossack-schema.json` inside `migrationDirectory`. Commit the
|
|
58
|
+
snapshot with the migrations. It is model metadata, not a database dump.
|
|
59
|
+
|
|
60
|
+
```sh
|
|
61
|
+
cossack-orm migration generate add_user_profiles
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
Choose another output path when needed:
|
|
65
|
+
|
|
66
|
+
```sh
|
|
67
|
+
cossack-orm migration generate add_user_profiles \
|
|
68
|
+
--output ./src/database/migrations/0003_add_user_profiles.ts
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Generation compares the committed model snapshot with current decorated model
|
|
72
|
+
metadata. It does not introspect the database. Adding or removing a `@Column()`,
|
|
73
|
+
entity, rename marker, or index produces deterministic TypeScript containing
|
|
74
|
+
reviewable `up()` and inverse `down()` SQL. The snapshot advances only after the
|
|
75
|
+
migration file is written successfully. When the output is directly inside
|
|
76
|
+
`migrationDirectory` and its `index.ts` exports the standard `migrations` array,
|
|
77
|
+
the generator registers the new migration automatically; otherwise it prints a
|
|
78
|
+
registration reminder.
|
|
79
|
+
|
|
80
|
+
A migration has this contract:
|
|
81
|
+
|
|
82
|
+
```ts
|
|
83
|
+
import {
|
|
84
|
+
sql,
|
|
85
|
+
type Migration,
|
|
86
|
+
} from "@cossackframework/database";
|
|
87
|
+
|
|
88
|
+
export default {
|
|
89
|
+
name: "0003_add_user_profiles",
|
|
90
|
+
|
|
91
|
+
up({ schema }) {
|
|
92
|
+
schema.raw(sql.unsafe(
|
|
93
|
+
"ALTER TABLE users ADD COLUMN biography text",
|
|
94
|
+
));
|
|
95
|
+
},
|
|
96
|
+
|
|
97
|
+
down({ schema }) {
|
|
98
|
+
schema.dropColumn("users", "biography");
|
|
99
|
+
},
|
|
100
|
+
} satisfies Migration;
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
`sql.unsafe()` is appropriate here only because the statement is trusted,
|
|
104
|
+
static, and reviewed. Use schema operations where they express the change
|
|
105
|
+
portably.
|
|
106
|
+
|
|
107
|
+
## Squash migration history
|
|
108
|
+
|
|
109
|
+
Create one baseline migration directly from the current models:
|
|
110
|
+
|
|
111
|
+
```sh
|
|
112
|
+
cossack-orm migration squash 0001_schema
|
|
113
|
+
```
|
|
114
|
+
|
|
115
|
+
After reviewing it, prune the replaced migration source files and rewrite the
|
|
116
|
+
migration barrel:
|
|
117
|
+
|
|
118
|
+
```sh
|
|
119
|
+
cossack-orm migration squash 0001_schema --prune
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
The squashed migration records the migration names it replaces. On a fresh
|
|
123
|
+
database its full schema is applied. On an existing database where every
|
|
124
|
+
replaced migration is already applied, `migration up` consolidates only the
|
|
125
|
+
bookkeeping and does not replay schema creation. A partially applied replacement
|
|
126
|
+
set is rejected. Squashed baselines are intentionally not reversible.
|
|
127
|
+
|
|
128
|
+
## Apply and revert
|
|
129
|
+
|
|
130
|
+
```sh
|
|
131
|
+
cossack-orm migration up
|
|
132
|
+
cossack-orm migration down
|
|
133
|
+
cossack-orm migration status
|
|
134
|
+
cossack-orm migration check
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
- `up` applies pending migrations in deterministic name order.
|
|
138
|
+
- `down` reverts the most recently applied migration by default.
|
|
139
|
+
- `status` reports pending, applied, or checksum-changed migrations.
|
|
140
|
+
- `check` fails unless all configured migrations are applied and unchanged.
|
|
141
|
+
|
|
142
|
+
Applied migrations are recorded in `_cossack_migrations` with their name,
|
|
143
|
+
SHA-256 checksum, batch, and application timestamp. Editing an applied migration
|
|
144
|
+
causes checksum validation to fail; create a new migration instead.
|
|
145
|
+
|
|
146
|
+
## Destructive changes
|
|
147
|
+
|
|
148
|
+
Dropping tables or columns and potentially narrowing column changes fail
|
|
149
|
+
generation/checking by default:
|
|
150
|
+
|
|
151
|
+
```sh
|
|
152
|
+
cossack-orm migration generate remove_legacy_data \
|
|
153
|
+
--allow-destructive
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Use this flag only after reviewing the generated operation and planning data
|
|
157
|
+
retention, backfills, and deployment ordering.
|
|
158
|
+
|
|
159
|
+
Renames are never guessed. Record their source explicitly:
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
@Entity({
|
|
163
|
+
tableName: "accounts",
|
|
164
|
+
renamedFrom: "users",
|
|
165
|
+
})
|
|
166
|
+
class Account extends BaseEntity {
|
|
167
|
+
@Column({
|
|
168
|
+
name: "display_name",
|
|
169
|
+
renamedFrom: "name",
|
|
170
|
+
})
|
|
171
|
+
declare displayName: string;
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
## Schema commands
|
|
176
|
+
|
|
177
|
+
```sh
|
|
178
|
+
cossack-orm schema diff
|
|
179
|
+
cossack-orm schema check
|
|
180
|
+
cossack-orm schema pull
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
- `schema diff` describes physical/model differences.
|
|
184
|
+
- `schema check` fails when drift exists.
|
|
185
|
+
- `schema pull` generates decorated models with explicit physical names and
|
|
186
|
+
logical types.
|
|
187
|
+
|
|
188
|
+
Unlike model-first migration generation, schema commands inspect the live
|
|
189
|
+
database. Destructive differences also require `--allow-destructive` for
|
|
190
|
+
`schema diff` and `schema check`.
|
|
191
|
+
|
|
192
|
+
By default, pulled models are written to `src/entities.generated.ts`. Existing
|
|
193
|
+
files are not overwritten unless `--force` is supplied:
|
|
194
|
+
|
|
195
|
+
```sh
|
|
196
|
+
cossack-orm schema pull \
|
|
197
|
+
--output ./src/models/generated.ts \
|
|
198
|
+
--force
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Unknown physical database types are retained with a `custom:<type>` logical
|
|
202
|
+
type rather than discarded.
|
|
203
|
+
|
|
204
|
+
## Baseline an existing database
|
|
205
|
+
|
|
206
|
+
Baseline lets an existing deployment adopt migration bookkeeping without
|
|
207
|
+
replaying its historical schema creation:
|
|
208
|
+
|
|
209
|
+
```sh
|
|
210
|
+
cossack-orm migration baseline
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
The CLI first verifies that the live database matches current model metadata.
|
|
214
|
+
Only then does it record the schema hash. Resolve drift before baselining.
|
|
215
|
+
|
|
216
|
+
## Programmatic runner
|
|
217
|
+
|
|
218
|
+
Framework CLIs can delegate to `MigrationRunner`:
|
|
219
|
+
|
|
220
|
+
```ts
|
|
221
|
+
import { MigrationRunner } from "@cossackframework/database";
|
|
222
|
+
|
|
223
|
+
const runner = new MigrationRunner(orm, migrations);
|
|
224
|
+
|
|
225
|
+
const applied = await orm.run(() => runner.up());
|
|
226
|
+
const status = await orm.run(() => runner.status());
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
Transactional adapters apply a migration and its bookkeeping together. D1 uses
|
|
230
|
+
an atomic prepared batch for schema statements plus bookkeeping.
|
|
231
|
+
|
|
232
|
+
See [Seeders](./seeders.md) for application data that may be safely rerun.
|