@cossackframework/database 0.9.0 → 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 (101) hide show
  1. package/README.md +17 -17
  2. package/dist/codec.d.ts +7 -0
  3. package/dist/codec.d.ts.map +1 -1
  4. package/dist/codec.js +31 -2
  5. package/dist/codec.js.map +1 -1
  6. package/dist/dialect/dialects.d.ts.map +1 -1
  7. package/dist/dialect/dialects.js +6 -0
  8. package/dist/dialect/dialects.js.map +1 -1
  9. package/dist/entity/base-entity.d.ts +22 -6
  10. package/dist/entity/base-entity.d.ts.map +1 -1
  11. package/dist/entity/base-entity.js +37 -8
  12. package/dist/entity/base-entity.js.map +1 -1
  13. package/dist/entity/manager.d.ts +22 -5
  14. package/dist/entity/manager.d.ts.map +1 -1
  15. package/dist/entity/manager.js +89 -4
  16. package/dist/entity/manager.js.map +1 -1
  17. package/dist/index.d.ts +3 -3
  18. package/dist/index.d.ts.map +1 -1
  19. package/dist/index.js +2 -2
  20. package/dist/index.js.map +1 -1
  21. package/dist/metadata/finalize.d.ts.map +1 -1
  22. package/dist/metadata/finalize.js +5 -0
  23. package/dist/metadata/finalize.js.map +1 -1
  24. package/dist/migration/checksum.d.ts +18 -0
  25. package/dist/migration/checksum.d.ts.map +1 -0
  26. package/dist/migration/checksum.js +164 -0
  27. package/dist/migration/checksum.js.map +1 -0
  28. package/dist/migration/runner.d.ts +5 -1
  29. package/dist/migration/runner.d.ts.map +1 -1
  30. package/dist/migration/runner.js +45 -16
  31. package/dist/migration/runner.js.map +1 -1
  32. package/dist/migration/types.d.ts +5 -0
  33. package/dist/migration/types.d.ts.map +1 -1
  34. package/dist/orm.d.ts +17 -2
  35. package/dist/orm.d.ts.map +1 -1
  36. package/dist/orm.js +137 -39
  37. package/dist/orm.js.map +1 -1
  38. package/dist/query/builder.d.ts +30 -2
  39. package/dist/query/builder.d.ts.map +1 -1
  40. package/dist/query/builder.js +138 -12
  41. package/dist/query/builder.js.map +1 -1
  42. package/dist/query/expression.d.ts +2 -0
  43. package/dist/query/expression.d.ts.map +1 -1
  44. package/dist/query/expression.js +16 -1
  45. package/dist/query/expression.js.map +1 -1
  46. package/dist/query/types.d.ts +17 -2
  47. package/dist/query/types.d.ts.map +1 -1
  48. package/dist/query/types.js +3 -0
  49. package/dist/query/types.js.map +1 -1
  50. package/dist/runtime/cloudflare.d.ts +45 -1
  51. package/dist/runtime/cloudflare.d.ts.map +1 -1
  52. package/dist/runtime/cloudflare.js +66 -35
  53. package/dist/runtime/cloudflare.js.map +1 -1
  54. package/dist/runtime/helpers.d.ts +21 -0
  55. package/dist/runtime/helpers.d.ts.map +1 -1
  56. package/dist/runtime/helpers.js +34 -0
  57. package/dist/runtime/helpers.js.map +1 -1
  58. package/dist/runtime/node.d.ts +14 -0
  59. package/dist/runtime/node.d.ts.map +1 -1
  60. package/dist/runtime/node.js +105 -26
  61. package/dist/runtime/node.js.map +1 -1
  62. package/dist/schema/ddl.d.ts.map +1 -1
  63. package/dist/schema/ddl.js +13 -2
  64. package/dist/schema/ddl.js.map +1 -1
  65. package/dist/schema/diff.d.ts.map +1 -1
  66. package/dist/schema/diff.js +62 -10
  67. package/dist/schema/diff.js.map +1 -1
  68. package/dist/schema/introspection.d.ts.map +1 -1
  69. package/dist/schema/introspection.js +266 -30
  70. package/dist/schema/introspection.js.map +1 -1
  71. package/dist/sql/fragment.d.ts.map +1 -1
  72. package/dist/sql/fragment.js +5 -1
  73. package/dist/sql/fragment.js.map +1 -1
  74. package/dist/sql/tag.d.ts.map +1 -1
  75. package/dist/sql/tag.js +8 -0
  76. package/dist/sql/tag.js.map +1 -1
  77. package/dist/tooling/cloudflare.d.ts +19 -3
  78. package/dist/tooling/cloudflare.d.ts.map +1 -1
  79. package/dist/tooling/cloudflare.js +77 -14
  80. package/dist/tooling/cloudflare.js.map +1 -1
  81. package/dist/tooling/index.d.ts +1 -1
  82. package/dist/tooling/index.d.ts.map +1 -1
  83. package/dist/tooling/index.js +20 -4
  84. package/dist/tooling/index.js.map +1 -1
  85. package/dist/uuid.d.ts +2 -0
  86. package/dist/uuid.d.ts.map +1 -0
  87. package/dist/uuid.js +22 -0
  88. package/dist/uuid.js.map +1 -0
  89. package/docs/advanced.md +1 -2
  90. package/docs/comparison.md +40 -43
  91. package/docs/inspection.md +5 -5
  92. package/docs/installation.md +1 -1
  93. package/docs/introduction.md +10 -9
  94. package/docs/migrations.md +43 -19
  95. package/docs/models.md +40 -7
  96. package/docs/queries.md +36 -5
  97. package/docs/raw-queries.md +9 -4
  98. package/docs/relationships.md +6 -3
  99. package/docs/runtimes.md +59 -15
  100. package/docs/seeders.md +9 -9
  101. package/package.json +3 -2
