@delali/sirannon-db 0.3.4-next.62 → 0.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +79 -56
- package/dist/backup/index.d.ts +44 -45
- package/dist/backup/index.mjs +7 -7
- package/dist/backup-scheduler/index.d.ts +42 -41
- package/dist/backup-scheduler/index.mjs +4 -4
- package/dist/baseline-Co3iIsN_.d.ts +19 -0
- package/dist/change-tracker-DqCdeEyx.d.ts +128 -0
- package/dist/{chunk-VOSJBZ6Q.mjs → chunk-2ES3DT4A.mjs} +71 -33
- package/dist/{chunk-6CMCO7S4.mjs → chunk-2O3PP4CF.mjs} +1 -1
- package/dist/{chunk-E7Q7HE46.mjs → chunk-3BZLZ47R.mjs} +26 -26
- package/dist/chunk-4KASJKKK.mjs +2501 -0
- package/dist/{chunk-MYNI2WFS.mjs → chunk-52XW3Z76.mjs} +40 -8
- package/dist/chunk-55YRLSM2.mjs +57 -0
- package/dist/{chunk-KDAHYFW7.mjs → chunk-6AOJCNRU.mjs} +5 -5
- package/dist/{chunk-KXTYR2OQ.mjs → chunk-A3PHCO6W.mjs} +39 -111
- package/dist/chunk-ADNWEHGZ.mjs +299 -0
- package/dist/{chunk-2QLXDHAP.mjs → chunk-BLMRNSUG.mjs} +11 -2
- package/dist/chunk-BVMZJOVM.mjs +5 -0
- package/dist/{chunk-IWGIYDMZ.mjs → chunk-BZWDPF4G.mjs} +1 -1
- package/dist/{chunk-YVF2ZJTE.mjs → chunk-CZL4D3BM.mjs} +1 -1
- package/dist/{chunk-7SC2TZAS.mjs → chunk-DX4WGJL7.mjs} +2 -2
- package/dist/{chunk-45MKELYA.mjs → chunk-ESTZZPT5.mjs} +132 -13
- package/dist/{chunk-QP5L5DFJ.mjs → chunk-FJPR2CJX.mjs} +3 -3
- package/dist/chunk-HR7K3GZW.mjs +36 -0
- package/dist/{chunk-LXCGPZPA.mjs → chunk-IEKIYO4V.mjs} +1 -1
- package/dist/{chunk-TUD5CJ76.mjs → chunk-KB4OLJAD.mjs} +26 -19
- package/dist/{chunk-PBRXXISQ.mjs → chunk-KINDFBQP.mjs} +3 -2
- package/dist/{chunk-E6IMX7RS.mjs → chunk-KJLNYOOL.mjs} +2 -2
- package/dist/{chunk-BTTFW4Z4.mjs → chunk-KU32OT4H.mjs} +2 -10
- package/dist/{chunk-JQWGHSEH.mjs → chunk-NVSQ2T5Q.mjs} +71 -70
- package/dist/{chunk-W4EUTJNT.mjs → chunk-NZGSDALX.mjs} +94 -96
- package/dist/{chunk-U445A3C4.mjs → chunk-QGVRRKB2.mjs} +1 -1
- package/dist/{chunk-GYCEJNU4.mjs → chunk-R7GBXNTO.mjs} +1 -1
- package/dist/{chunk-7C36BCSN.mjs → chunk-VDN6FHU4.mjs} +1 -1
- package/dist/{chunk-6IYS4WLH.mjs → chunk-VO3ZWA2G.mjs} +142 -115
- package/dist/chunk-WOMADPM4.mjs +12 -0
- package/dist/{chunk-OUSWVNWT.mjs → chunk-YCMB6GR4.mjs} +1 -1
- package/dist/{chunk-YNTDOFTN.mjs → chunk-YW7IKQ7T.mjs} +1 -1
- package/dist/{chunk-M2BEAH5I.mjs → chunk-ZN4HI5FK.mjs} +126 -72
- package/dist/{chunk-SBL6GN43.mjs → chunk-ZYWI45EN.mjs} +1 -1
- package/dist/client/index.d.ts +135 -137
- package/dist/client/index.mjs +177 -82
- package/dist/client/topology.d.ts +31 -31
- package/dist/client/topology.mjs +42 -23
- package/dist/client-base-BxTocU1x.d.ts +398 -0
- package/dist/client-streams-YKANXMWO.mjs +112 -0
- package/dist/codegen/cli.mjs +4 -4
- package/dist/codegen/index.d.ts +36 -29
- package/dist/codegen/index.mjs +4 -4
- package/dist/core/index.d.ts +130 -115
- package/dist/core/index.mjs +387 -297
- package/dist/core/writer-worker.mjs +3 -3
- package/dist/database-CGDsXW1K.d.ts +959 -0
- package/dist/database-backup-DcLhxoa1.d.ts +143 -0
- package/dist/driver/better-sqlite3.d.ts +11 -10
- package/dist/driver/better-sqlite3.mjs +11 -11
- package/dist/driver/bun.d.ts +9 -8
- package/dist/driver/expo.d.ts +6 -7
- package/dist/driver/node.d.ts +11 -10
- package/dist/driver/node.mjs +13 -11
- package/dist/driver/wa-sqlite.d.ts +6 -6
- package/dist/errors-CJUkQ-KL.d.ts +199 -0
- package/dist/file-migrations/index.d.ts +20 -18
- package/dist/file-migrations/index.mjs +3 -3
- package/dist/forward-rpc-VRWFZXHN.mjs +44 -0
- package/dist/operation-registry-CGhNT-Sn.d.ts +96 -0
- package/dist/primary-wins-CHNOYuN7.d.ts +81 -0
- package/dist/protocol-DFpNNsIT.d.ts +153 -0
- package/dist/query-types-5B02ixH0.d.ts +129 -0
- package/dist/react/index.d.ts +22 -22
- package/dist/replication/coordinator/etcd.d.ts +37 -31
- package/dist/replication/coordinator/etcd.mjs +296 -131
- package/dist/replication/index.d.ts +201 -161
- package/dist/replication/index.mjs +630 -529
- package/dist/restore-options-CH8mE-Tb.d.ts +84 -0
- package/dist/server/index.d.ts +201 -191
- package/dist/server/index.mjs +736 -619
- package/dist/server-options-aYtCkefX.d.ts +317 -0
- package/dist/server-streams-24XL4T43.mjs +254 -0
- package/dist/sirannon-J3EgPVD7.d.ts +179 -0
- package/dist/transport/grpc.d.ts +37 -24
- package/dist/transport/grpc.mjs +83 -3199
- package/dist/transport/memory.d.ts +25 -26
- package/dist/transport/memory.mjs +18 -9
- package/dist/types-BrAiDGOb.d.ts +505 -0
- package/dist/types-CEJG0eFS.d.ts +88 -0
- package/dist/types-Dc8oWlxj.d.ts +1580 -0
- package/dist/types-DqODKeOq.d.ts +123 -0
- package/dist/types-guEKSzMr.d.ts +367 -0
- package/package.json +7 -7
- package/dist/baseline-D93hcIEE.d.ts +0 -17
- package/dist/change-tracker-CjRFuZdB.d.ts +0 -144
- package/dist/client-base-Cmd9EVme.d.ts +0 -397
- package/dist/database-BLQvqjsw.d.ts +0 -917
- package/dist/database-backup-B7oszQ2n.d.ts +0 -141
- package/dist/errors-Dei4GdBb.d.ts +0 -196
- package/dist/operation-registry-oiPuSYI6.d.ts +0 -94
- package/dist/primary-wins-B0np8JS3.d.ts +0 -85
- package/dist/protocol-HI-nY8DD.d.ts +0 -152
- package/dist/query-types-p1liOP-b.d.ts +0 -127
- package/dist/restore-options-CksYsH1l.d.ts +0 -83
- package/dist/server-options-Bj5iJnjx.d.ts +0 -292
- package/dist/sirannon-BPxC5tC2.d.ts +0 -178
- package/dist/types-B0EhgDbr.d.ts +0 -340
- package/dist/types-CbjfRwrK.d.ts +0 -499
- package/dist/types-CjhxcjhA.d.ts +0 -123
- package/dist/types-DPSmjU54.d.ts +0 -1518
- package/dist/types-DTO4CYhW.d.ts +0 -88
|
@@ -0,0 +1,959 @@
|
|
|
1
|
+
import { D as DatabaseBackupController, B as BackupCapabilities, a as BackupChainLocation, b as BackupRestorePlan, c as BackupSafeToDeleteOptions } from './database-backup-DcLhxoa1.js';
|
|
2
|
+
import { S as SQLiteDriver, U as SynchronousLevel, ao as WorkerHostOptions, I as SQLiteConnection, T as Transaction, ap as WriterContext, M as Migration, W as MigrationResult, X as RollbackResult, aj as QueryHookContext, a4 as ConnectionHookContext, a1 as BeforeSubscribeHook, a0 as BeforeSnapshotHook, $ as BeforePushHook, a9 as HookConfig, ab as MetricsConfig, ak as QueryMetrics, a5 as ConnectionMetrics, a2 as CDCMetrics, D as DatabaseOptions, p as BackupFileReport, y as BackupToDestinationOptions, f as BackupRunReport, O as BackupScheduleOptions, g as BackupChain, o as BackupCycleStatus, z as BackupVerifyResult, k as BackupChainRecord, b as BeforeQueryHook, A as AfterQueryHook } from './types-Dc8oWlxj.js';
|
|
3
|
+
import { C as ChangeTracker } from './change-tracker-DqCdeEyx.js';
|
|
4
|
+
import { C as ChangeEvent, d as SubscriptionOptions, S as Subscription, c as SubscriptionBuilder, P as Params, Q as QueryOptions, E as ExecuteResult } from './query-types-5B02ixH0.js';
|
|
5
|
+
import { H as HLCTimestamp, c as ReplicationChange, C as ConflictResolver, A as ApplyResult, R as ReplicationBatch } from './types-DqODKeOq.js';
|
|
6
|
+
import { A as AppliedMigrationRow, c as BulkLoadOptions, a as BulkLoadResult } from './server-options-aYtCkefX.js';
|
|
7
|
+
import { L as LiveQueryOptions, a as LiveQuery } from './types-CEJG0eFS.js';
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Settings that {@link ConnectionPool.create} uses to open the writer and reader connections.
|
|
11
|
+
*
|
|
12
|
+
* @internal
|
|
13
|
+
*/
|
|
14
|
+
interface ConnectionPoolOptions {
|
|
15
|
+
driver: SQLiteDriver;
|
|
16
|
+
path: string;
|
|
17
|
+
readOnly?: boolean;
|
|
18
|
+
readPoolSize?: number;
|
|
19
|
+
walMode?: boolean;
|
|
20
|
+
synchronous?: SynchronousLevel;
|
|
21
|
+
walAutoCheckpoint?: number;
|
|
22
|
+
useWriterWorker?: boolean;
|
|
23
|
+
workerHostOptions?: WorkerHostOptions;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Opens one writer connection and a set of reader connections for a single database file, and returns the readers in rotation.
|
|
27
|
+
*
|
|
28
|
+
* @internal
|
|
29
|
+
*/
|
|
30
|
+
declare class ConnectionPool {
|
|
31
|
+
private readonly writer;
|
|
32
|
+
private readonly readers;
|
|
33
|
+
private readerIndex;
|
|
34
|
+
private closed;
|
|
35
|
+
private constructor();
|
|
36
|
+
static create(options: ConnectionPoolOptions): Promise<ConnectionPool>;
|
|
37
|
+
acquireReader(): SQLiteConnection;
|
|
38
|
+
acquireWriter(): SQLiteConnection;
|
|
39
|
+
/** Returns the writer and every reader, so that the caller can apply an operation to every connection in the pool. */
|
|
40
|
+
connections(): readonly SQLiteConnection[];
|
|
41
|
+
get readerCount(): number;
|
|
42
|
+
get isReadOnly(): boolean;
|
|
43
|
+
close(): Promise<void>;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
interface DeviceRetentionPolicy {
|
|
47
|
+
cursorRetentionMs: number;
|
|
48
|
+
maxChangesHeldForDevice: number;
|
|
49
|
+
}
|
|
50
|
+
interface DeviceRetentionOptions {
|
|
51
|
+
deviceCursorRetention?: number;
|
|
52
|
+
maxChangesHeldForDevice?: number;
|
|
53
|
+
}
|
|
54
|
+
interface ChangeRetentionOptions extends DeviceRetentionOptions {
|
|
55
|
+
cdcRetention?: number;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
type ChangeDispatchObserver = (event: ChangeEvent, subscriberCount: number) => void;
|
|
59
|
+
declare class SubscriptionManager {
|
|
60
|
+
private readonly onDispatched?;
|
|
61
|
+
constructor(onDispatched?: ChangeDispatchObserver | undefined);
|
|
62
|
+
private nextId;
|
|
63
|
+
private readonly subscriptions;
|
|
64
|
+
private readonly byTable;
|
|
65
|
+
private readonly batchEndListeners;
|
|
66
|
+
subscribe(table: string, filter: Record<string, unknown> | undefined, callback: (event: ChangeEvent) => void, options?: SubscriptionOptions): Subscription;
|
|
67
|
+
dispatch(events: ChangeEvent[]): void;
|
|
68
|
+
reportError(error: Error): void;
|
|
69
|
+
addBatchEndListener(listener: (atTxBoundary: boolean) => void): () => void;
|
|
70
|
+
endBatch(atTxBoundary: boolean): void;
|
|
71
|
+
get size(): number;
|
|
72
|
+
subscriberCount(table: string): number;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Generates hybrid logical clock (HLC) timestamps that order events across nodes.
|
|
77
|
+
*
|
|
78
|
+
* Each timestamp has the form `{wallMs hex}-{logical hex}-{nodeId}`, so a plain
|
|
79
|
+
* string comparison orders two timestamps by wall-clock time, then by logical
|
|
80
|
+
* counter, then by node ID. The wall-clock part holds milliseconds since the
|
|
81
|
+
* Unix epoch, and the logical counter orders events within one millisecond.
|
|
82
|
+
*
|
|
83
|
+
* Call `now()` to stamp a local write with a timestamp higher than every
|
|
84
|
+
* earlier one from this clock. Call `receive(remote)` for each remote change,
|
|
85
|
+
* so that the clock moves ahead of every timestamp that it receives.
|
|
86
|
+
*
|
|
87
|
+
* @public
|
|
88
|
+
*/
|
|
89
|
+
declare class HLC {
|
|
90
|
+
private wallMs;
|
|
91
|
+
private logical;
|
|
92
|
+
private readonly nodeId;
|
|
93
|
+
constructor(nodeId: string);
|
|
94
|
+
/**
|
|
95
|
+
* Returns a new timestamp and advances the clock.
|
|
96
|
+
*
|
|
97
|
+
* @internal
|
|
98
|
+
*/
|
|
99
|
+
now(): string;
|
|
100
|
+
/**
|
|
101
|
+
* Merges a remote timestamp into the clock and returns the new local timestamp.
|
|
102
|
+
*
|
|
103
|
+
* @internal
|
|
104
|
+
*/
|
|
105
|
+
receive(remote: string): string;
|
|
106
|
+
/** Compares two encoded timestamps as strings and returns -1, 0, or 1. */
|
|
107
|
+
static compare(a: string, b: string): number;
|
|
108
|
+
/** Parses an encoded timestamp into its wall-clock, logical, and node ID parts. */
|
|
109
|
+
static decode(hlc: string): HLCTimestamp;
|
|
110
|
+
/**
|
|
111
|
+
* Returns the string form of a clock reading, padded so that string order matches clock order.
|
|
112
|
+
*
|
|
113
|
+
* @param wallMs - The wall-clock milliseconds since the Unix epoch.
|
|
114
|
+
* @param logical - The counter that orders events within one millisecond.
|
|
115
|
+
* @param nodeId - The ID of the node that takes the reading.
|
|
116
|
+
* @returns The encoded stamp.
|
|
117
|
+
*/
|
|
118
|
+
static encode(wallMs: number, logical: number, nodeId: string): string;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
interface StampStatement {
|
|
122
|
+
sql: string;
|
|
123
|
+
params: unknown[];
|
|
124
|
+
trusted: true;
|
|
125
|
+
}
|
|
126
|
+
declare class SyncStamper {
|
|
127
|
+
readonly nodeId: string;
|
|
128
|
+
readonly hlc: HLC;
|
|
129
|
+
private readonly changesTable;
|
|
130
|
+
private constructor();
|
|
131
|
+
static init(conn: SQLiteConnection, changesTable?: string): Promise<SyncStamper>;
|
|
132
|
+
stampStatements(options?: {
|
|
133
|
+
persistClock?: boolean;
|
|
134
|
+
}): StampStatement[];
|
|
135
|
+
applyStamps(txConn: SQLiteConnection): Promise<void>;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
type RunExclusive$1 = <T>(op: () => Promise<T>) => Promise<T>;
|
|
139
|
+
declare class DatabaseCdcController {
|
|
140
|
+
private readonly runExclusive;
|
|
141
|
+
private readonly acquireWriter;
|
|
142
|
+
private readonly pollInterval;
|
|
143
|
+
private readonly retention;
|
|
144
|
+
private readonly openSnapshotConnection;
|
|
145
|
+
private readonly deviceRetention;
|
|
146
|
+
private readonly onDispatched?;
|
|
147
|
+
private tracker;
|
|
148
|
+
private subscriptionManager;
|
|
149
|
+
private liveConnRequest;
|
|
150
|
+
private stopPolling;
|
|
151
|
+
private stamper;
|
|
152
|
+
private epochRequest;
|
|
153
|
+
constructor(runExclusive: RunExclusive$1, acquireWriter: () => SQLiteConnection, pollInterval: number, retention: number, openSnapshotConnection: (() => Promise<SQLiteConnection>) | null, deviceRetention: DeviceRetentionPolicy, onDispatched?: ChangeDispatchObserver | undefined);
|
|
154
|
+
ensureEpoch(): Promise<string>;
|
|
155
|
+
readAtPositionWith<T>(read: (conn: SQLiteConnection) => Promise<T>): Promise<{
|
|
156
|
+
value: T;
|
|
157
|
+
position: string;
|
|
158
|
+
seq: bigint;
|
|
159
|
+
}>;
|
|
160
|
+
liveConnection(): Promise<{
|
|
161
|
+
conn: SQLiteConnection;
|
|
162
|
+
run: RunExclusive$1;
|
|
163
|
+
}>;
|
|
164
|
+
closeLiveConnection(): Promise<void>;
|
|
165
|
+
get subscriptions(): SubscriptionManager | null;
|
|
166
|
+
get changeTracker(): ChangeTracker | null;
|
|
167
|
+
watch(table: string): Promise<void>;
|
|
168
|
+
ensureStamper(): Promise<SyncStamper>;
|
|
169
|
+
ensureStamping(): Promise<void>;
|
|
170
|
+
stampStatements(options?: {
|
|
171
|
+
persistClock?: boolean;
|
|
172
|
+
}): readonly StampStatement[] | null;
|
|
173
|
+
applyStamps(txConn: SQLiteConnection): Promise<void>;
|
|
174
|
+
unwatch(table: string): Promise<void>;
|
|
175
|
+
on(table: string): SubscriptionBuilder;
|
|
176
|
+
runTransaction<T>(writer: SQLiteConnection, fn: (tx: Transaction) => Promise<T>): Promise<T>;
|
|
177
|
+
stop(): void;
|
|
178
|
+
private ensure;
|
|
179
|
+
private ensurePolling;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
declare class WriterLock {
|
|
183
|
+
private tail;
|
|
184
|
+
private readonly context;
|
|
185
|
+
constructor(context?: WriterContext);
|
|
186
|
+
run<T>(operation: () => Promise<T>): Promise<T>;
|
|
187
|
+
isHeld(): boolean;
|
|
188
|
+
settle(): Promise<void>;
|
|
189
|
+
detached<T>(operation: () => T): T;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
interface DatabaseMigrationDeps {
|
|
193
|
+
pool: ConnectionPool;
|
|
194
|
+
writerLock: WriterLock;
|
|
195
|
+
changeTracker: () => ChangeTracker | null;
|
|
196
|
+
}
|
|
197
|
+
declare class DatabaseMigrationController {
|
|
198
|
+
private readonly deps;
|
|
199
|
+
constructor(deps: DatabaseMigrationDeps);
|
|
200
|
+
migrate(migrations: Migration[]): Promise<MigrationResult>;
|
|
201
|
+
rollback(migrations: Migration[], version?: number): Promise<RollbackResult>;
|
|
202
|
+
applied(): Promise<AppliedMigrationRow[]>;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* The lifecycle points at which you can register a hook.
|
|
207
|
+
*
|
|
208
|
+
* @internal
|
|
209
|
+
*/
|
|
210
|
+
type HookEvent = 'beforeQuery' | 'afterQuery' | 'beforeConnect' | 'databaseOpen' | 'databaseClose' | 'beforeSubscribe' | 'beforeSnapshot' | 'beforePush';
|
|
211
|
+
/**
|
|
212
|
+
* The context that a subscribe hook receives.
|
|
213
|
+
*
|
|
214
|
+
* @internal
|
|
215
|
+
*/
|
|
216
|
+
type SubscribeHookContext = Parameters<BeforeSubscribeHook>[0];
|
|
217
|
+
/**
|
|
218
|
+
* The context that a snapshot hook receives.
|
|
219
|
+
*
|
|
220
|
+
* @internal
|
|
221
|
+
*/
|
|
222
|
+
type SnapshotHookContext = Parameters<BeforeSnapshotHook>[0];
|
|
223
|
+
/**
|
|
224
|
+
* The context that a push hook receives.
|
|
225
|
+
*
|
|
226
|
+
* @internal
|
|
227
|
+
*/
|
|
228
|
+
type PushHookContext = Parameters<BeforePushHook>[0];
|
|
229
|
+
/**
|
|
230
|
+
* Maps each lifecycle point to the context that its hooks receive.
|
|
231
|
+
*
|
|
232
|
+
* @internal
|
|
233
|
+
*/
|
|
234
|
+
interface HookEventContextMap {
|
|
235
|
+
beforeQuery: QueryHookContext;
|
|
236
|
+
afterQuery: QueryHookContext & {
|
|
237
|
+
durationMs: number;
|
|
238
|
+
};
|
|
239
|
+
beforeConnect: ConnectionHookContext;
|
|
240
|
+
databaseOpen: ConnectionHookContext;
|
|
241
|
+
databaseClose: ConnectionHookContext;
|
|
242
|
+
beforeSubscribe: SubscribeHookContext;
|
|
243
|
+
beforeSnapshot: SnapshotHookContext;
|
|
244
|
+
beforePush: PushHookContext;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* The function that you register as a hook for one lifecycle point.
|
|
248
|
+
*
|
|
249
|
+
* @internal
|
|
250
|
+
*/
|
|
251
|
+
type HookHandler<E extends HookEvent> = (ctx: HookEventContextMap[E]) => void | Promise<void>;
|
|
252
|
+
/**
|
|
253
|
+
* Removes the hook whose registration returned this function, and a second call has no effect.
|
|
254
|
+
*
|
|
255
|
+
* @public
|
|
256
|
+
*/
|
|
257
|
+
type HookDispose = () => void;
|
|
258
|
+
|
|
259
|
+
/**
|
|
260
|
+
* Stores the lifecycle hooks for a database or registry and calls each event's hooks in the order that the caller registered them.
|
|
261
|
+
*
|
|
262
|
+
* @internal
|
|
263
|
+
*/
|
|
264
|
+
declare class HookRegistry {
|
|
265
|
+
private hooks;
|
|
266
|
+
constructor(config?: HookConfig);
|
|
267
|
+
register<E extends HookEvent>(event: E, hook: HookHandler<E>): HookDispose;
|
|
268
|
+
invoke<E extends HookEvent>(event: E, ctx: HookEventContextMap[E]): Promise<void>;
|
|
269
|
+
invokeSync<E extends HookEvent>(event: E, ctx: HookEventContextMap[E]): void;
|
|
270
|
+
invokeSyncIgnoringFailures<E extends HookEvent>(event: E, ctx: HookEventContextMap[E]): void;
|
|
271
|
+
has(event: HookEvent): boolean;
|
|
272
|
+
count(event: HookEvent): number;
|
|
273
|
+
clear(event?: HookEvent): void;
|
|
274
|
+
private addHook;
|
|
275
|
+
private loadConfig;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
type QueryOutcome = Pick<QueryMetrics, 'rowsReturned' | 'changes'>;
|
|
279
|
+
type QueryOutcomeMeasure<T> = (result: T) => QueryOutcome;
|
|
280
|
+
/**
|
|
281
|
+
* Times each query and passes query timings, connection events, and change-capture events to the metrics callbacks that the caller configures.
|
|
282
|
+
*
|
|
283
|
+
* @internal
|
|
284
|
+
*/
|
|
285
|
+
declare class MetricsCollector {
|
|
286
|
+
private config;
|
|
287
|
+
constructor(config?: MetricsConfig);
|
|
288
|
+
trackQuery<T>(fn: () => Promise<T>, context: Omit<QueryMetrics, 'durationMs' | 'error'>, measure?: QueryOutcomeMeasure<T>): Promise<T>;
|
|
289
|
+
trackConnection(metrics: ConnectionMetrics): void;
|
|
290
|
+
trackCDCEvent(metrics: CDCMetrics): void;
|
|
291
|
+
observeDispatch(databaseId: string): ChangeDispatchObserver | undefined;
|
|
292
|
+
get active(): boolean;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
interface ObservedStatement {
|
|
296
|
+
sql: string;
|
|
297
|
+
params?: Params;
|
|
298
|
+
}
|
|
299
|
+
declare class DatabaseObserver {
|
|
300
|
+
private readonly databaseId;
|
|
301
|
+
private readonly localHooks;
|
|
302
|
+
private readonly parentHooks;
|
|
303
|
+
private readonly metrics;
|
|
304
|
+
constructor(databaseId: string, localHooks: HookRegistry, parentHooks: HookRegistry | null, metrics: MetricsCollector | null);
|
|
305
|
+
get observesQueries(): boolean;
|
|
306
|
+
withQueryHooks<T>(sql: string, params: Params | undefined, options: QueryOptions | undefined, op: () => Promise<T>): Promise<T>;
|
|
307
|
+
track<T>(sql: string, op: () => Promise<T>, measure?: QueryOutcomeMeasure<T>): Promise<T>;
|
|
308
|
+
withTransactionHooks(statements: readonly ObservedStatement[], op: () => Promise<ExecuteResult[]>): Promise<ExecuteResult[]>;
|
|
309
|
+
private fireAfterEach;
|
|
310
|
+
private trackEach;
|
|
311
|
+
private fireBefore;
|
|
312
|
+
private fireAfter;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
interface DatabaseReadDeps {
|
|
316
|
+
pool: ConnectionPool;
|
|
317
|
+
writerLock: WriterLock;
|
|
318
|
+
observer: DatabaseObserver;
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
interface StagedRecovery {
|
|
322
|
+
resumeSeq: bigint | null;
|
|
323
|
+
appliedSeq: bigint | null;
|
|
324
|
+
/**
|
|
325
|
+
* The error that stopped the recovery apply, or `null`. The staged rows stay
|
|
326
|
+
* in place after the failed apply, and the resume sequence still covers
|
|
327
|
+
* them, so the caller opens the subscription regardless. When the server's
|
|
328
|
+
* schema gate refuses that subscription, the refusal error states that the
|
|
329
|
+
* device has to migrate first. The caller then has to retry the recovery
|
|
330
|
+
* itself, because the resume sequence lies past the unapplied transaction,
|
|
331
|
+
* so the server resends none of it.
|
|
332
|
+
*/
|
|
333
|
+
applyError: unknown | null;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
/**
|
|
337
|
+
* The position up to which a device has pulled from its server, and the epoch
|
|
338
|
+
* of the server change log that the position comes from.
|
|
339
|
+
*
|
|
340
|
+
* @public
|
|
341
|
+
*/
|
|
342
|
+
interface DeviceSyncPullState {
|
|
343
|
+
/** The highest server change-log position that this device has applied. */
|
|
344
|
+
seq: bigint;
|
|
345
|
+
/** The epoch of the server change log that the position comes from. */
|
|
346
|
+
epoch: string | undefined;
|
|
347
|
+
}
|
|
348
|
+
/**
|
|
349
|
+
* Gives a device's sync loop the calls that it makes on the local database,
|
|
350
|
+
* which stage and apply the changes that the server sends, read the local
|
|
351
|
+
* changes that the device has yet to push, and store the push and pull
|
|
352
|
+
* cursors. Get one from {@link Database.deviceSync}.
|
|
353
|
+
*
|
|
354
|
+
* @public
|
|
355
|
+
*/
|
|
356
|
+
interface DeviceSyncPort {
|
|
357
|
+
/** Returns the node identifier that this device stamps on its own changes. */
|
|
358
|
+
identity(): Promise<{
|
|
359
|
+
nodeId: string;
|
|
360
|
+
}>;
|
|
361
|
+
/** Applies one pulled transaction and advances the pull cursor in the same write. */
|
|
362
|
+
applyPulledTransaction(changes: readonly ReplicationChange[], pullSeq: bigint, resolver?: ConflictResolver | ((table: string) => ConflictResolver)): Promise<ApplyResult>;
|
|
363
|
+
/** Stores pulled changes on disk without applying them, so that a device that stops part-way can resume from them, and returns the highest position stored, or null for an empty list. */
|
|
364
|
+
stagePulledChanges(events: readonly ChangeEvent[]): Promise<bigint | null>;
|
|
365
|
+
/** Applies each complete staged transaction in order, and returns the position of the last one applied, or null when it applied none. */
|
|
366
|
+
applyStagedPull(resolver?: ConflictResolver | ((table: string) => ConflictResolver), onChange?: (event: ChangeEvent) => void): Promise<bigint | null>;
|
|
367
|
+
/** Applies the changes that an earlier session left staged, and returns the position to resume from, the position applied, and any error that stopped this apply. */
|
|
368
|
+
recoverStagedPull(resolver?: ConflictResolver | ((table: string) => ConflictResolver), onChange?: (event: ChangeEvent) => void): Promise<StagedRecovery>;
|
|
369
|
+
/** Returns the next batch of local changes that the device has yet to push, or null when none remain. */
|
|
370
|
+
readOutboxBatch(afterSeq: bigint, limit: number): Promise<ReplicationBatch | null>;
|
|
371
|
+
/** Counts the local changes that the device has yet to push. */
|
|
372
|
+
countOutboxPending(afterSeq: bigint): Promise<number>;
|
|
373
|
+
/** Returns the position up to which the device has pushed. */
|
|
374
|
+
getPushCursor(): Promise<bigint>;
|
|
375
|
+
/** Stores the position up to which the device has pushed. */
|
|
376
|
+
setPushCursor(seq: bigint): Promise<void>;
|
|
377
|
+
/** Returns the position up to which the device has pulled, or null before its first pull. */
|
|
378
|
+
getPullState(): Promise<DeviceSyncPullState | null>;
|
|
379
|
+
/** Returns true when the device must download a fresh snapshot, as {@link DeviceSyncPort.setResyncRequired} last stored. */
|
|
380
|
+
getResyncRequired(): Promise<boolean>;
|
|
381
|
+
/** Stores whether the device must download a fresh snapshot. */
|
|
382
|
+
setResyncRequired(required: boolean): Promise<void>;
|
|
383
|
+
/** Stores the position up to which the device has pulled, and the epoch that the position comes from. */
|
|
384
|
+
setPullState(seq: bigint, epoch?: string): Promise<void>;
|
|
385
|
+
/** Stops change-log pruning from deleting any local change above the pushed position, so that the device can still push it. */
|
|
386
|
+
protectUnpushedChanges(pushedSeq: bigint): void;
|
|
387
|
+
/** Returns true when a snapshot download started and has yet to finish. */
|
|
388
|
+
snapshotLoadPending(): Promise<boolean>;
|
|
389
|
+
/** Drops the named tables, discards staged pulled changes, and rejects ordinary reads and writes on this database until {@link DeviceSyncPort.endSnapshotLoad} completes. */
|
|
390
|
+
beginSnapshotLoad(tables: readonly string[]): Promise<void>;
|
|
391
|
+
/** Executes the schema statements that a snapshot contains. */
|
|
392
|
+
applySnapshotSchema(schema: readonly string[]): Promise<void>;
|
|
393
|
+
/** Writes one page of snapshot rows into a table. */
|
|
394
|
+
loadSnapshotPage(table: string, rows: readonly Record<string, unknown>[]): Promise<void>;
|
|
395
|
+
/** Replaces the local migration history with the server's. */
|
|
396
|
+
replaceMigrationHistory(rows: readonly AppliedMigrationRow[]): Promise<void>;
|
|
397
|
+
/** Finishes a snapshot download: watches the named tables again, turns foreign keys back on, and lifts the block on ordinary reads and writes. */
|
|
398
|
+
endSnapshotLoad(tables: readonly string[]): Promise<void>;
|
|
399
|
+
/** Turns foreign keys back on after a failed snapshot download and leaves the download pending, so ordinary reads and writes stay rejected and {@link DeviceSyncPort.snapshotLoadPending} returns true. */
|
|
400
|
+
abortSnapshotLoad(): Promise<void>;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
type RunExclusive = <T>(op: () => Promise<T>) => Promise<T>;
|
|
404
|
+
|
|
405
|
+
declare class DatabaseSyncController {
|
|
406
|
+
private readonly runExclusive;
|
|
407
|
+
private readonly acquireWriter;
|
|
408
|
+
private readonly cdc;
|
|
409
|
+
private pkResolver;
|
|
410
|
+
private tablesReady;
|
|
411
|
+
private metaReady;
|
|
412
|
+
private outboxReader;
|
|
413
|
+
private localPruneBoundary;
|
|
414
|
+
private snapshotGate;
|
|
415
|
+
private readonly defaultResolver;
|
|
416
|
+
constructor(runExclusive: RunExclusive, acquireWriter: () => SQLiteConnection, cdc: DatabaseCdcController);
|
|
417
|
+
applyChanges(batch: ReplicationBatch, resolver?: ConflictResolver | ((table: string) => ConflictResolver)): Promise<ApplyResult>;
|
|
418
|
+
devicePort(): DeviceSyncPort;
|
|
419
|
+
get snapshotLoadBlocked(): boolean;
|
|
420
|
+
seedSnapshotGate(): void;
|
|
421
|
+
private applyPulledTransaction;
|
|
422
|
+
private stagePulled;
|
|
423
|
+
private applyStagedPull;
|
|
424
|
+
private recoverStagedPullState;
|
|
425
|
+
private stagedApplyOptions;
|
|
426
|
+
private replaceMigrationHistory;
|
|
427
|
+
private beginSnapshotLoad;
|
|
428
|
+
private endSnapshotLoad;
|
|
429
|
+
private applyLocalPruneBoundary;
|
|
430
|
+
private readOutboxBatch;
|
|
431
|
+
private countOutboxPending;
|
|
432
|
+
private getResyncRequired;
|
|
433
|
+
private setResyncRequired;
|
|
434
|
+
private getPullState;
|
|
435
|
+
private setPullState;
|
|
436
|
+
private getMetaSeq;
|
|
437
|
+
private setMetaSeq;
|
|
438
|
+
private ensureMeta;
|
|
439
|
+
private ensureSyncTables;
|
|
440
|
+
private ensureOutboxReader;
|
|
441
|
+
private ensureApplier;
|
|
442
|
+
private catchUpLocalColumnVersions;
|
|
443
|
+
private lastAppliedSeq;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Executes a read on one connection and returns the rows with their values decoded.
|
|
448
|
+
*
|
|
449
|
+
* @internal
|
|
450
|
+
*/
|
|
451
|
+
declare function query<T = Record<string, unknown>>(conn: SQLiteConnection, sql: string, params?: Params): Promise<T[]>;
|
|
452
|
+
/**
|
|
453
|
+
* Executes a read on one connection and returns the first row, or undefined when the read returns none.
|
|
454
|
+
*
|
|
455
|
+
* @internal
|
|
456
|
+
*/
|
|
457
|
+
declare function queryOne<T = Record<string, unknown>>(conn: SQLiteConnection, sql: string, params?: Params): Promise<T | undefined>;
|
|
458
|
+
/**
|
|
459
|
+
* Executes one write on a connection and returns the change count and the row id of the last inserted row.
|
|
460
|
+
*
|
|
461
|
+
* @internal
|
|
462
|
+
*/
|
|
463
|
+
declare function execute(conn: SQLiteConnection, sql: string, params?: Params, trusted?: boolean): Promise<ExecuteResult>;
|
|
464
|
+
interface GroupStatement {
|
|
465
|
+
sql: string;
|
|
466
|
+
params?: Params;
|
|
467
|
+
trusted?: boolean;
|
|
468
|
+
}
|
|
469
|
+
/**
|
|
470
|
+
* Executes one statement on a connection once for each parameter set, and returns one result per set.
|
|
471
|
+
*
|
|
472
|
+
* @internal
|
|
473
|
+
*/
|
|
474
|
+
declare function executeBatch(conn: SQLiteConnection, sql: string, paramsBatch: Params[]): Promise<ExecuteResult[]>;
|
|
475
|
+
|
|
476
|
+
interface GroupCommitterHooks {
|
|
477
|
+
acquireWriter: () => SQLiteConnection;
|
|
478
|
+
afterCommit: (writer: SQLiteConnection, sql: string) => Promise<void>;
|
|
479
|
+
stampStatements: (options?: {
|
|
480
|
+
persistClock?: boolean;
|
|
481
|
+
}) => readonly GroupStatement[] | null;
|
|
482
|
+
}
|
|
483
|
+
declare class GroupCommitter {
|
|
484
|
+
private readonly writerLock;
|
|
485
|
+
private readonly hooks;
|
|
486
|
+
private readonly maxGroup;
|
|
487
|
+
private readonly pending;
|
|
488
|
+
private running;
|
|
489
|
+
private scheduled;
|
|
490
|
+
private loopPromise;
|
|
491
|
+
constructor(writerLock: WriterLock, hooks: GroupCommitterHooks, maxGroup?: number);
|
|
492
|
+
submit(sql: string, params?: Params): Promise<ExecuteResult>;
|
|
493
|
+
submitTransaction(statements: readonly GroupStatement[]): Promise<ExecuteResult[]>;
|
|
494
|
+
drain(): Promise<void>;
|
|
495
|
+
runUngrouped(sql: string, params?: Params): Promise<ExecuteResult>;
|
|
496
|
+
private schedule;
|
|
497
|
+
private runLoop;
|
|
498
|
+
private takeBatch;
|
|
499
|
+
private flush;
|
|
500
|
+
private needsStamp;
|
|
501
|
+
private runSingle;
|
|
502
|
+
private runStampedAlone;
|
|
503
|
+
private runGroup;
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
declare class WriteGate {
|
|
507
|
+
private readonly limit;
|
|
508
|
+
private readonly retryAfterMs;
|
|
509
|
+
private inFlight;
|
|
510
|
+
constructor(limit: number, retryAfterMs: number);
|
|
511
|
+
run<T>(op: () => Promise<T>): Promise<T>;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
interface DatabaseWriteDeps {
|
|
515
|
+
pool: ConnectionPool;
|
|
516
|
+
writeGate: WriteGate;
|
|
517
|
+
writerLock: WriterLock;
|
|
518
|
+
groupCommitter: GroupCommitter;
|
|
519
|
+
cdc: DatabaseCdcController;
|
|
520
|
+
observer: DatabaseObserver;
|
|
521
|
+
synchronous: SynchronousLevel;
|
|
522
|
+
walMode: boolean;
|
|
523
|
+
capturesChangeLog: boolean;
|
|
524
|
+
}
|
|
525
|
+
declare class DatabaseWriteController {
|
|
526
|
+
private readonly deps;
|
|
527
|
+
constructor(deps: DatabaseWriteDeps);
|
|
528
|
+
execute(sql: string, params?: Params, options?: QueryOptions): Promise<ExecuteResult>;
|
|
529
|
+
executeBatch(sql: string, paramsBatch: Params[], options?: QueryOptions): Promise<ExecuteResult[]>;
|
|
530
|
+
bulkLoad(sql: string, paramsBatch: Params[], options?: BulkLoadOptions): Promise<BulkLoadResult>;
|
|
531
|
+
executeTransaction(statements: readonly {
|
|
532
|
+
sql: string;
|
|
533
|
+
params?: Params;
|
|
534
|
+
}[]): Promise<ExecuteResult[]>;
|
|
535
|
+
transaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>;
|
|
536
|
+
drain(): Promise<void>;
|
|
537
|
+
private runStatementsAlone;
|
|
538
|
+
private runInTransaction;
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
interface DatabaseInternals {
|
|
542
|
+
parentHooks?: HookRegistry;
|
|
543
|
+
metrics?: MetricsCollector;
|
|
544
|
+
}
|
|
545
|
+
interface DatabaseRuntime {
|
|
546
|
+
pool: ConnectionPool;
|
|
547
|
+
writeGate: WriteGate;
|
|
548
|
+
writerLock: WriterLock;
|
|
549
|
+
hookRegistry: HookRegistry;
|
|
550
|
+
observer: DatabaseObserver;
|
|
551
|
+
backups: DatabaseBackupController;
|
|
552
|
+
cdc: DatabaseCdcController;
|
|
553
|
+
sync: DatabaseSyncController;
|
|
554
|
+
groupCommitter: GroupCommitter;
|
|
555
|
+
writes: DatabaseWriteController;
|
|
556
|
+
reads: DatabaseReadDeps;
|
|
557
|
+
migrations: DatabaseMigrationController;
|
|
558
|
+
loadExtension: (extensionPath: string) => Promise<void>;
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
/**
|
|
562
|
+
* Gives every open database its identity, its file, the checks that each
|
|
563
|
+
* method makes before it does any work, and the methods that close the
|
|
564
|
+
* database.
|
|
565
|
+
*
|
|
566
|
+
* Open a database through {@link Sirannon.open}, which returns a
|
|
567
|
+
* {@link Database} that extends this class.
|
|
568
|
+
*
|
|
569
|
+
* @public
|
|
570
|
+
*/
|
|
571
|
+
declare class DatabaseLifecycle {
|
|
572
|
+
/** The identifier that this database was opened under. */
|
|
573
|
+
readonly id: string;
|
|
574
|
+
/** The file path of the SQLite database. */
|
|
575
|
+
readonly path: string;
|
|
576
|
+
/** Whether this database rejects writes. */
|
|
577
|
+
readonly readOnly: boolean;
|
|
578
|
+
/**
|
|
579
|
+
* The retention settings that this database was opened with, which form an empty object when the caller set none.
|
|
580
|
+
*
|
|
581
|
+
* @internal
|
|
582
|
+
*/
|
|
583
|
+
readonly changeRetention: ChangeRetentionOptions;
|
|
584
|
+
/**
|
|
585
|
+
* The pool, locks, and controllers that this database sends its work through.
|
|
586
|
+
*
|
|
587
|
+
* @internal
|
|
588
|
+
*/
|
|
589
|
+
protected readonly runtime: DatabaseRuntime;
|
|
590
|
+
/**
|
|
591
|
+
* The functions that Sirannon calls while this database closes.
|
|
592
|
+
*
|
|
593
|
+
* @internal
|
|
594
|
+
*/
|
|
595
|
+
protected readonly closeListeners: (() => void | Promise<void>)[];
|
|
596
|
+
private closing;
|
|
597
|
+
protected constructor(id: string, path: string, runtime: DatabaseRuntime, options?: DatabaseOptions);
|
|
598
|
+
/**
|
|
599
|
+
* Registers a function that Sirannon calls while this database closes.
|
|
600
|
+
*
|
|
601
|
+
* @param fn - The function to call during {@link DatabaseLifecycle.close}.
|
|
602
|
+
*
|
|
603
|
+
* @internal
|
|
604
|
+
*/
|
|
605
|
+
addCloseListener(fn: () => void | Promise<void>): void;
|
|
606
|
+
/**
|
|
607
|
+
* Closes every connection that this database opened, and stops delivering changes to its subscriptions.
|
|
608
|
+
*/
|
|
609
|
+
close(): Promise<void>;
|
|
610
|
+
/**
|
|
611
|
+
* True once {@link DatabaseLifecycle.close} has started on this database.
|
|
612
|
+
*/
|
|
613
|
+
get closed(): boolean;
|
|
614
|
+
/**
|
|
615
|
+
* The number of read connections in the pool.
|
|
616
|
+
*/
|
|
617
|
+
get readerCount(): number;
|
|
618
|
+
/**
|
|
619
|
+
* Throws when this database is closed, is loading a snapshot, or rejects writes.
|
|
620
|
+
*
|
|
621
|
+
* @internal
|
|
622
|
+
*/
|
|
623
|
+
protected ensureWritable(): void;
|
|
624
|
+
/**
|
|
625
|
+
* Throws when this database is closed or is loading a snapshot.
|
|
626
|
+
*
|
|
627
|
+
* @internal
|
|
628
|
+
*/
|
|
629
|
+
protected ensureOpen(): void;
|
|
630
|
+
/**
|
|
631
|
+
* Throws when this database is closed.
|
|
632
|
+
*
|
|
633
|
+
* @internal
|
|
634
|
+
*/
|
|
635
|
+
protected ensureNotClosed(): void;
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Gives an open database its backup methods: one-off copies, a cron schedule,
|
|
640
|
+
* the cycle that captures the change log, and queries over the chain of
|
|
641
|
+
* backups that the cycle writes.
|
|
642
|
+
*
|
|
643
|
+
* Open a database through {@link Sirannon.open}, which returns a
|
|
644
|
+
* {@link Database} that extends this class.
|
|
645
|
+
*
|
|
646
|
+
* @public
|
|
647
|
+
*/
|
|
648
|
+
declare class DatabaseBackups extends DatabaseLifecycle {
|
|
649
|
+
/**
|
|
650
|
+
* Copies this database to a file while it stays open for reads and writes.
|
|
651
|
+
*
|
|
652
|
+
* Sirannon holds the writer only until SQLite copies the first step of pages,
|
|
653
|
+
* so later writes commit between the steps of the copy and wait for none of it.
|
|
654
|
+
*
|
|
655
|
+
* @param destPath - The path of the file that Sirannon writes the copy to.
|
|
656
|
+
* @returns The run identifier, the timings, the number of pages copied, and the size of the file in bytes.
|
|
657
|
+
*/
|
|
658
|
+
backup(destPath: string): Promise<BackupFileReport>;
|
|
659
|
+
/**
|
|
660
|
+
* Copies this database to a destination that you supply, in fixed-size
|
|
661
|
+
* pieces, while it stays open for reads and writes.
|
|
662
|
+
*
|
|
663
|
+
* Sirannon includes no storage client, so you connect object storage, or any
|
|
664
|
+
* other store, through the destination.
|
|
665
|
+
*
|
|
666
|
+
* Where this runtime has the compiled streaming extension, Sirannon sends each
|
|
667
|
+
* piece to the destination as SQLite writes it, so the copy needs no local
|
|
668
|
+
* disk. On every other runtime, Sirannon writes one local file first, which
|
|
669
|
+
* needs local disk equal to the size of the backup.
|
|
670
|
+
* {@link DatabaseBackups.backupCapabilities} reports which of the two routes
|
|
671
|
+
* applies to this runtime.
|
|
672
|
+
*
|
|
673
|
+
* @param options - The destination, the naming, the piece size, and the progress callback.
|
|
674
|
+
* @returns The run identifier, the timings, what the copy wrote, and how many times the copy restarted.
|
|
675
|
+
*/
|
|
676
|
+
backupTo(options: BackupToDestinationOptions): Promise<BackupRunReport>;
|
|
677
|
+
/**
|
|
678
|
+
* Reports which backup operations this database's runtime supports, so that
|
|
679
|
+
* you can check them before you start a backup.
|
|
680
|
+
*
|
|
681
|
+
* @returns Whether this runtime can copy the database, whether a copy needs local disk, and whether it can schedule copies.
|
|
682
|
+
*/
|
|
683
|
+
backupCapabilities(): BackupCapabilities;
|
|
684
|
+
/**
|
|
685
|
+
* Starts copying this database to a directory on a cron schedule, and deletes
|
|
686
|
+
* the oldest copies beyond {@link BackupScheduleOptions.maxFiles}.
|
|
687
|
+
*
|
|
688
|
+
* @param options - The cron expression, destination directory, retention, and time zone, with the callbacks that Sirannon calls after each finished copy and after each failure.
|
|
689
|
+
*/
|
|
690
|
+
scheduleBackup(options: BackupScheduleOptions): void;
|
|
691
|
+
/**
|
|
692
|
+
* Takes one turn of the backup cycle now, without waiting for the interval. A
|
|
693
|
+
* turn sends any capture that is still waiting, captures the log frames
|
|
694
|
+
* written since the previous turn, and then checkpoints the log.
|
|
695
|
+
*
|
|
696
|
+
* Only a database opened with the `backups` option has a cycle, and on any
|
|
697
|
+
* other database this throws `BACKUP_UNSUPPORTED`. On a node whose replication
|
|
698
|
+
* group takes its backups from another node, Sirannon writes nothing on the
|
|
699
|
+
* turn and reports a skip through `onSkip`.
|
|
700
|
+
*
|
|
701
|
+
* @returns What Sirannon wrote on the turn, or undefined when it wrote nothing.
|
|
702
|
+
*/
|
|
703
|
+
captureBackupChanges(): Promise<BackupRunReport | undefined>;
|
|
704
|
+
/**
|
|
705
|
+
* Lists the backup chains at the destination.
|
|
706
|
+
*
|
|
707
|
+
* @returns One entry per chain, newest first, each with its full copy and its change pieces in order.
|
|
708
|
+
*/
|
|
709
|
+
backupChain(): Promise<BackupChain[]>;
|
|
710
|
+
/**
|
|
711
|
+
* Returns what this database's backup cycle is doing now, and the outcome of
|
|
712
|
+
* its recent turns.
|
|
713
|
+
*
|
|
714
|
+
* You can poll this while a turn that you started with
|
|
715
|
+
* {@link DatabaseBackups.captureBackupChanges} is still under way, and during
|
|
716
|
+
* a full copy of a large database, the status includes the pages that SQLite
|
|
717
|
+
* has copied so far.
|
|
718
|
+
*
|
|
719
|
+
* @returns Whether a turn is under way, how far it has got, and the last run, skip, and failure.
|
|
720
|
+
*/
|
|
721
|
+
backupStatus(): BackupCycleStatus;
|
|
722
|
+
/**
|
|
723
|
+
* Fetches one of this database's backups from the destination and checks it
|
|
724
|
+
* against the record that Sirannon wrote with that backup.
|
|
725
|
+
*
|
|
726
|
+
* A restore fails on a damaged piece only after the restore has started, so
|
|
727
|
+
* call this beforehand to check the backup. Sirannon fetches every piece in
|
|
728
|
+
* order, computes a SHA-256 over the bytes as they arrive, and compares that
|
|
729
|
+
* digest and the byte count with the record. Sirannon keeps one piece in
|
|
730
|
+
* memory at a time, so checking a large full copy needs no local disk.
|
|
731
|
+
*
|
|
732
|
+
* This method throws `BACKUP_DESTINATION_ERROR` when a piece is missing, or
|
|
733
|
+
* when the byte count or the digest differs from the record.
|
|
734
|
+
*
|
|
735
|
+
* @param name - The name that the backup is stored under, as each entry from {@link DatabaseBackups.backupChain} gives it.
|
|
736
|
+
* @returns The number of pieces read, their total size in bytes, and the digest when the backup recorded one.
|
|
737
|
+
*/
|
|
738
|
+
verifyBackup(name: string): Promise<BackupVerifyResult>;
|
|
739
|
+
/**
|
|
740
|
+
* Returns where this database's backups are stored.
|
|
741
|
+
*
|
|
742
|
+
* Pass the destination, chain name, and deadline from the result to
|
|
743
|
+
* `restoreBackup` to rebuild this database from its backups at a path that
|
|
744
|
+
* you name.
|
|
745
|
+
*
|
|
746
|
+
* @returns The destination, the name that the chains are listed under, and the directory where Sirannon stages captures.
|
|
747
|
+
*/
|
|
748
|
+
backupLocation(): BackupChainLocation;
|
|
749
|
+
/**
|
|
750
|
+
* Returns the full copy and the change pieces that a restore to a given
|
|
751
|
+
* moment would apply.
|
|
752
|
+
*
|
|
753
|
+
* The plan also includes the moment that the restored database would reflect,
|
|
754
|
+
* which is the capture time of its last piece. Each piece contains every write
|
|
755
|
+
* from one capture interval, so a restore stops at a piece boundary, which
|
|
756
|
+
* can fall before the millisecond that you name.
|
|
757
|
+
*
|
|
758
|
+
* @param moment - The moment to restore to, in epoch milliseconds.
|
|
759
|
+
* @returns The full copy, the change pieces to apply in order, and the moment that the result reflects.
|
|
760
|
+
*/
|
|
761
|
+
backupRestorePlan(moment: number): Promise<BackupRestorePlan>;
|
|
762
|
+
/**
|
|
763
|
+
* Returns the backup records that no restore needs any longer, so that you
|
|
764
|
+
* can delete them from the destination yourself.
|
|
765
|
+
*
|
|
766
|
+
* When you pass the earliest moment that a restore must still reach, the list
|
|
767
|
+
* also includes every chain whose full copy finished before the full copy
|
|
768
|
+
* that a restore to that moment would start from.
|
|
769
|
+
*
|
|
770
|
+
* @param options - The earliest moment that a restore must still reach.
|
|
771
|
+
* @returns The records that you can delete, oldest chain first.
|
|
772
|
+
*/
|
|
773
|
+
backupPiecesSafeToDelete(options?: BackupSafeToDeleteOptions): Promise<BackupChainRecord[]>;
|
|
774
|
+
}
|
|
775
|
+
|
|
776
|
+
/**
|
|
777
|
+
* An open SQLite database, with methods for reads, writes, transactions, migrations, change subscriptions, and live queries.
|
|
778
|
+
*
|
|
779
|
+
* Open one through {@link Sirannon.open}.
|
|
780
|
+
*
|
|
781
|
+
* @public
|
|
782
|
+
*/
|
|
783
|
+
declare class Database extends DatabaseBackups {
|
|
784
|
+
/** @internal */
|
|
785
|
+
static create(id: string, path: string, driver: SQLiteDriver, options?: DatabaseOptions, internals?: DatabaseInternals): Promise<Database>;
|
|
786
|
+
/** @internal */
|
|
787
|
+
applyChanges(batch: ReplicationBatch, resolver?: ConflictResolver | ((table: string) => ConflictResolver)): Promise<ApplyResult>;
|
|
788
|
+
/**
|
|
789
|
+
* Returns the device-sync port for this database, which a `SyncController`
|
|
790
|
+
* calls to apply pulled changes, read unpushed local changes, and store its
|
|
791
|
+
* push and pull cursors.
|
|
792
|
+
*
|
|
793
|
+
* @returns The port, which you can also pass to `downloadDatabaseSnapshot`.
|
|
794
|
+
*/
|
|
795
|
+
deviceSync(): DeviceSyncPort;
|
|
796
|
+
/**
|
|
797
|
+
* Executes a read and returns every row.
|
|
798
|
+
*
|
|
799
|
+
* @param sql - The statement to execute.
|
|
800
|
+
* @param params - The values to bind to the statement, named or positional.
|
|
801
|
+
* @param options - The read concern for this statement.
|
|
802
|
+
* @returns The rows that the statement returns.
|
|
803
|
+
*/
|
|
804
|
+
query<T = Record<string, unknown>>(sql: string, params?: Params, options?: QueryOptions): Promise<T[]>;
|
|
805
|
+
/** @internal */
|
|
806
|
+
queryForWire(sql: string, params?: Params, options?: QueryOptions): Promise<unknown[]>;
|
|
807
|
+
/**
|
|
808
|
+
* Executes a read and returns its first row.
|
|
809
|
+
*
|
|
810
|
+
* @param sql - The statement to execute.
|
|
811
|
+
* @param params - The values to bind to the statement, named or positional.
|
|
812
|
+
* @param options - The read concern for this statement.
|
|
813
|
+
* @returns The first row, or undefined when the statement returns none.
|
|
814
|
+
*/
|
|
815
|
+
queryOne<T = Record<string, unknown>>(sql: string, params?: Params, options?: QueryOptions): Promise<T | undefined>;
|
|
816
|
+
/**
|
|
817
|
+
* Executes one write statement.
|
|
818
|
+
*
|
|
819
|
+
* @param sql - The statement to execute.
|
|
820
|
+
* @param params - The values to bind to the statement, named or positional.
|
|
821
|
+
* @param options - The write concern for this statement.
|
|
822
|
+
* @returns The number of rows that the statement changed, and the row id of the last inserted row.
|
|
823
|
+
*/
|
|
824
|
+
execute(sql: string, params?: Params, options?: QueryOptions): Promise<ExecuteResult>;
|
|
825
|
+
/**
|
|
826
|
+
* Executes one statement once for each parameter set, inside a single transaction.
|
|
827
|
+
*
|
|
828
|
+
* @param sql - The statement to execute for each parameter set.
|
|
829
|
+
* @param paramsBatch - One parameter set for each execution.
|
|
830
|
+
* @param options - The write concern for the transaction.
|
|
831
|
+
* @returns One result per parameter set, in order.
|
|
832
|
+
*/
|
|
833
|
+
executeBatch(sql: string, paramsBatch: Params[], options?: QueryOptions): Promise<ExecuteResult[]>;
|
|
834
|
+
/**
|
|
835
|
+
* Imports many rows at a relaxed synchronous level, then restores the level that you configured.
|
|
836
|
+
*
|
|
837
|
+
* Use this for a load that you can re-run from scratch, and use
|
|
838
|
+
* {@link Database.executeBatch} where the rows need full durability. Sirannon
|
|
839
|
+
* holds the writer lock for the whole load and commits every row in one
|
|
840
|
+
* transaction. Whether the load succeeds or fails, Sirannon restores the
|
|
841
|
+
* configured level before this method resolves. The result contains totals
|
|
842
|
+
* for the whole load, so that a large load keeps no per-row results in memory.
|
|
843
|
+
*
|
|
844
|
+
* Like {@link Database.execute}, this writes to the local database only. A
|
|
845
|
+
* server whose execution target has no `bulkLoad` method, such as the
|
|
846
|
+
* replication engine, rejects bulk loads with `BULK_LOAD_UNSUPPORTED`.
|
|
847
|
+
*
|
|
848
|
+
* @param sql - The statement to execute for each parameter set.
|
|
849
|
+
* @param paramsBatch - One parameter set per row.
|
|
850
|
+
* @param options - The synchronous level during the load, and whether the load ends with a checkpoint.
|
|
851
|
+
* @returns The number of parameter sets that the load applied, and the number of rows that changed.
|
|
852
|
+
*/
|
|
853
|
+
bulkLoad(sql: string, paramsBatch: Params[], options?: BulkLoadOptions): Promise<BulkLoadResult>;
|
|
854
|
+
/**
|
|
855
|
+
* Executes a fixed list of statements in one transaction, which Sirannon can commit together with other callers' writes.
|
|
856
|
+
*
|
|
857
|
+
* When every statement is an INSERT, UPDATE, DELETE, or REPLACE, Sirannon
|
|
858
|
+
* groups the transaction with other writes into one commit, and otherwise it
|
|
859
|
+
* executes the transaction on its own. This method takes the statements up
|
|
860
|
+
* front, because a group commit that waited on a caller's callback would delay
|
|
861
|
+
* every other transaction in the group.
|
|
862
|
+
*
|
|
863
|
+
* @param statements - The statements to execute, in order, each with its own parameters.
|
|
864
|
+
* @returns One result per statement, in order.
|
|
865
|
+
*/
|
|
866
|
+
executeTransaction(statements: readonly {
|
|
867
|
+
sql: string;
|
|
868
|
+
params?: Params;
|
|
869
|
+
}[]): Promise<ExecuteResult[]>;
|
|
870
|
+
/**
|
|
871
|
+
* Calls a function inside one transaction, and commits when the function returns or rolls back when it throws.
|
|
872
|
+
*
|
|
873
|
+
* @param fn - The function to call with the transaction, which executes its statements through it.
|
|
874
|
+
* @returns The value that the function returns.
|
|
875
|
+
*/
|
|
876
|
+
transaction<T>(fn: (tx: Transaction) => Promise<T>): Promise<T>;
|
|
877
|
+
/**
|
|
878
|
+
* Starts recording changes to a table, so that subscribers and replication receive them.
|
|
879
|
+
*
|
|
880
|
+
* @param table - The name of the table to watch.
|
|
881
|
+
*/
|
|
882
|
+
watch(table: string): Promise<void>;
|
|
883
|
+
/**
|
|
884
|
+
* Stops recording changes to a table.
|
|
885
|
+
*
|
|
886
|
+
* @param table - The name of the table to stop watching.
|
|
887
|
+
*/
|
|
888
|
+
unwatch(table: string): Promise<void>;
|
|
889
|
+
/**
|
|
890
|
+
* Executes a change-log maintenance write, such as pruning, on the shared
|
|
891
|
+
* writer connection under the writer lock, so that it queues behind
|
|
892
|
+
* application writes and never contends with them for SQLite's write lock,
|
|
893
|
+
* which would block the event loop for the `busy_timeout`.
|
|
894
|
+
*
|
|
895
|
+
* @internal
|
|
896
|
+
*/
|
|
897
|
+
runCdcMaintenance(op: (writer: SQLiteConnection) => Promise<unknown>): Promise<void>;
|
|
898
|
+
/** @internal */
|
|
899
|
+
ensureChangeStamping(): Promise<void>;
|
|
900
|
+
/**
|
|
901
|
+
* Starts building a change subscription on a watched table.
|
|
902
|
+
*
|
|
903
|
+
* @param table - The name of the watched table.
|
|
904
|
+
* @returns A builder that you narrow with a filter and then subscribe to.
|
|
905
|
+
*/
|
|
906
|
+
on(table: string): SubscriptionBuilder;
|
|
907
|
+
/**
|
|
908
|
+
* Opens a live query that keeps the rows of a single-table SELECT current as that table changes, and watches the table first.
|
|
909
|
+
*
|
|
910
|
+
* @param sql - The SELECT statement to keep current.
|
|
911
|
+
* @param params - The values to bind to the statement, named or positional.
|
|
912
|
+
* @param options - The re-read jitter, the transaction size above which the query re-reads, and the reporter for listener failures.
|
|
913
|
+
* @returns The live query, holding its first rows and subscribed to changes.
|
|
914
|
+
*/
|
|
915
|
+
live<T = Record<string, unknown>>(sql: string, params?: Params, options?: LiveQueryOptions): Promise<LiveQuery<T>>;
|
|
916
|
+
/**
|
|
917
|
+
* Applies every migration that this database has yet to apply, in ascending version order.
|
|
918
|
+
*
|
|
919
|
+
* @param migrations - The full set of migrations for this database.
|
|
920
|
+
* @returns The migrations that this call applied, and the number that the database had already applied.
|
|
921
|
+
*/
|
|
922
|
+
migrate(migrations: Migration[]): Promise<MigrationResult>;
|
|
923
|
+
/**
|
|
924
|
+
* Lists the migrations that this database has applied.
|
|
925
|
+
*
|
|
926
|
+
* @returns One entry per applied migration, with its version, name, and checksum.
|
|
927
|
+
*/
|
|
928
|
+
appliedMigrations(): Promise<AppliedMigrationRow[]>;
|
|
929
|
+
/**
|
|
930
|
+
* Reverts applied migrations, newest first.
|
|
931
|
+
*
|
|
932
|
+
* @param migrations - The full set of migrations, from which the runner takes each down statement.
|
|
933
|
+
* @param version - The version to roll back to, which stays applied with every version below it; when you omit it, the runner reverts only the newest migration.
|
|
934
|
+
* @returns The migrations that this call reverted.
|
|
935
|
+
*/
|
|
936
|
+
rollback(migrations: Migration[], version?: number): Promise<RollbackResult>;
|
|
937
|
+
/**
|
|
938
|
+
* Loads a compiled SQLite extension into every connection of this database.
|
|
939
|
+
*
|
|
940
|
+
* @param extensionPath - The path to the extension, which the driver resolves to an absolute path.
|
|
941
|
+
*/
|
|
942
|
+
loadExtension(extensionPath: string): Promise<void>;
|
|
943
|
+
/**
|
|
944
|
+
* Registers a hook that Sirannon calls before each statement on this database; throw from the hook to reject the statement.
|
|
945
|
+
*
|
|
946
|
+
* @param hook - The hook, which Sirannon calls with the statement, its parameters, and its read or write concern.
|
|
947
|
+
* @returns A function that removes the hook.
|
|
948
|
+
*/
|
|
949
|
+
onBeforeQuery(hook: BeforeQueryHook): HookDispose;
|
|
950
|
+
/**
|
|
951
|
+
* Registers a hook that Sirannon calls after each statement on this database.
|
|
952
|
+
*
|
|
953
|
+
* @param hook - The hook, which Sirannon calls with the statement and its duration.
|
|
954
|
+
* @returns A function that removes the hook.
|
|
955
|
+
*/
|
|
956
|
+
onAfterQuery(hook: AfterQueryHook): HookDispose;
|
|
957
|
+
}
|
|
958
|
+
|
|
959
|
+
export { ConnectionPool as C, Database as D, HookRegistry as H, MetricsCollector as M, type PushHookContext as P, type SnapshotHookContext as S, type HookDispose as a, type ConnectionPoolOptions as b, DatabaseBackups as c, DatabaseLifecycle as d, type HookEvent as e, type HookEventContextMap as f, type HookHandler as g, type SubscribeHookContext as h, execute as i, executeBatch as j, queryOne as k, type DeviceSyncPort as l, HLC as m, query as q };
|