rivetkit 0.0.0-sqlite-uds.2015d80 → 0.0.0-sqlite-profiling-compat.87d61c0
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/browser/client.d.ts +28 -25
- package/dist/browser/client.js +6 -9
- package/dist/browser/client.js.map +1 -1
- package/dist/browser/inspector/client.d.ts +2 -22
- package/dist/browser/inspector/client.js +222 -996
- package/dist/browser/inspector/client.js.map +1 -1
- package/dist/tsup/actor/errors.cjs.map +1 -1
- package/dist/tsup/agent-os/index.cjs +34 -99
- package/dist/tsup/agent-os/index.cjs.map +1 -1
- package/dist/tsup/agent-os/index.d.cts +28 -25
- package/dist/tsup/agent-os/index.d.ts +28 -25
- package/dist/tsup/agent-os/index.js +36 -101
- package/dist/tsup/agent-os/index.js.map +1 -1
- package/dist/tsup/{chunk-MUPT34DY.js → chunk-3GVMLSND.js} +7 -7
- package/dist/tsup/{chunk-OAPVVVTX.cjs → chunk-3MHDOUD7.cjs} +1 -1
- package/dist/tsup/chunk-3MHDOUD7.cjs.map +1 -0
- package/dist/tsup/chunk-4FC7TVS6.cjs.map +1 -1
- package/dist/tsup/{chunk-D5TT3ZDY.js → chunk-B3GLD4IN.js} +2 -22
- package/dist/tsup/chunk-B3GLD4IN.js.map +1 -0
- package/dist/tsup/{chunk-HV4FLJQR.cjs → chunk-BWERJMV6.cjs} +141 -141
- package/dist/tsup/chunk-BWERJMV6.cjs.map +1 -0
- package/dist/tsup/{chunk-CKKFRK2T.cjs → chunk-D363DMNH.cjs} +3 -3
- package/dist/tsup/chunk-D363DMNH.cjs.map +1 -0
- package/dist/tsup/{chunk-COPUTOJF.cjs → chunk-DTRDGPHU.cjs} +7 -7
- package/dist/tsup/chunk-DTRDGPHU.cjs.map +1 -0
- package/dist/tsup/{chunk-5RC3H5MH.cjs → chunk-E4LNLI3W.cjs} +8 -12
- package/dist/tsup/chunk-E4LNLI3W.cjs.map +1 -0
- package/dist/tsup/{chunk-XPXLCL5I.js → chunk-EFU4JW3T.js} +7 -11
- package/dist/tsup/chunk-EFU4JW3T.js.map +1 -0
- package/dist/tsup/{chunk-IVUL2OMZ.cjs → chunk-FNAXD5BQ.cjs} +4 -24
- package/dist/tsup/chunk-FNAXD5BQ.cjs.map +1 -0
- package/dist/tsup/{chunk-YPIVGZVX.cjs → chunk-GM45LG7L.cjs} +10 -18
- package/dist/tsup/chunk-GM45LG7L.cjs.map +1 -0
- package/dist/tsup/{chunk-DNGOGOHS.js → chunk-O3ZUMAN7.js} +3 -3
- package/dist/tsup/{chunk-VBASTLLY.js → chunk-OAUFXOG7.js} +2 -2
- package/dist/tsup/{chunk-VP3RWGXE.cjs → chunk-P6F5MEWX.cjs} +5 -5
- package/dist/tsup/chunk-P6F5MEWX.cjs.map +1 -0
- package/dist/tsup/{chunk-VL3UZ6XX.js → chunk-PT7AC5PC.js} +4 -12
- package/dist/tsup/chunk-PT7AC5PC.js.map +1 -0
- package/dist/tsup/{chunk-5NJB7EE2.js → chunk-UV7INX3N.js} +2 -2
- package/dist/tsup/chunk-VE2X4KMG.cjs.map +1 -1
- package/dist/tsup/{chunk-3GLNEWGY.js → chunk-XIX5DOZN.js} +1 -1
- package/dist/tsup/chunk-XIX5DOZN.js.map +1 -0
- package/dist/tsup/chunk-ZA7FLHKH.cjs.map +1 -1
- package/dist/tsup/client/mod.cjs +7 -7
- package/dist/tsup/client/mod.cjs.map +1 -1
- package/dist/tsup/client/mod.d.cts +3 -3
- package/dist/tsup/client/mod.d.ts +3 -3
- package/dist/tsup/client/mod.js +6 -6
- package/dist/tsup/common/log.cjs +2 -4
- package/dist/tsup/common/log.cjs.map +1 -1
- package/dist/tsup/common/log.d.cts +1 -5
- package/dist/tsup/common/log.d.ts +1 -5
- package/dist/tsup/common/log.js +3 -5
- package/dist/tsup/common/websocket.cjs +3 -3
- package/dist/tsup/common/websocket.cjs.map +1 -1
- package/dist/tsup/common/websocket.js +2 -2
- package/dist/tsup/{config-DLOxy81E.d.cts → config-B0LNDrLY.d.cts} +25 -5
- package/dist/tsup/{config-DLOxy81E.d.ts → config-B0LNDrLY.d.ts} +25 -5
- package/dist/tsup/{config-Ds6DCRhV.d.ts → config-BR1Nn0QQ.d.ts} +5 -22
- package/dist/tsup/{config-CxG53eQo.d.cts → config-tCvkwzyN.d.cts} +5 -22
- package/dist/tsup/{context-Cfbm4QLr.d.ts → context-D4WDwf9m.d.cts} +2 -3
- package/dist/tsup/{context-8KlcOYIs.d.cts → context-oc9zhA3x.d.ts} +2 -3
- package/dist/tsup/db/drizzle.cjs +72 -144
- package/dist/tsup/db/drizzle.cjs.map +1 -1
- package/dist/tsup/db/drizzle.d.cts +2 -4
- package/dist/tsup/db/drizzle.d.ts +2 -4
- package/dist/tsup/db/drizzle.js +72 -144
- package/dist/tsup/db/drizzle.js.map +1 -1
- package/dist/tsup/db/mod.cjs +28 -90
- package/dist/tsup/db/mod.cjs.map +1 -1
- package/dist/tsup/db/mod.d.cts +8 -4
- package/dist/tsup/db/mod.d.ts +8 -4
- package/dist/tsup/db/mod.js +28 -90
- package/dist/tsup/db/mod.js.map +1 -1
- package/dist/tsup/dynamic/mod.cjs +2 -2
- package/dist/tsup/dynamic/mod.cjs.map +1 -1
- package/dist/tsup/dynamic/mod.d.cts +2 -2
- package/dist/tsup/dynamic/mod.d.ts +2 -2
- package/dist/tsup/dynamic/mod.js +1 -1
- package/dist/tsup/inspector/mod.cjs +11 -46
- package/dist/tsup/inspector/mod.cjs.map +1 -1
- package/dist/tsup/inspector/mod.d.cts +15 -37
- package/dist/tsup/inspector/mod.d.ts +15 -37
- package/dist/tsup/inspector/mod.js +10 -45
- package/dist/tsup/inspector/mod.js.map +1 -1
- package/dist/tsup/inspector-tab/mod.cjs +13 -2
- package/dist/tsup/inspector-tab/mod.cjs.map +1 -1
- package/dist/tsup/inspector-tab/mod.d.cts +14 -4
- package/dist/tsup/inspector-tab/mod.d.ts +14 -4
- package/dist/tsup/inspector-tab/mod.js +12 -1
- package/dist/tsup/inspector-tab/mod.js.map +1 -1
- package/dist/tsup/mod.cjs +91 -191
- package/dist/tsup/mod.cjs.map +1 -1
- package/dist/tsup/mod.d.cts +4 -4
- package/dist/tsup/mod.d.ts +4 -4
- package/dist/tsup/mod.js +27 -127
- package/dist/tsup/mod.js.map +1 -1
- package/dist/tsup/test/mod.cjs +10 -10
- package/dist/tsup/test/mod.cjs.map +1 -1
- package/dist/tsup/test/mod.d.cts +2 -2
- package/dist/tsup/test/mod.d.ts +2 -2
- package/dist/tsup/test/mod.js +6 -6
- package/dist/tsup/utils.cjs +2 -2
- package/dist/tsup/utils.cjs.map +1 -1
- package/dist/tsup/utils.js +1 -1
- package/dist/tsup/workflow/mod.cjs +11 -14
- package/dist/tsup/workflow/mod.cjs.map +1 -1
- package/dist/tsup/workflow/mod.d.cts +4 -4
- package/dist/tsup/workflow/mod.d.ts +4 -4
- package/dist/tsup/workflow/mod.js +5 -8
- package/dist/tsup/workflow/mod.js.map +1 -1
- package/package.json +14 -9
- package/src/actor/config.ts +0 -2
- package/src/common/bare/transport/v1.ts +0 -36
- package/src/common/database/config.ts +24 -19
- package/src/common/database/mod.test.ts +2 -91
- package/src/common/database/mod.ts +38 -102
- package/src/common/database/native-database.ts +4 -19
- package/src/common/log.ts +8 -14
- package/src/db/drizzle.ts +86 -189
- package/src/db/mod.ts +1 -0
- package/src/inspector/client.browser.ts +16 -73
- package/src/inspector/mod.ts +1 -1
- package/src/inspector-tab/mod.ts +15 -0
- package/src/registry/config/index.ts +0 -3
- package/src/registry/index.ts +0 -4
- package/src/registry/napi-runtime.ts +1 -25
- package/src/registry/native.ts +13 -95
- package/src/registry/runtime.ts +6 -18
- package/src/registry/wasm-runtime.ts +0 -22
- package/src/workflow/context.ts +0 -4
- package/src/workflow/inspector.ts +0 -8
- package/dist/tsup/chunk-3GLNEWGY.js.map +0 -1
- package/dist/tsup/chunk-5RC3H5MH.cjs.map +0 -1
- package/dist/tsup/chunk-CKKFRK2T.cjs.map +0 -1
- package/dist/tsup/chunk-COPUTOJF.cjs.map +0 -1
- package/dist/tsup/chunk-D5TT3ZDY.js.map +0 -1
- package/dist/tsup/chunk-HV4FLJQR.cjs.map +0 -1
- package/dist/tsup/chunk-IVUL2OMZ.cjs.map +0 -1
- package/dist/tsup/chunk-OAPVVVTX.cjs.map +0 -1
- package/dist/tsup/chunk-VL3UZ6XX.js.map +0 -1
- package/dist/tsup/chunk-VP3RWGXE.cjs.map +0 -1
- package/dist/tsup/chunk-XPXLCL5I.js.map +0 -1
- package/dist/tsup/chunk-YPIVGZVX.cjs.map +0 -1
- package/src/common/bare/generated/actor-uds-protocol/v1.ts +0 -802
- package/src/common/bare/generated/inspector/v5.ts +0 -1016
- /package/dist/tsup/{chunk-MUPT34DY.js.map → chunk-3GVMLSND.js.map} +0 -0
- /package/dist/tsup/{chunk-DNGOGOHS.js.map → chunk-O3ZUMAN7.js.map} +0 -0
- /package/dist/tsup/{chunk-VBASTLLY.js.map → chunk-OAUFXOG7.js.map} +0 -0
- /package/dist/tsup/{chunk-5NJB7EE2.js.map → chunk-UV7INX3N.js.map} +0 -0
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type {
|
|
2
|
+
DatabaseProvider,
|
|
3
|
+
RawAccess,
|
|
4
|
+
SqliteDatabase,
|
|
5
|
+
SqliteProfilingOptions,
|
|
6
|
+
} from "./config";
|
|
2
7
|
import { isSqliteBindingObject, toSqliteBindings } from "./shared";
|
|
3
8
|
|
|
4
9
|
export type { RawAccess } from "./config";
|
|
5
10
|
|
|
6
|
-
interface DatabaseFactoryConfig {
|
|
11
|
+
export interface DatabaseFactoryConfig {
|
|
7
12
|
onMigrate?: (db: RawAccess) => Promise<void> | void;
|
|
13
|
+
/**
|
|
14
|
+
* @experimental This entire configuration surface is subject to change.
|
|
15
|
+
*/
|
|
16
|
+
profiling?: SqliteProfilingOptions;
|
|
8
17
|
}
|
|
9
18
|
|
|
10
|
-
const MIGRATION_LEASE_TIMEOUT_MS = 60_000;
|
|
11
|
-
|
|
12
19
|
function hasMultipleStatements(query: string): boolean {
|
|
13
20
|
const trimmed = query.trim().replace(/;+$/, "").trimEnd();
|
|
14
21
|
return trimmed.includes(";");
|
|
@@ -16,8 +23,10 @@ function hasMultipleStatements(query: string): boolean {
|
|
|
16
23
|
|
|
17
24
|
export function db({
|
|
18
25
|
onMigrate,
|
|
26
|
+
profiling,
|
|
19
27
|
}: DatabaseFactoryConfig = {}): DatabaseProvider<RawAccess> {
|
|
20
28
|
return {
|
|
29
|
+
sqliteProfiling: profiling,
|
|
21
30
|
createClient: async (ctx) => {
|
|
22
31
|
const nativeDatabaseProvider = ctx.nativeDatabaseProvider;
|
|
23
32
|
if (!nativeDatabaseProvider) {
|
|
@@ -28,7 +37,6 @@ export function db({
|
|
|
28
37
|
|
|
29
38
|
const db = await nativeDatabaseProvider.open(ctx.actorId);
|
|
30
39
|
let closed = false;
|
|
31
|
-
let transactionActive = false;
|
|
32
40
|
const ensureOpen = () => {
|
|
33
41
|
if (closed) {
|
|
34
42
|
throw new Error(
|
|
@@ -37,10 +45,7 @@ export function db({
|
|
|
37
45
|
}
|
|
38
46
|
};
|
|
39
47
|
|
|
40
|
-
const
|
|
41
|
-
key: string;
|
|
42
|
-
timeoutMs?: number;
|
|
43
|
-
}): RawAccess => ({
|
|
48
|
+
const client: RawAccess = {
|
|
44
49
|
execute: async <
|
|
45
50
|
TRow extends Record<string, unknown> = Record<
|
|
46
51
|
string,
|
|
@@ -51,11 +56,6 @@ export function db({
|
|
|
51
56
|
...args: unknown[]
|
|
52
57
|
): Promise<TRow[]> => {
|
|
53
58
|
ensureOpen();
|
|
54
|
-
if (!lease && transactionActive) {
|
|
55
|
-
throw new Error(
|
|
56
|
-
"Use the tx argument inside db.transaction(); the outer database client cannot run work from the transaction callback.",
|
|
57
|
-
);
|
|
58
|
-
}
|
|
59
59
|
|
|
60
60
|
const kvReadsBefore = ctx.metrics?.totalKvReads ?? 0;
|
|
61
61
|
const kvWritesBefore = ctx.metrics?.totalKvWrites ?? 0;
|
|
@@ -71,7 +71,6 @@ export function db({
|
|
|
71
71
|
const { rows, columns } = await db.execute(
|
|
72
72
|
query,
|
|
73
73
|
bindings,
|
|
74
|
-
lease,
|
|
75
74
|
);
|
|
76
75
|
return rows.map((row) =>
|
|
77
76
|
rowToObject<TRow>(row, columns),
|
|
@@ -79,17 +78,13 @@ export function db({
|
|
|
79
78
|
}
|
|
80
79
|
|
|
81
80
|
if (!hasMultipleStatements(query)) {
|
|
82
|
-
const { rows, columns } = await db.execute(
|
|
83
|
-
query,
|
|
84
|
-
undefined,
|
|
85
|
-
lease,
|
|
86
|
-
);
|
|
81
|
+
const { rows, columns } = await db.execute(query);
|
|
87
82
|
return rows.map((row) =>
|
|
88
83
|
rowToObject<TRow>(row, columns),
|
|
89
84
|
);
|
|
90
85
|
}
|
|
91
86
|
|
|
92
|
-
return await execMultiStatement<TRow>(db, query
|
|
87
|
+
return await execMultiStatement<TRow>(db, query);
|
|
93
88
|
} finally {
|
|
94
89
|
const durationMs = performance.now() - start;
|
|
95
90
|
ctx.metrics?.trackSql(query, durationMs);
|
|
@@ -108,48 +103,6 @@ export function db({
|
|
|
108
103
|
}
|
|
109
104
|
}
|
|
110
105
|
},
|
|
111
|
-
transaction: async <T>(
|
|
112
|
-
callback: (tx: RawAccess) => Promise<T> | T,
|
|
113
|
-
): Promise<T> => {
|
|
114
|
-
if (lease || transactionActive) {
|
|
115
|
-
throw new Error(
|
|
116
|
-
"Nested or concurrent db.transaction() calls are not supported.",
|
|
117
|
-
);
|
|
118
|
-
}
|
|
119
|
-
const transactionLease = { key: crypto.randomUUID() };
|
|
120
|
-
const tx = createClient(transactionLease);
|
|
121
|
-
await db.execute("BEGIN", undefined, transactionLease);
|
|
122
|
-
transactionActive = true;
|
|
123
|
-
try {
|
|
124
|
-
const result = await callback(tx);
|
|
125
|
-
await db.execute("COMMIT", undefined, transactionLease);
|
|
126
|
-
return result;
|
|
127
|
-
} catch (error) {
|
|
128
|
-
try {
|
|
129
|
-
await db.execute(
|
|
130
|
-
"ROLLBACK",
|
|
131
|
-
undefined,
|
|
132
|
-
transactionLease,
|
|
133
|
-
);
|
|
134
|
-
} catch {
|
|
135
|
-
// A deadline poisons the lease key, so its cleanup rollback is
|
|
136
|
-
// expected to fail. Preserve the callback or commit error.
|
|
137
|
-
}
|
|
138
|
-
throw error;
|
|
139
|
-
} finally {
|
|
140
|
-
transactionActive = false;
|
|
141
|
-
}
|
|
142
|
-
},
|
|
143
|
-
__withSqliteLease: async <T>(
|
|
144
|
-
timeoutMs: number,
|
|
145
|
-
callback: (leased: RawAccess) => Promise<T> | T,
|
|
146
|
-
): Promise<T> => {
|
|
147
|
-
const internalLease = {
|
|
148
|
-
key: crypto.randomUUID(),
|
|
149
|
-
timeoutMs,
|
|
150
|
-
};
|
|
151
|
-
return await callback(createClient(internalLease));
|
|
152
|
-
},
|
|
153
106
|
close: async () => {
|
|
154
107
|
if (!closed) {
|
|
155
108
|
closed = true;
|
|
@@ -157,15 +110,12 @@ export function db({
|
|
|
157
110
|
}
|
|
158
111
|
},
|
|
159
112
|
nativeMetrics: () => db.nativeMetrics?.() ?? null,
|
|
160
|
-
}
|
|
161
|
-
const client = createClient();
|
|
113
|
+
};
|
|
162
114
|
return client;
|
|
163
115
|
},
|
|
164
116
|
onMigrate: async (client) => {
|
|
165
117
|
if (onMigrate) {
|
|
166
|
-
await withMigrationSavepoint(client, (
|
|
167
|
-
onMigrate(leased),
|
|
168
|
-
);
|
|
118
|
+
await withMigrationSavepoint(client, () => onMigrate(client));
|
|
169
119
|
}
|
|
170
120
|
},
|
|
171
121
|
};
|
|
@@ -185,47 +135,33 @@ function rowToObject<TRow extends Record<string, unknown>>(
|
|
|
185
135
|
async function execMultiStatement<TRow extends Record<string, unknown>>(
|
|
186
136
|
db: SqliteDatabase,
|
|
187
137
|
query: string,
|
|
188
|
-
lease?: { key: string; timeoutMs?: number },
|
|
189
138
|
): Promise<TRow[]> {
|
|
190
139
|
const results: Record<string, unknown>[] = [];
|
|
191
140
|
let columnNames: string[] | null = null;
|
|
192
|
-
await db.exec(
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
results.push(rowToObject(row, columnNames));
|
|
199
|
-
},
|
|
200
|
-
lease,
|
|
201
|
-
);
|
|
141
|
+
await db.exec(query, (row: unknown[], columns: string[]) => {
|
|
142
|
+
if (!columnNames) {
|
|
143
|
+
columnNames = columns;
|
|
144
|
+
}
|
|
145
|
+
results.push(rowToObject(row, columnNames));
|
|
146
|
+
});
|
|
202
147
|
return results as TRow[];
|
|
203
148
|
}
|
|
204
149
|
|
|
205
150
|
async function withMigrationSavepoint<T>(
|
|
206
151
|
client: RawAccess,
|
|
207
|
-
callback: (
|
|
152
|
+
callback: () => Promise<T> | T,
|
|
208
153
|
): Promise<T> {
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
} finally {
|
|
223
|
-
await leased.execute(
|
|
224
|
-
"RELEASE SAVEPOINT __rivet_on_migrate",
|
|
225
|
-
);
|
|
226
|
-
}
|
|
227
|
-
throw error;
|
|
228
|
-
}
|
|
229
|
-
},
|
|
230
|
-
);
|
|
154
|
+
await client.execute("SAVEPOINT __rivet_on_migrate");
|
|
155
|
+
try {
|
|
156
|
+
const result = await callback();
|
|
157
|
+
await client.execute("RELEASE SAVEPOINT __rivet_on_migrate");
|
|
158
|
+
return result;
|
|
159
|
+
} catch (error) {
|
|
160
|
+
try {
|
|
161
|
+
await client.execute("ROLLBACK TO SAVEPOINT __rivet_on_migrate");
|
|
162
|
+
} finally {
|
|
163
|
+
await client.execute("RELEASE SAVEPOINT __rivet_on_migrate");
|
|
164
|
+
}
|
|
165
|
+
throw error;
|
|
166
|
+
}
|
|
231
167
|
}
|
|
@@ -3,7 +3,6 @@ import type {
|
|
|
3
3
|
SqliteBindings,
|
|
4
4
|
SqliteDatabase,
|
|
5
5
|
SqliteExecuteResult,
|
|
6
|
-
SqliteLeaseOptions,
|
|
7
6
|
SqliteNativeMetrics,
|
|
8
7
|
} from "./config";
|
|
9
8
|
|
|
@@ -67,16 +66,10 @@ interface NativeExecuteResult {
|
|
|
67
66
|
}
|
|
68
67
|
|
|
69
68
|
export interface JsNativeDatabaseLike {
|
|
70
|
-
exec(
|
|
71
|
-
sql: string,
|
|
72
|
-
leaseKey?: string,
|
|
73
|
-
leaseTimeoutMs?: number,
|
|
74
|
-
): Promise<NativeExecResult>;
|
|
69
|
+
exec(sql: string): Promise<NativeExecResult>;
|
|
75
70
|
execute(
|
|
76
71
|
sql: string,
|
|
77
72
|
params?: NativeBindParam[] | null,
|
|
78
|
-
leaseKey?: string,
|
|
79
|
-
leaseTimeoutMs?: number,
|
|
80
73
|
): Promise<NativeExecuteResult>;
|
|
81
74
|
query(
|
|
82
75
|
sql: string,
|
|
@@ -297,7 +290,6 @@ export function wrapJsNativeDatabase(
|
|
|
297
290
|
const executeNative = async (
|
|
298
291
|
sql: string,
|
|
299
292
|
params?: SqliteBindings,
|
|
300
|
-
lease?: SqliteLeaseOptions,
|
|
301
293
|
): Promise<SqliteExecuteResult> => {
|
|
302
294
|
const lastInsertRowIdColumn = lastInsertRowIdColumnName(sql);
|
|
303
295
|
if (lastInsertRowIdColumn) {
|
|
@@ -312,12 +304,7 @@ export function wrapJsNativeDatabase(
|
|
|
312
304
|
const release = gate.enter();
|
|
313
305
|
try {
|
|
314
306
|
const nativeParams = toNativeBindings(sql, params);
|
|
315
|
-
const result = await database.execute(
|
|
316
|
-
sql,
|
|
317
|
-
nativeParams,
|
|
318
|
-
lease?.key,
|
|
319
|
-
lease?.timeoutMs,
|
|
320
|
-
);
|
|
307
|
+
const result = await database.execute(sql, nativeParams);
|
|
321
308
|
if (result.lastInsertRowId !== undefined) {
|
|
322
309
|
lastInsertRowId = result.lastInsertRowId;
|
|
323
310
|
}
|
|
@@ -333,12 +320,11 @@ export function wrapJsNativeDatabase(
|
|
|
333
320
|
async exec(
|
|
334
321
|
sql: string,
|
|
335
322
|
callback?: (row: unknown[], columns: string[]) => void,
|
|
336
|
-
lease?: SqliteLeaseOptions,
|
|
337
323
|
): Promise<void> {
|
|
338
324
|
const release = gate.enter();
|
|
339
325
|
let result: NativeExecResult;
|
|
340
326
|
try {
|
|
341
|
-
result = await database.exec(sql
|
|
327
|
+
result = await database.exec(sql);
|
|
342
328
|
} catch (error) {
|
|
343
329
|
enrichNativeDatabaseError(database, error);
|
|
344
330
|
} finally {
|
|
@@ -354,9 +340,8 @@ export function wrapJsNativeDatabase(
|
|
|
354
340
|
async execute(
|
|
355
341
|
sql: string,
|
|
356
342
|
params?: SqliteBindings,
|
|
357
|
-
lease?: SqliteLeaseOptions,
|
|
358
343
|
): Promise<SqliteExecuteResult> {
|
|
359
|
-
return await executeNative(sql, params
|
|
344
|
+
return await executeNative(sql, params);
|
|
360
345
|
},
|
|
361
346
|
async run(sql: string, params?: SqliteBindings): Promise<void> {
|
|
362
347
|
await executeNative(sql, params);
|
package/src/common/log.ts
CHANGED
|
@@ -62,10 +62,15 @@ export function configureBaseLogger(logger: Logger): void {
|
|
|
62
62
|
}
|
|
63
63
|
|
|
64
64
|
/**
|
|
65
|
-
*
|
|
65
|
+
* Configure the default logger with optional log level.
|
|
66
66
|
*/
|
|
67
|
-
export function
|
|
68
|
-
|
|
67
|
+
export function configureDefaultLogger(logLevel?: LogLevel) {
|
|
68
|
+
// Store the configured log level
|
|
69
|
+
if (logLevel) {
|
|
70
|
+
configuredLogLevel = logLevel;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
baseLogger = pino(
|
|
69
74
|
{
|
|
70
75
|
level: getPinoLevel(logLevel),
|
|
71
76
|
messageKey: "msg",
|
|
@@ -82,18 +87,7 @@ export function makeDefaultLogger(logLevel?: LogLevel): Logger {
|
|
|
82
87
|
},
|
|
83
88
|
createLogfmtDestination(),
|
|
84
89
|
);
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
/**
|
|
88
|
-
* Configure the default logger with optional log level.
|
|
89
|
-
*/
|
|
90
|
-
export function configureDefaultLogger(logLevel?: LogLevel) {
|
|
91
|
-
// Store the configured log level
|
|
92
|
-
if (logLevel) {
|
|
93
|
-
configuredLogLevel = logLevel;
|
|
94
|
-
}
|
|
95
90
|
|
|
96
|
-
baseLogger = makeDefaultLogger(logLevel);
|
|
97
91
|
loggerCache.clear();
|
|
98
92
|
}
|
|
99
93
|
|
package/src/db/drizzle.ts
CHANGED
|
@@ -28,19 +28,8 @@ export {
|
|
|
28
28
|
} from "drizzle-orm/sqlite-core";
|
|
29
29
|
|
|
30
30
|
type DrizzleSchema = Record<string, unknown>;
|
|
31
|
-
type DrizzleDatabase<TSchema extends DrizzleSchema> =
|
|
32
|
-
SqliteRemoteDatabase<TSchema
|
|
33
|
-
"transaction"
|
|
34
|
-
> &
|
|
35
|
-
Omit<RawAccess, "transaction"> & {
|
|
36
|
-
transaction: <T>(
|
|
37
|
-
callback: (
|
|
38
|
-
tx: DrizzleDatabase<TSchema>,
|
|
39
|
-
) => Promise<T> | T,
|
|
40
|
-
) => Promise<T>;
|
|
41
|
-
};
|
|
42
|
-
|
|
43
|
-
const MIGRATION_LEASE_TIMEOUT_MS = 60_000;
|
|
31
|
+
type DrizzleDatabase<TSchema extends DrizzleSchema> =
|
|
32
|
+
SqliteRemoteDatabase<TSchema> & RawAccess;
|
|
44
33
|
|
|
45
34
|
interface DrizzleMigrationJournalEntry {
|
|
46
35
|
idx: number;
|
|
@@ -101,7 +90,6 @@ export function db<TSchema extends DrizzleSchema = Record<string, never>>({
|
|
|
101
90
|
|
|
102
91
|
const nativeDb = await nativeDatabaseProvider.open(ctx.actorId);
|
|
103
92
|
let closed = false;
|
|
104
|
-
let transactionActive = false;
|
|
105
93
|
const ensureOpen = () => {
|
|
106
94
|
if (closed) {
|
|
107
95
|
throw new Error(
|
|
@@ -110,192 +98,107 @@ export function db<TSchema extends DrizzleSchema = Record<string, never>>({
|
|
|
110
98
|
}
|
|
111
99
|
};
|
|
112
100
|
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
params: unknown[],
|
|
120
|
-
method: "run" | "all" | "values" | "get",
|
|
121
|
-
) => {
|
|
122
|
-
ensureOpen();
|
|
123
|
-
if (!lease && transactionActive) {
|
|
124
|
-
throw new Error(
|
|
125
|
-
"Use the tx argument inside db.transaction(); the outer database client cannot run work from the transaction callback.",
|
|
126
|
-
);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
const start = performance.now();
|
|
130
|
-
const kvReadsBefore = ctx.metrics?.totalKvReads ?? 0;
|
|
131
|
-
const kvWritesBefore = ctx.metrics?.totalKvWrites ?? 0;
|
|
132
|
-
try {
|
|
133
|
-
const { rows } = await nativeDb.execute(
|
|
134
|
-
query,
|
|
135
|
-
toSqliteBindings(params),
|
|
136
|
-
lease,
|
|
137
|
-
);
|
|
138
|
-
if (method === "run") {
|
|
139
|
-
return { rows: [] };
|
|
140
|
-
}
|
|
141
|
-
if (method === "get") {
|
|
142
|
-
return { rows: rows[0] };
|
|
143
|
-
}
|
|
144
|
-
return { rows };
|
|
145
|
-
} finally {
|
|
146
|
-
const durationMs = performance.now() - start;
|
|
147
|
-
ctx.metrics?.trackSql(query, durationMs);
|
|
148
|
-
if (ctx.metrics) {
|
|
149
|
-
ctx.log?.debug({
|
|
150
|
-
msg: "sql query",
|
|
151
|
-
query: query.slice(0, 120),
|
|
152
|
-
durationMs,
|
|
153
|
-
kvReads:
|
|
154
|
-
ctx.metrics.totalKvReads - kvReadsBefore,
|
|
155
|
-
kvWrites:
|
|
156
|
-
ctx.metrics.totalKvWrites - kvWritesBefore,
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
};
|
|
101
|
+
const runSql = async (
|
|
102
|
+
query: string,
|
|
103
|
+
params: unknown[],
|
|
104
|
+
method: "run" | "all" | "values" | "get",
|
|
105
|
+
) => {
|
|
106
|
+
ensureOpen();
|
|
161
107
|
|
|
162
|
-
const
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
return await runSql(query, params, method);
|
|
168
|
-
};
|
|
169
|
-
|
|
170
|
-
const drizzleDb = drizzle(callback, {
|
|
171
|
-
schema,
|
|
172
|
-
}) as unknown as DrizzleDatabase<TSchema>;
|
|
173
|
-
drizzleDb.execute = async <
|
|
174
|
-
TRow extends Record<string, unknown> = Record<
|
|
175
|
-
string,
|
|
176
|
-
unknown
|
|
177
|
-
>,
|
|
178
|
-
>(
|
|
179
|
-
query: string,
|
|
180
|
-
...args: unknown[]
|
|
181
|
-
): Promise<TRow[]> => {
|
|
182
|
-
return await executeRaw<TRow>(
|
|
183
|
-
nativeDb,
|
|
184
|
-
ctx,
|
|
185
|
-
ensureOpen,
|
|
108
|
+
const start = performance.now();
|
|
109
|
+
const kvReadsBefore = ctx.metrics?.totalKvReads ?? 0;
|
|
110
|
+
const kvWritesBefore = ctx.metrics?.totalKvWrites ?? 0;
|
|
111
|
+
try {
|
|
112
|
+
const { rows } = await nativeDb.execute(
|
|
186
113
|
query,
|
|
187
|
-
|
|
188
|
-
lease,
|
|
114
|
+
toSqliteBindings(params),
|
|
189
115
|
);
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
transactionCallback: (
|
|
193
|
-
tx: DrizzleDatabase<TSchema>,
|
|
194
|
-
) => Promise<T> | T,
|
|
195
|
-
): Promise<T> => {
|
|
196
|
-
if (lease || transactionActive) {
|
|
197
|
-
throw new Error(
|
|
198
|
-
"Nested or concurrent db.transaction() calls are not supported.",
|
|
199
|
-
);
|
|
116
|
+
if (method === "run") {
|
|
117
|
+
return { rows: [] };
|
|
200
118
|
}
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
await nativeDb.execute(
|
|
204
|
-
"BEGIN",
|
|
205
|
-
undefined,
|
|
206
|
-
transactionLease,
|
|
207
|
-
);
|
|
208
|
-
transactionActive = true;
|
|
209
|
-
try {
|
|
210
|
-
const result = await transactionCallback(tx);
|
|
211
|
-
await nativeDb.execute(
|
|
212
|
-
"COMMIT",
|
|
213
|
-
undefined,
|
|
214
|
-
transactionLease,
|
|
215
|
-
);
|
|
216
|
-
return result;
|
|
217
|
-
} catch (error) {
|
|
218
|
-
try {
|
|
219
|
-
await nativeDb.execute(
|
|
220
|
-
"ROLLBACK",
|
|
221
|
-
undefined,
|
|
222
|
-
transactionLease,
|
|
223
|
-
);
|
|
224
|
-
} catch {
|
|
225
|
-
// Preserve the callback or commit error after lease poisoning.
|
|
226
|
-
}
|
|
227
|
-
throw error;
|
|
228
|
-
} finally {
|
|
229
|
-
transactionActive = false;
|
|
119
|
+
if (method === "get") {
|
|
120
|
+
return { rows: rows[0] };
|
|
230
121
|
}
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
drizzleDb.close = async () => {
|
|
245
|
-
if (!closed) {
|
|
246
|
-
closed = true;
|
|
247
|
-
await nativeDb.close();
|
|
122
|
+
return { rows };
|
|
123
|
+
} finally {
|
|
124
|
+
const durationMs = performance.now() - start;
|
|
125
|
+
ctx.metrics?.trackSql(query, durationMs);
|
|
126
|
+
if (ctx.metrics) {
|
|
127
|
+
ctx.log?.debug({
|
|
128
|
+
msg: "sql query",
|
|
129
|
+
query: query.slice(0, 120),
|
|
130
|
+
durationMs,
|
|
131
|
+
kvReads: ctx.metrics.totalKvReads - kvReadsBefore,
|
|
132
|
+
kvWrites:
|
|
133
|
+
ctx.metrics.totalKvWrites - kvWritesBefore,
|
|
134
|
+
});
|
|
248
135
|
}
|
|
249
|
-
}
|
|
136
|
+
}
|
|
137
|
+
};
|
|
250
138
|
|
|
251
|
-
|
|
139
|
+
const callback: RemoteCallback = async (query, params, method) => {
|
|
140
|
+
return await runSql(query, params, method);
|
|
252
141
|
};
|
|
253
142
|
|
|
254
|
-
|
|
143
|
+
const drizzleDb = drizzle(callback, {
|
|
144
|
+
schema,
|
|
145
|
+
}) as DrizzleDatabase<TSchema>;
|
|
146
|
+
drizzleDb.execute = async <
|
|
147
|
+
TRow extends Record<string, unknown> = Record<string, unknown>,
|
|
148
|
+
>(
|
|
149
|
+
query: string,
|
|
150
|
+
...args: unknown[]
|
|
151
|
+
): Promise<TRow[]> => {
|
|
152
|
+
return await executeRaw<TRow>(
|
|
153
|
+
nativeDb,
|
|
154
|
+
ctx,
|
|
155
|
+
ensureOpen,
|
|
156
|
+
query,
|
|
157
|
+
args,
|
|
158
|
+
);
|
|
159
|
+
};
|
|
160
|
+
drizzleDb.close = async () => {
|
|
161
|
+
if (!closed) {
|
|
162
|
+
closed = true;
|
|
163
|
+
await nativeDb.close();
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
|
|
167
|
+
return drizzleDb;
|
|
255
168
|
},
|
|
256
169
|
onMigrate: async (client) => {
|
|
257
170
|
if (!migrations && !onMigrate) {
|
|
258
171
|
return;
|
|
259
172
|
}
|
|
260
|
-
await withMigrationSavepoint(client, async (
|
|
173
|
+
await withMigrationSavepoint(client, async () => {
|
|
261
174
|
if (migrations) {
|
|
262
|
-
await runMigrations(
|
|
175
|
+
await runMigrations(client, migrations);
|
|
263
176
|
}
|
|
264
177
|
if (onMigrate) {
|
|
265
|
-
await onMigrate(
|
|
178
|
+
await onMigrate(client);
|
|
266
179
|
}
|
|
267
180
|
});
|
|
268
181
|
},
|
|
269
182
|
};
|
|
270
183
|
}
|
|
271
184
|
|
|
272
|
-
async function withMigrationSavepoint<
|
|
273
|
-
client:
|
|
274
|
-
callback: (
|
|
185
|
+
async function withMigrationSavepoint<T>(
|
|
186
|
+
client: RawAccess,
|
|
187
|
+
callback: () => Promise<T> | T,
|
|
275
188
|
): Promise<T> {
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
);
|
|
290
|
-
} finally {
|
|
291
|
-
await leased.execute(
|
|
292
|
-
"RELEASE SAVEPOINT __rivet_on_migrate",
|
|
293
|
-
);
|
|
294
|
-
}
|
|
295
|
-
throw error;
|
|
296
|
-
}
|
|
297
|
-
},
|
|
298
|
-
);
|
|
189
|
+
await client.execute("SAVEPOINT __rivet_on_migrate");
|
|
190
|
+
try {
|
|
191
|
+
const result = await callback();
|
|
192
|
+
await client.execute("RELEASE SAVEPOINT __rivet_on_migrate");
|
|
193
|
+
return result;
|
|
194
|
+
} catch (error) {
|
|
195
|
+
try {
|
|
196
|
+
await client.execute("ROLLBACK TO SAVEPOINT __rivet_on_migrate");
|
|
197
|
+
} finally {
|
|
198
|
+
await client.execute("RELEASE SAVEPOINT __rivet_on_migrate");
|
|
199
|
+
}
|
|
200
|
+
throw error;
|
|
201
|
+
}
|
|
299
202
|
}
|
|
300
203
|
|
|
301
204
|
async function runMigrations<TSchema extends DrizzleSchema>(
|
|
@@ -383,7 +286,6 @@ async function executeRaw<TRow extends Record<string, unknown>>(
|
|
|
383
286
|
ensureOpen: () => void,
|
|
384
287
|
query: string,
|
|
385
288
|
args: unknown[],
|
|
386
|
-
lease?: { key: string; timeoutMs?: number },
|
|
387
289
|
): Promise<TRow[]> {
|
|
388
290
|
ensureOpen();
|
|
389
291
|
|
|
@@ -395,28 +297,23 @@ async function executeRaw<TRow extends Record<string, unknown>>(
|
|
|
395
297
|
const { rows, columns } = await db.execute(
|
|
396
298
|
query,
|
|
397
299
|
toSqliteBindings(args),
|
|
398
|
-
lease,
|
|
399
300
|
);
|
|
400
301
|
return rows.map((row) => rowToObject<TRow>(row, columns));
|
|
401
302
|
}
|
|
402
303
|
|
|
403
304
|
if (!hasMultipleStatements(query)) {
|
|
404
|
-
const { rows, columns } = await db.execute(query
|
|
305
|
+
const { rows, columns } = await db.execute(query);
|
|
405
306
|
return rows.map((row) => rowToObject<TRow>(row, columns));
|
|
406
307
|
}
|
|
407
308
|
|
|
408
309
|
const results: Record<string, unknown>[] = [];
|
|
409
310
|
let columnNames: string[] | null = null;
|
|
410
|
-
await db.exec(
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
results.push(rowToObject(row, columnNames));
|
|
417
|
-
},
|
|
418
|
-
lease,
|
|
419
|
-
);
|
|
311
|
+
await db.exec(query, (row, columns) => {
|
|
312
|
+
if (!columnNames) {
|
|
313
|
+
columnNames = columns;
|
|
314
|
+
}
|
|
315
|
+
results.push(rowToObject(row, columnNames));
|
|
316
|
+
});
|
|
420
317
|
return results as TRow[];
|
|
421
318
|
} finally {
|
|
422
319
|
const durationMs = performance.now() - start;
|