@@ -49,7 +49,7 @@ await orm.sql`
49
49
  `;
50
50
  ```
51
51
 
52
- `input` is data, never executable SQL. PostgreSQL compiles it to `$1`; SQLite
52
+ `input` is data, never executable SQL. PostgreSQL compiles it to `$1`. SQLite
53
53
  and MySQL use their corresponding placeholders.
54
54
 
55
55
  Do not interpolate table or column names as strings. Use `sql.id()`:
@@ -102,6 +102,11 @@ const result = await orm.sql`
102
102
  `;
103
103
  ```
104
104
 
105
+ An empty list throws a `ConfigurationError`, both in `sql.join([])` and when
106
+ you interpolate an empty array directly. Otherwise the query would compile to
107
+ `IN ()`, which is invalid SQL. Check for an empty list before you build the
108
+ query.
109
+
105
110
  Use `values()` for object or bulk inserts:
106
111
 
107
112
  ```ts
@@ -116,7 +121,7 @@ await orm.sql`
116
121
  ```
117
122
 
118
123
  Every row passed to `values()` must have the same columns in the same order.
119
- Large application writes should respect `orm.driver.capabilities.parameterLimit`
124
+ Large application writes must respect `orm.driver.capabilities.parameterLimit`
120
125
  and `batchLimit`.
121
126
 
122
127
  ## Unsafe SQL
@@ -173,7 +178,7 @@ try {
173
178
 
174
179
  In Node.js, URL or path inference supports PostgreSQL, MySQL, Turso HTTPS URLs, and
175
180
  SQLite. Bun delegates to native Bun SQL. The package root's `SQL` export accepts
176
- explicit adapters and remains runtime neutral. Workers should use an
181
+ explicit adapters and remains runtime neutral. On Workers, use an
177
182
  explicit adapter rather than URL guessing:
178
183
 
179
184
  ```ts
@@ -187,7 +192,7 @@ See [Runtimes](./runtimes.md) for adapter setup and
187
192
 
188
193
  The ORM logger receives `sql`, parameter type markers, `durationMs`, `dialect`,
189
194
  `provider`, `operation`, and `outcome`. A failed query includes only the fixed
190
- code `COSSACK_DATABASE_QUERY_FAILED`; the event has no error object or message.
195
+ code `COSSACK_DATABASE_QUERY_FAILED`. The event has no error object or message.
191
196
  The SQL field is a bounded structural representation: literals and comments are
192
197
  removed, and quoted identifiers are anonymous because some providers interpret
193
198
  those tokens as values. It is diagnostic output, not executable SQL.
@@ -137,6 +137,9 @@ class User extends BaseEntity {
137
137
  }
