@cossackframework/database 0.8.3 → 0.9.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.
Files changed (173) hide show
  1. package/README.md +21 -22
  2. package/agent-docs.json +90 -0
  3. package/dist/adapter/types.d.ts +14 -1
  4. package/dist/adapter/types.d.ts.map +1 -1
  5. package/dist/codec.d.ts +7 -0
  6. package/dist/codec.d.ts.map +1 -1
  7. package/dist/codec.js +31 -2
  8. package/dist/codec.js.map +1 -1
  9. package/dist/config.d.ts +10 -1
  10. package/dist/config.d.ts.map +1 -1
  11. package/dist/config.js.map +1 -1
  12. package/dist/cossack/cache-policy.d.ts +18 -0
  13. package/dist/cossack/cache-policy.d.ts.map +1 -0
  14. package/dist/cossack/cache-policy.js +64 -0
  15. package/dist/cossack/cache-policy.js.map +1 -0
  16. package/dist/cossack/index.d.ts +20 -3
  17. package/dist/cossack/index.d.ts.map +1 -1
  18. package/dist/cossack/index.js +105 -51
  19. package/dist/cossack/index.js.map +1 -1
  20. package/dist/dialect/dialects.d.ts.map +1 -1
  21. package/dist/dialect/dialects.js +6 -0
  22. package/dist/dialect/dialects.js.map +1 -1
  23. package/dist/entity/base-entity.d.ts +22 -6
  24. package/dist/entity/base-entity.d.ts.map +1 -1
  25. package/dist/entity/base-entity.js +37 -8
  26. package/dist/entity/base-entity.js.map +1 -1
  27. package/dist/entity/manager.d.ts +22 -5
  28. package/dist/entity/manager.d.ts.map +1 -1
  29. package/dist/entity/manager.js +89 -4
  30. package/dist/entity/manager.js.map +1 -1
  31. package/dist/index.d.ts +7 -5
  32. package/dist/index.d.ts.map +1 -1
  33. package/dist/index.js +4 -2
  34. package/dist/index.js.map +1 -1
  35. package/dist/metadata/finalize.d.ts.map +1 -1
  36. package/dist/metadata/finalize.js +27 -5
  37. package/dist/metadata/finalize.js.map +1 -1
  38. package/dist/metadata/inspection.d.ts +51 -0
  39. package/dist/metadata/inspection.d.ts.map +1 -0
  40. package/dist/metadata/inspection.js +19 -0
  41. package/dist/metadata/inspection.js.map +1 -0
  42. package/dist/migration/checksum.d.ts +18 -0
  43. package/dist/migration/checksum.d.ts.map +1 -0
  44. package/dist/migration/checksum.js +164 -0
  45. package/dist/migration/checksum.js.map +1 -0
  46. package/dist/migration/plan.d.ts +13 -0
  47. package/dist/migration/plan.d.ts.map +1 -0
  48. package/dist/migration/plan.js +73 -0
  49. package/dist/migration/plan.js.map +1 -0
  50. package/dist/migration/runner.d.ts +17 -3
  51. package/dist/migration/runner.d.ts.map +1 -1
  52. package/dist/migration/runner.js +175 -14
  53. package/dist/migration/runner.js.map +1 -1
  54. package/dist/migration/types.d.ts +47 -2
  55. package/dist/migration/types.d.ts.map +1 -1
  56. package/dist/orm.d.ts +68 -2
  57. package/dist/orm.d.ts.map +1 -1
  58. package/dist/orm.js +181 -50
  59. package/dist/orm.js.map +1 -1
  60. package/dist/query/builder.d.ts +33 -4
  61. package/dist/query/builder.d.ts.map +1 -1
  62. package/dist/query/builder.js +200 -23
  63. package/dist/query/builder.js.map +1 -1
  64. package/dist/query/diagnostics.d.ts +3 -0
  65. package/dist/query/diagnostics.d.ts.map +1 -0
  66. package/dist/query/diagnostics.js +75 -0
  67. package/dist/query/diagnostics.js.map +1 -0
  68. package/dist/query/expression.d.ts +2 -0
  69. package/dist/query/expression.d.ts.map +1 -1
  70. package/dist/query/expression.js +16 -1
  71. package/dist/query/expression.js.map +1 -1
  72. package/dist/query/types.d.ts +17 -2
  73. package/dist/query/types.d.ts.map +1 -1
  74. package/dist/query/types.js +3 -0
  75. package/dist/query/types.js.map +1 -1
  76. package/dist/runtime/cloudflare-turso.d.ts +29 -0
  77. package/dist/runtime/cloudflare-turso.d.ts.map +1 -0
  78. package/dist/runtime/cloudflare-turso.js +47 -0
  79. package/dist/runtime/cloudflare-turso.js.map +1 -0
  80. package/dist/runtime/cloudflare.d.ts +40 -21
  81. package/dist/runtime/cloudflare.d.ts.map +1 -1
  82. package/dist/runtime/cloudflare.js +69 -79
  83. package/dist/runtime/cloudflare.js.map +1 -1
  84. package/dist/runtime/helpers.d.ts +21 -0
  85. package/dist/runtime/helpers.d.ts.map +1 -1
  86. package/dist/runtime/helpers.js +34 -0
  87. package/dist/runtime/helpers.js.map +1 -1
  88. package/dist/runtime/node.d.ts +16 -0
  89. package/dist/runtime/node.d.ts.map +1 -1
  90. package/dist/runtime/node.js +107 -23
  91. package/dist/runtime/node.js.map +1 -1
  92. package/dist/runtime/turso.d.ts +5 -1
  93. package/dist/runtime/turso.d.ts.map +1 -1
  94. package/dist/runtime/turso.js +19 -5
  95. package/dist/runtime/turso.js.map +1 -1
  96. package/dist/runtime-capabilities.d.ts +25 -0
  97. package/dist/runtime-capabilities.d.ts.map +1 -0
  98. package/dist/runtime-capabilities.js +17 -0
  99. package/dist/runtime-capabilities.js.map +1 -0
  100. package/dist/schema/builder.d.ts.map +1 -1
  101. package/dist/schema/builder.js +6 -10
  102. package/dist/schema/builder.js.map +1 -1
  103. package/dist/schema/ddl.d.ts.map +1 -1
  104. package/dist/schema/ddl.js +42 -7
  105. package/dist/schema/ddl.js.map +1 -1
  106. package/dist/schema/diff.d.ts.map +1 -1
  107. package/dist/schema/diff.js +62 -10
  108. package/dist/schema/diff.js.map +1 -1
  109. package/dist/schema/introspection.d.ts +3 -1
  110. package/dist/schema/introspection.d.ts.map +1 -1
  111. package/dist/schema/introspection.js +274 -36
  112. package/dist/schema/introspection.js.map +1 -1
  113. package/dist/seeding/runner.d.ts +5 -1
  114. package/dist/seeding/runner.d.ts.map +1 -1
  115. package/dist/seeding/runner.js +66 -1
  116. package/dist/seeding/runner.js.map +1 -1
  117. package/dist/seeding/types.d.ts +15 -1
  118. package/dist/seeding/types.d.ts.map +1 -1
  119. package/dist/seeding/types.js.map +1 -1
  120. package/dist/sql/client.d.ts +1 -1
  121. package/dist/sql/client.d.ts.map +1 -1
  122. package/dist/sql/client.js +10 -51
  123. package/dist/sql/client.js.map +1 -1
  124. package/dist/sql/fragment.d.ts.map +1 -1
  125. package/dist/sql/fragment.js +5 -1
  126. package/dist/sql/fragment.js.map +1 -1
  127. package/dist/sql/node-client.d.ts +7 -0
  128. package/dist/sql/node-client.d.ts.map +1 -0
  129. package/dist/sql/node-client.js +61 -0
  130. package/dist/sql/node-client.js.map +1 -0
  131. package/dist/sql/tag.d.ts.map +1 -1
  132. package/dist/sql/tag.js +8 -0
  133. package/dist/sql/tag.js.map +1 -1
  134. package/dist/tooling/cloudflare.d.ts +77 -0
  135. package/dist/tooling/cloudflare.d.ts.map +1 -0
  136. package/dist/tooling/cloudflare.js +193 -0
  137. package/dist/tooling/cloudflare.js.map +1 -0
  138. package/dist/tooling/generate.d.ts.map +1 -1
  139. package/dist/tooling/generate.js +5 -15
  140. package/dist/tooling/generate.js.map +1 -1
  141. package/dist/tooling/index.d.ts +6 -2
  142. package/dist/tooling/index.d.ts.map +1 -1
  143. package/dist/tooling/index.js +247 -64
  144. package/dist/tooling/index.js.map +1 -1
  145. package/dist/tooling/inspection.d.ts +40 -0
  146. package/dist/tooling/inspection.d.ts.map +1 -0
  147. package/dist/tooling/inspection.js +63 -0
  148. package/dist/tooling/inspection.js.map +1 -0
  149. package/dist/tooling/process-json.d.ts +4 -0
  150. package/dist/tooling/process-json.d.ts.map +1 -0
  151. package/dist/tooling/process-json.js +79 -0
  152. package/dist/tooling/process-json.js.map +1 -0
  153. package/dist/uuid.d.ts +2 -0
  154. package/dist/uuid.d.ts.map +1 -0
  155. package/dist/uuid.js +22 -0
  156. package/dist/uuid.js.map +1 -0
  157. package/docs/advanced.md +1 -2
  158. package/docs/comparison.md +42 -45
  159. package/docs/inspection.md +50 -0
  160. package/docs/installation.md +1 -3
  161. package/docs/introduction.md +11 -10
  162. package/docs/migrations.md +118 -5
  163. package/docs/models.md +40 -7
  164. package/docs/queries.md +53 -4
  165. package/docs/raw-queries.md +26 -6
  166. package/docs/relationships.md +17 -3
  167. package/docs/runtimes.md +130 -45
  168. package/docs/seeders.md +42 -4
  169. package/package.json +7 -5
  170. package/dist/runtime/deno.d.ts +0 -38
  171. package/dist/runtime/deno.d.ts.map +0 -1
  172. package/dist/runtime/deno.js +0 -57
  173. package/dist/runtime/deno.js.map +0 -1
