@cossackframework/database 0.8.1 → 0.8.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 +6 -4
- 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/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 +10 -5
package/docs/seeders.md
ADDED
|
@@ -0,0 +1,198 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: Seeders
|
|
3
|
+
description: Define and run ordered, idempotent seed data with scope and transaction policies.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Seeders
|
|
7
|
+
|
|
8
|
+
Seeders create or update application data after migrations have established the
|
|
9
|
+
schema. Cossack ORM provides a reusable `SeederRunner`; `tsx` or another runtime
|
|
10
|
+
loader only executes the TypeScript entry point.
|
|
11
|
+
|
|
12
|
+
## Define a named seeder
|
|
13
|
+
|
|
14
|
+
```ts
|
|
15
|
+
import { defineSeeder } from "@cossackframework/database";
|
|
16
|
+
import { User } from "../src/models/User.js";
|
|
17
|
+
|
|
18
|
+
export const usersSeeder = defineSeeder({
|
|
19
|
+
name: "users",
|
|
20
|
+
transaction: "auto",
|
|
21
|
+
|
|
22
|
+
async run() {
|
|
23
|
+
const email = "admin@example.com";
|
|
24
|
+
const existing = await User.findOne({
|
|
25
|
+
where: { email },
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
if (!existing) {
|
|
29
|
+
await User.create({
|
|
30
|
+
email,
|
|
31
|
+
name: "Administrator",
|
|
32
|
+
}).save();
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
`SeederRunner` establishes ORM scope, so static Active Record calls work inside
|
|
39
|
+
`run()` without another `orm.run()` wrapper.
|
|
40
|
+
|
|
41
|
+
The context also exposes bound ORM and SQL access:
|
|
42
|
+
|
|
43
|
+
```ts
|
|
44
|
+
export const settingsSeeder = defineSeeder({
|
|
45
|
+
name: "settings",
|
|
46
|
+
|
|
47
|
+
async run({ orm, sql, signal }) {
|
|
48
|
+
// SQLite/PostgreSQL upsert syntax; use the model API for portable upserts.
|
|
49
|
+
await sql`
|
|
50
|
+
INSERT INTO ${sql.id("settings")}
|
|
51
|
+
(${sql.id("key")}, ${sql.id("value")})
|
|
52
|
+
VALUES (${"theme"}, ${"system"})
|
|
53
|
+
ON CONFLICT (${sql.id("key")})
|
|
54
|
+
DO UPDATE SET ${sql.id("value")} = ${"system"}
|
|
55
|
+
`;
|
|
56
|
+
},
|
|
57
|
+
});
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
## Export one ordered list
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
// seeders/index.ts
|
|
64
|
+
import { rolesSeeder } from "./roles.seeder.js";
|
|
65
|
+
import { usersSeeder } from "./users.seeder.js";
|
|
66
|
+
import { postsSeeder } from "./posts.seeder.js";
|
|
67
|
+
|
|
68
|
+
// Configuration order is execution order.
|
|
69
|
+
export const seeders = [
|
|
70
|
+
rolesSeeder,
|
|
71
|
+
usersSeeder,
|
|
72
|
+
postsSeeder,
|
|
73
|
+
] as const;
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Seeder names must be non-empty and unique. Execution is sequential and stops at
|
|
77
|
+
the first failure, preserving declared dependencies.
|
|
78
|
+
|
|
79
|
+
Register the same list in `orm.config.ts`:
|
|
80
|
+
|
|
81
|
+
```ts
|
|
82
|
+
export default defineConfig({
|
|
83
|
+
adapter: createAdapter,
|
|
84
|
+
entities,
|
|
85
|
+
migrations,
|
|
86
|
+
seeds: seeders,
|
|
87
|
+
});
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Run seeders
|
|
91
|
+
|
|
92
|
+
```sh
|
|
93
|
+
cossack-orm seed list
|
|
94
|
+
cossack-orm seed run
|
|
95
|
+
cossack-orm seed run --only users,posts
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
`--only` selects names but preserves configuration order. Unknown names fail
|
|
99
|
+
with a list of available seeders.
|
|
100
|
+
|
|
101
|
+
Programmatic execution uses the same runner:
|
|
102
|
+
|
|
103
|
+
```ts
|
|
104
|
+
import { SeederRunner } from "@cossackframework/database";
|
|
105
|
+
|
|
106
|
+
const runner = new SeederRunner(orm, seeders);
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
const results = await runner.run({
|
|
110
|
+
only: ["users", "posts"],
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
for (const result of results) {
|
|
114
|
+
console.log(
|
|
115
|
+
result.name,
|
|
116
|
+
result.durationMs,
|
|
117
|
+
result.usedTransaction,
|
|
118
|
+
);
|
|
119
|
+
}
|
|
120
|
+
} finally {
|
|
121
|
+
await orm.close();
|
|
122
|
+
}
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
Use an `AbortSignal` for cooperative cancellation:
|
|
126
|
+
|
|
127
|
+
```ts
|
|
128
|
+
const controller = new AbortController();
|
|
129
|
+
|
|
130
|
+
await runner.run({
|
|
131
|
+
signal: controller.signal,
|
|
132
|
+
});
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The signal is exposed to the seeder context. A seeder should pass it to any
|
|
136
|
+
lower-level operation that supports cancellation.
|
|
137
|
+
|
|
138
|
+
## Transaction policies
|
|
139
|
+
|
|
140
|
+
Each seeder declares one of three policies:
|
|
141
|
+
|
|
142
|
+
| Policy | Behavior |
|
|
143
|
+
| --- | --- |
|
|
144
|
+
| `"auto"` | Use one transaction when supported; otherwise run without one |
|
|
145
|
+
| `"required"` | Fail before executing when interactive transactions are unavailable |
|
|
146
|
+
| `"none"` | Never create a runner-managed transaction |
|
|
147
|
+
|
|
148
|
+
`"auto"` is the default.
|
|
149
|
+
|
|
150
|
+
```ts
|
|
151
|
+
export const billingSeeder = defineSeeder({
|
|
152
|
+
name: "billing",
|
|
153
|
+
transaction: "required",
|
|
154
|
+
async run() {
|
|
155
|
+
// All writes must commit or roll back together.
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
D1 does not support callback-based interactive transactions. An `"auto"` seeder
|
|
161
|
+
runs without one, while `"required"` fails with a `SeederError` whose cause is
|
|
162
|
+
`UnsupportedCapabilityError`. Use `"none"` and a database-specific prepared
|
|
163
|
+
batch when D1 seed work must be atomic.
|
|
164
|
+
|
|
165
|
+
## Make seeders idempotent
|
|
166
|
+
|
|
167
|
+
Seeders are not tracked like migrations because they commonly need to be
|
|
168
|
+
rerunnable and environment-specific. Use stable natural keys, existence checks,
|
|
169
|
+
or upserts:
|
|
170
|
+
|
|
171
|
+
```ts
|
|
172
|
+
await User.upsert(
|
|
173
|
+
{
|
|
174
|
+
email: "admin@example.com",
|
|
175
|
+
name: "Administrator",
|
|
176
|
+
},
|
|
177
|
+
["email"],
|
|
178
|
+
);
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
Avoid random identifiers for records that must not be duplicated. Keep
|
|
182
|
+
production-only or destructive data policies in the application/framework CLI,
|
|
183
|
+
where environment and confirmation information is available.
|
|
184
|
+
|
|
185
|
+
## Framework integration
|
|
186
|
+
|
|
187
|
+
A framework should:
|
|
188
|
+
|
|
189
|
+
1. Load the application's `orm.config.ts`.
|
|
190
|
+
2. Construct its normal ORM instance.
|
|
191
|
+
3. Delegate execution to `SeederRunner`.
|
|
192
|
+
4. Add framework-specific environment and production confirmation checks.
|
|
193
|
+
5. Close the ORM in `finally`.
|
|
194
|
+
|
|
195
|
+
Do not create a second seeding engine or reimplement transaction behavior.
|
|
196
|
+
|
|
197
|
+
The complete pattern is available in the
|
|
198
|
+
[SQLite starter](../examples/sqlite-starter/README.md).
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cossackframework/database",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"description": "Serverless-first Active Record ORM for Cossack",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -22,6 +22,7 @@
|
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
24
|
"dist",
|
|
25
|
+
"docs",
|
|
25
26
|
"README.md",
|
|
26
27
|
"LICENSE"
|
|
27
28
|
],
|
|
@@ -68,15 +69,19 @@
|
|
|
68
69
|
"tsx": "^4.23.0"
|
|
69
70
|
},
|
|
70
71
|
"peerDependencies": {
|
|
71
|
-
"@
|
|
72
|
+
"@tursodatabase/database": "^0.7.2",
|
|
73
|
+
"@tursodatabase/serverless": "^1.4.0",
|
|
72
74
|
"better-sqlite3": ">=11",
|
|
73
|
-
"hono": "^4.12.
|
|
75
|
+
"hono": "^4.12.34",
|
|
74
76
|
"mysql2": ">=3.11",
|
|
75
77
|
"pg": ">=8.13",
|
|
76
78
|
"reflect-metadata": ">=0.2"
|
|
77
79
|
},
|
|
78
80
|
"peerDependenciesMeta": {
|
|
79
|
-
"@
|
|
81
|
+
"@tursodatabase/database": {
|
|
82
|
+
"optional": true
|
|
83
|
+
},
|
|
84
|
+
"@tursodatabase/serverless": {
|
|
80
85
|
"optional": true
|
|
81
86
|
},
|
|
82
87
|
"better-sqlite3": {
|
|
@@ -94,7 +99,7 @@
|
|
|
94
99
|
},
|
|
95
100
|
"devDependencies": {
|
|
96
101
|
"@types/node": "^22.19.0",
|
|
97
|
-
"hono": "^4.12.
|
|
102
|
+
"hono": "^4.12.34",
|
|
98
103
|
"reflect-metadata": "^0.2.2",
|
|
99
104
|
"typescript": "^7.0.2",
|
|
100
105
|
"vitest": "^4.1.10"
|