138
138
  ```
139
139
 
140
+ Load either side with `relations`. `User.find({ relations: ["profile"] })` sets
141
+ `profile` to the matching profile, or `null` when there is none.
142
+
140
143
  ## Many-to-many
141
144
 
142
145
  Put `@JoinTable()` on exactly one side:
@@ -199,7 +202,7 @@ does not walk an in-memory object graph and delete related entities.
199
202
 
200
203
  ## Nullable and logical relations
201
204
 
202
- Use `nullable` to describe whether an owning relation may be absent:
205
+ Use `nullable` to describe whether an owning relation can be absent:
203
206
 
204
207
  ```ts
205
208
  @ManyToOne(() => Team, (team) => team.members, {
@@ -209,8 +212,8 @@ Use `nullable` to describe whether an owning relation may be absent:
209
212
  declare team: Relation<Team | null>;
210
213
  ```
211
214
 
212
- Set `createForeignKeyConstraints: false` for a logical relation that should
213
- appear in ORM metadata without creating a database foreign-key constraint:
215
+ Set `createForeignKeyConstraints: false` for a logical relation that appears
216
+ in ORM metadata without creating a database foreign-key constraint:
214
217
 
215
218
  ```ts
216
219
  @ManyToOne(() => ExternalAccount, {
package/docs/runtimes.md CHANGED
@@ -78,6 +78,18 @@ const adapter = await mysql({
78
78
  });
79
79
  ```
80
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
+
81
93
  Turso (choose the client matching the connection):
82
94
 
83
95
  ```sh
@@ -96,7 +108,7 @@ const adapter = await turso({
96
108
  });
97
109
  ```
98
110
 
99
- Workers that use remote Turso should import `turso` from
111
+ Workers that use remote Turso must import `turso` from
100
112
  `@cossackframework/database/cloudflare-turso`. Keeping that optional provider in
101
113
  its own entry lets a D1-only Worker bundle without the Turso client installed.
102
114
 
@@ -158,7 +170,7 @@ export async function createRequestORM(env: Env) {
158
170
 
159
171
  D1 compiles SQLite SQL and uses prepared bindings. It supports prepared batches
160
172
  but not interactive transactions, savepoints, connection reservation, or query
161
- cancellation. Unsupported operations throw `UnsupportedCapabilityError`; the ORM
173
+ cancellation. Unsupported operations throw `UnsupportedCapabilityError`. The ORM
162
174
  does not emulate them unsafely.
163
175
 
164
176
  Migration schema operations and migration bookkeeping are submitted in one D1
@@ -196,14 +208,35 @@ parser. Local tooling disables remote bindings and shares the application's
196
208
  Remote tooling forces the selected D1 binding to use the remote resource,
197
209
  requires a real `database_id`, and closes the proxy when the ORM closes. The
198
210
  helper does not provision a remote database. JSON results include the selected
199
- account ID, environment, binding and D1 identity when Wrangler provides them;
200
- they never include credential values.
211
+ account ID, environment, binding and D1 identity when Wrangler provides them.
212
+ They never include credential values.
201
213
 
202
214
  Named environments use their own binding declarations. Missing environments
203
215
  fail instead of falling back to production. CLI and Studio share this binding
204
- discovery through `readD1ToolingBindings()`. These exports are Node tooling;
216
+ discovery through `readD1ToolingBindings()`. These exports are Node tooling.
205
217
  Worker request code continues to import `d1()` from the Cloudflare entry.
206
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
+
207
240
  ## Cloudflare Hyperdrive
208
241
 
209
242
  PostgreSQL through Hyperdrive:
@@ -234,11 +267,22 @@ import {
234
267
  const adapter = await hyperdriveMySQL(env.HYPERDRIVE);
235
268
  ```
236
269
 
237
- Hyperdrive owns the global connection pool. The adapters create request-local
238
- clients and release them deterministically. Enable `nodejs_compat` for the
239
- 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
240
278
  [Hyperdrive driver guidance](https://developers.cloudflare.com/hyperdrive/).
241
279
 
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.
285
+
242
286
  ## Custom Runtime
243
287
 
244
288
  Cossack ORM does not require runtime support to live in this package. A custom
@@ -296,7 +340,7 @@ export const databases = {
296
340
  ```
297
341
 
298
342
  Each instance owns its adapter, model metadata, entity snapshots, transaction
299
- 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
300
344
  entity list.
301
345
 
302
346
  When a function touches one connection, Active Record syntax remains available
@@ -324,7 +368,7 @@ const [users, events] = await Promise.all([
324
368
  ]);
325
369
  ```
326
370
 
327
- 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
328
372
  query. It also supports registering the same entity class with two connections:
329
373
 
330
374
  ```ts
@@ -338,8 +382,8 @@ const [live, archived] = await Promise.all([
338
382
  ```
339
383
 
340
384
  Do not nest Active Record scopes from different ORM instances. Both scopes
341
- would be active in the same asynchronous chain, making a static call such as
342
- `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
343
387
  methods for cross-connection work.
344
388
 
345
389
  ### Concurrent scopes
@@ -382,7 +426,7 @@ connection explicitly and combine the results in application code.
382
426
 
383
427
  ### Migrations and shutdown
384
428
 
385
- 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
386
430
  connection:
387
431
 
388
432
  ```text
@@ -417,7 +461,7 @@ container. Close it during graceful shutdown:
417
461
  await orm.close();
418
462
  ```
419
463
 
420
- 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
421
465
  and idempotent.
422
466
 
423
467
  ## Capabilities
@@ -455,7 +499,7 @@ accurate limits and implement the advertised methods.
455
499
  | Direct column type alteration | No | Yes | Yes | No |
456
500
 
457
501
  UUID generation happens in the client. PostgreSQL and MySQL column alterations
458
- support type changes only; changes to defaults or constraints need explicit SQL.
502
+ support type changes only. Changes to defaults or constraints need explicit SQL.
459
503
  SQLite type changes need a table-rebuild migration. Schema queues compile every
460
504
  operation before execution, so unsupported later operations cannot publish earlier
461
505
  DDL. MySQL DDL can implicitly commit and must not be described as rollback-safe.
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
 
@@ -192,7 +192,7 @@ pnpm exec cossack seeder run --only users --env staging --target remote --dry-ru
192
192
 
193
193
  The result reports selected seeders, ordered statements, affected objects,
194
194
  parameter counts, risk, target identity and transaction limitations. Callbacks are
195
- never invoked during planning. A callback seeder reports `complete: false`; an
195
+ never invoked during planning. A callback seeder reports `complete: false`. An
196
196
  empty SQL list does not imply that its eventual callback makes no changes.
197
197
 
198
198
  For a complete plan, declare SQL fragments instead of a callback:
@@ -207,22 +207,22 @@ export const settings = defineSeeder({
207
207
  });
208
208
  ```
209
209
 
210
- Declared `operations` are authoritative for execution as well as planning; a
210
+ Declared `operations` are authoritative for execution as well as planning. A
211
211
  `run` callback on the same definition is not executed. A required transaction on
212
212
  an unsupported provider produces an incomplete plan. Statements without an
213
213
  interactive transaction run individually and can leave partial changes on failure.
214
- Parameter values stay private; raw SQL object discovery remains incomplete.
214
+ Parameter values stay private. Raw SQL object discovery remains incomplete.
215
215
 
216
216
  `await runner.applyPlan(await runner.plan())` applies a plan returned by that
217
217
  live runner after rechecking its target and definitions. Plans use detached
218
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.
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
221
  `seed list` reads registration metadata without constructing an adapter.
222
222
 
223
223
  ## Framework integration
224
224
 
225
- A framework should:
225
+ A framework integration follows these steps:
226
226
 
227
227
  1. Load the application's `orm.config.ts`.
228
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.9.0",
3
+ "version": "0.9.1",
4
4
  "description": "Serverless-first Active Record ORM for Cossack",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -101,9 +101,10 @@
101
101
  "devDependencies": {
102
102
  "@types/node": "^22.19.0",
103
103
  "hono": "^4.13.7",
104
+ "miniflare": "4.20260722.0",
104
105
  "reflect-metadata": "^0.2.2",
105
106
  "typescript": "^7.0.2",
106
- "vitest": "^4.1.11"
107
+ "vitest": "^4.1.12"
107
108
  },
108
109
  "scripts": {
109
110
  "build": "tsc -p tsconfig.build.json",