durable-db 0.8.0 → 0.9.0
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 +124 -0
- package/dist/cli/index.d.ts +2 -2
- package/dist/db/index.d.ts +3 -3
- package/dist/db/index.js +215 -8
- package/dist/defineDatabase-BvFOZVkv.d.ts +377 -0
- package/dist/defineDatabase-CaqKQagA.js +767 -0
- package/dist/describeAction-CEKfxTQ4.js +24 -0
- package/dist/{index-rlnVnzJJ.d.ts → index-DoxI1p4-.d.ts} +1 -1
- package/dist/index.d.ts +19 -3
- package/dist/index.js +3 -2
- package/dist/kysely-D8JQ4v8n.js +278 -0
- package/dist/registry.d.ts +15 -0
- package/dist/registry.js +30 -2
- package/dist/schema.d.ts +15 -2
- package/dist/testing/index.js +2 -2
- package/dist/vite/durableDb.d.ts +1 -1
- package/dist/vite/durableDb.js +35 -1
- package/package.json +1 -1
- package/virtual.d.ts +12 -0
- package/dist/defineDatabase-595bNB6M.js +0 -110
- package/dist/defineDatabase-By401iHp.d.ts +0 -200
- package/dist/kysely-DvAoap5h.js +0 -631
|
@@ -1,110 +0,0 @@
|
|
|
1
|
-
import { r as type } from "./type-CUgZsZp3.js";
|
|
2
|
-
import { t as actionArgsType } from "./actionArgs-DP2tQrlf.js";
|
|
3
|
-
//#region src/db/defineDatabase.ts
|
|
4
|
-
/**
|
|
5
|
-
* Non-enumerable property the source `action()` stashes on the function it
|
|
6
|
-
* returns, carrying the original `{ args, handler }` config plus the compiled
|
|
7
|
-
* arktype validator. The production Vite transform replaces `action()`
|
|
8
|
-
* call-sites entirely, so this only ever exists in the UNTRANSFORMED world —
|
|
9
|
-
* i.e. tests. The `durable-db/testing` harness reads it to run a handler
|
|
10
|
-
* against a real in-memory database. See src/testing/index.ts.
|
|
11
|
-
*/
|
|
12
|
-
const ACTION_TEST_META = Symbol.for("durable-db.actionTestMeta");
|
|
13
|
-
/**
|
|
14
|
-
* Where the live test databases are listed, newest last. On `globalThis`, not
|
|
15
|
-
* module state: a test run can load `durable-db` (the app's actions) and
|
|
16
|
-
* `durable-db/testing` as separate module copies.
|
|
17
|
-
*/
|
|
18
|
-
const LIVE_TEST_DATABASES = Symbol.for("durable-db.liveTestDatabases");
|
|
19
|
-
/** The live test databases (created on first use). */
|
|
20
|
-
function liveTestDatabases() {
|
|
21
|
-
const store = globalThis;
|
|
22
|
-
return store[LIVE_TEST_DATABASES] ??= [];
|
|
23
|
-
}
|
|
24
|
-
/** The newest live test database holding every table of `schema`, if any. */
|
|
25
|
-
function testDatabaseFor(schema) {
|
|
26
|
-
const tables = Object.values(schema);
|
|
27
|
-
if (tables.length === 0) return void 0;
|
|
28
|
-
const live = globalThis[LIVE_TEST_DATABASES];
|
|
29
|
-
if (!live) return void 0;
|
|
30
|
-
for (let i = live.length - 1; i >= 0; i--) if (tables.every((table) => live[i].tables.has(table))) return live[i];
|
|
31
|
-
}
|
|
32
|
-
/** `createTestDatabase({ schema: { a, b } })` for this database, for messages. */
|
|
33
|
-
function testDatabaseHint(schema) {
|
|
34
|
-
const names = Object.keys(schema);
|
|
35
|
-
return `createTestDatabase({ schema: { ${names.length > 0 ? names.join(", ") : "…"} } })`;
|
|
36
|
-
}
|
|
37
|
-
/**
|
|
38
|
-
* Define a database with its schema and migrations.
|
|
39
|
-
*
|
|
40
|
-
* This function is transformed at build time by the durable-db Vite plugin.
|
|
41
|
-
* At runtime, the actions returned will be RPC stubs that call into the
|
|
42
|
-
* generated Durable Object.
|
|
43
|
-
*
|
|
44
|
-
* Outside the build — code under test calling an action directly — an action
|
|
45
|
-
* runs against the test database `createTestDatabase()` created for its
|
|
46
|
-
* database's tables (the newest one, when there are several); without one it
|
|
47
|
-
* throws.
|
|
48
|
-
*
|
|
49
|
-
* @example
|
|
50
|
-
* ```ts
|
|
51
|
-
* import { defineDatabase } from 'durable-db';
|
|
52
|
-
* import { users, posts } from './schema';
|
|
53
|
-
*
|
|
54
|
-
* export const { action } = defineDatabase({
|
|
55
|
-
* schema: { users, posts },
|
|
56
|
-
* });
|
|
57
|
-
*
|
|
58
|
-
* export const createUser = action({
|
|
59
|
-
* args: { name: 'string', email: 'string.email' },
|
|
60
|
-
* handler: async (db, args) => {
|
|
61
|
-
* return db.insertInto('users').values({
|
|
62
|
-
* id: crypto.randomUUID(),
|
|
63
|
-
* name: args.name,
|
|
64
|
-
* email: args.email
|
|
65
|
-
* }).returningAll().executeTakeFirst();
|
|
66
|
-
* },
|
|
67
|
-
* });
|
|
68
|
-
* ```
|
|
69
|
-
*/
|
|
70
|
-
function defineDatabase(config) {
|
|
71
|
-
const schema = config?.schema ?? {};
|
|
72
|
-
const action = (actionConfig) => {
|
|
73
|
-
const validator = actionArgsType(actionConfig.args);
|
|
74
|
-
const argsDefinition = actionConfig.args;
|
|
75
|
-
const argKeys = argsDefinition !== null && typeof argsDefinition === "object" ? Object.keys(argsDefinition) : null;
|
|
76
|
-
const argsHint = argKeys ? ` (args: ${argKeys.length > 0 ? `{ ${argKeys.join(", ")} }` : "{}"})` : "";
|
|
77
|
-
const actionFn = async (args) => {
|
|
78
|
-
const testDatabase = testDatabaseFor(schema);
|
|
79
|
-
if (testDatabase) return await testDatabase.run(actionFn, args);
|
|
80
|
-
const result = validator(args);
|
|
81
|
-
if (result instanceof type.errors) throw new Error(`Invalid args for action${argsHint}: ${result.summary}`);
|
|
82
|
-
throw new Error(`Action${argsHint} called without transformation: action() call sites are transformed by the build; in tests, create the database with ${testDatabaseHint(schema)} and call the action, or use db.run(action, args).`);
|
|
83
|
-
};
|
|
84
|
-
Object.defineProperty(actionFn, ACTION_TEST_META, {
|
|
85
|
-
value: {
|
|
86
|
-
args: actionConfig.args,
|
|
87
|
-
handler: actionConfig.handler,
|
|
88
|
-
validator,
|
|
89
|
-
schema
|
|
90
|
-
},
|
|
91
|
-
enumerable: false,
|
|
92
|
-
configurable: true
|
|
93
|
-
});
|
|
94
|
-
return actionFn;
|
|
95
|
-
};
|
|
96
|
-
const destroyDatabase = async () => {
|
|
97
|
-
const testDatabase = testDatabaseFor(schema);
|
|
98
|
-
if (testDatabase) {
|
|
99
|
-
testDatabase.clear();
|
|
100
|
-
return;
|
|
101
|
-
}
|
|
102
|
-
throw new Error(`destroyDatabase() called without transformation: it is transformed by the build; in tests, create the database with ${testDatabaseHint(schema)} first.`);
|
|
103
|
-
};
|
|
104
|
-
return {
|
|
105
|
-
action,
|
|
106
|
-
destroyDatabase
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
//#endregion
|
|
110
|
-
export { defineDatabase as n, liveTestDatabases as r, ACTION_TEST_META as t };
|
|
@@ -1,200 +0,0 @@
|
|
|
1
|
-
import "./index-bUUlOWfP.js";
|
|
2
|
-
import { t as Outbox } from "./outbox-D3Q9n7Rt.js";
|
|
3
|
-
import { type } from "arktype";
|
|
4
|
-
import { SQLiteTableWithColumns } from "drizzle-orm/sqlite-core";
|
|
5
|
-
import { Kysely } from "kysely";
|
|
6
|
-
import { Kyselify } from "drizzle-orm/kysely";
|
|
7
|
-
//#region src/db/types.d.ts
|
|
8
|
-
/**
|
|
9
|
-
* Base type for ArkType schema definitions.
|
|
10
|
-
* This represents any valid arktype definition object.
|
|
11
|
-
*/
|
|
12
|
-
type ArgsSchema = Record<string, unknown>;
|
|
13
|
-
/**
|
|
14
|
-
* Configuration for defineDatabase
|
|
15
|
-
*/
|
|
16
|
-
interface DatabaseConfig<TSchema extends Record<string, SQLiteTableWithColumns<any>>> {
|
|
17
|
-
/** Drizzle schema tables */
|
|
18
|
-
schema: TSchema;
|
|
19
|
-
/** Instance strategy - 'per-tenant' (default) or 'global' */
|
|
20
|
-
instance?: 'per-tenant' | 'global';
|
|
21
|
-
/** Enable Outerbase Studio browsable SQL endpoint on this DO.
|
|
22
|
-
* - true: always enabled
|
|
23
|
-
* - false: always disabled (default)
|
|
24
|
-
* - 'development': enabled only in dev mode */
|
|
25
|
-
browsable?: boolean | 'development';
|
|
26
|
-
/** Transport for action stubs: 'rpc' (default) or 'websocket'.
|
|
27
|
-
* WebSocket uses Cloudflare's 20:1 message billing ratio for cheaper high-volume calls. */
|
|
28
|
-
transport?: 'rpc' | 'websocket';
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Convert a Drizzle schema to Kysely database types using drizzle-kysely
|
|
32
|
-
*/
|
|
33
|
-
type DrizzleToKysely<TSchema extends Record<string, SQLiteTableWithColumns<any>>> = { [K in keyof TSchema]: TSchema[K] extends SQLiteTableWithColumns<any> ? Kyselify<TSchema[K]> : never; };
|
|
34
|
-
/**
|
|
35
|
-
* ArkType helpers – mirror Ark's own API:
|
|
36
|
-
* - arkType.validate<Def> → definition shape with DSL validation/autocomplete
|
|
37
|
-
* - arkType.infer<Def> → inferred output type
|
|
38
|
-
*/
|
|
39
|
-
type ArkDef<Def> = type.validate<Def>;
|
|
40
|
-
type InferArgs<Def> = type.infer<Def>;
|
|
41
|
-
/**
|
|
42
|
-
* Context passed to action handlers
|
|
43
|
-
*/
|
|
44
|
-
interface ActionContext<TEnv = unknown> {
|
|
45
|
-
/** Environment bindings (DO bindings, KV, etc.) */
|
|
46
|
-
env: TEnv;
|
|
47
|
-
/**
|
|
48
|
-
* The instance key used to address this Durable Object.
|
|
49
|
-
* For per-tenant databases, this is the tenant ID.
|
|
50
|
-
* For global databases, this is 'global'.
|
|
51
|
-
*/
|
|
52
|
-
instanceKey: string;
|
|
53
|
-
/**
|
|
54
|
-
* Messages for something outside the Durable Object — a queue, an email, a
|
|
55
|
-
* webhook — delivered after the action succeeds (`durableDb({ outbox })`
|
|
56
|
-
* names where): `ctx.outbox.push(message)`. Written with the action's
|
|
57
|
-
* writes, at least once; a handler that throws records none.
|
|
58
|
-
*/
|
|
59
|
-
outbox: Outbox;
|
|
60
|
-
}
|
|
61
|
-
/**
|
|
62
|
-
* Configuration for an action
|
|
63
|
-
*
|
|
64
|
-
* `Def` is "the ArkType definition" – whatever you'd normally pass to `type(...)`:
|
|
65
|
-
* { name: "string", email: "string.email" }
|
|
66
|
-
* unions, tuples, ranges, etc. all work.
|
|
67
|
-
*/
|
|
68
|
-
interface ActionConfig<Def, TResult, TSchema extends Record<string, SQLiteTableWithColumns<any>>, TEnv = unknown> {
|
|
69
|
-
/**
|
|
70
|
-
* ArkType definition for args.
|
|
71
|
-
* Typed as `arkType.validate<Def>` so you get the SAME DSL autocomplete
|
|
72
|
-
* as when you do `type(Def)` directly.
|
|
73
|
-
*/
|
|
74
|
-
args: ArkDef<Def>;
|
|
75
|
-
/**
|
|
76
|
-
* The action handler
|
|
77
|
-
*/
|
|
78
|
-
handler: (db: Kysely<DrizzleToKysely<TSchema>>, args: InferArgs<Def>, ctx: ActionContext<TEnv>) => Promise<TResult>;
|
|
79
|
-
}
|
|
80
|
-
/**
|
|
81
|
-
* An action function that can be called from routes or other actions
|
|
82
|
-
*/
|
|
83
|
-
type Action<TArgs, TResult> = (args: TArgs) => Promise<TResult>;
|
|
84
|
-
/**
|
|
85
|
-
* Return type of defineDatabase
|
|
86
|
-
*/
|
|
87
|
-
interface DatabaseDefinition<TSchema extends Record<string, SQLiteTableWithColumns<any>>> {
|
|
88
|
-
/**
|
|
89
|
-
* Define an action that runs within the database context
|
|
90
|
-
*
|
|
91
|
-
* NOTE: the `const Def` *here* is crucial – it matches ArkType's own
|
|
92
|
-
* `<const def>(def: type.validate<def>)` pattern and is what makes
|
|
93
|
-
* `"string.email"` etc. behave properly.
|
|
94
|
-
*/
|
|
95
|
-
action: <const Def, TResult, TEnv = unknown>(config: ActionConfig<Def, TResult, TSchema, TEnv>) => Action<InferArgs<Def>, TResult>;
|
|
96
|
-
/**
|
|
97
|
-
* Destroy the database instance, clearing all data via `ctx.storage.deleteAll()`.
|
|
98
|
-
* The next action call will re-run migrations and start fresh.
|
|
99
|
-
*
|
|
100
|
-
* For per-tenant databases, targets the current tenant (via `getTenantId()`).
|
|
101
|
-
* For global databases, targets the single shared instance.
|
|
102
|
-
*/
|
|
103
|
-
destroyDatabase: () => Promise<void>;
|
|
104
|
-
}
|
|
105
|
-
/**
|
|
106
|
-
* Information about a parsed database file
|
|
107
|
-
*/
|
|
108
|
-
interface DatabaseInfo {
|
|
109
|
-
/** File path to the database definition file */
|
|
110
|
-
filePath: string;
|
|
111
|
-
/** Database name derived from filename */
|
|
112
|
-
name: string;
|
|
113
|
-
/** Class name for the Durable Object */
|
|
114
|
-
className: string;
|
|
115
|
-
/** Binding name for wrangler config */
|
|
116
|
-
bindingName: string;
|
|
117
|
-
/** Instance strategy */
|
|
118
|
-
instance: 'per-tenant' | 'global';
|
|
119
|
-
/** Path to migrations directory */
|
|
120
|
-
migrationsDir: string;
|
|
121
|
-
/** Schema import source (relative path) */
|
|
122
|
-
schemaImport: string | null;
|
|
123
|
-
/** Names of schema tables */
|
|
124
|
-
schemaTableNames: string[];
|
|
125
|
-
/** Whether the DO should expose a browsable SQL endpoint */
|
|
126
|
-
browsable: boolean | 'development';
|
|
127
|
-
/** Transport for action stubs */
|
|
128
|
-
transport: 'rpc' | 'websocket';
|
|
129
|
-
/** Loaded migrations - Map of name -> chunks (each chunk is array of SQL statements) */
|
|
130
|
-
migrations?: Map<string, string[][]>;
|
|
131
|
-
}
|
|
132
|
-
/**
|
|
133
|
-
* Information about a parsed action
|
|
134
|
-
*/
|
|
135
|
-
interface ActionInfo {
|
|
136
|
-
/** Export name of the action */
|
|
137
|
-
exportName: string;
|
|
138
|
-
/** The args schema (as source code string for codegen) */
|
|
139
|
-
argsSchemaSource: string;
|
|
140
|
-
/** The handler function (as source code string for codegen) */
|
|
141
|
-
handlerSource: string;
|
|
142
|
-
/** The database this action belongs to */
|
|
143
|
-
databaseName: string;
|
|
144
|
-
/** File where the action is defined */
|
|
145
|
-
sourceFile: string;
|
|
146
|
-
}
|
|
147
|
-
/**
|
|
148
|
-
* Result of parsing a database file
|
|
149
|
-
*/
|
|
150
|
-
interface ParsedDatabaseFile {
|
|
151
|
-
/** Absolute file path */
|
|
152
|
-
filePath: string;
|
|
153
|
-
/** Database configuration if this file has defineDatabase */
|
|
154
|
-
database: DatabaseInfo | null;
|
|
155
|
-
/** Actions defined in this file */
|
|
156
|
-
actions: ActionInfo[];
|
|
157
|
-
/** Local imports (for dependency resolution) */
|
|
158
|
-
localImports: Map<string, {
|
|
159
|
-
source: string;
|
|
160
|
-
imported: string;
|
|
161
|
-
}>;
|
|
162
|
-
}
|
|
163
|
-
//#endregion
|
|
164
|
-
//#region src/db/defineDatabase.d.ts
|
|
165
|
-
/**
|
|
166
|
-
* Define a database with its schema and migrations.
|
|
167
|
-
*
|
|
168
|
-
* This function is transformed at build time by the durable-db Vite plugin.
|
|
169
|
-
* At runtime, the actions returned will be RPC stubs that call into the
|
|
170
|
-
* generated Durable Object.
|
|
171
|
-
*
|
|
172
|
-
* Outside the build — code under test calling an action directly — an action
|
|
173
|
-
* runs against the test database `createTestDatabase()` created for its
|
|
174
|
-
* database's tables (the newest one, when there are several); without one it
|
|
175
|
-
* throws.
|
|
176
|
-
*
|
|
177
|
-
* @example
|
|
178
|
-
* ```ts
|
|
179
|
-
* import { defineDatabase } from 'durable-db';
|
|
180
|
-
* import { users, posts } from './schema';
|
|
181
|
-
*
|
|
182
|
-
* export const { action } = defineDatabase({
|
|
183
|
-
* schema: { users, posts },
|
|
184
|
-
* });
|
|
185
|
-
*
|
|
186
|
-
* export const createUser = action({
|
|
187
|
-
* args: { name: 'string', email: 'string.email' },
|
|
188
|
-
* handler: async (db, args) => {
|
|
189
|
-
* return db.insertInto('users').values({
|
|
190
|
-
* id: crypto.randomUUID(),
|
|
191
|
-
* name: args.name,
|
|
192
|
-
* email: args.email
|
|
193
|
-
* }).returningAll().executeTakeFirst();
|
|
194
|
-
* },
|
|
195
|
-
* });
|
|
196
|
-
* ```
|
|
197
|
-
*/
|
|
198
|
-
declare function defineDatabase<TSchema extends Record<string, SQLiteTableWithColumns<any>>>(config: DatabaseConfig<TSchema>): DatabaseDefinition<TSchema>;
|
|
199
|
-
//#endregion
|
|
200
|
-
export { ActionInfo as a, DatabaseDefinition as c, InferArgs as d, ParsedDatabaseFile as f, ActionContext as i, DatabaseInfo as l, Action as n, ArgsSchema as o, ActionConfig as r, DatabaseConfig as s, defineDatabase as t, DrizzleToKysely as u };
|