package/docs/runtimes.md CHANGED
@@ -1,6 +1,6 @@
1
1
  ---
2
2
  title: Runtimes
3
- description: Configure runtime adapters, custom drivers, and multiple database connections across Node.js, Bun, Workers, and Deno.
3
+ description: Configure runtime adapters, custom drivers, and multiple database connections across Node.js, Bun, and Workers.
4
4
  ---
5
5
 
6
6
  # Runtimes
@@ -10,11 +10,10 @@ eagerly include Node built-ins, native SQLite packages, or unused drivers.
10
10
 
11
11
  | Runtime entry | Adapters |
12
12
  | --- | --- |
13
- | `@cossackframework/database/node` | Node SQLite, better-sqlite3, PostgreSQL, MySQL, Turso |
14
- | `@cossackframework/database/deno` | Turso embedded SQLite, PostgreSQL, MySQL, remote Turso, or an injected driver |
13
+ | `@cossackframework/database/node` | Node SQLite, better-sqlite3, PostgreSQL, MySQL, Turso, automatic `SQL` client |
15
14
  | `@cossackframework/database/bun` | Native Bun SQL for SQLite, PostgreSQL, MySQL |
16
15
  | `@cossackframework/database/cloudflare` | D1 and Hyperdrive PostgreSQL/MySQL |
