@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.
- package/README.md +17 -17
- package/dist/codec.d.ts +7 -0
- package/dist/codec.d.ts.map +1 -1
- package/dist/codec.js +31 -2
- package/dist/codec.js.map +1 -1
- package/dist/dialect/dialects.d.ts.map +1 -1
- package/dist/dialect/dialects.js +6 -0
- package/dist/dialect/dialects.js.map +1 -1
- package/dist/entity/base-entity.d.ts +22 -6
- package/dist/entity/base-entity.d.ts.map +1 -1
- package/dist/entity/base-entity.js +37 -8
- package/dist/entity/base-entity.js.map +1 -1
- package/dist/entity/manager.d.ts +22 -5
- package/dist/entity/manager.d.ts.map +1 -1
- package/dist/entity/manager.js +89 -4
- package/dist/entity/manager.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/metadata/finalize.d.ts.map +1 -1
- package/dist/metadata/finalize.js +5 -0
- package/dist/metadata/finalize.js.map +1 -1
- package/dist/migration/checksum.d.ts +18 -0
- package/dist/migration/checksum.d.ts.map +1 -0
- package/dist/migration/checksum.js +164 -0
- package/dist/migration/checksum.js.map +1 -0
- package/dist/migration/runner.d.ts +5 -1
- package/dist/migration/runner.d.ts.map +1 -1
- package/dist/migration/runner.js +45 -16
- package/dist/migration/runner.js.map +1 -1
- package/dist/migration/types.d.ts +5 -0
- package/dist/migration/types.d.ts.map +1 -1
- package/dist/orm.d.ts +17 -2
- package/dist/orm.d.ts.map +1 -1
- package/dist/orm.js +137 -39
- package/dist/orm.js.map +1 -1
- package/dist/query/builder.d.ts +30 -2
- package/dist/query/builder.d.ts.map +1 -1
- package/dist/query/builder.js +138 -12
- package/dist/query/builder.js.map +1 -1
- package/dist/query/expression.d.ts +2 -0
- package/dist/query/expression.d.ts.map +1 -1
- package/dist/query/expression.js +16 -1
- package/dist/query/expression.js.map +1 -1
- package/dist/query/types.d.ts +17 -2
- package/dist/query/types.d.ts.map +1 -1
- package/dist/query/types.js +3 -0
- package/dist/query/types.js.map +1 -1
- package/dist/runtime/cloudflare.d.ts +45 -1
- package/dist/runtime/cloudflare.d.ts.map +1 -1
- package/dist/runtime/cloudflare.js +66 -35
- package/dist/runtime/cloudflare.js.map +1 -1
- package/dist/runtime/helpers.d.ts +21 -0
- package/dist/runtime/helpers.d.ts.map +1 -1
- package/dist/runtime/helpers.js +34 -0
- package/dist/runtime/helpers.js.map +1 -1
- package/dist/runtime/node.d.ts +14 -0
- package/dist/runtime/node.d.ts.map +1 -1
- package/dist/runtime/node.js +105 -26
- package/dist/runtime/node.js.map +1 -1
- package/dist/schema/ddl.d.ts.map +1 -1
- package/dist/schema/ddl.js +13 -2
- package/dist/schema/ddl.js.map +1 -1
- package/dist/schema/diff.d.ts.map +1 -1
- package/dist/schema/diff.js +62 -10
- package/dist/schema/diff.js.map +1 -1
- package/dist/schema/introspection.d.ts.map +1 -1
- package/dist/schema/introspection.js +266 -30
- package/dist/schema/introspection.js.map +1 -1
- package/dist/sql/fragment.d.ts.map +1 -1
- package/dist/sql/fragment.js +5 -1
- package/dist/sql/fragment.js.map +1 -1
- package/dist/sql/tag.d.ts.map +1 -1
- package/dist/sql/tag.js +8 -0
- package/dist/sql/tag.js.map +1 -1
- package/dist/tooling/cloudflare.d.ts +19 -3
- package/dist/tooling/cloudflare.d.ts.map +1 -1
- package/dist/tooling/cloudflare.js +77 -14
- package/dist/tooling/cloudflare.js.map +1 -1
- package/dist/tooling/index.d.ts +1 -1
- package/dist/tooling/index.d.ts.map +1 -1
- package/dist/tooling/index.js +20 -4
- package/dist/tooling/index.js.map +1 -1
- package/dist/uuid.d.ts +2 -0
- package/dist/uuid.d.ts.map +1 -0
- package/dist/uuid.js +22 -0
- package/dist/uuid.js.map +1 -0
- package/docs/advanced.md +1 -2
- package/docs/comparison.md +40 -43
- package/docs/inspection.md +5 -5
- package/docs/installation.md +1 -1
- package/docs/introduction.md +10 -9
- package/docs/migrations.md +43 -19
- package/docs/models.md +40 -7
- package/docs/queries.md +36 -5
- package/docs/raw-queries.md +9 -4
- package/docs/relationships.md +6 -3
- package/docs/runtimes.md +59 -15
- package/docs/seeders.md +9 -9
- package/package.json +3 -2
package/docs/comparison.md
CHANGED
|
@@ -5,19 +5,17 @@ description: Compare Cossack ORM with Drizzle ORM, TypeORM, Kysely, and Prisma a
|
|
|
5
5
|
|
|
6
6
|
# ORM Comparison
|
|
7
7
|
|
|
8
|
-
Cossack ORM
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
8
|
+
Cossack ORM does not replace every TypeScript database library. It is a focused
|
|
9
|
+
choice for applications that want decorated Active Record models, explicit
|
|
10
|
+
relation loading, safe SQL, and runtime-specific adapters, especially Cossack
|
|
11
|
+
applications.
|
|
12
12
|
|
|
13
13
|
This page compares architecture and currently implemented features, not
|
|
14
|
-
benchmarks. “Yes” means the feature is part of the project's supported API
|
|
14
|
+
benchmarks. “Yes” means the feature is part of the project's supported API. It
|
|
15
15
|
does not imply that every implementation has equal depth. “Manual” means the
|
|
16
16
|
result is possible with SQL or application code but is not modeled as a
|
|
17
17
|
first-class ORM feature.
|
|
18
18
|
|
|
19
|
-
Last reviewed: July 2026.
|
|
20
|
-
|
|
21
19
|
## Feature matrix
|
|
22
20
|
|
|
23
21
|
| Area | Cossack ORM | Drizzle ORM | TypeORM | Kysely | Prisma ORM |
|
|
@@ -26,23 +24,23 @@ Last reviewed: July 2026.
|
|
|
26
24
|
| Active Record methods | Yes | No | Yes | No | No |
|
|
27
25
|
| Repository/Data Mapper API | No, by design | No repository layer | Yes | No | Generated client, not a classic repository |
|
|
28
26
|
| Code generation required for queries | No | No | No | No | Yes |
|
|
29
|
-
| Typed SQL-style query builder | Yes, with some less precise complex join/projection types | Yes
|
|
27
|
+
| Typed SQL-style query builder | Yes, with some less precise complex join/projection types | Yes, a primary interface | Yes, an entity-aware QueryBuilder | Yes, the primary focus | No general SQL builder. The client API plus raw/TypedSQL |
|
|
30
28
|
| Relation metadata | Decorators and a serializable schema graph | TypeScript relation declarations | Decorators with extensive relation options | None in core | Prisma schema relations |
|
|
31
|
-
| Relation loading | Explicit `relations`/`with
|
|
29
|
+
| Relation loading | Explicit `relations`/`with` and batched follow-up queries | Relational query API, normally one SQL statement | Explicit, eager, or lazy | Manual joins or application code | `include`/`select` and nested queries |
|
|
32
30
|
| Entity instances, hooks, and dirty tracking | Yes | No | Yes | No | Returns plain objects |
|
|
33
31
|
| Parameterized raw SQL | Fragment AST and tagged template | Tagged SQL templates | `sql` tag and lower-level query APIs | Tagged SQL templates | `$queryRaw`/`$executeRaw` and TypedSQL |
|
|
34
|
-
| Generated migrations | Yes
|
|
32
|
+
| Generated migrations | Yes, model-snapshot diffs with reviewable TypeScript and generated inverses | Yes, through Drizzle Kit | Yes | Migration primitives, generation is not the core focus | Yes, through Prisma Migrate |
|
|
35
33
|
| Migration baseline | Built in | Possible through migration workflow | Manual migration-history workflow | Application-managed | Built in |
|
|
36
|
-
| Database introspection | SQLite, PostgreSQL, and MySQL adapters | Yes | Used for schema comparison
|
|
34
|
+
| Database introspection | SQLite, PostgreSQL, and MySQL adapters | Yes | Used for schema comparison, with no first-party entity pull | Types commonly generated by external `kysely-codegen` | `db pull` |
|
|
37
35
|
| Model generation from a database | Decorated classes through `schema pull` | TypeScript schema through `pull` | Not built in | External code generator | Prisma schema through `db pull` |
|
|
38
36
|
| Runtime schema push/synchronization | No, by design | `push` available | `synchronize`/`schema:sync` available | No | `db push` available |
|
|
39
37
|
| First-party seeding support | Ordered `SeederRunner` with transaction policy | `drizzle-seed` data generator | Application or community tooling | Application code | CLI seed command runs an application script |
|
|
40
|
-
| SQL database coverage | SQLite, PostgreSQL, MySQL
|
|
38
|
+
| SQL database coverage | SQLite, PostgreSQL, MySQL, plus D1 and current Turso adapters | Broad PostgreSQL, MySQL, SQLite, SingleStore, MSSQL, and CockroachDB ecosystem | Broad SQL driver set | Official PostgreSQL, MySQL, MSSQL, SQLite, and PGlite dialects | PostgreSQL, MySQL/MariaDB, SQLite, SQL Server, and CockroachDB |
|
|
41
39
|
| MongoDB/NoSQL | No | No | MongoDB support | No | MongoDB support |
|
|
42
|
-
| Edge/serverless focus | First-class Bun, D1, and Hyperdrive entry points | Strong, with many serverless drivers | Driver-dependent | Runtime-neutral
|
|
40
|
+
| Edge/serverless focus | First-class Bun, D1, and Hyperdrive entry points | Strong, with many serverless drivers | Driver-dependent | Runtime-neutral, dialect/driver-dependent | Supported through driver adapters and supported deployments |
|
|
43
41
|
| Multiple connections | Multiple isolated `ORM` instances | Multiple database instances | Multiple `DataSource` instances | Multiple `Kysely` instances | Separate schemas/generated clients |
|
|
44
|
-
| Cross-database relations or transactions | No | No general cross-database unit of work | Limited multi-database querying
|
|
45
|
-
| Maturity and ecosystem | New
|
|
42
|
+
| Cross-database relations or transactions | No | No general cross-database unit of work | Limited multi-database querying, with no general distributed transaction | No | No general distributed transaction |
|
|
43
|
+
| Maturity and ecosystem | New package | Established ecosystem | Long-established, feature-rich ORM | Established and deliberately focused | Long-established ecosystem and tooling |
|
|
46
44
|
|
|
47
45
|
Database support in this table describes first-party, documented paths rather
|
|
48
46
|
than every community adapter. Capabilities still vary by driver. For example,
|
|
@@ -53,20 +51,20 @@ transactions or savepoints.
|
|
|
53
51
|
|
|
54
52
|
Choose Cossack ORM when most of these are true:
|
|
55
53
|
|
|
56
|
-
- Entity classes
|
|
54
|
+
- Entity classes own persistence methods such as `find()`, `save()`, and
|
|
57
55
|
`remove()`.
|
|
58
56
|
- Decorators are an acceptable source of schema metadata.
|
|
59
|
-
- Request-scoped Active Record calls
|
|
60
|
-
|
|
61
|
-
- Relations
|
|
62
|
-
|
|
57
|
+
- Request-scoped Active Record calls participate in the current transaction
|
|
58
|
+
automatically.
|
|
59
|
+
- Relations are requested explicitly and load in batches, not behind lazy
|
|
60
|
+
properties.
|
|
63
61
|
- The same model layer must run through isolated Node, Bun, or Cloudflare
|
|
64
62
|
adapters.
|
|
65
63
|
- Cossack Studio or other framework tooling needs serializable logical schema
|
|
66
64
|
metadata, including virtual relations.
|
|
67
65
|
|
|
68
|
-
Do not choose it
|
|
69
|
-
are stronger in several
|
|
66
|
+
Do not choose it only because it has a familiar API. The alternatives below
|
|
67
|
+
are stronger in several areas.
|
|
70
68
|
|
|
71
69
|
## Compared with Drizzle ORM
|
|
72
70
|
|
|
@@ -76,14 +74,13 @@ a larger ecosystem, relational queries designed to compile to one SQL
|
|
|
76
74
|
statement, Drizzle Kit, Drizzle Studio, and a dedicated data-generation package.
|
|
77
75
|
|
|
78
76
|
Cossack adds stateful entity instances, decorators, lifecycle hooks, dirty
|
|
79
|
-
tracking, and Active Record methods. Its relation loader
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
complete.
|
|
77
|
+
tracking, and Active Record methods. Its relation loader uses batched follow-up
|
|
78
|
+
queries. This design is easier to reason about across adapters, but it adds
|
|
79
|
+
round trips compared with the relational query API from Drizzle. For complex
|
|
80
|
+
joins and calculated projections, Drizzle's types are currently more complete.
|
|
84
81
|
|
|
85
82
|
Choose Drizzle for a SQL-first, schema-object workflow. Choose Cossack when the
|
|
86
|
-
entity class and its behavior
|
|
83
|
+
entity class and its behavior are the center of the application model.
|
|
87
84
|
|
|
88
85
|
See Drizzle's official [overview](https://orm.drizzle.team/docs/overview),
|
|
89
86
|
[migration workflows](https://orm.drizzle.team/docs/migrations), and
|
|
@@ -103,9 +100,9 @@ return, it has an ESM-only package shape, explicit request/transaction scoping,
|
|
|
103
100
|
isolated runtime adapters, explicit batched relation loading, and a versioned
|
|
104
101
|
logical schema contract intended for framework tooling.
|
|
105
102
|
|
|
106
|
-
Choose TypeORM when feature breadth, database breadth, or Data Mapper
|
|
107
|
-
|
|
108
|
-
serverless/runtime boundaries
|
|
103
|
+
Choose TypeORM when you need feature breadth, database breadth, or Data Mapper.
|
|
104
|
+
Choose Cossack when its smaller Active Record-only model and
|
|
105
|
+
serverless/runtime boundaries fit the application.
|
|
109
106
|
|
|
110
107
|
See TypeORM's official [Active Record and Data Mapper
|
|
111
108
|
guide](https://typeorm.io/docs/guides/active-record-data-mapper/),
|
|
@@ -116,7 +113,7 @@ guide](https://typeorm.io/docs/guides/active-record-data-mapper/),
|
|
|
116
113
|
## Compared with Kysely
|
|
117
114
|
|
|
118
115
|
Kysely is a query builder, not an entity ORM. That is its advantage when a team
|
|
119
|
-
wants a thin, SQL-shaped abstraction with
|
|
116
|
+
wants a thin, SQL-shaped abstraction with composability and
|
|
120
117
|
compile-time result inference. It does not impose model instances, decorators,
|
|
121
118
|
relation loaders, dirty tracking, or lifecycle behavior.
|
|
122
119
|
|
|
@@ -127,8 +124,8 @@ tooling layers: Active Record, relation metadata, codecs, migrations,
|
|
|
127
124
|
introspection, generated models, and seed orchestration.
|
|
128
125
|
|
|
129
126
|
Choose Kysely when SQL and query typing are the product boundary and the
|
|
130
|
-
application
|
|
131
|
-
conventions
|
|
127
|
+
application owns its model conventions. Choose Cossack when those
|
|
128
|
+
conventions come from the ORM.
|
|
132
129
|
|
|
133
130
|
See the official [Kysely overview](https://www.kysely.dev/), which describes
|
|
134
131
|
its SQL-first scope, dialects, runtimes, migrations, and external code-generation
|
|
@@ -138,20 +135,20 @@ workflow.
|
|
|
138
135
|
|
|
139
136
|
Prisma provides a mature schema language, generated type-safe client,
|
|
140
137
|
introspection, migration workflow, Studio, and broad ecosystem. Its records are
|
|
141
|
-
plain objects queried through `PrismaClient
|
|
138
|
+
plain objects queried through `PrismaClient`. They are not Active Record
|
|
142
139
|
instances. The generated client and Prisma schema are intentional parts of its
|
|
143
140
|
workflow.
|
|
144
141
|
|
|
145
142
|
Cossack keeps the TypeScript entity class as the model source and does not
|
|
146
143
|
require client generation. It offers direct adapters, scoped static methods,
|
|
147
144
|
and a SQL-style builder, but it has fewer supported databases, less polished
|
|
148
|
-
tooling, and far less production history. Prisma
|
|
149
|
-
schema/client workflow is acceptable and
|
|
145
|
+
tooling, and far less production history. Prisma fits when its
|
|
146
|
+
schema/client workflow is acceptable and established tooling matters more than
|
|
150
147
|
Active Record.
|
|
151
148
|
|
|
152
149
|
Choose Prisma for its generated-client workflow and established tooling. Choose
|
|
153
150
|
Cossack when decorated entity behavior, direct runtime adapters, and Cossack
|
|
154
|
-
framework integration
|
|
151
|
+
framework integration matter more.
|
|
155
152
|
|
|
156
153
|
See Prisma's official [ORM overview](https://docs.prisma.io/docs/orm),
|
|
157
154
|
[supported databases](https://docs.prisma.io/docs/orm/reference/supported-databases),
|
|
@@ -159,22 +156,22 @@ and [Prisma Migrate guide](https://www.prisma.io/docs/orm/prisma-migrate/getting
|
|
|
159
156
|
|
|
160
157
|
## Current Cossack limitations
|
|
161
158
|
|
|
162
|
-
The
|
|
159
|
+
The largest differences are not syntax:
|
|
163
160
|
|
|
164
161
|
- Cossack ORM is new. It does not yet have the production history, community
|
|
165
162
|
integrations, or troubleshooting corpus of these alternatives.
|
|
166
163
|
- Its first-party database surface is intentionally smaller. SQL Server,
|
|
167
164
|
CockroachDB-specific behavior, Oracle, MongoDB, and many specialized drivers
|
|
168
|
-
are
|
|
165
|
+
are not supported.
|
|
169
166
|
- Query types are strong for model properties and common operations, but
|
|
170
167
|
complex raw joins, aliases, and computed projections can require explicit
|
|
171
168
|
typing. Kysely and Drizzle are stronger for this work.
|
|
172
169
|
- Relation loading is explicit and batched, not lazy and not guaranteed to use
|
|
173
|
-
one statement. TypeORM and Prisma support more relation behaviors
|
|
170
|
+
one statement. TypeORM and Prisma support more relation behaviors. Drizzle
|
|
174
171
|
can produce a single relational statement.
|
|
175
172
|
- Generated migrations require review. Ordinary model-diff migrations include
|
|
176
|
-
generated `down()` operations where the schema operation is reversible
|
|
177
|
-
|
|
173
|
+
generated `down()` operations where the schema operation is reversible.
|
|
174
|
+
Squashed baselines are not reversible. There is no automatic
|
|
178
175
|
startup synchronization.
|
|
179
176
|
- Automated integration coverage in this repository is currently strongest for
|
|
180
177
|
Node SQLite. The runtime adapters and public contracts exist, but PostgreSQL,
|
package/docs/inspection.md
CHANGED
|
@@ -31,20 +31,20 @@ This service passes `readOnly: true` to the application adapter factory and clos
|
|
|
31
31
|
its temporary connection after success or failure. Factories must honor the
|
|
32
32
|
selected environment/target and avoid setup writes when read-only. If ORM model
|
|
33
33
|
validation fails after creating an adapter, the adapter is closed too.
|
|
34
|
-
`inspectDatabase(orm, migrations)` inspects an existing borrowed connection
|
|
35
|
-
|
|
34
|
+
`inspectDatabase(orm, migrations)` inspects an existing borrowed connection.
|
|
35
|
+
Its owner remains responsible for closing it.
|
|
36
36
|
|
|
37
37
|
Resolved identity includes only bounded public fields. Connection URLs, credential
|
|
38
38
|
fields, getter properties and bound SQL values are excluded. Driver capabilities
|
|
39
39
|
are declarations of support, not a connectivity check or proof of server version.
|
|
40
40
|
Absent target metadata remains unspecified rather than guessing from a URL.
|
|
41
41
|
The `defaultValues: omitted` policy removes model and physical column default
|
|
42
|
-
values from shared inspection
|
|
42
|
+
values from shared inspection. Entity metadata reports `hasDefault`. Use the
|
|
43
43
|
application-owned migration SQL when reviewing an explicit default change. Unknown historical migration names remain in `history` even
|
|
44
44
|
when their declarations are no longer loaded.
|
|
45
45
|
|
|
46
|
-
Studio displays these same facts under
|
|
46
|
+
Studio displays these same facts under "Database capabilities and migrations".
|
|
47
47
|
Its richer row editor still provides indexes, views and row locators. Those editor
|
|
48
48
|
details are separate from the shared versioned inspection result. Remote Studio
|
|
49
49
|
inspection loads the application's `orm.config.ts` and uses its remote adapter
|
|
50
|
-
factory
|
|
50
|
+
factory. The selected binding must match an explicit `--database` request.
|
package/docs/installation.md
CHANGED
|
@@ -128,7 +128,7 @@ cossack-orm migration status
|
|
|
128
128
|
cossack-orm seed list
|
|
129
129
|
```
|
|
130
130
|
|
|
131
|
-
Pass a non-default
|
|
131
|
+
Pass a non-default configuration location with `--config`:
|
|
132
132
|
|
|
133
133
|
```sh
|
|
134
134
|
cossack-orm schema check --config ./config/orm.config.ts
|
package/docs/introduction.md
CHANGED
|
@@ -10,14 +10,14 @@ and Cloudflare Workers. It combines decorated models, Active Record
|
|
|
10
10
|
methods, a typed query builder, safe SQL fragments, migrations, introspection,
|
|
11
11
|
and explicit runtime adapters.
|
|
12
12
|
|
|
13
|
-
The package is deliberately small
|
|
13
|
+
The package is deliberately small:
|
|
14
14
|
|
|
15
15
|
- Models are ordinary classes that extend `BaseEntity`.
|
|
16
16
|
- Every application registers its complete entity list with `createORM()`.
|
|
17
17
|
- Static Active Record methods resolve through the current request or job scope.
|
|
18
18
|
- Dialects compile SQL while adapters execute it.
|
|
19
19
|
- Relations are loaded explicitly and batched.
|
|
20
|
-
- Migrations are generated and reviewed
|
|
20
|
+
- Migrations are generated and reviewed. Startup never mutates the schema.
|
|
21
21
|
|
|
22
22
|
## A first model and query
|
|
23
23
|
|
|
@@ -67,7 +67,8 @@ try {
|
|
|
67
67
|
|
|
68
68
|
`orm.run()` establishes the current ORM for static methods such as
|
|
69
69
|
`User.find()`, `entity.save()`, and the global `sql` tag. Put one scope around a
|
|
70
|
-
request, queue job, scheduled task, or command
|
|
70
|
+
request, queue job, scheduled task, or command. Do not put a scope around each
|
|
71
|
+
query.
|
|
71
72
|
|
|
72
73
|
Framework middleware normally installs this scope automatically. For example,
|
|
73
74
|
the Cossack/Hono middleware wraps the request once:
|
|
@@ -91,26 +92,26 @@ const users = await orm.model(User).find({
|
|
|
91
92
|
});
|
|
92
93
|
```
|
|
93
94
|
|
|
94
|
-
This form is useful for framework integrations and
|
|
95
|
+
This form is useful for framework integrations and read-only scripts.
|
|
95
96
|
Use `orm.run()` when you want static Active Record methods, instance persistence,
|
|
96
97
|
the global `sql` tag, or automatic transaction rebinding.
|
|
97
98
|
|
|
98
99
|
## What `orm.run()` does
|
|
99
100
|
|
|
100
101
|
An ORM scope identifies the correct ORM and transaction client across
|
|
101
|
-
asynchronous calls. It does not
|
|
102
|
-
|
|
102
|
+
asynchronous calls. It does not reserve a database connection by itself.
|
|
103
|
+
Pooling adapters generally acquire connections as needed.
|
|
103
104
|
`orm.transaction()` and `orm.reserve()` explicitly pin the appropriate client
|
|
104
105
|
when the adapter supports those capabilities.
|
|
105
106
|
|
|
106
|
-
This distinction
|
|
107
|
+
This distinction applies when an application has multiple databases, concurrent
|
|
107
108
|
requests, request-specific Workers bindings, or nested transactions. Cossack ORM
|
|
108
|
-
throws an actionable `ScopeError` instead of
|
|
109
|
+
throws an actionable `ScopeError` instead of guessing a global
|
|
109
110
|
connection.
|
|
110
111
|
|
|
111
112
|
## Design boundaries
|
|
112
113
|
|
|
113
|
-
Cossack ORM
|
|
114
|
+
Cossack ORM does not provide a repository/Data Mapper layer, automatic schema
|
|
114
115
|
synchronization, a legacy query-builder compatibility API, NoSQL or GraphQL integration, or
|
|
115
116
|
query-result caching. Database-specific work remains possible through safe SQL
|
|
116
117
|
fragments and public adapter contracts.
|
package/docs/migrations.md
CHANGED
|
@@ -74,7 +74,7 @@ entity, rename marker, or index produces deterministic TypeScript containing
|
|
|
74
74
|
reviewable `operations.up` and inverse `operations.down` SQL. The snapshot advances only after the
|
|
75
75
|
migration file is written successfully. When the output is directly inside
|
|
76
76
|
`migrationDirectory` and its `index.ts` exports the standard `migrations` array,
|
|
77
|
-
the generator registers the new migration automatically
|
|
77
|
+
the generator registers the new migration automatically. Otherwise it prints a
|
|
78
78
|
registration reminder.
|
|
79
79
|
|
|
80
80
|
A migration has this contract:
|
|
@@ -141,7 +141,14 @@ cossack-orm migration check
|
|
|
141
141
|
|
|
142
142
|
Applied migrations are recorded in `_cossack_migrations` with their name,
|
|
143
143
|
SHA-256 checksum, batch, and application timestamp. Editing an applied migration
|
|
144
|
-
causes checksum validation to fail
|
|
144
|
+
causes checksum validation to fail. Create a new migration instead.
|
|
145
|
+
|
|
146
|
+
Operation-based migrations (the generated form) hash their declared operations,
|
|
147
|
+
so their checksum is the same in every build. A callback-only migration hashes
|
|
148
|
+
the normalized source of `up()` and `down()`: comments, whitespace, `;`,
|
|
149
|
+
trailing commas and quote style are ignored. A build that rewrites the code
|
|
150
|
+
(minification, renamed variables, older `async` targets) still changes it, so
|
|
151
|
+
use `operations` when the checksum must survive different build tools.
|
|
145
152
|
|
|
146
153
|
## Targets and plans
|
|
147
154
|
|
|
@@ -155,18 +162,18 @@ pnpm exec cossack migration up --env staging --target remote
|
|
|
155
162
|
|
|
156
163
|
`--local` and `--remote` are aliases for `--target`. The default is local.
|
|
157
164
|
The selected environment also accepts `CLOUDFLARE_ENV`. Adapter factories
|
|
158
|
-
receive an `ORMToolingContext` with the project root,
|
|
165
|
+
receive an `ORMToolingContext` with the project root, configuration path, environment,
|
|
159
166
|
target, and `readOnly` flag. A remote command requires a factory so the adapter
|
|
160
167
|
can honor that selection. See [D1 tooling](./runtimes.md#d1-tooling) for generated
|
|
161
168
|
Cloudflare projects.
|
|
162
169
|
|
|
163
170
|
`plan` reads migration history without creating its table and never executes
|
|
164
171
|
migration callbacks. Generated migrations declare authoritative `operations` for
|
|
165
|
-
`up` and `down`. Those same operations drive execution
|
|
172
|
+
`up` and `down`. Those same operations drive execution. Callbacks cannot override
|
|
166
173
|
them. Each entry includes its direction, checksum, mode, ordered SQL, affected
|
|
167
174
|
objects, parameter counts, risk, target connection identity, atomicity and
|
|
168
175
|
provider limitations. Parameter values are omitted. Raw SQL object discovery and
|
|
169
|
-
risk classification are conservative
|
|
176
|
+
risk classification are conservative. `affectedObjectsComplete: false` makes
|
|
170
177
|
that limit explicit. Review the SQL itself.
|
|
171
178
|
Use `--direction down` to preview the most recent reversal. A squash that
|
|
172
179
|
replaces fully applied migrations reports `mode: "consolidate"` without
|
|
@@ -183,16 +190,15 @@ pnpm exec cossack migration up --env staging --target remote
|
|
|
183
190
|
pnpm exec cossack migration check --env staging --target remote --json
|
|
184
191
|
```
|
|
185
192
|
|
|
186
|
-
This is
|
|
187
|
-
|
|
188
|
-
commands require `--target remote` (or `--remote`); an environment name alone
|
|
193
|
+
This procedure is opt-in. Never point it at production for a smoke test. Remote
|
|
194
|
+
commands require `--target remote` (or `--remote`). An environment name alone
|
|
189
195
|
always uses local state.
|
|
190
196
|
|
|
191
197
|
Legacy callbacks remain executable through explicit `up()` and `down()`, but
|
|
192
|
-
planning reports them as incomplete without invoking them.
|
|
193
|
-
|
|
194
|
-
external services, or using another captured connection. Configuration
|
|
195
|
-
loading is
|
|
198
|
+
planning reports them as incomplete without invoking them. Planning does not
|
|
199
|
+
invoke a callback because a disconnected ORM cannot stop it from writing files,
|
|
200
|
+
calling external services, or using another captured connection. Configuration
|
|
201
|
+
module loading is trusted application code. This boundary does not sandbox imports.
|
|
196
202
|
|
|
197
203
|
For application-controlled review and apply in one process:
|
|
198
204
|
|
|
@@ -207,14 +213,14 @@ await runner.applyPlan(plan);
|
|
|
207
213
|
incomplete plans and rechecks all migration definitions, checksums, history and
|
|
208
214
|
target identity before writing. Its private SQL/parameter snapshot prevents later
|
|
209
215
|
application mutations from changing the approved batch. JSON reports are inspection
|
|
210
|
-
artifacts
|
|
216
|
+
artifacts. They cannot be imported as executable approvals. Separate CLI invocations
|
|
211
217
|
must replan on their selected target. Concurrent external writers still need a
|
|
212
|
-
provider-specific deployment lock
|
|
218
|
+
provider-specific deployment lock. History rechecks are not a distributed lock.
|
|
213
219
|
|
|
214
220
|
Bookkeeping SQL is reported separately, including squash consolidation deletes
|
|
215
221
|
and the replacement history insert. Missing declarative `down` operations mean a
|
|
216
222
|
reversal is unknown and cannot be applied from a plan. MySQL DDL can commit
|
|
217
|
-
implicitly
|
|
223
|
+
implicitly. A transaction does not guarantee schema rollback. Providers with
|
|
218
224
|
neither transactions nor an atomic batch report possible partial application.
|
|
219
225
|
|
|
220
226
|
`--dry-run` also previews snapshot, generate, squash, and schema pull file
|
|
@@ -226,11 +232,11 @@ before opening the adapter.
|
|
|
226
232
|
|
|
227
233
|
JSON commands execute configuration and adapters in a child process. Console
|
|
228
234
|
messages, direct stdout/stderr writes, and inherited subprocess output are
|
|
229
|
-
suppressed
|
|
235
|
+
suppressed. The structured result returns over IPC. This keeps Wrangler
|
|
230
236
|
connection logs out of JSON and prevents configuration code from changing the
|
|
231
237
|
calling process's environment. It is output isolation, not a sandbox for
|
|
232
238
|
application code. Unexpected child termination produces
|
|
233
|
-
`COSSACK_ORM_PROCESS_FAILED
|
|
239
|
+
`COSSACK_ORM_PROCESS_FAILED`. Text mode retains raw diagnostics.
|
|
234
240
|
|
|
235
241
|
## Destructive changes
|
|
236
242
|
|
|
@@ -290,6 +296,24 @@ cossack-orm schema pull \
|
|
|
290
296
|
Unknown physical database types are retained with a `custom:<type>` logical
|
|
291
297
|
type rather than discarded.
|
|
292
298
|
|
|
299
|
+
The comparison ignores differences that the database cannot store or that
|
|
300
|
+
mean the same thing:
|
|
301
|
+
|
|
302
|
+
- `now()`, `CURRENT_TIMESTAMP` and `transaction_timestamp()` are the same
|
|
303
|
+
default.
|
|
304
|
+
- The enum `CHECK (column IN (...))` that the ORM creates is read back as an
|
|
305
|
+
`enum` column with the same values. Other check constraints still stop
|
|
306
|
+
PostgreSQL and MySQL introspection, because they cannot be represented.
|
|
307
|
+
- On SQLite, a single `INTEGER PRIMARY KEY` matches a model key whether or not
|
|
308
|
+
the model marks it as generated. Only `UNIQUE` constraints mark a column as
|
|
309
|
+
unique. A `CREATE UNIQUE INDEX` stays an index.
|
|
310
|
+
- On PostgreSQL, `increment` and identity columns match.
|
|
311
|
+
- On MySQL, a column-level `UNIQUE` is recognized by its index name, which is
|
|
312
|
+
the column name. Indexes that MySQL creates for foreign keys are not reported.
|
|
313
|
+
Functional, prefix, `FULLTEXT` and `SPATIAL` indexes stop introspection.
|
|
314
|
+
- Many-to-many join tables belong to their owning model and are not reported as
|
|
315
|
+
extra tables.
|
|
316
|
+
|
|
293
317
|
## Baseline an existing database
|
|
294
318
|
|
|
295
319
|
Baseline lets an existing deployment adopt migration bookkeeping without
|
|
@@ -299,7 +323,7 @@ replaying its historical schema creation:
|
|
|
299
323
|
cossack-orm migration baseline
|
|
300
324
|
```
|
|
301
325
|
|
|
302
|
-
The CLI first
|
|
326
|
+
The CLI first makes sure that the live database matches current model metadata.
|
|
303
327
|
Only then does it record the schema hash. Resolve drift before baselining.
|
|
304
328
|
|
|
305
329
|
## Programmatic runner
|
|
@@ -318,4 +342,4 @@ const status = await orm.run(() => runner.status());
|
|
|
318
342
|
Transactional adapters apply a migration and its bookkeeping together. D1 uses
|
|
319
343
|
an atomic prepared batch for schema statements plus bookkeeping.
|
|
320
344
|
|
|
321
|
-
See [Seeders](./seeders.md) for application data that
|
|
345
|
+
See [Seeders](./seeders.md) for application data that is safe to rerun.
|
package/docs/models.md
CHANGED
|
@@ -6,7 +6,7 @@ description: Define decorated Active Record models, columns, keys, indexes, time
|
|
|
6
6
|
# Models
|
|
7
7
|
|
|
8
8
|
A model is a decorated class extending `BaseEntity`. Decorators describe its
|
|
9
|
-
logical schema
|
|
9
|
+
logical schema. `BaseEntity` supplies static query methods and instance
|
|
10
10
|
persistence methods.
|
|
11
11
|
|
|
12
12
|
```ts
|
|
@@ -73,11 +73,11 @@ class User extends BaseEntity {
|
|
|
73
73
|
}
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
`name` is the logical model name
|
|
76
|
+
`name` is the logical model name. `tableName` is the database table.
|
|
77
77
|
|
|
78
78
|
## Column types
|
|
79
79
|
|
|
80
|
-
When reflection is available, `@Column()`
|
|
80
|
+
When reflection is available, `@Column()` infers these logical types:
|
|
81
81
|
|
|
82
82
|
| TypeScript design type | Logical type |
|
|
83
83
|
| --- | --- |
|
|
@@ -107,6 +107,24 @@ Supported logical types are `varchar`, `text`, `integer`, `bigint`, `decimal`,
|
|
|
107
107
|
`boolean`, `datetime`, `date`, `json`, `enum`, `blob`, `uuid`, and
|
|
108
108
|
`custom:<database-type>`.
|
|
109
109
|
|
|
110
|
+
`datetime` values are always stored in UTC. On SQLite, D1, Turso and MySQL, the
|
|
111
|
+
ORM writes a `Date` as `YYYY-MM-DD HH:MM:SS.SSS`. This is the same text shape
|
|
112
|
+
that `DEFAULT CURRENT_TIMESTAMP` produces, so values that the application writes
|
|
113
|
+
and values that the database fills in sort and compare correctly. On
|
|
114
|
+
PostgreSQL, the ORM sends an ISO string with `Z` to a `timestamptz` column. When
|
|
115
|
+
the ORM reads a timestamp that has no offset, it reads it as UTC, not as the
|
|
116
|
+
time zone of the server or of the process. `date` values are written as
|
|
117
|
+
`YYYY-MM-DD` and read as midnight UTC.
|
|
118
|
+
|
|
119
|
+
On MySQL, a `datetime` column is `DATETIME(3)`, so milliseconds survive a round
|
|
120
|
+
trip, and a `CURRENT_TIMESTAMP` default is written as `CURRENT_TIMESTAMP(3)`.
|
|
121
|
+
Tables created before this keep a `DATETIME` column, which rounds values to
|
|
122
|
+
whole seconds. Change such a column with explicit SQL, for example
|
|
123
|
+
`ALTER TABLE events MODIFY created_at DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3)`.
|
|
124
|
+
The Node MySQL adapter sets each session's time zone to UTC so that
|
|
125
|
+
`CURRENT_TIMESTAMP` defaults are UTC. Through Hyperdrive, the MySQL server's
|
|
126
|
+
time zone must be UTC (see [Runtimes](./runtimes.md#cloudflare-hyperdrive)).
|
|
127
|
+
|
|
110
128
|
Common column options include:
|
|
111
129
|
|
|
112
130
|
```ts
|
|
@@ -123,6 +141,20 @@ Common column options include:
|
|
|
123
141
|
declare externalId: string;
|
|
124
142
|
```
|
|
125
143
|
|
|
144
|
+
`select: false` hides a column (for example a password hash) from every query
|
|
145
|
+
that does not ask for it: `find()`, `findOne()`, `query().getMany()` and raw
|
|
146
|
+
rows. The property stays `undefined`, and saving such an entity leaves the
|
|
147
|
+
stored value alone. Load it explicitly with `query().addSelect("passwordHash")`,
|
|
148
|
+
which keeps the default columns, or with `find({ select: [...] })`, which loads
|
|
149
|
+
only the listed columns. The default columns always include primary, version
|
|
150
|
+
and owned relation join columns, even when they are marked `select: false`.
|
|
151
|
+
`reload()` refreshes a hidden column only when the instance already holds it.
|
|
152
|
+
|
|
153
|
+
`insert: false` and `update: false` apply to entity saves and to the static
|
|
154
|
+
`insert()`, `upsert()` and `update()` methods. Entity saves skip such columns.
|
|
155
|
+
The static methods reject a value for them with a `ConfigurationError`. An
|
|
156
|
+
upsert inserts `update: false` columns but never overwrites them on conflict.
|
|
157
|
+
|
|
126
158
|
Use `renamedFrom` when a model property maps to a renamed database column.
|
|
127
159
|
Migration generation never guesses renames heuristically.
|
|
128
160
|
|
|
@@ -197,8 +229,9 @@ declare version: number;
|
|
|
197
229
|
```
|
|
198
230
|
|
|
199
231
|
`save()` fills create/update timestamps and increments version columns.
|
|
200
|
-
Optimistic updates fail when the stored version
|
|
201
|
-
|
|
232
|
+
Optimistic updates fail when another writer changed the stored version.
|
|
233
|
+
`remove()` writes the
|
|
234
|
+
delete timestamp when a `@DeleteDateColumn()` exists. Otherwise it deletes the
|
|
202
235
|
row. Queries do not implicitly hide timestamped rows, so add an explicit
|
|
203
236
|
`deletedAt: null` predicate when that is the desired application policy.
|
|
204
237
|
|
|
@@ -222,7 +255,7 @@ await orm.run(async () => {
|
|
|
222
255
|
```
|
|
223
256
|
|
|
224
257
|
Hydrated entities retain an original snapshot. Existing entities update only
|
|
225
|
-
changed persisted fields
|
|
258
|
+
changed persisted fields. New entities are inserted.
|
|
226
259
|
|
|
227
260
|
## Lifecycle hooks
|
|
228
261
|
|
|
@@ -233,7 +266,7 @@ Available hook decorators are:
|
|
|
233
266
|
- `@BeforeRemove()` and `@AfterRemove()`
|
|
234
267
|
- `@AfterLoad()`
|
|
235
268
|
|
|
236
|
-
Hooks
|
|
269
|
+
Hooks can be synchronous or asynchronous. Keep database side effects deliberate:
|
|
237
270
|
a hook runs as part of the surrounding save/remove operation and uses the
|
|
238
271
|
current ORM or transaction scope.
|
|
239
272
|
|
package/docs/queries.md
CHANGED
|
@@ -101,6 +101,11 @@ const users = await User.find({
|
|
|
101
101
|
|
|
102
102
|
Properties inside one object are joined with `AND`.
|
|
103
103
|
|
|
104
|
+
A property set to `undefined` throws a `ConfigurationError` instead of
|
|
105
|
+
compiling to `= NULL`, which never matches. Use `null` or `IsNull()` to match
|
|
106
|
+
`NULL`, or leave the property out to skip that filter. `In([])` matches no rows,
|
|
107
|
+
and an empty array of `where` objects also matches no rows.
|
|
108
|
+
|
|
104
109
|
## Query builder
|
|
105
110
|
|
|
106
111
|
Start a builder with `Entity.query()`:
|
|
@@ -145,6 +150,17 @@ const rows = await User.query("user")
|
|
|
145
150
|
.getRawMany();
|
|
146
151
|
```
|
|
147
152
|
|
|
153
|
+
`addSelect()` adds properties to the default columns (or to an earlier
|
|
154
|
+
`select()`). Use it to load `select: false` columns:
|
|
155
|
+
|
|
156
|
+
```ts
|
|
157
|
+
const user = await User.query().addSelect("passwordHash").where({ email }).getOne();
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
A `select()` that includes every primary column still hydrates entities and
|
|
161
|
+
paginates like a default query. Projections without the primary key need an
|
|
162
|
+
explicit unique `orderBy()` before `limit()` or `offset()`.
|
|
163
|
+
|
|
148
164
|
Use `selectRaw()` for calculated or database-specific projections:
|
|
149
165
|
|
|
150
166
|
```ts
|
|
@@ -159,8 +175,8 @@ const totals = await User.query("user")
|
|
|
159
175
|
.getRawMany();
|
|
160
176
|
```
|
|
161
177
|
|
|
162
|
-
Use `getMany()`
|
|
163
|
-
|
|
178
|
+
Use `getMany()` for complete entity rows. The ORM hydrates them as model
|
|
179
|
+
instances. Use `getRawMany()` for custom projections.
|
|
164
180
|
|
|
165
181
|
## Joins
|
|
166
182
|
|
|
@@ -182,7 +198,7 @@ const rows = await User.query("user")
|
|
|
182
198
|
```
|
|
183
199
|
|
|
184
200
|
Available joins are `innerJoin()` and `leftJoin()`. For ordinary model
|
|
185
|
-
relationships, prefer explicit `relations` loading
|
|
201
|
+
relationships, prefer explicit `relations` loading. It hydrates both sides and
|
|
186
202
|
batches relation keys.
|
|
187
203
|
|
|
188
204
|
## Aggregates
|
|
@@ -225,7 +241,22 @@ await User.delete({ active: false });
|
|
|
225
241
|
```
|
|
226
242
|
|
|
227
243
|
These methods return a normalized `QueryResult` containing `rows` and `meta`.
|
|
228
|
-
|
|
244
|
+
They reject values for columns declared with `insert: false` (insert/upsert) or
|
|
245
|
+
`update: false` (update).
|
|
246
|
+
|
|
247
|
+
`update()` and `delete()` need a `where` with at least one condition. An empty
|
|
248
|
+
object such as `User.delete({})` throws a `ConfigurationError`, because it
|
|
249
|
+
would change every row. To change every row on purpose, call the explicit
|
|
250
|
+
methods:
|
|
251
|
+
|
|
252
|
+
```ts
|
|
253
|
+
await Session.deleteAll();
|
|
254
|
+
await User.updateAll({ active: false });
|
|
255
|
+
```
|
|
256
|
+
|
|
257
|
+
`User.insert({})` inserts one row of column defaults. It compiles to
|
|
258
|
+
`INSERT INTO ... DEFAULT VALUES` on SQLite and PostgreSQL, and to
|
|
259
|
+
`INSERT INTO ... () VALUES ()` on MySQL.
|
|
229
260
|
|
|
230
261
|
For adapters that support `RETURNING`, use a mutation builder:
|
|
231
262
|
|
|
@@ -268,7 +299,7 @@ This makes ties in other sort fields deterministic. An entity without a primary
|
|
|
268
299
|
key cannot use this automatic order.
|
|
269
300
|
|
|
270
301
|
Joined, grouped, distinct, and projected pages require an explicit unique order.
|
|
271
|
-
Include all fields needed to distinguish result rows
|
|
302
|
+
Include all fields needed to distinguish result rows. Cossack cannot infer their
|
|
272
303
|
uniqueness. Offset pages can shift when concurrent writes insert or delete rows.
|
|
273
304
|
Use a transaction when the provider supports the consistency your application needs.
|
|
274
305
|
|