rivetkit 0.0.0-sqlite-query-metrics-v3.1dff84f → 0.0.0-sqlite-sync-3-0-1-final.5663d2f
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 +48 -49
- package/dist/browser/client.js +1 -1
- package/dist/browser/client.js.map +1 -1
- package/dist/browser/inspector/client.js +1 -1
- package/dist/browser/inspector/client.js.map +1 -1
- package/dist/tsup/{chunk-QQIFRR3B.js → chunk-3ZKCK6NL.js} +3 -3
- package/dist/tsup/{chunk-Y4QLVCCS.cjs → chunk-6UDO6UYM.cjs} +2 -2
- package/dist/tsup/{chunk-Y4QLVCCS.cjs.map → chunk-6UDO6UYM.cjs.map} +1 -1
- package/dist/tsup/{chunk-CWN7N5AF.cjs → chunk-C2NON6V4.cjs} +7 -7
- package/dist/tsup/{chunk-CWN7N5AF.cjs.map → chunk-C2NON6V4.cjs.map} +1 -1
- package/dist/tsup/{chunk-HXJ5U2ZI.cjs → chunk-DGU2H5IJ.cjs} +3 -3
- package/dist/tsup/{chunk-HXJ5U2ZI.cjs.map → chunk-DGU2H5IJ.cjs.map} +1 -1
- package/dist/tsup/{chunk-NNCL4IBC.js → chunk-DU7V4SDK.js} +2 -2
- package/dist/tsup/{chunk-NNCL4IBC.js.map → chunk-DU7V4SDK.js.map} +1 -1
- package/dist/tsup/{chunk-Y5CFDGPA.cjs → chunk-EKERHPCE.cjs} +4 -4
- package/dist/tsup/{chunk-Y5CFDGPA.cjs.map → chunk-EKERHPCE.cjs.map} +1 -1
- package/dist/tsup/{chunk-JJUSO7GJ.cjs → chunk-G26OT2Q6.cjs} +3 -3
- package/dist/tsup/{chunk-JJUSO7GJ.cjs.map → chunk-G26OT2Q6.cjs.map} +1 -1
- package/dist/tsup/{chunk-SW33HMM5.js → chunk-GAFMEUAL.js} +3 -3
- package/dist/tsup/{chunk-FT6VRK4C.js → chunk-MT4SROQM.js} +2 -2
- package/dist/tsup/{chunk-HRKFVQPS.js → chunk-SEKTNJU6.js} +4 -4
- package/dist/tsup/{chunk-D7DWNYEO.js → chunk-T7DJZSPH.js} +2 -2
- package/dist/tsup/{chunk-2CTZU7GL.js → chunk-TTMWSZZA.js} +2 -2
- package/dist/tsup/{chunk-HFGXV2CC.cjs → chunk-X567WA3N.cjs} +10 -10
- package/dist/tsup/{chunk-HFGXV2CC.cjs.map → chunk-X567WA3N.cjs.map} +1 -1
- package/dist/tsup/{chunk-22S5RCCJ.cjs → chunk-XLD6TRSE.cjs} +3 -3
- package/dist/tsup/{chunk-22S5RCCJ.cjs.map → chunk-XLD6TRSE.cjs.map} +1 -1
- package/dist/tsup/{chunk-NG5CUXMM.cjs → chunk-YJ2X5STT.cjs} +143 -143
- package/dist/tsup/{chunk-NG5CUXMM.cjs.map → chunk-YJ2X5STT.cjs.map} +1 -1
- package/dist/tsup/{chunk-F5PWAGS7.js → chunk-YNAEH775.js} +2 -2
- package/dist/tsup/client/mod.cjs +5 -5
- package/dist/tsup/client/mod.d.cts +3 -3
- package/dist/tsup/client/mod.d.ts +3 -3
- package/dist/tsup/client/mod.js +4 -4
- package/dist/tsup/common/log.cjs +2 -2
- package/dist/tsup/common/log.js +1 -1
- package/dist/tsup/common/websocket.cjs +3 -3
- package/dist/tsup/common/websocket.js +2 -2
- package/dist/tsup/{config-CBTr4IhE.d.cts → config-BfG1qpYz.d.cts} +34 -50
- package/dist/tsup/{config-CBTr4IhE.d.ts → config-BfG1qpYz.d.ts} +34 -50
- package/dist/tsup/{config-BiiS5T5M.d.ts → config-CASGH-5t.d.ts} +16 -1
- package/dist/tsup/{config-BnMhcPHl.d.cts → config-RMLak20L.d.cts} +16 -1
- package/dist/tsup/{context-iMxXq_Lt.d.ts → context-C1tMYvV-.d.ts} +2 -2
- package/dist/tsup/{context-DVE8XMm1.d.cts → context-CxNE2KvK.d.cts} +2 -2
- package/dist/tsup/db/drizzle.cjs +2 -2
- package/dist/tsup/db/drizzle.d.cts +1 -1
- package/dist/tsup/db/drizzle.d.ts +1 -1
- package/dist/tsup/db/drizzle.js +1 -1
- package/dist/tsup/db/mod.cjs +3 -3
- package/dist/tsup/db/mod.d.cts +2 -2
- package/dist/tsup/db/mod.d.ts +2 -2
- package/dist/tsup/db/mod.js +2 -2
- package/dist/tsup/dynamic/mod.d.cts +2 -2
- package/dist/tsup/dynamic/mod.d.ts +2 -2
- package/dist/tsup/inspector/mod.cjs +5 -5
- package/dist/tsup/inspector/mod.js +4 -4
- package/dist/tsup/inspector/workflow.cjs +4 -4
- package/dist/tsup/inspector/workflow.js +3 -3
- package/dist/tsup/inspector-tab/mod.d.cts +3 -3
- package/dist/tsup/inspector-tab/mod.d.ts +3 -3
- package/dist/tsup/mod.cjs +380 -121
- 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 +300 -41
- package/dist/tsup/mod.js.map +1 -1
- package/dist/tsup/test/mod.cjs +6 -6
- package/dist/tsup/test/mod.d.cts +2 -2
- package/dist/tsup/test/mod.d.ts +2 -2
- package/dist/tsup/test/mod.js +4 -4
- package/dist/tsup/unstable/migrations.d.cts +1 -1
- package/dist/tsup/unstable/migrations.d.ts +1 -1
- package/dist/tsup/utils.cjs +2 -2
- package/dist/tsup/utils.js +1 -1
- package/dist/tsup/workflow/mod.cjs +7 -7
- 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 +4 -4
- package/package.json +9 -9
- package/src/common/database/config.ts +46 -50
- package/src/common/database/native-database.ts +230 -21
- package/src/db/mod.ts +0 -1
- package/src/registry/napi-runtime.ts +78 -0
- package/src/registry/native.ts +57 -17
- package/src/registry/runtime.ts +52 -0
- package/src/registry/wasm-runtime.ts +88 -0
- /package/dist/tsup/{chunk-QQIFRR3B.js.map → chunk-3ZKCK6NL.js.map} +0 -0
- /package/dist/tsup/{chunk-SW33HMM5.js.map → chunk-GAFMEUAL.js.map} +0 -0
- /package/dist/tsup/{chunk-FT6VRK4C.js.map → chunk-MT4SROQM.js.map} +0 -0
- /package/dist/tsup/{chunk-HRKFVQPS.js.map → chunk-SEKTNJU6.js.map} +0 -0
- /package/dist/tsup/{chunk-D7DWNYEO.js.map → chunk-T7DJZSPH.js.map} +0 -0
- /package/dist/tsup/{chunk-2CTZU7GL.js.map → chunk-TTMWSZZA.js.map} +0 -0
- /package/dist/tsup/{chunk-F5PWAGS7.js.map → chunk-YNAEH775.js.map} +0 -0
|
@@ -52,16 +52,13 @@ export interface SqliteTransactionOptions {
|
|
|
52
52
|
* to change without notice.
|
|
53
53
|
*/
|
|
54
54
|
export interface SqliteProfilingOptions {
|
|
55
|
-
/** Exact SQL
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
* All actors in a process enabling the duration histogram must use identical buckets. */
|
|
63
|
-
durationHistogramBucketsSeconds?: number[];
|
|
64
|
-
/** Maximum distinct statement fingerprints per process. Default: 512. */
|
|
55
|
+
/** Exact SQL statements to give fingerprint labels. Profiling defaults to actor
|
|
56
|
+
* aggregates without fingerprint labels. Profile logs remain enabled.
|
|
57
|
+
* RIVETKIT_SQLITE_PROFILING overrides selection with a JSON object containing
|
|
58
|
+
* queries and/or fingerprints arrays. An empty object clears fingerprint selection. */
|
|
59
|
+
queries?: string[];
|
|
60
|
+
/** Exact query fingerprints to label separately, as reported in profile logs. */
|
|
61
|
+
fingerprints?: string[];
|
|
65
62
|
maxTrackedStatementFingerprints?: number;
|
|
66
63
|
maxTrackedTransactionFingerprints?: number;
|
|
67
64
|
maxPrometheusSeries?: number;
|
|
@@ -74,46 +71,6 @@ export interface SqliteProfilingOptions {
|
|
|
74
71
|
diagnosticEventQueueCapacity?: number;
|
|
75
72
|
}
|
|
76
73
|
|
|
77
|
-
/** SQLite profiling metrics. Physical-request metrics and gauges have no query labels. */
|
|
78
|
-
export interface SqliteProfilingMetricsOptions {
|
|
79
|
-
/** Execution count and cumulative seconds. Uses histogram count/sum when enabled, otherwise counters. Default: true. */
|
|
80
|
-
duration?: boolean;
|
|
81
|
-
/** Execution duration histogram. Only applies when duration is enabled. Default: true. */
|
|
82
|
-
durationHistogram?: boolean;
|
|
83
|
-
/** Outcome counters, including success and errors. Default: true. */
|
|
84
|
-
outcomes?: boolean;
|
|
85
|
-
/** Per-query timing phase histograms. Default: false. */
|
|
86
|
-
phaseDurationHistogram?: boolean;
|
|
87
|
-
/** Storage round trips per operation histogram. Default: false. */
|
|
88
|
-
getPagesRoundTripsHistogram?: boolean;
|
|
89
|
-
/** Statements per transaction histogram. Default: false. */
|
|
90
|
-
transactionStatementCountHistogram?: boolean;
|
|
91
|
-
/** Per-query pages returned from storage, including returned prefetch pages. Default: true. */
|
|
92
|
-
pagesFetched?: boolean;
|
|
93
|
-
/** Actor-level page counters by page kind (no query labels). Default: false. */
|
|
94
|
-
localPages?: boolean;
|
|
95
|
-
/** Actor-level byte counters by byte kind (no query labels). Default: false. */
|
|
96
|
-
localBytes?: boolean;
|
|
97
|
-
/** Actor-level physical storage request duration histogram. Default: false. */
|
|
98
|
-
getPagesDurationHistogram?: boolean;
|
|
99
|
-
/** Actor-level pages per storage request histograms, by page kind. Default: false. */
|
|
100
|
-
getPagesPagesHistogram?: boolean;
|
|
101
|
-
/** Actor-level storage response byte histogram. Default: false. */
|
|
102
|
-
getPagesResponseBytesHistogram?: boolean;
|
|
103
|
-
/** Actor-level missing response page counter. Default: false. */
|
|
104
|
-
getPagesMissingPages?: boolean;
|
|
105
|
-
/** Queued SQLite work, aggregated by actor name. Default: true. */
|
|
106
|
-
workerQueueDepth?: boolean;
|
|
107
|
-
/** Executing SQLite work, aggregated by actor name. Default: true. */
|
|
108
|
-
workerInflight?: boolean;
|
|
109
|
-
/** Transaction waiters, aggregated by actor name. Default: true. */
|
|
110
|
-
coordinatorQueueDepth?: boolean;
|
|
111
|
-
/** Counts observations exceeding fingerprint or series limits. Default: true. */
|
|
112
|
-
fingerprintOverflow?: boolean;
|
|
113
|
-
/** Counts diagnostic events dropped by rate limits or backpressure. Default: true. */
|
|
114
|
-
eventDropped?: boolean;
|
|
115
|
-
}
|
|
116
|
-
|
|
117
74
|
export interface SqliteNativeMetrics {
|
|
118
75
|
requestBuildNs: number;
|
|
119
76
|
serializeNs: number;
|
|
@@ -133,7 +90,12 @@ export interface SqliteDatabase {
|
|
|
133
90
|
sql: string,
|
|
134
91
|
callback?: (row: unknown[], columns: string[]) => void,
|
|
135
92
|
): Promise<void>;
|
|
93
|
+
execSync?(
|
|
94
|
+
sql: string,
|
|
95
|
+
callback?: (row: unknown[], columns: string[]) => void,
|
|
96
|
+
): void;
|
|
136
97
|
execute(sql: string, params?: SqliteBindings): Promise<SqliteExecuteResult>;
|
|
98
|
+
executeSync?(sql: string, params?: SqliteBindings): SqliteExecuteResult;
|
|
137
99
|
executeBatch(
|
|
138
100
|
statements: SqliteBatchStatement[],
|
|
139
101
|
): Promise<SqliteExecuteResult[]>;
|
|
@@ -141,12 +103,31 @@ export interface SqliteDatabase {
|
|
|
141
103
|
timeoutMs?: number,
|
|
142
104
|
name?: string,
|
|
143
105
|
): Promise<SqliteTransactionDatabase>;
|
|
106
|
+
beginTransactionSync?(
|
|
107
|
+
timeoutMs?: number,
|
|
108
|
+
name?: string,
|
|
109
|
+
): SynchronousSqliteTransactionDatabase;
|
|
144
110
|
run(sql: string, params?: SqliteBindings): Promise<void>;
|
|
145
111
|
query(sql: string, params?: SqliteBindings): Promise<SqliteQueryResult>;
|
|
146
112
|
nativeMetrics?():
|
|
147
113
|
| SqliteNativeMetrics
|
|
148
114
|
| Promise<SqliteNativeMetrics | null>
|
|
149
115
|
| null;
|
|
116
|
+
/**
|
|
117
|
+
* Returns the ticket of the latest local commit. Synchronous writes return before they are
|
|
118
|
+
* durable; pass the ticket to `whenDurable` before releasing output that depends on them.
|
|
119
|
+
*/
|
|
120
|
+
commitTicket?(): number;
|
|
121
|
+
/**
|
|
122
|
+
* Resolves once the commit `ticket` names and every earlier commit are durable. Rejects with
|
|
123
|
+
* `sqlite.durability_failed` once the database broke and the actor is restarting.
|
|
124
|
+
*/
|
|
125
|
+
whenDurable?(ticket: number): Promise<void>;
|
|
126
|
+
/**
|
|
127
|
+
* Restarts the owning actor from durable state. Every later write fails, including the final
|
|
128
|
+
* state save. For native database providers whose in-memory state can no longer be trusted.
|
|
129
|
+
*/
|
|
130
|
+
abortGeneration?(reason: string): void;
|
|
150
131
|
close(): Promise<void>;
|
|
151
132
|
}
|
|
152
133
|
|
|
@@ -155,11 +136,26 @@ export interface SqliteTransactionDatabase {
|
|
|
155
136
|
sql: string,
|
|
156
137
|
callback?: (row: unknown[], columns: string[]) => void,
|
|
157
138
|
): Promise<void>;
|
|
139
|
+
execSync?(
|
|
140
|
+
sql: string,
|
|
141
|
+
callback?: (row: unknown[], columns: string[]) => void,
|
|
142
|
+
): void;
|
|
158
143
|
execute(sql: string, params?: SqliteBindings): Promise<SqliteExecuteResult>;
|
|
144
|
+
executeSync?(sql: string, params?: SqliteBindings): SqliteExecuteResult;
|
|
159
145
|
commit(): Promise<void>;
|
|
160
146
|
rollback(): Promise<void>;
|
|
161
147
|
}
|
|
162
148
|
|
|
149
|
+
export type SynchronousSqliteTransactionDatabase = SqliteTransactionDatabase & {
|
|
150
|
+
execSync(
|
|
151
|
+
sql: string,
|
|
152
|
+
callback?: (row: unknown[], columns: string[]) => void,
|
|
153
|
+
): void;
|
|
154
|
+
executeSync(sql: string, params?: SqliteBindings): SqliteExecuteResult;
|
|
155
|
+
commitSync(): void;
|
|
156
|
+
rollbackSync(): void;
|
|
157
|
+
};
|
|
158
|
+
|
|
163
159
|
/**
|
|
164
160
|
* Provider for opening native databases from the active runtime.
|
|
165
161
|
*/
|
|
@@ -6,6 +6,7 @@ import type {
|
|
|
6
6
|
SqliteExecuteResult,
|
|
7
7
|
SqliteNativeMetrics,
|
|
8
8
|
SqliteTransactionDatabase,
|
|
9
|
+
SynchronousSqliteTransactionDatabase,
|
|
9
10
|
} from "./config";
|
|
10
11
|
|
|
11
12
|
type NativeBindNoValues = {
|
|
@@ -74,10 +75,15 @@ interface NativeBatchStatement {
|
|
|
74
75
|
|
|
75
76
|
export interface JsNativeDatabaseLike {
|
|
76
77
|
exec(sql: string): Promise<NativeExecResult>;
|
|
78
|
+
execSync(sql: string): NativeExecResult;
|
|
77
79
|
execute(
|
|
78
80
|
sql: string,
|
|
79
81
|
params?: NativeBindParam[] | null,
|
|
80
82
|
): Promise<NativeExecuteResult>;
|
|
83
|
+
executeSync(
|
|
84
|
+
sql: string,
|
|
85
|
+
params?: NativeBindParam[] | null,
|
|
86
|
+
): NativeExecuteResult;
|
|
81
87
|
executeBatch?(
|
|
82
88
|
statements: NativeBatchStatement[],
|
|
83
89
|
): Promise<NativeExecuteResult[]>;
|
|
@@ -89,6 +95,10 @@ export interface JsNativeDatabaseLike {
|
|
|
89
95
|
timeoutMs?: number,
|
|
90
96
|
name?: string,
|
|
91
97
|
): Promise<JsNativeTransactionLike>;
|
|
98
|
+
beginTransactionSync(
|
|
99
|
+
timeoutMs?: number,
|
|
100
|
+
name?: string,
|
|
101
|
+
): JsNativeSynchronousTransactionLike;
|
|
92
102
|
query(
|
|
93
103
|
sql: string,
|
|
94
104
|
params?: NativeBindParam[] | null,
|
|
@@ -99,9 +109,16 @@ export interface JsNativeDatabaseLike {
|
|
|
99
109
|
): Promise<NativeRunResult>;
|
|
100
110
|
metrics?(): SqliteNativeMetrics | null;
|
|
101
111
|
takeLastKvError?(): string | null;
|
|
112
|
+
open(): Promise<void>;
|
|
113
|
+
commitTicket(): number;
|
|
114
|
+
whenDurable(ticket: number): Promise<void>;
|
|
115
|
+
abortGeneration(reason: string): void;
|
|
102
116
|
close(): Promise<void>;
|
|
103
117
|
}
|
|
104
118
|
export type StateAwareSqliteDatabase = SqliteDatabase & {
|
|
119
|
+
open(): Promise<void>;
|
|
120
|
+
commitTicket(): number;
|
|
121
|
+
whenDurable(ticket: number): Promise<void>;
|
|
105
122
|
beginStateTransaction(
|
|
106
123
|
timeoutMs?: number,
|
|
107
124
|
context?: unknown,
|
|
@@ -123,6 +140,28 @@ export interface JsNativeTransactionLike {
|
|
|
123
140
|
rollback(): Promise<void>;
|
|
124
141
|
}
|
|
125
142
|
|
|
143
|
+
export interface JsNativeSynchronousTransactionLike
|
|
144
|
+
extends JsNativeTransactionLike {
|
|
145
|
+
execSync(sql: string): NativeExecResult;
|
|
146
|
+
executeSync(
|
|
147
|
+
sql: string,
|
|
148
|
+
params?: NativeBindParam[] | null,
|
|
149
|
+
): NativeExecuteResult;
|
|
150
|
+
commitSync(): void;
|
|
151
|
+
rollbackSync(): void;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
function isSynchronousTransaction(
|
|
155
|
+
transaction: JsNativeTransactionLike,
|
|
156
|
+
): transaction is JsNativeSynchronousTransactionLike {
|
|
157
|
+
const candidate =
|
|
158
|
+
transaction as Partial<JsNativeSynchronousTransactionLike>;
|
|
159
|
+
return (
|
|
160
|
+
typeof candidate.commitSync === "function" &&
|
|
161
|
+
typeof candidate.rollbackSync === "function"
|
|
162
|
+
);
|
|
163
|
+
}
|
|
164
|
+
|
|
126
165
|
function shouldAttachNativeKvError(message: string): boolean {
|
|
127
166
|
return /i\/o error|unable to open database file/i.test(message);
|
|
128
167
|
}
|
|
@@ -161,13 +200,19 @@ function toNativeBinding(arg: unknown): NativeBindParam {
|
|
|
161
200
|
if (arg === null || arg === undefined) {
|
|
162
201
|
return { kind: "null" };
|
|
163
202
|
}
|
|
164
|
-
if (
|
|
165
|
-
|
|
203
|
+
if (
|
|
204
|
+
typeof arg === "bigint" ||
|
|
205
|
+
(typeof arg === "number" && Number.isInteger(arg))
|
|
206
|
+
) {
|
|
207
|
+
const value = Number(arg);
|
|
208
|
+
if (!Number.isSafeInteger(value)) {
|
|
209
|
+
throw new RangeError(
|
|
210
|
+
"SQLite integer bindings must be within the JavaScript safe integer range.",
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
return { kind: "int", intValue: value };
|
|
166
214
|
}
|
|
167
215
|
if (typeof arg === "number") {
|
|
168
|
-
if (Number.isInteger(arg)) {
|
|
169
|
-
return { kind: "int", intValue: arg };
|
|
170
|
-
}
|
|
171
216
|
return { kind: "float", floatValue: arg };
|
|
172
217
|
}
|
|
173
218
|
if (typeof arg === "string") {
|
|
@@ -185,9 +230,13 @@ function toNativeBinding(arg: unknown): NativeBindParam {
|
|
|
185
230
|
function extractNamedSqliteParameters(sql: string): string[] {
|
|
186
231
|
const orderedNames: string[] = [];
|
|
187
232
|
const seen = new Set<string>();
|
|
188
|
-
|
|
233
|
+
// SQLite identifiers may contain "$" and any non-ASCII character. Consume
|
|
234
|
+
// whole identifiers so their suffixes cannot be mistaken for parameters.
|
|
235
|
+
const pattern =
|
|
236
|
+
/'(?:''|[^'])*'|"(?:""|[^"])*"|`(?:``|[^`])*`|\[[^\]]*\]|--[^\n]*|\/\*[\s\S]*?(?:\*\/|$)|([:@$][\w$\u0080-\u{10FFFF}]+(?:::[\w$\u0080-\u{10FFFF}]+)*(?:\([^)]*\))?)|[A-Za-z_\u0080-\u{10FFFF}][\w$\u0080-\u{10FFFF}]*/gu;
|
|
189
237
|
for (const match of sql.matchAll(pattern)) {
|
|
190
238
|
const name = match[1];
|
|
239
|
+
if (!name) continue;
|
|
191
240
|
if (seen.has(name)) {
|
|
192
241
|
continue;
|
|
193
242
|
}
|
|
@@ -341,21 +390,49 @@ export function wrapJsNativeDatabase(
|
|
|
341
390
|
sql: string,
|
|
342
391
|
params?: SqliteBindings,
|
|
343
392
|
): Promise<SqliteExecuteResult> => {
|
|
344
|
-
const lastInsertRowIdColumn = lastInsertRowIdColumnName(sql);
|
|
345
|
-
if (lastInsertRowIdColumn) {
|
|
346
|
-
return {
|
|
347
|
-
columns: [lastInsertRowIdColumn],
|
|
348
|
-
rows: [[lastInsertRowId ?? 0]],
|
|
349
|
-
changes: 0,
|
|
350
|
-
lastInsertRowId,
|
|
351
|
-
};
|
|
352
|
-
}
|
|
353
|
-
|
|
354
393
|
const release = gate.enter();
|
|
355
394
|
try {
|
|
395
|
+
const lastInsertRowIdColumn = lastInsertRowIdColumnName(sql);
|
|
396
|
+
if (lastInsertRowIdColumn) {
|
|
397
|
+
return {
|
|
398
|
+
columns: [lastInsertRowIdColumn],
|
|
399
|
+
rows: [[lastInsertRowId ?? 0]],
|
|
400
|
+
changes: 0,
|
|
401
|
+
lastInsertRowId,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
|
|
356
405
|
const nativeParams = toNativeBindings(sql, params);
|
|
357
406
|
const result = await database.execute(sql, nativeParams);
|
|
358
|
-
if (result.lastInsertRowId
|
|
407
|
+
if (result.lastInsertRowId != null) {
|
|
408
|
+
lastInsertRowId = result.lastInsertRowId;
|
|
409
|
+
}
|
|
410
|
+
return result;
|
|
411
|
+
} catch (error) {
|
|
412
|
+
enrichNativeDatabaseError(database, error);
|
|
413
|
+
} finally {
|
|
414
|
+
release();
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
const executeNativeSync = (
|
|
418
|
+
sql: string,
|
|
419
|
+
params?: SqliteBindings,
|
|
420
|
+
): SqliteExecuteResult => {
|
|
421
|
+
const release = gate.enter();
|
|
422
|
+
try {
|
|
423
|
+
const nativeParams = toNativeBindings(sql, params);
|
|
424
|
+
const result = database.executeSync(sql, nativeParams);
|
|
425
|
+
// Execute first so runtime availability and transaction admission are enforced.
|
|
426
|
+
const rowIdColumn = lastInsertRowIdColumnName(sql);
|
|
427
|
+
if (rowIdColumn) {
|
|
428
|
+
return {
|
|
429
|
+
columns: [rowIdColumn],
|
|
430
|
+
rows: [[lastInsertRowId ?? 0]],
|
|
431
|
+
changes: 0,
|
|
432
|
+
lastInsertRowId,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
if (result.lastInsertRowId != null) {
|
|
359
436
|
lastInsertRowId = result.lastInsertRowId;
|
|
360
437
|
}
|
|
361
438
|
return result;
|
|
@@ -387,12 +464,34 @@ export function wrapJsNativeDatabase(
|
|
|
387
464
|
callback(row, result.columns);
|
|
388
465
|
}
|
|
389
466
|
},
|
|
467
|
+
execSync(
|
|
468
|
+
sql: string,
|
|
469
|
+
callback?: (row: unknown[], columns: string[]) => void,
|
|
470
|
+
): void {
|
|
471
|
+
const release = gate.enter();
|
|
472
|
+
let result: NativeExecResult;
|
|
473
|
+
try {
|
|
474
|
+
result = database.execSync(sql);
|
|
475
|
+
} catch (error) {
|
|
476
|
+
enrichNativeDatabaseError(database, error);
|
|
477
|
+
} finally {
|
|
478
|
+
release();
|
|
479
|
+
}
|
|
480
|
+
if (callback) {
|
|
481
|
+
for (const row of result.rows) {
|
|
482
|
+
callback(row, result.columns);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
},
|
|
390
486
|
async execute(
|
|
391
487
|
sql: string,
|
|
392
488
|
params?: SqliteBindings,
|
|
393
489
|
): Promise<SqliteExecuteResult> {
|
|
394
490
|
return await executeNative(sql, params);
|
|
395
491
|
},
|
|
492
|
+
executeSync(sql: string, params?: SqliteBindings): SqliteExecuteResult {
|
|
493
|
+
return executeNativeSync(sql, params);
|
|
494
|
+
},
|
|
396
495
|
async executeBatch(
|
|
397
496
|
statements: SqliteBatchStatement[],
|
|
398
497
|
): Promise<SqliteExecuteResult[]> {
|
|
@@ -419,7 +518,7 @@ export function wrapJsNativeDatabase(
|
|
|
419
518
|
return transactionResults;
|
|
420
519
|
})();
|
|
421
520
|
for (const result of results) {
|
|
422
|
-
if (result.lastInsertRowId
|
|
521
|
+
if (result.lastInsertRowId != null) {
|
|
423
522
|
lastInsertRowId = result.lastInsertRowId;
|
|
424
523
|
}
|
|
425
524
|
}
|
|
@@ -451,7 +550,26 @@ export function wrapJsNativeDatabase(
|
|
|
451
550
|
release();
|
|
452
551
|
}
|
|
453
552
|
return wrapTransaction(database, transaction, gate, (result) => {
|
|
454
|
-
if (result.lastInsertRowId
|
|
553
|
+
if (result.lastInsertRowId != null) {
|
|
554
|
+
lastInsertRowId = result.lastInsertRowId;
|
|
555
|
+
}
|
|
556
|
+
});
|
|
557
|
+
},
|
|
558
|
+
beginTransactionSync(
|
|
559
|
+
timeoutMs?: number,
|
|
560
|
+
name?: string,
|
|
561
|
+
): SynchronousSqliteTransactionDatabase {
|
|
562
|
+
const release = gate.enter();
|
|
563
|
+
let transaction: JsNativeSynchronousTransactionLike;
|
|
564
|
+
try {
|
|
565
|
+
transaction = database.beginTransactionSync(timeoutMs, name);
|
|
566
|
+
} catch (error) {
|
|
567
|
+
enrichNativeDatabaseError(database, error);
|
|
568
|
+
} finally {
|
|
569
|
+
release();
|
|
570
|
+
}
|
|
571
|
+
return wrapTransaction(database, transaction, gate, (result) => {
|
|
572
|
+
if (result.lastInsertRowId != null) {
|
|
455
573
|
lastInsertRowId = result.lastInsertRowId;
|
|
456
574
|
}
|
|
457
575
|
});
|
|
@@ -476,7 +594,7 @@ export function wrapJsNativeDatabase(
|
|
|
476
594
|
release();
|
|
477
595
|
}
|
|
478
596
|
return wrapTransaction(database, transaction, gate, (result) => {
|
|
479
|
-
if (result.lastInsertRowId
|
|
597
|
+
if (result.lastInsertRowId != null) {
|
|
480
598
|
lastInsertRowId = result.lastInsertRowId;
|
|
481
599
|
}
|
|
482
600
|
});
|
|
@@ -491,6 +609,25 @@ export function wrapJsNativeDatabase(
|
|
|
491
609
|
nativeMetrics(): SqliteNativeMetrics | null {
|
|
492
610
|
return normalizeNativeMetrics(database.metrics?.());
|
|
493
611
|
},
|
|
612
|
+
async open(): Promise<void> {
|
|
613
|
+
const release = gate.enter();
|
|
614
|
+
try {
|
|
615
|
+
await database.open();
|
|
616
|
+
} catch (error) {
|
|
617
|
+
enrichNativeDatabaseError(database, error);
|
|
618
|
+
} finally {
|
|
619
|
+
release();
|
|
620
|
+
}
|
|
621
|
+
},
|
|
622
|
+
commitTicket(): number {
|
|
623
|
+
return database.commitTicket();
|
|
624
|
+
},
|
|
625
|
+
async whenDurable(ticket: number): Promise<void> {
|
|
626
|
+
await database.whenDurable(ticket);
|
|
627
|
+
},
|
|
628
|
+
abortGeneration(reason: string): void {
|
|
629
|
+
database.abortGeneration(reason);
|
|
630
|
+
},
|
|
494
631
|
async close(): Promise<void> {
|
|
495
632
|
terminalPromise ??= gate.close(() => database.close());
|
|
496
633
|
await terminalPromise;
|
|
@@ -502,13 +639,25 @@ export function wrapJsNativeDatabase(
|
|
|
502
639
|
};
|
|
503
640
|
}
|
|
504
641
|
|
|
642
|
+
function wrapTransaction(
|
|
643
|
+
database: JsNativeDatabaseLike,
|
|
644
|
+
transaction: JsNativeSynchronousTransactionLike,
|
|
645
|
+
gate: NativeCloseGate,
|
|
646
|
+
onExecute: (result: NativeExecuteResult) => void,
|
|
647
|
+
): SynchronousSqliteTransactionDatabase;
|
|
648
|
+
function wrapTransaction(
|
|
649
|
+
database: JsNativeDatabaseLike,
|
|
650
|
+
transaction: JsNativeTransactionLike,
|
|
651
|
+
gate: NativeCloseGate,
|
|
652
|
+
onExecute: (result: NativeExecuteResult) => void,
|
|
653
|
+
): SqliteTransactionDatabase;
|
|
505
654
|
function wrapTransaction(
|
|
506
655
|
database: JsNativeDatabaseLike,
|
|
507
656
|
transaction: JsNativeTransactionLike,
|
|
508
657
|
gate: NativeCloseGate,
|
|
509
658
|
onExecute: (result: NativeExecuteResult) => void,
|
|
510
659
|
): SqliteTransactionDatabase {
|
|
511
|
-
|
|
660
|
+
const wrapped: SqliteTransactionDatabase = {
|
|
512
661
|
async exec(sql, callback) {
|
|
513
662
|
const release = gate.enter();
|
|
514
663
|
let result: NativeExecResult;
|
|
@@ -559,6 +708,66 @@ function wrapTransaction(
|
|
|
559
708
|
}
|
|
560
709
|
},
|
|
561
710
|
};
|
|
711
|
+
|
|
712
|
+
if (isSynchronousTransaction(transaction)) {
|
|
713
|
+
return Object.assign(wrapped, {
|
|
714
|
+
execSync(
|
|
715
|
+
sql: string,
|
|
716
|
+
callback?: (row: unknown[], columns: string[]) => void,
|
|
717
|
+
) {
|
|
718
|
+
const release = gate.enter();
|
|
719
|
+
let result: NativeExecResult;
|
|
720
|
+
try {
|
|
721
|
+
result = transaction.execSync(sql);
|
|
722
|
+
} catch (error) {
|
|
723
|
+
enrichNativeDatabaseError(database, error);
|
|
724
|
+
} finally {
|
|
725
|
+
release();
|
|
726
|
+
}
|
|
727
|
+
if (callback) {
|
|
728
|
+
for (const row of result.rows)
|
|
729
|
+
callback(row, result.columns);
|
|
730
|
+
}
|
|
731
|
+
},
|
|
732
|
+
executeSync(sql: string, params?: SqliteBindings) {
|
|
733
|
+
const release = gate.enter();
|
|
734
|
+
try {
|
|
735
|
+
const result = transaction.executeSync(
|
|
736
|
+
sql,
|
|
737
|
+
toNativeBindings(sql, params),
|
|
738
|
+
);
|
|
739
|
+
onExecute(result);
|
|
740
|
+
return result;
|
|
741
|
+
} catch (error) {
|
|
742
|
+
enrichNativeDatabaseError(database, error);
|
|
743
|
+
} finally {
|
|
744
|
+
release();
|
|
745
|
+
}
|
|
746
|
+
},
|
|
747
|
+
commitSync() {
|
|
748
|
+
const release = gate.enter();
|
|
749
|
+
try {
|
|
750
|
+
transaction.commitSync();
|
|
751
|
+
} catch (error) {
|
|
752
|
+
enrichNativeDatabaseError(database, error);
|
|
753
|
+
} finally {
|
|
754
|
+
release();
|
|
755
|
+
}
|
|
756
|
+
},
|
|
757
|
+
rollbackSync() {
|
|
758
|
+
const release = gate.enter();
|
|
759
|
+
try {
|
|
760
|
+
transaction.rollbackSync();
|
|
761
|
+
} catch (error) {
|
|
762
|
+
enrichNativeDatabaseError(database, error);
|
|
763
|
+
} finally {
|
|
764
|
+
release();
|
|
765
|
+
}
|
|
766
|
+
},
|
|
767
|
+
}) as SynchronousSqliteTransactionDatabase;
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
return wrapped;
|
|
562
771
|
}
|
|
563
772
|
|
|
564
773
|
function lastInsertRowIdColumnName(sql: string): string | undefined {
|
package/src/db/mod.ts
CHANGED
|
@@ -860,6 +860,13 @@ export class NapiCoreRuntime implements CoreRuntime {
|
|
|
860
860
|
return await this.#actorSql(ctx).exec(sql);
|
|
861
861
|
}
|
|
862
862
|
|
|
863
|
+
actorSqlExecSync(
|
|
864
|
+
ctx: ActorContextHandle,
|
|
865
|
+
sql: string,
|
|
866
|
+
): RuntimeSqlExecResult {
|
|
867
|
+
return this.#actorSql(ctx).execSync(sql);
|
|
868
|
+
}
|
|
869
|
+
|
|
863
870
|
async actorSqlExecute(
|
|
864
871
|
ctx: ActorContextHandle,
|
|
865
872
|
sql: string,
|
|
@@ -872,6 +879,18 @@ export class NapiCoreRuntime implements CoreRuntime {
|
|
|
872
879
|
return normalizeRuntimeSqlExecuteResult(result);
|
|
873
880
|
}
|
|
874
881
|
|
|
882
|
+
actorSqlExecuteSync(
|
|
883
|
+
ctx: ActorContextHandle,
|
|
884
|
+
sql: string,
|
|
885
|
+
params?: RuntimeSqlBindParams,
|
|
886
|
+
): RuntimeSqlExecuteResult {
|
|
887
|
+
const result = this.#actorSql(ctx).executeSync(
|
|
888
|
+
sql,
|
|
889
|
+
toNapiSqlBindParams(params),
|
|
890
|
+
);
|
|
891
|
+
return normalizeRuntimeSqlExecuteResult(result);
|
|
892
|
+
}
|
|
893
|
+
|
|
875
894
|
async actorSqlExecuteBatch(
|
|
876
895
|
ctx: ActorContextHandle,
|
|
877
896
|
statements: RuntimeSqlBatchStatement[],
|
|
@@ -893,6 +912,17 @@ export class NapiCoreRuntime implements CoreRuntime {
|
|
|
893
912
|
)) as unknown as SqliteTransactionHandle;
|
|
894
913
|
}
|
|
895
914
|
|
|
915
|
+
actorSqlBeginTransactionSync(
|
|
916
|
+
ctx: ActorContextHandle,
|
|
917
|
+
timeoutMs?: number,
|
|
918
|
+
name?: string,
|
|
919
|
+
): SqliteTransactionHandle {
|
|
920
|
+
return this.#actorSql(ctx).beginTransactionSync(
|
|
921
|
+
timeoutMs,
|
|
922
|
+
name,
|
|
923
|
+
) as unknown as SqliteTransactionHandle;
|
|
924
|
+
}
|
|
925
|
+
|
|
896
926
|
async actorSqlTransactionExec(
|
|
897
927
|
transaction: SqliteTransactionHandle,
|
|
898
928
|
sql: string,
|
|
@@ -900,6 +930,13 @@ export class NapiCoreRuntime implements CoreRuntime {
|
|
|
900
930
|
return await asNativeSqlTransaction(transaction).exec(sql);
|
|
901
931
|
}
|
|
902
932
|
|
|
933
|
+
actorSqlTransactionExecSync(
|
|
934
|
+
transaction: SqliteTransactionHandle,
|
|
935
|
+
sql: string,
|
|
936
|
+
): RuntimeSqlExecResult {
|
|
937
|
+
return asNativeSqlTransaction(transaction).execSync(sql);
|
|
938
|
+
}
|
|
939
|
+
|
|
903
940
|
async actorSqlTransactionExecute(
|
|
904
941
|
transaction: SqliteTransactionHandle,
|
|
905
942
|
sql: string,
|
|
@@ -912,17 +949,39 @@ export class NapiCoreRuntime implements CoreRuntime {
|
|
|
912
949
|
return normalizeRuntimeSqlExecuteResult(result);
|
|
913
950
|
}
|
|
914
951
|
|
|
952
|
+
actorSqlTransactionExecuteSync(
|
|
953
|
+
transaction: SqliteTransactionHandle,
|
|
954
|
+
sql: string,
|
|
955
|
+
params?: RuntimeSqlBindParams,
|
|
956
|
+
): RuntimeSqlExecuteResult {
|
|
957
|
+
const result = asNativeSqlTransaction(transaction).executeSync(
|
|
958
|
+
sql,
|
|
959
|
+
toNapiSqlBindParams(params),
|
|
960
|
+
);
|
|
961
|
+
return normalizeRuntimeSqlExecuteResult(result);
|
|
962
|
+
}
|
|
963
|
+
|
|
915
964
|
async actorSqlTransactionCommit(
|
|
916
965
|
transaction: SqliteTransactionHandle,
|
|
917
966
|
): Promise<void> {
|
|
918
967
|
await asNativeSqlTransaction(transaction).commit();
|
|
919
968
|
}
|
|
920
969
|
|
|
970
|
+
actorSqlTransactionCommitSync(transaction: SqliteTransactionHandle): void {
|
|
971
|
+
asNativeSqlTransaction(transaction).commitSync();
|
|
972
|
+
}
|
|
973
|
+
|
|
921
974
|
async actorSqlTransactionRollback(
|
|
922
975
|
transaction: SqliteTransactionHandle,
|
|
923
976
|
): Promise<void> {
|
|
924
977
|
await asNativeSqlTransaction(transaction).rollback();
|
|
925
978
|
}
|
|
979
|
+
|
|
980
|
+
actorSqlTransactionRollbackSync(
|
|
981
|
+
transaction: SqliteTransactionHandle,
|
|
982
|
+
): void {
|
|
983
|
+
asNativeSqlTransaction(transaction).rollbackSync();
|
|
984
|
+
}
|
|
926
985
|
async actorBeginStateTransaction(
|
|
927
986
|
ctx: ActorContextHandle,
|
|
928
987
|
timeoutMs?: number,
|
|
@@ -986,6 +1045,25 @@ export class NapiCoreRuntime implements CoreRuntime {
|
|
|
986
1045
|
return this.#actorSql(ctx).takeLastKvError?.() ?? null;
|
|
987
1046
|
}
|
|
988
1047
|
|
|
1048
|
+
async actorSqlOpen(ctx: ActorContextHandle): Promise<void> {
|
|
1049
|
+
await this.#actorSql(ctx).open();
|
|
1050
|
+
}
|
|
1051
|
+
|
|
1052
|
+
actorSqlCommitTicket(ctx: ActorContextHandle): number {
|
|
1053
|
+
return this.#actorSql(ctx).commitTicket();
|
|
1054
|
+
}
|
|
1055
|
+
|
|
1056
|
+
actorSqlAbortGeneration(ctx: ActorContextHandle, reason: string): void {
|
|
1057
|
+
this.#actorSql(ctx).abortGeneration(reason);
|
|
1058
|
+
}
|
|
1059
|
+
|
|
1060
|
+
async actorSqlWhenDurable(
|
|
1061
|
+
ctx: ActorContextHandle,
|
|
1062
|
+
ticket: number,
|
|
1063
|
+
): Promise<void> {
|
|
1064
|
+
await this.#actorSql(ctx).whenDurable(ticket);
|
|
1065
|
+
}
|
|
1066
|
+
|
|
989
1067
|
async actorSqlClose(ctx: ActorContextHandle): Promise<void> {
|
|
990
1068
|
const nativeCtx = asNativeActorContext(ctx);
|
|
991
1069
|
if (!this.#sqlOpened.delete(nativeCtx)) {
|