17
- | `@cossackframework/database/deno` | Injected SQLite, PostgreSQL, MySQL-compatible drivers |
16
+ | `@cossackframework/database/cloudflare-turso` | Remote Turso for Workers |
18
17
  | `@cossackframework/database/adapter` | Public contracts for custom adapters |
19
18
 
20
19
  ## Node.js
@@ -79,11 +78,23 @@ const adapter = await mysql({
79
78
  });
80
79
  ```
81
80
 
81
+ The adapter sets the mysql2 `timezone` option to `"Z"` (UTC) unless you pass
82
+ your own `timezone`, in the options object or as a `timezone` URL parameter.
83
+ The ORM stores datetimes in UTC, so a different time zone shifts the values
84
+ that you read. With UTC, the adapter also runs `SET time_zone = '+00:00'` on
85
+ each new pool connection. MySQL fills `DEFAULT CURRENT_TIMESTAMP` and `NOW()`
86
+ in the session time zone, which is otherwise the server's zone, so this keeps
87
+ database-filled timestamps in UTC as well.
88
+
89
+ The PostgreSQL adapter reads `date` and `timestamp` (without time zone) values
90
+ as UTC. The `pg` driver otherwise reads them in the process time zone. Pass
91
+ your own `types` option to keep different parsers.
92
+
82
93
  Turso (choose the client matching the connection):
83
94
 
84
95
  ```sh
85
96
  pnpm add @tursodatabase/database # embedded SQLite / Desktop
86
- pnpm add @tursodatabase/serverless # remote Turso / Deno Deploy
97
+ pnpm add @tursodatabase/serverless # remote Turso
87
98
  ```
88
99
 
89
100
  ```ts
@@ -97,6 +108,10 @@ const adapter = await turso({
97
108
  });
98
109
  ```
99
110
 
111
+ Workers that use remote Turso must import `turso` from
112
+ `@cossackframework/database/cloudflare-turso`. Keeping that optional provider in
113
+ its own entry lets a D1-only Worker bundle without the Turso client installed.
114
+
100
115
  Node adapters install `AsyncLocalStorage` request scopes automatically.
101
116
 
102
117
  ## Bun
@@ -135,16 +150,19 @@ by that client.
135
150
  Create the adapter from a D1 binding:
136
151
 
137
152
  ```ts
