@cossackframework/database 0.9.1 → 0.9.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/adapter/types.d.ts +2 -0
- package/dist/adapter/types.d.ts.map +1 -1
- package/dist/atomic.d.ts +10 -0
- package/dist/atomic.d.ts.map +1 -0
- package/dist/atomic.js +20 -0
- package/dist/atomic.js.map +1 -0
- package/dist/codec.d.ts.map +1 -1
- package/dist/codec.js +24 -1
- package/dist/codec.js.map +1 -1
- package/dist/errors.d.ts +5 -0
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +20 -0
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +6 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/index.js.map +1 -1
- package/dist/metadata/finalize.d.ts.map +1 -1
- package/dist/metadata/finalize.js +46 -3
- package/dist/metadata/finalize.js.map +1 -1
- package/dist/metadata/types.d.ts +7 -1
- package/dist/metadata/types.d.ts.map +1 -1
- package/dist/migration/startup.d.ts +17 -0
- package/dist/migration/startup.d.ts.map +1 -0
- package/dist/migration/startup.js +34 -0
- package/dist/migration/startup.js.map +1 -0
- package/dist/orm.d.ts +11 -0
- package/dist/orm.d.ts.map +1 -1
- package/dist/orm.js +49 -12
- package/dist/orm.js.map +1 -1
- package/dist/query/builder.d.ts.map +1 -1
- package/dist/query/builder.js +15 -3
- package/dist/query/builder.js.map +1 -1
- package/dist/record-link.d.ts +18 -0
- package/dist/record-link.d.ts.map +1 -0
- package/dist/record-link.js +30 -0
- package/dist/record-link.js.map +1 -0
- package/dist/runtime/romedb.d.ts +79 -0
- package/dist/runtime/romedb.d.ts.map +1 -0
- package/dist/runtime/romedb.js +334 -0
- package/dist/runtime/romedb.js.map +1 -0
- package/dist/schema/ddl.d.ts.map +1 -1
- package/dist/schema/ddl.js +23 -14
- package/dist/schema/ddl.js.map +1 -1
- package/dist/schema/diff.d.ts.map +1 -1
- package/dist/schema/diff.js +12 -3
- package/dist/schema/diff.js.map +1 -1
- package/dist/schema/introspection.d.ts +27 -1
- package/dist/schema/introspection.d.ts.map +1 -1
- package/dist/schema/introspection.js +63 -53
- package/dist/schema/introspection.js.map +1 -1
- package/dist/schema/operations.d.ts +2 -1
- package/dist/schema/operations.d.ts.map +1 -1
- package/dist/schema/types.d.ts +10 -0
- package/dist/schema/types.d.ts.map +1 -1
- package/dist/scope.d.ts +10 -0
- package/dist/scope.d.ts.map +1 -1
- package/dist/scope.js +26 -3
- package/dist/scope.js.map +1 -1
- package/dist/sql/fragment.d.ts +11 -0
- package/dist/sql/fragment.d.ts.map +1 -1
- package/dist/sql/fragment.js +11 -0
- package/dist/sql/fragment.js.map +1 -1
- package/dist/sql/tag.d.ts +17 -1
- package/dist/sql/tag.d.ts.map +1 -1
- package/dist/sql/tag.js +6 -0
- package/dist/sql/tag.js.map +1 -1
- package/dist/tenants.d.ts +42 -0
- package/dist/tenants.d.ts.map +1 -0
- package/dist/tenants.js +111 -0
- package/dist/tenants.js.map +1 -0
- package/docs/introduction.md +1 -0
- package/docs/migrations.md +13 -0
- package/docs/queries.md +4 -2
- package/docs/raw-queries.md +21 -0
- package/docs/romedb.md +150 -0
- package/docs/runtimes.md +1 -0
- package/package.json +7 -3
package/dist/sql/tag.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tag.js","sourceRoot":"","sources":["../../src/sql/tag.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBAAkB,EAAE,UAAU,EAAE,MAAM,cAAc,CAAC;AAC9D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,kBAAkB,EAAE,MAAM,iBAAiB,CAAC;AACrD,OAAO,EACL,WAAW,GAGZ,MAAM,eAAe,CAAC;
|
|
1
|
+
{"version":3,"file":"tag.js","sourceRoot":"","sources":["../../src/sql/tag.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,kBA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
import { type ORM, type ORMOptions } from "./orm.js";
|
|
4
|
+
/** What names a tenant's database: `database` is a file's name or the `{tenant}` of a URL. */
|
|
5
|
+
export interface TenantDatabase {
|
|
6
|
+
readonly database: string;
|
|
7
|
+
readonly slug: string;
|
|
8
|
+
}
|
|
9
|
+
/** A tenant's ORM for one piece of work. Call `release` when the work is done. */
|
|
10
|
+
export interface TenantORM {
|
|
11
|
+
readonly orm: ORM;
|
|
12
|
+
release(): Promise<void>;
|
|
13
|
+
}
|
|
14
|
+
export interface TenantORMsOptions {
|
|
15
|
+
/** Open the adapter of a tenant's database. */
|
|
16
|
+
adapter(tenant: TenantDatabase): Adapter | Promise<Adapter>;
|
|
17
|
+
entities: ORMOptions["entities"];
|
|
18
|
+
/** Applied to a tenant's database when it is first opened. Every database has every migration. */
|
|
19
|
+
migrations: readonly Migration[];
|
|
20
|
+
/**
|
|
21
|
+
* True where a connection belongs to one request (Workers): each `open` makes an ORM and
|
|
22
|
+
* `release` closes it. False (default) keeps one ORM per tenant for the process.
|
|
23
|
+
*/
|
|
24
|
+
perRequest?: boolean;
|
|
25
|
+
/** How many tenants stay open in a process. The least recently used one is closed. Default: 32. */
|
|
26
|
+
max?: number;
|
|
27
|
+
}
|
|
28
|
+
export interface TenantORMs {
|
|
29
|
+
open(tenant: TenantDatabase): Promise<TenantORM>;
|
|
30
|
+
/** Close every ORM that is kept open. */
|
|
31
|
+
close(): Promise<void>;
|
|
32
|
+
}
|
|
33
|
+
/** A database's name is part of a file path or a URL: letters, digits, `-` and `_` only. */
|
|
34
|
+
export declare const TENANT_DATABASE: RegExp;
|
|
35
|
+
/** `{tenant}` in a URL replaced by the tenant's database name. */
|
|
36
|
+
export declare function tenantUrl(template: string | undefined, tenant: TenantDatabase, name?: string): string;
|
|
37
|
+
/**
|
|
38
|
+
* One ORM per tenant database. A tenant's migrations are applied the first time its
|
|
39
|
+
* database is opened in the process.
|
|
40
|
+
*/
|
|
41
|
+
export declare function createTenantORMs(options: TenantORMsOptions):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,EAAE,MAAM,EAAE,cAAc,EAAE,IAAI,SAAwB,GAAG,MAAM,CAGpH;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,iBAAiB,GAAG,UAAU,CAoFvE"}
|
package/dist/tenants.js
ADDED
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { applyPendingMigrations } from "./migration/startup.js";
|
|
2
|
+
import { createORM } from "./orm.js";
|
|
3
|
+
/** A database's name is part of a file path or a URL: letters, digits, `-` and `_` only. */
|
|
4
|
+
export const TENANT_DATABASE = /^[a-z0-9][a-z0-9_-]{0,62}$/;
|
|
5
|
+
/** `{tenant}` in a URL replaced by the tenant's database name. */
|
|
6
|
+
export function tenantUrl(template, tenant, name = "TENANT_DATABASE_URL") {
|
|
7
|
+
if (!template || !template.includes("{tenant}"))
|
|
8
|
+
throw new Error(`${name} is required and must contain {tenant}, such as postgres://user:pass@host/app_{tenant}.`);
|
|
9
|
+
return template.replaceAll("{tenant}", tenant.database);
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* One ORM per tenant database. A tenant's migrations are applied the first time its
|
|
13
|
+
* database is opened in the process.
|
|
14
|
+
*/
|
|
15
|
+
export function createTenantORMs(options) {
|
|
16
|
+
const max = Math.max(options.max ?? 32, 1);
|
|
17
|
+
const kept = new Map();
|
|
18
|
+
// ORMs that left the kept ones while work still holds them: a new request for the same
|
|
19
|
+
// database takes the same ORM again, so a database is never open twice.
|
|
20
|
+
const leaving = new Map();
|
|
21
|
+
const migrated = new Map();
|
|
22
|
+
const make = async (tenant) => {
|
|
23
|
+
const orm = createORM({ adapter: await options.adapter(tenant), entities: options.entities });
|
|
24
|
+
try {
|
|
25
|
+
let applied = migrated.get(tenant.database);
|
|
26
|
+
if (!applied) {
|
|
27
|
+
applied = applyPendingMigrations(orm, options.migrations);
|
|
28
|
+
migrated.set(tenant.database, applied);
|
|
29
|
+
// A failed migration is not kept: the next request tries again.
|
|
30
|
+
applied.catch(() => { if (migrated.get(tenant.database) === applied)
|
|
31
|
+
migrated.delete(tenant.database); });
|
|
32
|
+
}
|
|
33
|
+
await applied;
|
|
34
|
+
return orm;
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
await orm.close().catch(() => undefined);
|
|
38
|
+
throw error;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const close = (entry) => { void entry.opening.then(orm => orm.close()).catch(() => undefined); };
|
|
42
|
+
return {
|
|
43
|
+
async open(tenant) {
|
|
44
|
+
if (!TENANT_DATABASE.test(tenant.database))
|
|
45
|
+
throw new Error("A tenant's database name has lowercase letters, digits, - and _ only.");
|
|
46
|
+
if (options.perRequest) {
|
|
47
|
+
const orm = await make(tenant);
|
|
48
|
+
return { orm, release: () => orm.close() };
|
|
49
|
+
}
|
|
50
|
+
const name = tenant.database;
|
|
51
|
+
let entry = kept.get(name) ?? leaving.get(name);
|
|
52
|
+
if (entry) {
|
|
53
|
+
// Most recently used last.
|
|
54
|
+
kept.delete(name);
|
|
55
|
+
leaving.delete(name);
|
|
56
|
+
entry.evicted = false;
|
|
57
|
+
kept.set(name, entry);
|
|
58
|
+
}
|
|
59
|
+
else {
|
|
60
|
+
const created = { opening: make(tenant), users: 0, evicted: false };
|
|
61
|
+
entry = created;
|
|
62
|
+
kept.set(name, created);
|
|
63
|
+
created.opening.catch(() => { if (kept.get(name) === created)
|
|
64
|
+
kept.delete(name); if (leaving.get(name) === created)
|
|
65
|
+
leaving.delete(name); });
|
|
66
|
+
}
|
|
67
|
+
const held = entry;
|
|
68
|
+
held.users++;
|
|
69
|
+
if (kept.size > max) {
|
|
70
|
+
// The least recently used one that is not this one leaves. It closes when its work is done.
|
|
71
|
+
for (const [oldest, candidate] of kept) {
|
|
72
|
+
if (candidate === held)
|
|
73
|
+
continue;
|
|
74
|
+
kept.delete(oldest);
|
|
75
|
+
candidate.evicted = true;
|
|
76
|
+
if (candidate.users === 0)
|
|
77
|
+
close(candidate);
|
|
78
|
+
else
|
|
79
|
+
leaving.set(oldest, candidate);
|
|
80
|
+
break;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
let released = false;
|
|
84
|
+
const release = async () => {
|
|
85
|
+
if (released)
|
|
86
|
+
return;
|
|
87
|
+
released = true;
|
|
88
|
+
held.users--;
|
|
89
|
+
if (held.evicted && held.users === 0) {
|
|
90
|
+
if (leaving.get(name) === held)
|
|
91
|
+
leaving.delete(name);
|
|
92
|
+
close(held);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
try {
|
|
96
|
+
return { orm: await held.opening, release };
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
await release();
|
|
100
|
+
throw error;
|
|
101
|
+
}
|
|
102
|
+
},
|
|
103
|
+
async close() {
|
|
104
|
+
const open = [...kept.values(), ...leaving.values()];
|
|
105
|
+
kept.clear();
|
|
106
|
+
leaving.clear();
|
|
107
|
+
await Promise.all(open.map(entry => entry.opening.then(orm => orm.close()).catch(() => undefined)));
|
|
108
|
+
},
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=tenants.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tenants.js","sourceRoot":"","sources":["../src/tenants.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AAEhE,OAAO,EAAE,SAAS,EAA6B,MAAM,UAAU,CAAC;AAmChE,4FAA4F;AAC5F,MAAM,CAAC,MAAM,eAAe,GAAG,4BAA4B,CAAC;AAE5D,kEAAkE;AAClE,MAAM,UAAU,SAAS,CAAC,QAA4B,EAAE,MAAsB,EAAE,IAAI,GAAG,qBAAqB;IAC1G,IAAI,CAAC,QAAQ,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,UAAU,CAAC;QAAE,MAAM,IAAI,KAAK,CAAC,GAAG,IAAI,yFAAyF,CAAC,CAAC;IACnK,OAAO,QAAQ,CAAC,UAAU,CAAC,U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AE,CAAC;oBACrC,IAAI,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,KAAK,IAAI;wBAAE,OAAO,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC;oBACrD,KAAK,CAAC,IAAI,CAAC,CAAC;gBACd,CAAC;YACH,CAAC,CAAC;YACF,IAAI,CAAC;gBAAC,OAAO,EAAE,GAAG,EAAE,MAAM,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,CAAC;YAAC,CAAC;YACpD,OAAO,KAAK,EAAE,CAAC;gBAAC,MAAM,OAAO,EAAE,CAAC;gBAAC,MAAM,KAAK,CAAC;YAAC,CAAC;QACjD,CAAC;QACD,KAAK,CAAC,KAAK;YACT,MAAM,IAAI,GAAG,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,EAAE,GAAG,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC;YACrD,IAAI,CAAC,KAAK,EAAE,CAAC;YACb,OAAO,CAAC,KAAK,EAAE,CAAC;YAChB,MAAM,OAAO,CAAC,GAAG,CAAC,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,EAAE,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC,SAAS,CAAC,CAAC,CAAC,CAAC;QACtG,CAAC;KACF,CAAC;AACJ,CAAC"}
|
package/docs/introduction.md
CHANGED
|
@@ -122,6 +122,7 @@ fragments and public adapter contracts.
|
|
|
122
122
|
- [Models](./models.md)
|
|
123
123
|
- [Queries](./queries.md)
|
|
124
124
|
- [Relationships](./relationships.md)
|
|
125
|
+
- [RomeDB](./romedb.md)
|
|
125
126
|
- [Raw queries](./raw-queries.md)
|
|
126
127
|
- [Comparison](./comparison.md)
|
|
127
128
|
- [Complete SQLite starter](../examples/sqlite-starter/README.md)
|
package/docs/migrations.md
CHANGED
|
@@ -104,6 +104,19 @@ export default {
|
|
|
104
104
|
static, and reviewed. Use schema operations where they express the change
|
|
105
105
|
portably.
|
|
106
106
|
|
|
107
|
+
### Column types that differ between databases
|
|
108
|
+
|
|
109
|
+
A statement that runs on more than one database names such a type with `sql.type()`, which
|
|
110
|
+
writes the database's own type when the statement runs:
|
|
111
|
+
|
|
112
|
+
```ts
|
|
113
|
+
schema.raw(sql.fragment`ALTER TABLE users ADD COLUMN meta ${sql.type("json")}`);
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
`sql.type("json")` is `jsonb` on PostgreSQL and `json` on MySQL and SQLite. SQLite has no
|
|
117
|
+
JSON storage type: the column keeps the JSON text, and its JSON functions read it. A
|
|
118
|
+
`@Column({ type: "json" })` property reads and writes the value as an object on all three.
|
|
119
|
+
|
|
107
120
|
## Squash migration history
|
|
108
121
|
|
|
109
122
|
Create one baseline migration directly from the current models:
|
package/docs/queries.md
CHANGED
|
@@ -296,9 +296,11 @@ the model APIs.
|
|
|
296
296
|
For an entity query with `limit()` or `offset()`, Cossack appends each primary
|
|
297
297
|
key field to the order. An explicit primary key order keeps its direction.
|
|
298
298
|
This makes ties in other sort fields deterministic. An entity without a primary
|
|
299
|
-
key cannot use this automatic order.
|
|
299
|
+
key cannot use this automatic order. A `select()` of model fields gets the same
|
|
300
|
+
order, even when it leaves out the primary key, so `findOne({ select })` works
|
|
301
|
+
with any fields.
|
|
300
302
|
|
|
301
|
-
Joined, grouped, distinct, and
|
|
303
|
+
Joined, grouped, distinct, and `selectRaw()` pages require an explicit unique order.
|
|
302
304
|
Include all fields needed to distinguish result rows. Cossack cannot infer their
|
|
303
305
|
uniqueness. Offset pages can shift when concurrent writes insert or delete rows.
|
|
304
306
|
Use a transaction when the provider supports the consistency your application needs.
|
package/docs/raw-queries.md
CHANGED
|
@@ -124,6 +124,27 @@ Every row passed to `values()` must have the same columns in the same order.
|
|
|
124
124
|
Large application writes must respect `orm.driver.capabilities.parameterLimit`
|
|
125
125
|
and `batchLimit`.
|
|
126
126
|
|
|
127
|
+
## JSON values
|
|
128
|
+
|
|
129
|
+
To compare a `json` column with a value, or to write one, pass the value through
|
|
130
|
+
`sql.json()`:
|
|
131
|
+
|
|
132
|
+
```ts
|
|
133
|
+
await orm.sql`
|
|
134
|
+
DELETE FROM ${orm.sql.id("sessions")}
|
|
135
|
+
WHERE ${orm.sql.id("meta")} = ${orm.sql.json({ type: "auth" })}
|
|
136
|
+
`;
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
It binds the JSON text as a parameter and adds the cast each database needs (`::jsonb` on
|
|
140
|
+
PostgreSQL, `CAST(? AS JSON)` on MySQL, none on SQLite). Do not bind
|
|
141
|
+
`JSON.stringify(value)` yourself: MySQL compares a JSON column with a bare string as with
|
|
142
|
+
a JSON string, so the row is never found. A value with no JSON form (`undefined`) is bound as SQL
|
|
143
|
+
`NULL`.
|
|
144
|
+
|
|
145
|
+
A raw `SELECT` returns a JSON column as text on SQLite and as the parsed value on
|
|
146
|
+
PostgreSQL and MySQL. Read it through the model, or accept both.
|
|
147
|
+
|
|
127
148
|
## Unsafe SQL
|
|
128
149
|
|
|
129
150
|
`unsafe()` is the only API that injects literal SQL:
|
package/docs/romedb.md
ADDED
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: RomeDB
|
|
3
|
+
description: Use RomeDB as an ORM database, with relational entities, document entities, record links, and the RomeDB instance for RomeQL.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# RomeDB
|
|
7
|
+
|
|
8
|
+
RomeDB is a multi-model database with a SQLite-compatible relational model. The ORM runs on
|
|
9
|
+
it through an adapter, so the same entities, queries, migrations and seeders work as on
|
|
10
|
+
SQLite. RomeDB also stores documents, and an entity can be a document.
|
|
11
|
+
|
|
12
|
+
```ts
|
|
13
|
+
import { createORM } from "@cossackframework/database";
|
|
14
|
+
import { createRomeDB } from "@cossackframework/romedb";
|
|
15
|
+
import { connectNode, FilePendingWriteStore } from "@cossackframework/romedb/node";
|
|
16
|
+
import { romedb } from "@cossackframework/romedb/orm";
|
|
17
|
+
|
|
18
|
+
const url = "storage/romedb/app.romedb";
|
|
19
|
+
const db = createRomeDB({
|
|
20
|
+
url,
|
|
21
|
+
connect: () => connectNode({ url }),
|
|
22
|
+
pending: new FilePendingWriteStore(".cossack/romedb-pending"),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const orm = createORM({ adapter: await romedb(db), entities });
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
On Workers, create the instance with `url`, `token` and a `DurableObjectPendingWriteStore`
|
|
29
|
+
(see the `@cossackframework/romedb` README). The RomeDB instance is yours: `orm.close()`
|
|
30
|
+
leaves it open, so other code can keep using it. Close it yourself when the process ends.
|
|
31
|
+
|
|
32
|
+
## What the adapter does
|
|
33
|
+
|
|
34
|
+
- A query (`SELECT`, `WITH`, `INFO`) is one read. Every other statement is one write.
|
|
35
|
+
- A write goes through `RomeDB.write`, so it is saved before it is sent and can be recovered.
|
|
36
|
+
A write that loses to a concurrent write of the same rows runs again, up to five times
|
|
37
|
+
(`retryConflicts` in the adapter's options).
|
|
38
|
+
- A batch is one atomic write of at most 32 statements. Migrations run as one batch: the
|
|
39
|
+
schema changes and the migration's record apply together or not at all.
|
|
40
|
+
- There are no interactive transactions. `orm.transaction()` throws
|
|
41
|
+
`UnsupportedCapabilityError`, as on D1.
|
|
42
|
+
- `RETURNING` is supported, so `save()` reads back the database's defaults.
|
|
43
|
+
- The schema is read with `INFO FOR` and the stored DDL text. `orm.introspect()` and the
|
|
44
|
+
drift check work.
|
|
45
|
+
|
|
46
|
+
| Limit | Value |
|
|
47
|
+
| --- | --- |
|
|
48
|
+
| Statements in one batch | 32 |
|
|
49
|
+
| Parameters in one statement | 500 |
|
|
50
|
+
| Request size | 64 KiB on an engine that reports none; see the RomeDB package |
|
|
51
|
+
|
|
52
|
+
## Document entities
|
|
53
|
+
|
|
54
|
+
Set `model: "document"` to store an entity in a RomeDB document table.
|
|
55
|
+
|
|
56
|
+
```ts
|
|
57
|
+
@Entity({ tableName: "people", model: "document" })
|
|
58
|
+
@Index(["profile.city"])
|
|
59
|
+
class Person extends BaseEntity {
|
|
60
|
+
@PrimaryGeneratedColumn("uuid") declare id: string;
|
|
61
|
+
@Column({ type: "varchar", unique: true }) declare handle: string;
|
|
62
|
+
@Column({ type: "boolean" }) declare active: boolean;
|
|
63
|
+
@Column({ type: "json", nullable: true }) declare profile: { city: string; tags: string[] } | null;
|
|
64
|
+
}
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
The API is the same as for a relational entity: `find`, `findOne`, `count`, `insert`,
|
|
68
|
+
`update`, `upsert`, `delete`, `save`, `remove`, relations and the query builder. These
|
|
69
|
+
things differ:
|
|
70
|
+
|
|
71
|
+
- **Fields keep their types.** A boolean is stored as a boolean and a `json` column as a
|
|
72
|
+
nested object or array, not as text.
|
|
73
|
+
- **The primary column is the record's `id`.** There is exactly one, stored as `id`. Use
|
|
74
|
+
`@PrimaryGeneratedColumn("uuid")`, or `@PrimaryColumn()` with your own key. A counter
|
|
75
|
+
(`increment`) is not available. In the entity the value is the key (`"ada"`); in the
|
|
76
|
+
database it is the record `people:ada`.
|
|
77
|
+
- **The table has no columns.** Its DDL is `CREATE TABLE "people"` and its indexes. Adding
|
|
78
|
+
or removing a column needs no migration. A `unique` column is a unique index.
|
|
79
|
+
- **A field that is not set is left out.** Reading it gives `null`.
|
|
80
|
+
- **An index can name a nested field**, written as a path: `@Index(["profile.city"])`.
|
|
81
|
+
- **A table does not change its model.** To turn a relational table into a document table,
|
|
82
|
+
create a table under a new name and copy the data.
|
|
83
|
+
|
|
84
|
+
To filter on a nested field, write the path with `sql.id()`:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
const people = await Person.query("p")
|
|
88
|
+
.where(sql.fragment`${sql.id("p", "profile", "city")} = ${"Paris"}`)
|
|
89
|
+
.getMany();
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
Document entities need RomeDB. Another adapter refuses them when the ORM is created.
|
|
93
|
+
|
|
94
|
+
## Record links
|
|
95
|
+
|
|
96
|
+
A relation between two document entities is stored as a RomeDB record link.
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
@Entity({ tableName: "articles", model: "document" })
|
|
100
|
+
class Article extends BaseEntity {
|
|
101
|
+
@PrimaryColumn({ type: "varchar" }) declare id: string;
|
|
102
|
+
@ManyToOne(() => Person, person => person.articles) declare author: Relation<Person>;
|
|
103
|
+
declare authorId: string;
|
|
104
|
+
}
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
`authorId` is the key in the entity and the record `people:<key>` in the database. Load the
|
|
108
|
+
relation as usual: `Article.find({ relations: ["author"] })`. In RomeQL the link is a link:
|
|
109
|
+
`record::fetch(author_id)` gives the author's document.
|
|
110
|
+
|
|
111
|
+
A relation between a document entity and a relational entity keeps the other row's key as a
|
|
112
|
+
plain value, in either direction. No foreign key constraint is made for it, because a
|
|
113
|
+
constraint cannot name a document table. A many-to-many relation uses a relational join
|
|
114
|
+
table that holds the keys.
|
|
115
|
+
|
|
116
|
+
`RecordLink` is the ORM's value for a link. Pass one to the `sql` tag to compare with a
|
|
117
|
+
link, and expect one for a record in a raw result:
|
|
118
|
+
|
|
119
|
+
```ts
|
|
120
|
+
import { RecordLink } from "@cossackframework/database";
|
|
121
|
+
|
|
122
|
+
const rows = await orm.sql`SELECT d.title FROM articles AS d WHERE d.author_id = ${new RecordLink("people", "ada")}`;
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## RomeQL the ORM does not write
|
|
126
|
+
|
|
127
|
+
`romedbClient()` gives the RomeDB instance the ORM runs on. Use it for graph traversal,
|
|
128
|
+
vectors, search and any other RomeQL. The calls share the ORM's connection, its
|
|
129
|
+
pending-write store and its limits.
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
import { romedbClient } from "@cossackframework/romedb/orm";
|
|
133
|
+
|
|
134
|
+
const db = romedbClient(); // the ORM of the current request
|
|
135
|
+
const result = await db.read([{
|
|
136
|
+
rql: "SELECT g.node FROM GRAPH(people:ada -> follows -> people) AS g",
|
|
137
|
+
}]);
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
Pass an ORM (`romedbClient(orm)`) outside a request scope.
|
|
141
|
+
|
|
142
|
+
## Known limits
|
|
143
|
+
|
|
144
|
+
- A graph edge joins two document tables. A relational row cannot be the end of an edge,
|
|
145
|
+
and `record::fetch` does not follow a link to a relational row.
|
|
146
|
+
- RomeDB refuses `null` in a field that is the parent of an indexed nested field
|
|
147
|
+
(`profile` with an index on `profile.city`). Inserts leave unset fields out, so this
|
|
148
|
+
applies only to an update that sets such a field to `null`.
|
|
149
|
+
- One process owns a RomeDB file. Run migrations and Studio against a file only while the
|
|
150
|
+
application is stopped, or share the database through `romedb serve`.
|
package/docs/runtimes.md
CHANGED
|
@@ -14,6 +14,7 @@ eagerly include Node built-ins, native SQLite packages, or unused drivers.
|
|
|
14
14
|
| `@cossackframework/database/bun` | Native Bun SQL for SQLite, PostgreSQL, MySQL |
|
|
15
15
|
| `@cossackframework/database/cloudflare` | D1 and Hyperdrive PostgreSQL/MySQL |
|
|
16
16
|
| `@cossackframework/database/cloudflare-turso` | Remote Turso for Workers |
|
|
17
|
+
| `@cossackframework/romedb/orm` | RomeDB, with relational and document entities: see [RomeDB](./romedb.md) |
|
|
17
18
|
| `@cossackframework/database/adapter` | Public contracts for custom adapters |
|
|
18
19
|
|
|
19
20
|
## Node.js
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cossackframework/database",
|
|
3
|
-
"version": "0.9.
|
|
3
|
+
"version": "0.9.3",
|
|
4
4
|
"description": "Serverless-first Active Record ORM for Cossack",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -52,6 +52,10 @@
|
|
|
52
52
|
"types": "./dist/runtime/cloudflare-turso.d.ts",
|
|
53
53
|
"import": "./dist/runtime/cloudflare-turso.js"
|
|
54
54
|
},
|
|
55
|
+
"./romedb": {
|
|
56
|
+
"types": "./dist/runtime/romedb.d.ts",
|
|
57
|
+
"import": "./dist/runtime/romedb.js"
|
|
58
|
+
},
|
|
55
59
|
"./cossack": {
|
|
56
60
|
"types": "./dist/cossack/index.d.ts",
|
|
57
61
|
"import": "./dist/cossack/index.js"
|
|
@@ -73,7 +77,7 @@
|
|
|
73
77
|
"@tursodatabase/database": "^0.7.2",
|
|
74
78
|
"@tursodatabase/serverless": "^1.4.0",
|
|
75
79
|
"better-sqlite3": ">=11",
|
|
76
|
-
"hono": "^4.13.
|
|
80
|
+
"hono": "^4.13.18",
|
|
77
81
|
"mysql2": ">=3.11",
|
|
78
82
|
"pg": ">=8.13",
|
|
79
83
|
"reflect-metadata": ">=0.2"
|
|
@@ -100,7 +104,7 @@
|
|
|
100
104
|
},
|
|
101
105
|
"devDependencies": {
|
|
102
106
|
"@types/node": "^22.19.0",
|
|
103
|
-
"hono": "^4.13.
|
|
107
|
+
"hono": "^4.13.18",
|
|
104
108
|
"miniflare": "4.20260722.0",
|
|
105
109
|
"reflect-metadata": "^0.2.2",
|
|
106
110
|
"typescript": "^7.0.2",
|