durable-db 0.0.4 → 0.1.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/dist/cli/bin.js +7 -7
- package/dist/cli/index.d.ts +3 -3
- package/dist/cli/index.js +7 -7
- package/dist/{cli-AzoPlqMA.js → cli-BsxcF5xB.js} +7 -7
- package/dist/context/index.d.ts +1 -1
- package/dist/context/index.js +3 -3
- package/dist/{context-BcWMofTp.js → context-NjS9iv2J.js} +2 -2
- package/dist/db/index.d.ts +2 -2
- package/dist/db/index.js +5 -386
- package/dist/{defineDatabase-B8do9yYZ.d.ts → defineDatabase-B8KJ1xYJ.d.ts} +1 -1
- package/dist/{defineDatabase-C91B-LMQ.js → defineDatabase-DXA53pG3.js} +20 -2
- package/dist/{generator-B4yBWHRz.js → generator-BVwDQvKA.js} +2 -2
- package/dist/{index-nm3-NwUh.d.ts → index-C5C4vaBs.d.ts} +19 -17
- package/dist/index.d.ts +3 -3
- package/dist/index.js +6 -6
- package/dist/kysely-yPsCWEvR.js +388 -0
- package/dist/migrations/generator.d.ts +1 -1
- package/dist/migrations/generator.js +3 -3
- package/dist/migrations/snapshot.d.ts +1 -1
- package/dist/migrations/snapshot.js +1 -1
- package/dist/{parser-qOiKNcYc.js → parser-V6t9KSi1.js} +1 -1
- package/dist/push-DJExqk8A.js +8 -0
- package/dist/{push-BymAOkB9.js → push-tAKBd8b1.js} +5 -5
- package/dist/registry.js +2 -2
- package/dist/schema.d.ts +1 -1
- package/dist/schema.js +1 -1
- package/dist/{state-DDHYOyvD.js → state-CMRjqCDw.js} +1 -1
- package/dist/{state-BUCIODvB.js → state-DCXcGtiu.js} +2 -2
- package/dist/testing/index.d.ts +57 -0
- package/dist/testing/index.js +72 -0
- package/dist/transport/protocol.js +1 -1
- package/dist/transport/websocket.js +1 -1
- package/dist/vite/durableDb.js +6 -6
- package/dist/{websocket-Cwc7933g.js → websocket-eao2rnHL.js} +1 -1
- package/package.json +5 -1
- package/dist/push-CT2aX_AM.js +0 -8
- /package/dist/{debug-BZJbKybf.js → debug-DzvNVBs5.js} +0 -0
- /package/dist/{index-DN2vKPw_.d.ts → index-DaDdJ_cg.d.ts} +0 -0
- /package/dist/{out-CuoxKfaH.js → out-on886o85.js} +0 -0
- /package/dist/{protocol-v7B4_9xU.js → protocol-DEubbBbR.js} +0 -0
- /package/dist/{schema-ou5L5I9Y.js → schema-IFq7ovQq.js} +0 -0
- /package/dist/{schema-CjGvnsaZ.d.ts → schema-suxWBY7-.d.ts} +0 -0
- /package/dist/{snapshot-CgEvcMZb.js → snapshot-DMW6j2Ix.js} +0 -0
- /package/dist/{snapshot-oD3wAJF2.d.ts → snapshot-DOerumb7.d.ts} +0 -0
package/dist/cli/bin.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
import "../debug-
|
|
3
|
-
import "../state-
|
|
4
|
-
import "../snapshot-
|
|
5
|
-
import "../generator-
|
|
6
|
-
import "../parser-
|
|
7
|
-
import "../push-
|
|
8
|
-
import { n as registerDbCommands } from "../cli-
|
|
2
|
+
import "../debug-DzvNVBs5.js";
|
|
3
|
+
import "../state-CMRjqCDw.js";
|
|
4
|
+
import "../snapshot-DMW6j2Ix.js";
|
|
5
|
+
import "../generator-BVwDQvKA.js";
|
|
6
|
+
import "../parser-V6t9KSi1.js";
|
|
7
|
+
import "../push-tAKBd8b1.js";
|
|
8
|
+
import { n as registerDbCommands } from "../cli-BsxcF5xB.js";
|
|
9
9
|
import { Command } from "commander";
|
|
10
10
|
|
|
11
11
|
//#region src/cli/bin.ts
|
package/dist/cli/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { t as Snapshot } from "../snapshot-
|
|
2
|
-
import { l as DatabaseInfo } from "../defineDatabase-
|
|
3
|
-
import "../index-
|
|
1
|
+
import { t as Snapshot } from "../snapshot-DOerumb7.js";
|
|
2
|
+
import { l as DatabaseInfo } from "../defineDatabase-B8KJ1xYJ.js";
|
|
3
|
+
import "../index-C5C4vaBs.js";
|
|
4
4
|
import { Command } from "commander";
|
|
5
5
|
|
|
6
6
|
//#region src/cli/command.d.ts
|
package/dist/cli/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import "../debug-
|
|
2
|
-
import { _ as saveDevState, a as generateEpoch, c as getDevPaths, f as loadDevMigrations, l as getInstanceKey, m as loadDevState, s as getDevEpoch } from "../state-
|
|
3
|
-
import "../snapshot-
|
|
4
|
-
import "../generator-
|
|
5
|
-
import "../parser-
|
|
6
|
-
import { a as loadSchema, i as discoverDatabases, n as pushOne, o as reportCliError, r as diffSchema, t as push } from "../push-
|
|
7
|
-
import { a as resetAndPush, c as generate, i as reset, l as generateOne, n as registerDbCommands, o as formatStatus, r as validate, s as status, t as createDbCommand } from "../cli-
|
|
1
|
+
import "../debug-DzvNVBs5.js";
|
|
2
|
+
import { _ as saveDevState, a as generateEpoch, c as getDevPaths, f as loadDevMigrations, l as getInstanceKey, m as loadDevState, s as getDevEpoch } from "../state-CMRjqCDw.js";
|
|
3
|
+
import "../snapshot-DMW6j2Ix.js";
|
|
4
|
+
import "../generator-BVwDQvKA.js";
|
|
5
|
+
import "../parser-V6t9KSi1.js";
|
|
6
|
+
import { a as loadSchema, i as discoverDatabases, n as pushOne, o as reportCliError, r as diffSchema, t as push } from "../push-tAKBd8b1.js";
|
|
7
|
+
import { a as resetAndPush, c as generate, i as reset, l as generateOne, n as registerDbCommands, o as formatStatus, r as validate, s as status, t as createDbCommand } from "../cli-BsxcF5xB.js";
|
|
8
8
|
|
|
9
9
|
export { createDbCommand, diffSchema, discoverDatabases, formatStatus, generate, generateEpoch, generateOne, getDevEpoch, getDevPaths, getInstanceKey, loadDevMigrations, loadDevState, loadSchema, push, pushOne, registerDbCommands, reportCliError, reset, resetAndPush, saveDevState, status, validate };
|
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { i as init_debug, t as debugCli } from "./debug-
|
|
2
|
-
import { _ as saveDevState, a as generateEpoch, c as getDevPaths, d as init_state, f as loadDevMigrations, m as loadDevState, t as clearDatabaseDevState } from "./state-
|
|
3
|
-
import { a as hashSnapshot } from "./snapshot-
|
|
4
|
-
import { a as loadMigrationFiles, i as generateMigrationName, l as saveSnapshot, n as generateCreateTableSQL, s as loadSnapshot, t as buildAndLoadSchema } from "./generator-
|
|
5
|
-
import { a as resolveImportPath } from "./parser-
|
|
6
|
-
import { a as loadSchema, i as discoverDatabases, o as reportCliError, r as diffSchema, t as push } from "./push-
|
|
1
|
+
import { i as init_debug, t as debugCli } from "./debug-DzvNVBs5.js";
|
|
2
|
+
import { _ as saveDevState, a as generateEpoch, c as getDevPaths, d as init_state, f as loadDevMigrations, m as loadDevState, t as clearDatabaseDevState } from "./state-CMRjqCDw.js";
|
|
3
|
+
import { a as hashSnapshot } from "./snapshot-DMW6j2Ix.js";
|
|
4
|
+
import { a as loadMigrationFiles, i as generateMigrationName, l as saveSnapshot, n as generateCreateTableSQL, s as loadSnapshot, t as buildAndLoadSchema } from "./generator-BVwDQvKA.js";
|
|
5
|
+
import { a as resolveImportPath } from "./parser-V6t9KSi1.js";
|
|
6
|
+
import { a as loadSchema, i as discoverDatabases, o as reportCliError, r as diffSchema, t as push } from "./push-tAKBd8b1.js";
|
|
7
7
|
import * as fs from "node:fs";
|
|
8
8
|
import * as path from "node:path";
|
|
9
9
|
import { Command } from "commander";
|
|
@@ -322,7 +322,7 @@ async function reset(ctx = {}, options = {}) {
|
|
|
322
322
|
* Convenience function that combines reset + push
|
|
323
323
|
*/
|
|
324
324
|
async function resetAndPush(ctx = {}, options = {}) {
|
|
325
|
-
const { push: push$1 } = await import("./push-
|
|
325
|
+
const { push: push$1 } = await import("./push-DJExqk8A.js");
|
|
326
326
|
return {
|
|
327
327
|
reset: await reset(ctx, options),
|
|
328
328
|
push: await push$1(ctx)
|
package/dist/context/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { a as setTenantIdResolver, i as hasTenantId, n as getInstanceKey, r as getTenantId, t as getDevInstanceKeySuffix } from "../index-
|
|
1
|
+
import { a as setTenantIdResolver, i as hasTenantId, n as getInstanceKey, r as getTenantId, t as getDevInstanceKeySuffix } from "../index-DaDdJ_cg.js";
|
|
2
2
|
export { getDevInstanceKeySuffix, getInstanceKey, getTenantId, hasTenantId, setTenantIdResolver };
|
package/dist/context/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import "../debug-
|
|
2
|
-
import "../state-
|
|
3
|
-
import { a as setTenantIdResolver, i as hasTenantId, n as getInstanceKey, r as getTenantId, t as getDevInstanceKeySuffix } from "../context-
|
|
1
|
+
import "../debug-DzvNVBs5.js";
|
|
2
|
+
import "../state-CMRjqCDw.js";
|
|
3
|
+
import { a as setTenantIdResolver, i as hasTenantId, n as getInstanceKey, r as getTenantId, t as getDevInstanceKeySuffix } from "../context-NjS9iv2J.js";
|
|
4
4
|
|
|
5
5
|
export { getDevInstanceKeySuffix, getInstanceKey, getTenantId, hasTenantId, setTenantIdResolver };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { s as __toCommonJS } from "./debug-
|
|
2
|
-
import { d as init_state, v as state_exports } from "./state-
|
|
1
|
+
import { s as __toCommonJS } from "./debug-DzvNVBs5.js";
|
|
2
|
+
import { d as init_state, v as state_exports } from "./state-CMRjqCDw.js";
|
|
3
3
|
|
|
4
4
|
//#region src/context/index.ts
|
|
5
5
|
/**
|
package/dist/db/index.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import { a as ActionInfo, c as DatabaseDefinition, d as InferArgs, f as ParsedDatabaseFile, i as ActionContext, l as DatabaseInfo, n as Action, o as ArgsSchema, r as ActionConfig, s as DatabaseConfig, t as defineDatabase, u as DrizzleToKysely } from "../defineDatabase-
|
|
2
|
-
import { _ as
|
|
1
|
+
import { a as ActionInfo, c as DatabaseDefinition, d as InferArgs, f as ParsedDatabaseFile, i as ActionContext, l as DatabaseInfo, n as Action, o as ArgsSchema, r as ActionConfig, s as DatabaseConfig, t as defineDatabase, u as DrizzleToKysely } from "../defineDatabase-B8KJ1xYJ.js";
|
|
2
|
+
import { _ as SqliteDurableObject, a as QueryResult, b as createKyselyFromSql, c as DateSerializePlugin, d as createDrizzlePlugins, f as dateSerializers, g as Migrations, h as MigrationSchemaConflictError, i as KyselyPlugin, l as DrizzleDefaultsPlugin, m as MigrationAttemptInfo, n as CompiledQuery, o as studio, p as Migration, r as Kysely, s as type, t as CamelCasePlugin, u as SchemaPlugin, v as CreateKyselyOptions, y as SqlExecutor } from "../index-C5C4vaBs.js";
|
|
3
3
|
export { Action, ActionConfig, ActionContext, ActionInfo, ArgsSchema, CamelCasePlugin, CompiledQuery, CreateKyselyOptions, DatabaseConfig, DatabaseDefinition, DatabaseInfo, DateSerializePlugin, DrizzleDefaultsPlugin, DrizzleToKysely, InferArgs, Kysely, KyselyPlugin, Migration, MigrationAttemptInfo, MigrationSchemaConflictError, Migrations, ParsedDatabaseFile, QueryResult, SchemaPlugin, SqlExecutor, SqliteDurableObject, createDrizzlePlugins, createKyselyFromSql, dateSerializers, defineDatabase, studio, type };
|
package/dist/db/index.js
CHANGED
|
@@ -1,395 +1,14 @@
|
|
|
1
|
-
import { i as init_debug, n as debugMigrations } from "../debug-
|
|
2
|
-
import { t as type } from "../out-
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { toCamelCase, toSnakeCase } from "drizzle-orm/casing";
|
|
1
|
+
import { i as init_debug, n as debugMigrations } from "../debug-DzvNVBs5.js";
|
|
2
|
+
import { t as type } from "../out-on886o85.js";
|
|
3
|
+
import { n as defineDatabase } from "../defineDatabase-DXA53pG3.js";
|
|
4
|
+
import { a as createDrizzlePlugins, i as SchemaPlugin, n as DateSerializePlugin, o as dateSerializers, r as DrizzleDefaultsPlugin, t as createKyselyFromSql } from "../kysely-yPsCWEvR.js";
|
|
6
5
|
import { DurableObject } from "cloudflare:workers";
|
|
7
|
-
import { CamelCasePlugin
|
|
8
|
-
import { Table, getTableColumns, getTableName, is, isSQLWrapper } from "drizzle-orm";
|
|
6
|
+
import { CamelCasePlugin } from "kysely";
|
|
9
7
|
import { studio } from "@outerbase/browsable-durable-object";
|
|
10
8
|
|
|
11
|
-
//#region src/db/plugins.ts
|
|
12
|
-
/**
|
|
13
|
-
* Schema-aware CamelCasePlugin that uses Drizzle schema for column name mapping.
|
|
14
|
-
*
|
|
15
|
-
* Extends CamelCasePlugin to handle non-standard column names that don't follow
|
|
16
|
-
* simple camelCase ↔ snake_case conversion. For example, a JS property `userId`
|
|
17
|
-
* mapped to SQL column `user_identifier` would be handled correctly.
|
|
18
|
-
*
|
|
19
|
-
* Falls back to standard CamelCasePlugin behavior for columns not in the schema.
|
|
20
|
-
*/
|
|
21
|
-
var SchemaPlugin = class extends CamelCasePlugin$1 {
|
|
22
|
-
jsToSql;
|
|
23
|
-
sqlToJs;
|
|
24
|
-
constructor(schema) {
|
|
25
|
-
super();
|
|
26
|
-
this.jsToSql = /* @__PURE__ */ new Map();
|
|
27
|
-
this.sqlToJs = /* @__PURE__ */ new Map();
|
|
28
|
-
for (const [schemaKey, table] of Object.entries(schema)) {
|
|
29
|
-
const tableConfig = getTableConfig(table);
|
|
30
|
-
const sqlTableName = tableConfig.name;
|
|
31
|
-
if (schemaKey !== sqlTableName) {
|
|
32
|
-
this.jsToSql.set(schemaKey, sqlTableName);
|
|
33
|
-
this.sqlToJs.set(sqlTableName, schemaKey);
|
|
34
|
-
}
|
|
35
|
-
const sqlColumnNames = new Set(tableConfig.columns.map((c) => c.name));
|
|
36
|
-
for (const [propertyName, value] of Object.entries(table)) if (value && typeof value === "object" && "name" in value && typeof value.name === "string" && sqlColumnNames.has(value.name) && propertyName !== value.name) {
|
|
37
|
-
this.jsToSql.set(propertyName, value.name);
|
|
38
|
-
this.sqlToJs.set(value.name, propertyName);
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
snakeCase(str) {
|
|
43
|
-
return this.jsToSql.get(str) ?? super.snakeCase(str);
|
|
44
|
-
}
|
|
45
|
-
camelCase(str) {
|
|
46
|
-
return this.sqlToJs.get(str) ?? super.camelCase(str);
|
|
47
|
-
}
|
|
48
|
-
};
|
|
49
|
-
function getKyselyTableName(node) {
|
|
50
|
-
if (node && TableNode.is(node)) return node.table.identifier.name;
|
|
51
|
-
return null;
|
|
52
|
-
}
|
|
53
|
-
function getKyselyColumnName(node) {
|
|
54
|
-
if (!node) return null;
|
|
55
|
-
if (ColumnNode.is(node)) return node.column.name;
|
|
56
|
-
if (ColumnUpdateNode.is(node) && node.column) return getKyselyColumnName(node.column);
|
|
57
|
-
return null;
|
|
58
|
-
}
|
|
59
|
-
function convertDrizzleSql(drizzleSql) {
|
|
60
|
-
return drizzleSql.sql.split("?").reduce((acc, part, index) => {
|
|
61
|
-
const param = index < drizzleSql.params.length ? drizzleSql.params[index] : "";
|
|
62
|
-
return acc + part + param;
|
|
63
|
-
}, "");
|
|
64
|
-
}
|
|
65
|
-
function createValueNode(val) {
|
|
66
|
-
if (isSQLWrapper(val)) {
|
|
67
|
-
const drizzleSql = new SQLiteSyncDialect().sqlToQuery(val);
|
|
68
|
-
return sql.raw(convertDrizzleSql(drizzleSql)).toOperationNode();
|
|
69
|
-
}
|
|
70
|
-
return ValueNode.create(val);
|
|
71
|
-
}
|
|
72
|
-
function extractDefaults(schemas) {
|
|
73
|
-
const defaults = /* @__PURE__ */ new Map();
|
|
74
|
-
for (const schema of Object.values(schemas)) {
|
|
75
|
-
const tableName = toCamelCase(getTableName(schema));
|
|
76
|
-
const columns = getTableColumns(schema);
|
|
77
|
-
const tableDefaults = {};
|
|
78
|
-
for (const column of Object.values(columns)) {
|
|
79
|
-
const { defaultFn, onUpdateFn, name } = column;
|
|
80
|
-
if (defaultFn || onUpdateFn) tableDefaults[name] = {
|
|
81
|
-
defaultFn,
|
|
82
|
-
onUpdateFn
|
|
83
|
-
};
|
|
84
|
-
}
|
|
85
|
-
if (Object.keys(tableDefaults).length > 0) defaults.set(tableName, tableDefaults);
|
|
86
|
-
}
|
|
87
|
-
return defaults;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Plugin that handles Drizzle's defaultFn and onUpdateFn during Kysely queries.
|
|
91
|
-
*
|
|
92
|
-
* On INSERT: auto-populates columns that have a defaultFn but weren't provided,
|
|
93
|
-
* and resolves DefaultInsertValueNode markers to actual values.
|
|
94
|
-
*
|
|
95
|
-
* On UPDATE: auto-adds column updates for columns with onUpdateFn that weren't
|
|
96
|
-
* already included in the SET clause.
|
|
97
|
-
*/
|
|
98
|
-
var DrizzleDefaultsPlugin = class {
|
|
99
|
-
defaults;
|
|
100
|
-
constructor(drizzleSchemas) {
|
|
101
|
-
this.defaults = extractDefaults(drizzleSchemas);
|
|
102
|
-
}
|
|
103
|
-
transformInsertQuery(node) {
|
|
104
|
-
const tableName = getKyselyTableName(node.into);
|
|
105
|
-
if (!tableName) return node;
|
|
106
|
-
const tableDefaults = this.defaults.get(tableName);
|
|
107
|
-
if (!tableDefaults) return node;
|
|
108
|
-
if (!node.values || !ValuesNode.is(node.values)) return node;
|
|
109
|
-
const providedColumns = node.columns ?? [];
|
|
110
|
-
const providedColumnNames = providedColumns.map((col) => col.column.name);
|
|
111
|
-
const missingDefaults = Object.entries(tableDefaults).filter(([colName, { defaultFn }]) => defaultFn && !providedColumnNames.includes(colName)).map(([colName, { defaultFn }]) => ({
|
|
112
|
-
name: colName,
|
|
113
|
-
defaultFn
|
|
114
|
-
}));
|
|
115
|
-
const newColumns = [...providedColumns, ...missingDefaults.map(({ name }) => ColumnNode.create(name))];
|
|
116
|
-
const newRows = node.values.values.map((rowNode) => {
|
|
117
|
-
if (missingDefaults.length === 0 && PrimitiveValueListNode.is(rowNode)) return rowNode;
|
|
118
|
-
let originalValues;
|
|
119
|
-
if (PrimitiveValueListNode.is(rowNode)) originalValues = rowNode.values.map((val) => ValueNode.create(val));
|
|
120
|
-
else if (ValueListNode.is(rowNode)) originalValues = [...rowNode.values];
|
|
121
|
-
else throw new Error("Unsupported row node type in insert query");
|
|
122
|
-
const processedValues = originalValues.map((val, idx) => {
|
|
123
|
-
if (val.kind === "DefaultInsertValueNode") {
|
|
124
|
-
const colDef = tableDefaults[providedColumns[idx].column.name];
|
|
125
|
-
if (colDef?.defaultFn) return createValueNode(colDef.defaultFn());
|
|
126
|
-
}
|
|
127
|
-
return val;
|
|
128
|
-
});
|
|
129
|
-
const missingValues = missingDefaults.map(({ defaultFn }) => createValueNode(defaultFn()));
|
|
130
|
-
return ValueListNode.create([...processedValues, ...missingValues]);
|
|
131
|
-
});
|
|
132
|
-
return InsertQueryNode.cloneWith(node, {
|
|
133
|
-
columns: newColumns,
|
|
134
|
-
values: ValuesNode.create(newRows)
|
|
135
|
-
});
|
|
136
|
-
}
|
|
137
|
-
transformUpdateQuery(node) {
|
|
138
|
-
const tableName = getKyselyTableName(node.table);
|
|
139
|
-
if (!tableName) return node;
|
|
140
|
-
const tableDefaults = this.defaults.get(tableName);
|
|
141
|
-
if (!tableDefaults) return node;
|
|
142
|
-
const existingUpdates = new Set(node.updates?.map(getKyselyColumnName) || []);
|
|
143
|
-
const newUpdates = node.updates ? [...node.updates] : [];
|
|
144
|
-
for (const [colName, { onUpdateFn }] of Object.entries(tableDefaults)) {
|
|
145
|
-
if (!onUpdateFn || existingUpdates.has(colName)) continue;
|
|
146
|
-
newUpdates.push(ColumnUpdateNode.create(ColumnNode.create(colName), createValueNode(onUpdateFn())));
|
|
147
|
-
}
|
|
148
|
-
if (newUpdates.length === (node.updates?.length || 0)) return node;
|
|
149
|
-
return {
|
|
150
|
-
...node,
|
|
151
|
-
updates: newUpdates
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
transformQuery(args) {
|
|
155
|
-
const { node } = args;
|
|
156
|
-
switch (node.kind) {
|
|
157
|
-
case "InsertQueryNode": return this.transformInsertQuery(node);
|
|
158
|
-
case "UpdateQueryNode": return this.transformUpdateQuery(node);
|
|
159
|
-
default: return node;
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
async transformResult(args) {
|
|
163
|
-
return args.result;
|
|
164
|
-
}
|
|
165
|
-
};
|
|
166
|
-
/**
|
|
167
|
-
* Date serialization helpers for SQLite
|
|
168
|
-
* SQLite stores dates as TEXT in ISO format (without timezone)
|
|
169
|
-
*/
|
|
170
|
-
const dateSerializers = {
|
|
171
|
-
serialize(value) {
|
|
172
|
-
return value.toISOString().replace("T", " ").slice(0, 19);
|
|
173
|
-
},
|
|
174
|
-
deserialize(value) {
|
|
175
|
-
const normalized = value.replace(" ", "T");
|
|
176
|
-
if (/(?:Z|[+-]\d{2}:?\d{2})$/.test(normalized)) return new Date(normalized);
|
|
177
|
-
return /* @__PURE__ */ new Date(normalized + "Z");
|
|
178
|
-
},
|
|
179
|
-
isDateString(value) {
|
|
180
|
-
return /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}/.test(value);
|
|
181
|
-
},
|
|
182
|
-
isSerializedDateString(value) {
|
|
183
|
-
return /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(value);
|
|
184
|
-
}
|
|
185
|
-
};
|
|
186
|
-
/**
|
|
187
|
-
* Collect the names of columns that Drizzle declares as date-typed
|
|
188
|
-
* (e.g. `integer({ mode: 'timestamp' })`). Includes both the SQL column
|
|
189
|
-
* name and the JS property name, since result rows may be seen in either
|
|
190
|
-
* form depending on where this plugin sits relative to CamelCasePlugin.
|
|
191
|
-
*/
|
|
192
|
-
function collectDateColumnNames(schema) {
|
|
193
|
-
const dateColumns = /* @__PURE__ */ new Set();
|
|
194
|
-
for (const table of Object.values(schema)) {
|
|
195
|
-
const columns = getTableColumns(table);
|
|
196
|
-
for (const [propertyName, column] of Object.entries(columns)) if (column.dataType === "date") {
|
|
197
|
-
dateColumns.add(column.name);
|
|
198
|
-
dateColumns.add(propertyName);
|
|
199
|
-
dateColumns.add(toSnakeCase(propertyName));
|
|
200
|
-
}
|
|
201
|
-
}
|
|
202
|
-
return dateColumns;
|
|
203
|
-
}
|
|
204
|
-
/**
|
|
205
|
-
* Plugin that serializes/deserializes Date objects for SQLite
|
|
206
|
-
*
|
|
207
|
-
* On query: Converts Date objects to ISO strings
|
|
208
|
-
* On result: Converts stored date strings back to Date objects — but only
|
|
209
|
-
* for values in the exact format the write path produces, and (when a schema
|
|
210
|
-
* is provided) only for columns Drizzle declares as date-typed. Plain text
|
|
211
|
-
* columns holding date-looking strings round-trip verbatim.
|
|
212
|
-
*/
|
|
213
|
-
var DateSerializePlugin = class {
|
|
214
|
-
/**
|
|
215
|
-
* Names of date-typed columns (SQL and JS forms) when constructed with a
|
|
216
|
-
* schema; null means "no schema available", which falls back to matching
|
|
217
|
-
* on value format alone.
|
|
218
|
-
*/
|
|
219
|
-
dateColumns;
|
|
220
|
-
constructor(schema) {
|
|
221
|
-
this.dateColumns = schema ? collectDateColumnNames(schema) : null;
|
|
222
|
-
}
|
|
223
|
-
transformQuery(args) {
|
|
224
|
-
return this.transformNode(args.node);
|
|
225
|
-
}
|
|
226
|
-
transformNode(node) {
|
|
227
|
-
if (node === null || node === void 0) return node;
|
|
228
|
-
if (node instanceof Date) return dateSerializers.serialize(node);
|
|
229
|
-
if (Array.isArray(node)) return node.map((item) => this.transformNode(item));
|
|
230
|
-
if (typeof node === "object") {
|
|
231
|
-
const result = {};
|
|
232
|
-
for (const [key, value] of Object.entries(node)) result[key] = this.transformNode(value);
|
|
233
|
-
return result;
|
|
234
|
-
}
|
|
235
|
-
return node;
|
|
236
|
-
}
|
|
237
|
-
async transformResult(args) {
|
|
238
|
-
return {
|
|
239
|
-
...args.result,
|
|
240
|
-
rows: args.result.rows.map((row) => this.transformRow(row))
|
|
241
|
-
};
|
|
242
|
-
}
|
|
243
|
-
transformRow(row) {
|
|
244
|
-
const result = {};
|
|
245
|
-
for (const [key, value] of Object.entries(row)) if (typeof value === "string" && this.shouldDeserialize(key, value)) result[key] = dateSerializers.deserialize(value);
|
|
246
|
-
else result[key] = value;
|
|
247
|
-
return result;
|
|
248
|
-
}
|
|
249
|
-
shouldDeserialize(key, value) {
|
|
250
|
-
if (this.dateColumns && !this.dateColumns.has(key)) return false;
|
|
251
|
-
return dateSerializers.isSerializedDateString(value);
|
|
252
|
-
}
|
|
253
|
-
};
|
|
254
|
-
/**
|
|
255
|
-
* Validate that a schema object is shaped like a record of Drizzle tables.
|
|
256
|
-
*
|
|
257
|
-
* Throws a descriptive error pointing at the offending key when the schema
|
|
258
|
-
* is not a plain object or when any value isn't a Drizzle `Table`. This
|
|
259
|
-
* catches the common foot-guns (passing a Drizzle relations helper, a plain
|
|
260
|
-
* column builder, or `undefined` because of a missing import) before Kysely
|
|
261
|
-
* plugins crash with an unhelpful stack deep inside their constructors.
|
|
262
|
-
*/
|
|
263
|
-
function assertValidSchema(schema) {
|
|
264
|
-
if (schema === null || typeof schema !== "object" || Array.isArray(schema)) throw new Error(`[db] Invalid schema: expected a record of Drizzle tables (e.g. { users, posts }), got ${Array.isArray(schema) ? "array" : schema === null ? "null" : typeof schema}.`);
|
|
265
|
-
for (const [key, value] of Object.entries(schema)) if (!is(value, Table)) {
|
|
266
|
-
const got = value === null ? "null" : value === void 0 ? "undefined" : typeof value;
|
|
267
|
-
throw new Error(`[db] Invalid schema: '${key}' is not a Drizzle table (got ${got}). Make sure every value is defined with table() from 'durable-db/schema'.`);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
/**
|
|
271
|
-
* Create all recommended plugins for use with Drizzle schemas.
|
|
272
|
-
*
|
|
273
|
-
* Includes:
|
|
274
|
-
* - DrizzleDefaultsPlugin: auto-populates defaultFn/onUpdateFn values
|
|
275
|
-
* - SchemaPlugin (extends CamelCasePlugin): schema-aware camelCase ↔ snake_case
|
|
276
|
-
* - DateSerializePlugin: Date ↔ SQLite text serialization
|
|
277
|
-
*
|
|
278
|
-
* When camelCase is false, SchemaPlugin is omitted.
|
|
279
|
-
*/
|
|
280
|
-
function createDrizzlePlugins(schema, camelCase = true) {
|
|
281
|
-
assertValidSchema(schema);
|
|
282
|
-
const plugins = [];
|
|
283
|
-
plugins.push(new DrizzleDefaultsPlugin(schema));
|
|
284
|
-
if (camelCase) plugins.push(new SchemaPlugin(schema));
|
|
285
|
-
plugins.push(new DateSerializePlugin(schema));
|
|
286
|
-
return plugins;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
//#endregion
|
|
290
9
|
//#region src/db/SqliteDurableObject.ts
|
|
291
10
|
init_debug();
|
|
292
11
|
/**
|
|
293
|
-
* A Kysely driver that uses Cloudflare DO SQLite storage
|
|
294
|
-
*/
|
|
295
|
-
var CloudflareSqliteDriver = class {
|
|
296
|
-
sql;
|
|
297
|
-
constructor(sql$1) {
|
|
298
|
-
this.sql = sql$1;
|
|
299
|
-
}
|
|
300
|
-
async init() {}
|
|
301
|
-
async acquireConnection() {
|
|
302
|
-
return new CloudflareSqliteConnection(this.sql);
|
|
303
|
-
}
|
|
304
|
-
async beginTransaction(connection, settings) {
|
|
305
|
-
if (settings.isolationLevel) throw new Error("SQLite does not support isolation levels");
|
|
306
|
-
await connection.executeQuery(createCompiledQuery("BEGIN"));
|
|
307
|
-
}
|
|
308
|
-
async commitTransaction(connection) {
|
|
309
|
-
await connection.executeQuery(createCompiledQuery("COMMIT"));
|
|
310
|
-
}
|
|
311
|
-
async rollbackTransaction(connection) {
|
|
312
|
-
await connection.executeQuery(createCompiledQuery("ROLLBACK"));
|
|
313
|
-
}
|
|
314
|
-
async releaseConnection() {}
|
|
315
|
-
async destroy() {}
|
|
316
|
-
};
|
|
317
|
-
/**
|
|
318
|
-
* Helper to create a CompiledQuery object
|
|
319
|
-
*/
|
|
320
|
-
function createCompiledQuery(sql$1, parameters = []) {
|
|
321
|
-
return {
|
|
322
|
-
sql: sql$1,
|
|
323
|
-
parameters,
|
|
324
|
-
query: {
|
|
325
|
-
kind: "RawNode",
|
|
326
|
-
sqlFragments: [sql$1],
|
|
327
|
-
parameters: []
|
|
328
|
-
}
|
|
329
|
-
};
|
|
330
|
-
}
|
|
331
|
-
/**
|
|
332
|
-
* A Kysely connection that wraps Cloudflare's SqlStorage
|
|
333
|
-
*/
|
|
334
|
-
var CloudflareSqliteConnection = class {
|
|
335
|
-
sql;
|
|
336
|
-
constructor(sql$1) {
|
|
337
|
-
this.sql = sql$1;
|
|
338
|
-
}
|
|
339
|
-
async executeQuery(compiledQuery) {
|
|
340
|
-
const { sql: queryStr, parameters } = compiledQuery;
|
|
341
|
-
const isSelect = /^\s*(SELECT|RETURNING)/i.test(queryStr) || /\s+RETURNING\s+/i.test(queryStr);
|
|
342
|
-
try {
|
|
343
|
-
if (isSelect) return {
|
|
344
|
-
rows: this.sql.exec(queryStr, ...parameters).toArray(),
|
|
345
|
-
numAffectedRows: void 0,
|
|
346
|
-
insertId: void 0
|
|
347
|
-
};
|
|
348
|
-
else {
|
|
349
|
-
this.sql.exec(queryStr, ...parameters);
|
|
350
|
-
const changes = this.sql.exec("SELECT changes() as changes").toArray()[0]?.changes ?? 0;
|
|
351
|
-
const lastId = this.sql.exec("SELECT last_insert_rowid() as id").toArray()[0]?.id;
|
|
352
|
-
return {
|
|
353
|
-
rows: [],
|
|
354
|
-
numAffectedRows: BigInt(changes),
|
|
355
|
-
insertId: typeof lastId === "number" || typeof lastId === "bigint" ? BigInt(lastId) : void 0
|
|
356
|
-
};
|
|
357
|
-
}
|
|
358
|
-
} catch (error) {
|
|
359
|
-
throw error;
|
|
360
|
-
}
|
|
361
|
-
}
|
|
362
|
-
streamQuery() {
|
|
363
|
-
throw new Error("Streaming is not supported in Cloudflare SQLite");
|
|
364
|
-
}
|
|
365
|
-
};
|
|
366
|
-
/**
|
|
367
|
-
* Create a Kysely instance from Cloudflare SqlStorage
|
|
368
|
-
*
|
|
369
|
-
* @param sqlStorage - The Cloudflare SqlStorage instance
|
|
370
|
-
* @param options - Configuration options including schema for plugins
|
|
371
|
-
*/
|
|
372
|
-
function createKyselyFromSql(sqlStorage, options = {}) {
|
|
373
|
-
const { schema, plugins = [], camelCase = true } = options;
|
|
374
|
-
const sqliteDialect = new SqliteDialect({ database: null });
|
|
375
|
-
const allPlugins = [];
|
|
376
|
-
if (schema) allPlugins.push(...createDrizzlePlugins(schema, camelCase));
|
|
377
|
-
else if (camelCase) {
|
|
378
|
-
allPlugins.push(new CamelCasePlugin$1());
|
|
379
|
-
allPlugins.push(new DateSerializePlugin());
|
|
380
|
-
} else allPlugins.push(new DateSerializePlugin());
|
|
381
|
-
allPlugins.push(...plugins);
|
|
382
|
-
return new Kysely({
|
|
383
|
-
dialect: {
|
|
384
|
-
createAdapter: () => sqliteDialect.createAdapter(),
|
|
385
|
-
createDriver: () => new CloudflareSqliteDriver(sqlStorage),
|
|
386
|
-
createIntrospector: (db) => sqliteDialect.createIntrospector(db),
|
|
387
|
-
createQueryCompiler: () => sqliteDialect.createQueryCompiler()
|
|
388
|
-
},
|
|
389
|
-
plugins: allPlugins
|
|
390
|
-
});
|
|
391
|
-
}
|
|
392
|
-
/**
|
|
393
12
|
* Maximum number of PITR restore attempts before giving up
|
|
394
13
|
*/
|
|
395
14
|
const MAX_PITR_ATTEMPTS = 3;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { SQLiteTableWithColumns } from "drizzle-orm/sqlite-core";
|
|
2
2
|
import { Kysely } from "kysely";
|
|
3
|
-
import { Kyselify } from "drizzle-orm/kysely";
|
|
4
3
|
import { type } from "arktype";
|
|
4
|
+
import { Kyselify } from "drizzle-orm/kysely";
|
|
5
5
|
|
|
6
6
|
//#region src/db/types.d.ts
|
|
7
7
|
|
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import { t as type } from "./out-
|
|
1
|
+
import { t as type } from "./out-on886o85.js";
|
|
2
2
|
|
|
3
3
|
//#region src/db/defineDatabase.ts
|
|
4
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
|
+
/**
|
|
5
14
|
* Define a database with its schema and migrations.
|
|
6
15
|
*
|
|
7
16
|
* This function is transformed at build time by the durable-db Vite plugin.
|
|
@@ -39,6 +48,15 @@ function defineDatabase(_config) {
|
|
|
39
48
|
if (result instanceof type.errors) throw new Error(`Invalid args for action (${argsHint}): ${result.summary}`);
|
|
40
49
|
throw new Error(`Action (${argsHint}) called without transformation. Make sure the durable-db plugin is configured in your Vite config.`);
|
|
41
50
|
};
|
|
51
|
+
Object.defineProperty(actionFn, ACTION_TEST_META, {
|
|
52
|
+
value: {
|
|
53
|
+
args: actionConfig.args,
|
|
54
|
+
handler: actionConfig.handler,
|
|
55
|
+
validator
|
|
56
|
+
},
|
|
57
|
+
enumerable: false,
|
|
58
|
+
configurable: true
|
|
59
|
+
});
|
|
42
60
|
return actionFn;
|
|
43
61
|
};
|
|
44
62
|
const destroyDatabase = async () => {
|
|
@@ -51,4 +69,4 @@ function defineDatabase(_config) {
|
|
|
51
69
|
}
|
|
52
70
|
|
|
53
71
|
//#endregion
|
|
54
|
-
export { defineDatabase as t };
|
|
72
|
+
export { defineDatabase as n, ACTION_TEST_META as t };
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { i as init_debug, n as debugMigrations } from "./debug-
|
|
2
|
-
import { i as generateSnapshotId, n as generateMigrationStatements, o as snapshotsEqual, r as generateSnapshotFromSchema, t as createEmptySnapshot } from "./snapshot-
|
|
1
|
+
import { i as init_debug, n as debugMigrations } from "./debug-DzvNVBs5.js";
|
|
2
|
+
import { i as generateSnapshotId, n as generateMigrationStatements, o as snapshotsEqual, r as generateSnapshotFromSchema, t as createEmptySnapshot } from "./snapshot-DMW6j2Ix.js";
|
|
3
3
|
import * as fs from "node:fs";
|
|
4
4
|
import * as path from "node:path";
|
|
5
5
|
import * as os from "node:os";
|
|
@@ -5,26 +5,11 @@ import { Table } from "drizzle-orm";
|
|
|
5
5
|
import { studio } from "@outerbase/browsable-durable-object";
|
|
6
6
|
import { type as type$1 } from "arktype";
|
|
7
7
|
|
|
8
|
-
//#region src/db/
|
|
8
|
+
//#region src/db/kysely.d.ts
|
|
9
9
|
/**
|
|
10
10
|
* SQL executor function type
|
|
11
11
|
*/
|
|
12
12
|
type SqlExecutor = (sql: string) => void;
|
|
13
|
-
/**
|
|
14
|
-
* Migration definition - SQL statements with optional breakpoints
|
|
15
|
-
*
|
|
16
|
-
* Each migration is an array of "chunks" - groups of statements separated by breakpoints.
|
|
17
|
-
* Each chunk is executed as a unit. This allows long migrations to be split into
|
|
18
|
-
* smaller pieces that can survive DO restarts.
|
|
19
|
-
*/
|
|
20
|
-
interface Migration {
|
|
21
|
-
/** SQL statement chunks (split by breakpoints) */
|
|
22
|
-
chunks: string[][];
|
|
23
|
-
}
|
|
24
|
-
/**
|
|
25
|
-
* Migrations object - keys are migration names (should sort alphabetically by timestamp)
|
|
26
|
-
*/
|
|
27
|
-
type Migrations = Record<string, Migration>;
|
|
28
13
|
/**
|
|
29
14
|
* Options for creating a Kysely instance
|
|
30
15
|
*/
|
|
@@ -43,6 +28,23 @@ interface CreateKyselyOptions {
|
|
|
43
28
|
* @param options - Configuration options including schema for plugins
|
|
44
29
|
*/
|
|
45
30
|
declare function createKyselyFromSql<T>(sqlStorage: SqlStorage, options?: CreateKyselyOptions): Kysely<T>;
|
|
31
|
+
//#endregion
|
|
32
|
+
//#region src/db/SqliteDurableObject.d.ts
|
|
33
|
+
/**
|
|
34
|
+
* Migration definition - SQL statements with optional breakpoints
|
|
35
|
+
*
|
|
36
|
+
* Each migration is an array of "chunks" - groups of statements separated by breakpoints.
|
|
37
|
+
* Each chunk is executed as a unit. This allows long migrations to be split into
|
|
38
|
+
* smaller pieces that can survive DO restarts.
|
|
39
|
+
*/
|
|
40
|
+
interface Migration {
|
|
41
|
+
/** SQL statement chunks (split by breakpoints) */
|
|
42
|
+
chunks: string[][];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Migrations object - keys are migration names (should sort alphabetically by timestamp)
|
|
46
|
+
*/
|
|
47
|
+
type Migrations = Record<string, Migration>;
|
|
46
48
|
/**
|
|
47
49
|
* Migration attempt info returned by getMigrationAttempts()
|
|
48
50
|
*/
|
|
@@ -346,4 +348,4 @@ declare class DateSerializePlugin implements KyselyPlugin {
|
|
|
346
348
|
*/
|
|
347
349
|
declare function createDrizzlePlugins(schema: Record<string, SQLiteTableWithColumns<any>>, camelCase?: boolean): KyselyPlugin[];
|
|
348
350
|
//#endregion
|
|
349
|
-
export {
|
|
351
|
+
export { SqliteDurableObject as _, QueryResult$1 as a, createKyselyFromSql as b, DateSerializePlugin as c, createDrizzlePlugins as d, dateSerializers as f, Migrations as g, MigrationSchemaConflictError as h, KyselyPlugin$1 as i, DrizzleDefaultsPlugin as l, MigrationAttemptInfo as m, CompiledQuery as n, studio as o, Migration as p, Kysely$1 as r, type$1 as s, CamelCasePlugin$1 as t, SchemaPlugin as u, CreateKyselyOptions as v, SqlExecutor as y };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import { c as DatabaseDefinition, d as InferArgs, i as ActionContext, n as Action, r as ActionConfig, s as DatabaseConfig, t as defineDatabase, u as DrizzleToKysely } from "./defineDatabase-
|
|
2
|
-
import { a as setTenantIdResolver, i as hasTenantId, r as getTenantId } from "./index-
|
|
3
|
-
import { a as integer, c as real, d as uniqueIndex, i as index, l as table, n as blob, o as numeric, r as foreignKey, s as primaryKey, t as AnySQLiteColumn, u as text } from "./schema-
|
|
1
|
+
import { c as DatabaseDefinition, d as InferArgs, i as ActionContext, n as Action, r as ActionConfig, s as DatabaseConfig, t as defineDatabase, u as DrizzleToKysely } from "./defineDatabase-B8KJ1xYJ.js";
|
|
2
|
+
import { a as setTenantIdResolver, i as hasTenantId, r as getTenantId } from "./index-DaDdJ_cg.js";
|
|
3
|
+
import { a as integer, c as real, d as uniqueIndex, i as index, l as table, n as blob, o as numeric, r as foreignKey, s as primaryKey, t as AnySQLiteColumn, u as text } from "./schema-suxWBY7-.js";
|
|
4
4
|
import { Kysely } from "kysely";
|
|
5
5
|
import { type } from "arktype";
|
|
6
6
|
export { type Action, type ActionConfig, type ActionContext, type AnySQLiteColumn, type DatabaseConfig, type DatabaseDefinition, type DrizzleToKysely, type InferArgs, type Kysely, blob, defineDatabase, foreignKey, getTenantId, hasTenantId, index, integer, numeric, primaryKey, real, setTenantIdResolver, table, text, type, uniqueIndex };
|
package/dist/index.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import "./debug-
|
|
2
|
-
import { t as type } from "./out-
|
|
3
|
-
import {
|
|
4
|
-
import { a as numeric, c as table, i as integer, l as text, n as foreignKey, o as primaryKey, r as index, s as real, t as blob, u as uniqueIndex } from "./schema-
|
|
5
|
-
import "./state-
|
|
6
|
-
import { a as setTenantIdResolver, i as hasTenantId, r as getTenantId } from "./context-
|
|
1
|
+
import "./debug-DzvNVBs5.js";
|
|
2
|
+
import { t as type } from "./out-on886o85.js";
|
|
3
|
+
import { n as defineDatabase } from "./defineDatabase-DXA53pG3.js";
|
|
4
|
+
import { a as numeric, c as table, i as integer, l as text, n as foreignKey, o as primaryKey, r as index, s as real, t as blob, u as uniqueIndex } from "./schema-IFq7ovQq.js";
|
|
5
|
+
import "./state-CMRjqCDw.js";
|
|
6
|
+
import { a as setTenantIdResolver, i as hasTenantId, r as getTenantId } from "./context-NjS9iv2J.js";
|
|
7
7
|
|
|
8
8
|
export { blob, defineDatabase, foreignKey, getTenantId, hasTenantId, index, integer, numeric, primaryKey, real, setTenantIdResolver, table, text, type, uniqueIndex };
|