153
+ import { AsyncLocalStorage } from "node:async_hooks";
138
154
  import { createORM } from "@cossackframework/database";
139
155
  import { d1 } from "@cossackframework/database/cloudflare";
156
+ import { createAsyncLocalScope } from "@cossackframework/database/adapter";
140
157
 
141
158
  interface Env {
142
159
  DB: D1Database;
143
160
  }
144
161
 
145
162
  export async function createRequestORM(env: Env) {
163
+ const adapter = await d1(env.DB);
146
164
  return createORM({
147
- adapter: await d1(env.DB),
165
+ adapter: { ...adapter, scope: createAsyncLocalScope(new AsyncLocalStorage()) },
148
166
  entities,
149
167
  });
150
168
  }
@@ -152,17 +170,73 @@ export async function createRequestORM(env: Env) {
152
170
 
153
171
  D1 compiles SQLite SQL and uses prepared bindings. It supports prepared batches
154
172
  but not interactive transactions, savepoints, connection reservation, or query
155
- cancellation. Unsupported operations throw `UnsupportedCapabilityError`; the ORM
173
+ cancellation. Unsupported operations throw `UnsupportedCapabilityError`. The ORM
156
174
  does not emulate them unsafely.
157
175
 
158
176
  Migration schema operations and migration bookkeeping are submitted in one D1
159
177
  batch. General callback-based transactions remain unsupported.
160
178
 
161
- The adapter uses async-local request scoping through `node:async_hooks`. Enable
179
+ The application factory adds async-local request scoping through `node:async_hooks`.
180
+ Generated Cloudflare applications include this scope. Enable
162
181
  the Workers compatibility support required by the deployment's compatibility
163
182
  date, such as
164
183
  [`nodejs_als`](https://developers.cloudflare.com/workers/runtime-apis/nodejs/asynclocalstorage/).
165
184
 
185
+ `ormMiddleware()` owns databases created by its request factory. Component RPC
186
+ streams retain these databases until completion, failure, or cancellation. The
187
+ framework captures the SQL scope when it creates the response, so Active Record
188
+ and global `sql` calls can run between yielded values. Ordinary responses close
189
+ their owned ORM when middleware finishes. Caller-owned ORM instances remain
190
+ the caller's responsibility.
191
+
192
+ ### D1 tooling
193
+
194
+ Keep tooling separate from the request adapter. Generated D1 applications use:
195
+
196
+ ```ts
197
+ import type { ORMToolingContext } from "@cossackframework/database";
198
+ import { createD1ToolingAdapter } from "@cossackframework/database/tooling";
199
+
200
+ export function createToolingAdapter(context: ORMToolingContext) {
201
+ return createD1ToolingAdapter(context, { binding: "DB" });
202
+ }
203
+ ```
204
+
205
+ The helper resolves Wrangler from the application and uses its configuration
206
+ parser. Local tooling disables remote bindings and shares the application's
207
+ `.wrangler/state/v3` storage used by Wrangler and the Cloudflare Vite plugin.
208
+ Remote tooling forces the selected D1 binding to use the remote resource,
209
+ requires a real `database_id`, and closes the proxy when the ORM closes. The
210
+ helper does not provision a remote database. JSON results include the selected
211
+ account ID, environment, binding and D1 identity when Wrangler provides them.
212
+ They never include credential values.
213
+
214
+ Named environments use their own binding declarations. Missing environments
215
+ fail instead of falling back to production. CLI and Studio share this binding
216
+ discovery through `readD1ToolingBindings()`. These exports are Node tooling.
217
+ Worker request code continues to import `d1()` from the Cloudflare entry.
218
+
219
+ ### Other Cloudflare tooling
220
+
221
+ Turso and Hyperdrive tooling read secrets and bindings through
222
+ `createCloudflareToolingPlatform()`. It starts Wrangler's platform proxy with
223
+ the same target, environment and local state rules as the D1 helper:
224
+
225
+ ```ts
226
+ import type { ORMToolingContext } from "@cossackframework/database";
227
+ import { createCloudflareToolingPlatform } from "@cossackframework/database/tooling";
228
+
229
+ const platform = await createCloudflareToolingPlatform<{ TURSO_DATABASE_URL?: string }>(context);
230
+ // Read platform.env, then call platform.dispose() when the adapter closes.
231
+ ```
232
+
233
+ Tooling does not run the Worker entry. Because of this, both helpers remove
234
+ Durable Objects and Workflows that the Worker defines itself (bindings without
235
+ `script_name`) before they start the proxy. Wrangler would otherwise warn about
236
+ each one. The helpers write the changed configuration to a temporary file next
237
+ to the original, so relative paths and `.dev.vars` still work. They delete the
238
+ file when the proxy starts.
239
+
166
240
  ## Cloudflare Hyperdrive
167
241
 
168
242
  PostgreSQL through Hyperdrive:
@@ -193,39 +267,21 @@ import {
193
267
  const adapter = await hyperdriveMySQL(env.HYPERDRIVE);
194
268
  ```
195
269
 
196
- Hyperdrive owns the global connection pool. The adapters create request-local
197
- clients and release them deterministically. Enable `nodejs_compat` for the
198
- official `pg` and `mysql2` drivers. See Cloudflare's
270
+ Hyperdrive owns the global connection pool. Each adapter opens one client the
271
+ first time it runs a query and uses that client for all later queries. A
272
+ transaction or `reserve()` call uses its own client and closes it when the
273
+ callback ends. `orm.close()` closes the shared client. A Worker cannot use a
274
+ socket that another request opened, so create the adapter and the ORM for each
275
+ request and close them at the end. The generated `createRequestORM()` and
276
+ `ormMiddleware()` do this. Enable `nodejs_compat` for the official `pg` and
277
+ `mysql2` drivers. See Cloudflare's
199
278
  [Hyperdrive driver guidance](https://developers.cloudflare.com/hyperdrive/).
200
279
 
201
- ## Deno
202
-
203
- Deno accepts an injected driver rather than importing a specific database
204
- library into the neutral package:
205
-
206
- ```ts
207
- import { createORM } from "@cossackframework/database";
208
- import {
209
- deno,
210
- type InjectedDenoDriver,
211
- } from "@cossackframework/database/deno";
212
-
213
- declare const driver: InjectedDenoDriver;
214
-
215
- const orm = createORM({
216
- adapter: deno(driver),
217
- entities,
218
- });
219
- ```
220
-
221
- The injected driver declares its dialect, executes parameterized statements,
222
- normalizes rows affected and insert IDs, and optionally supplies transactions.
223
- An application can also inject a runtime-appropriate `ScopeStorage`.
224
-
225
- Deno can connect through npm-compatible PostgreSQL/MySQL clients or an
226
- application-provided SQLite driver. See Deno's
227
- [database connection guide](https://docs.deno.com/examples/connecting_to_databases_tutorial/)
228
- for runtime permissions and driver choices.
280
+ Hyperdrive shares server connections between clients, so the MySQL adapter
281
+ does not change the session time zone. Set the MySQL server's `time_zone` to
282
+ UTC (the default on most managed MySQL services). Otherwise `DEFAULT
283
+ CURRENT_TIMESTAMP` values are in the server's zone and do not compare
284
+ correctly with the UTC values that the ORM writes.
229
285
 
230
286
  ## Custom Runtime
231
287
 
@@ -284,7 +340,7 @@ export const databases = {
284
340
  ```
285
341
 
286
342
  Each instance owns its adapter, model metadata, entity snapshots, transaction
287
- scope, and shutdown lifecycle. It may use a different dialect and a different
343
+ scope, and shutdown lifecycle. It can use a different dialect and a different
288
344
  entity list.
289
345
 
290
346
  When a function touches one connection, Active Record syntax remains available
@@ -312,7 +368,7 @@ const [users, events] = await Promise.all([
312
368
  ]);
313
369
  ```
314
370
 
315
- This avoids asking a static model method to infer which ORM should handle the
371
+ This avoids asking a static model method to infer which ORM handles the
316
372
  query. It also supports registering the same entity class with two connections:
317
373
 
318
374
  ```ts
@@ -326,8 +382,8 @@ const [live, archived] = await Promise.all([
326
382
  ```
327
383
 
328
384
  Do not nest Active Record scopes from different ORM instances. Both scopes
329
- would be active in the same asynchronous chain, making a static call such as
330
- `User.find()` ambiguous. Use `orm.model(Entity)`, `orm.sql`, and explicit ORM
385
+ are then active in the same asynchronous chain. A static call such as
386
+ `User.find()` becomes ambiguous. Use `orm.model(Entity)`, `orm.sql`, and explicit ORM
331
387
  methods for cross-connection work.
332
388
 
333
389
  ### Concurrent scopes
@@ -370,7 +426,7 @@ connection explicitly and combine the results in application code.
370
426
 
371
427
  ### Migrations and shutdown
372
428
 
373
- Give each database its own config so schema commands cannot target the wrong
429
+ Give each database its own configuration file so schema commands cannot target the wrong
374
430
  connection:
375
431
 
376
432
  ```text
@@ -405,7 +461,7 @@ container. Close it during graceful shutdown:
405
461
  await orm.close();
406
462
  ```
407
463
 
408
- Request-local adapters may make `close()` a no-op, but calling it remains safe
464
+ Request-local adapters can make `close()` a no-op. Calling it remains safe
409
465
  and idempotent.
410
466
 
411
467
  ## Capabilities
@@ -424,3 +480,32 @@ if (orm.driver.capabilities.transactions) {
424
480
 
425
481
  Capabilities include transactions, savepoints, returning clauses, batches,
426
482
  connection reservation, cancellation, parameter limits, and batch limits.
483
+
484
+ ## Provider capabilities
485
+
486
+ `orm.inspectCapabilities()` returns frozen driver declarations, including provider,
487
+ dialect, transaction modes, schema operations, generated values, returning support,
488
+ connection reservation, cancellation, and parameter/batch limits. These facts do
489
+ not establish remote connectivity or a server version. Custom drivers must declare
490
+ accurate limits and implement the advertised methods.
491
+
492
+ | Operation | SQLite / embedded Turso | PostgreSQL | MySQL | D1 |
493
+ | --- | --- | --- | --- | --- |
494
+ | Interactive transactions | Yes | Yes | Yes | No |
495
+ | Nested savepoints | Yes | Yes | Yes | No |
496
+ | Transactional DDL rollback | Yes | Yes | No | No |
497
+ | Generated increment key | Integer primary key | Identity column | AUTO_INCREMENT | Integer primary key |
498
+ | Explicit generated identity | No | Yes | No | No |
499
+ | Direct column type alteration | No | Yes | Yes | No |
500
+
501
+ UUID generation happens in the client. PostgreSQL and MySQL column alterations
502
+ support type changes only. Changes to defaults or constraints need explicit SQL.
503
+ SQLite type changes need a table-rebuild migration. Schema queues compile every
504
+ operation before execution, so unsupported later operations cannot publish earlier
505
+ DDL. MySQL DDL can implicitly commit and must not be described as rollback-safe.
506
+
507
+ D1 supports its driver's atomic batch facility, not interactive callbacks or
508
+ nested transactions. Remote Turso and injected drivers use their own declared
509
+ capabilities. Unsupported interactive/nested operations reject before entering the
510
+ callback. Queries over the declared parameter limit reject before driver execution.
511
+ Unsupported generated identity and native array metadata reject during ORM setup.
package/docs/seeders.md CHANGED
@@ -6,7 +6,7 @@ description: Define and run ordered, idempotent seed data with scope and transac
6
6
  # Seeders
7
7
 
8
8
  Seeders create or update application data after migrations have established the
9
- schema. Cossack ORM provides a reusable `SeederRunner`; `tsx` or another runtime
9
+ schema. Cossack ORM provides a reusable `SeederRunner`. `tsx` or another runtime
10
10
  loader only executes the TypeScript entry point.
11
11
 
12
12
  ## Define a named seeder
@@ -132,7 +132,7 @@ await runner.run({
132
132
  });
133
133
  ```
134
134
 
135
- The signal is exposed to the seeder context. A seeder should pass it to any
135
+ The signal is exposed to the seeder context. Pass it to every
136
136
  lower-level operation that supports cancellation.
137
137
 
138
138
  ## Transaction policies
@@ -141,7 +141,7 @@ Each seeder declares one of three policies:
141
141
 
142
142
  | Policy | Behavior |
143
143
  | --- | --- |
144
- | `"auto"` | Use one transaction when supported; otherwise run without one |
144
+ | `"auto"` | Use one transaction when supported, or run without one |
145
145
  | `"required"` | Fail before executing when interactive transactions are unavailable |
146
146
  | `"none"` | Never create a runner-managed transaction |
147
147
 
@@ -182,9 +182,47 @@ Avoid random identifiers for records that must not be duplicated. Keep
182
182
  production-only or destructive data policies in the application/framework CLI,
183
183
  where environment and confirmation information is available.
184
184
 
185
+ ## Preview seeders
186
+
187
+ Preview selected seeders without executing their callbacks:
188
+
189
+ ```sh
190
+ pnpm exec cossack seeder run --only users --env staging --target remote --dry-run --json
191
+ ```
192
+
193
+ The result reports selected seeders, ordered statements, affected objects,
194
+ parameter counts, risk, target identity and transaction limitations. Callbacks are
195
+ never invoked during planning. A callback seeder reports `complete: false`. An
196
+ empty SQL list does not imply that its eventual callback makes no changes.
197
+
198
+ For a complete plan, declare SQL fragments instead of a callback:
199
+
200
+ ```ts
201
+ import { defineSeeder, sql } from "@cossackframework/database";
202
+
203
+ export const settings = defineSeeder({
204
+ name: "settings",
205
+ transaction: "required",
206
+ operations: [sql.fragment`INSERT INTO settings (key, value) VALUES (${"theme"}, ${"system"})`],
207
+ });
208
+ ```
209
+
210
+ Declared `operations` are authoritative for execution as well as planning. A
211
+ `run` callback on the same definition is not executed. A required transaction on
212
+ an unsupported provider produces an incomplete plan. Statements without an
213
+ interactive transaction run individually and can leave partial changes on failure.
214
+ Parameter values stay private. Raw SQL object discovery remains incomplete.
215
+
216
+ `await runner.applyPlan(await runner.plan())` applies a plan returned by that
217
+ live runner after rechecking its target and definitions. Plans use detached
218
+ statement/parameter snapshots. A copied or deserialized JSON report is not an
219
+ executable plan. Seeders have no applied-history ledger and remain rerunnable.
220
+ Planning does not prove that data-dependent SQL is safe or idempotent.
221
+ `seed list` reads registration metadata without constructing an adapter.
222
+
185
223
  ## Framework integration
186
224
 
187
- A framework should:
225
+ A framework integration follows these steps:
188
226
 
189
227
  1. Load the application's `orm.config.ts`.
190
228
  2. Construct its normal ORM instance.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cossackframework/database",
3
- "version": "0.8.3",
3
+ "version": "0.9.1",
4
4
  "description": "Serverless-first Active Record ORM for Cossack",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -23,6 +23,7 @@
23
23
  "files": [
24
24
  "dist",
25
25
  "docs",
26
+ "agent-docs.json",
26
27
  "README.md",
27
28
  "LICENSE"
28
29
  ],
@@ -47,9 +48,9 @@
47
48
  "types": "./dist/runtime/cloudflare.d.ts",
48
49
  "import": "./dist/runtime/cloudflare.js"
49
50
  },
50
- "./deno": {
51
- "types": "./dist/runtime/deno.d.ts",
52
- "import": "./dist/runtime/deno.js"
51
+ "./cloudflare-turso": {
52
+ "types": "./dist/runtime/cloudflare-turso.d.ts",
53
+ "import": "./dist/runtime/cloudflare-turso.js"
53
54
  },
54
55
  "./cossack": {
55
56
  "types": "./dist/cossack/index.d.ts",
@@ -100,9 +101,10 @@
100
101
  "devDependencies": {
101
102
  "@types/node": "^22.19.0",
102
103
  "hono": "^4.13.7",
104
+ "miniflare": "4.20260722.0",
103
105
  "reflect-metadata": "^0.2.2",
104
106
  "typescript": "^7.0.2",
105
- "vitest": "^4.1.11"
107
+ "vitest": "^4.1.12"
106
108
  },
107
109
  "scripts": {
108
110
  "build": "tsc -p tsconfig.build.json",
@@ -1,38 +0,0 @@
1
- import type { Adapter, Driver, QueryOperation, QueryResult, ScopeStorage } from "../adapter/types.js";
2
- import type { CompiledQuery } from "../sql/fragment.js";
3
- import { type TursoEmbeddedOptions } from "./turso.js";
4
- export { turso, type TursoOptions, type TursoEmbeddedOptions, type TursoRemoteOptions } from "./turso.js";
5
- export interface InjectedDenoDriver {
6
- readonly dialect: "sqlite" | "postgres" | "mysql";
7
- execute<Row = Record<string, unknown>>(text: string, parameters: readonly unknown[]): Promise<{
8
- readonly rows: readonly Row[];
9
- readonly rowsAffected?: number;
10
- readonly lastInsertId?: string | number | bigint;
11
- }>;
12
- transaction?<T>(callback: (driver: InjectedDenoDriver) => Promise<T>): Promise<T>;
13
- close(): Promise<void>;
14
- }
15
- declare class DenoDriver implements Driver {
16
- private readonly injected;
17
- readonly dialect: "mysql" | "postgres" | "sqlite";
18
- readonly capabilities: import("../adapter/types.js").DriverCapabilities;
19
- constructor(injected: InjectedDenoDriver);
20
- execute<Row = Record<string, unknown>>(query: CompiledQuery, operation?: QueryOperation): Promise<QueryResult<Row>>;
21
- transaction<T>(callback: (driver: Driver) => Promise<T>): Promise<T>;
22
- close(): Promise<void>;
23
- }
24
- export declare function deno(driver: InjectedDenoDriver, options?: {
25
- readonly scope?: ScopeStorage<unknown>;
26
- }): Adapter;
27
- /** Local SQLite-compatible Turso Database engine for Deno and desktop. */
28
- export interface DenoSQLiteOptions {
29
- readonly filename?: string;
30
- readonly encryption?: TursoEmbeddedOptions["encryption"];
31
- }
32
- export declare function denoSQLite(options?: DenoSQLiteOptions): Promise<Adapter>;
33
- /** PostgreSQL adapter with per-request AsyncLocalStorage scoping. */
34
- export declare function postgres(options: string | Readonly<Record<string, unknown>>): Promise<Adapter>;
35
- /** MySQL adapter with per-request AsyncLocalStorage scoping. */
36
- export declare function mysql(options: string | Readonly<Record<string, unknown>>): Promise<Adapter>;
37
- export { DenoDriver };
38
- //# sourceMappingURL=deno.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"deno.d.ts","sourceRoot":"","sources":["../../src/runtime/deno.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,OAAO,EACP,MAAM,EACN,cAAc,EACd,WAAW,EACX,YAAY,EACb,MAAM,qBAAqB,CAAC;AAC7B,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC;AAMxD,OAAO,EAAS,KAAK,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAC9D,OAAO,EAAE,KAAK,EAAE,KAAK,YAAY,EAAE,KAAK,oBAAoB,EAAE,KAAK,kBAAkB,EAAE,MAAM,YAAY,CAAC;AAE1G,MAAM,WAAW,kBAAkB;IACjC,QAAQ,CAAC,OAAO,EAAE,QAAQ,GAAG,UAAU,GAAG,OAAO,CAAC;IAClD,OAAO,CAAC,GAAG,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,EACnC,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,SAAS,OAAO,EAAE,GAC7B,OAAO,CAAC;QACT,QAAQ,CAAC,IAAI,EAAE,SAAS,GAAG,EAAE,CAAC;QAC9B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;QAC/B,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAAC;KAClD,CAAC,CAAC;IACH,WAAW,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,MAAM,EAAE,kBAAkB,KAAK,OAAO,CAAC,CAAC,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;IAClF,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB;AAED,cAAM,UAAW,YAAW,MAAM;IAIpB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAHrC,QAAQ,CAAC,OAAO,kCAAC;IACjB,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
- import { postgres as nodePostgres, mysql as nodeMySQL, } from "./node.js";
3
- import { turso } from "./turso.js";
4
- export { turso } from "./turso.js";
5
- class DenoDriver {
6
- constructor(injected) {
7
- this.injected = injected;
8
- this.dialect = injected.dialect;
9
- this.capabilities = capabilities({
10
- transactions: Boolean(injected.transaction),
11
- returning: injected.dialect !== "mysql",
12
- reserveConnection: false,
13
- parameterLimit: injected.dialect === "sqlite" ? 999 : 65_535,
14
- });
15
- }
16
- async execute(query, operation = "raw") {
17
- const start = performance.now();
18
- const result = await this.injected.execute(query.text, query.parameters);
19
- return {
20
- rows: result.rows,
21
- meta: meta(this.dialect, operation, start, {
22
- rowsAffected: result.rowsAffected ?? 0,
23
- ...(result.lastInsertId === undefined ? {} : { lastInsertId: result.lastInsertId }),
24
- }),
25
- };
26
- }
27
- transaction(callback) {
28
- if (!this.injected.transaction)
29
- throw new Error("The injected Deno driver does not support transactions.");
30
- return this.injected.transaction((transaction) => callback(new DenoDriver(transaction)));
31
- }
32
- close() {
33
- return this.injected.close();
34
- }
35
- }
36
- export function deno(driver, options = {}) {
37
- return {
38
- driver: new DenoDriver(driver),
39
- ...(options.scope === undefined ? {} : { scope: options.scope }),
40
- };
41
- }
42
- export function denoSQLite(options = {}) {
43
- return turso({
44
- path: options.filename ?? ":memory:",
45
- ...(options.encryption ? { encryption: options.encryption } : {}),
46
- });
47
- }
48
- /** PostgreSQL adapter with per-request AsyncLocalStorage scoping. */
49
- export function postgres(options) {
50
- return nodePostgres(options);
51
- }
52
- /** MySQL adapter with per-request AsyncLocalStorage scoping. */
53
- export function mysql(options) {
54
- return nodeMySQL(options);
55
- }
56
- export { DenoDriver };
57
- //# sourceMappingURL=deno.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"deno.js","sourceRoot":"","sources":["../../src/runtime/deno.ts"],"names":[],"mappings":"AAQA,OAAO,EAAE,YAAY,EAAE,IAAI,EAAE,MAAM,cAAc,CAAC;AAClD,OAAO,EACL,QAAQ,IAAI,YAAY,E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