@delali/sirannon-db 0.1.1 → 0.1.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.
@@ -1,88 +1,36 @@
1
- import Database from 'better-sqlite3';
2
- import { B as BackupScheduleOptions, D as DatabaseOptions, L as LifecycleConfig, P as Params, E as ExecuteResult } from '../types-DArCObcu.js';
3
- export { A as AfterQueryHook, a as BeforeConnectHook, b as BeforeQueryHook, c as BeforeSubscribeHook, C as CDCMetrics, d as ChangeEvent, e as ChangeOperation, f as ClientOptions, g as ConnectionHookContext, h as ConnectionMetrics, i as CorsOptions, j as DatabaseCloseHook, k as DatabaseOpenHook, H as HookConfig, M as MetricsConfig, O as OnRequestHook, Q as QueryHookContext, l as QueryMetrics, R as RequestContext, m as RequestDenial, S as ServerOptions, n as SirannonOptions, o as Subscription, p as SubscriptionBuilder, W as WSHandlerOptions } from '../types-DArCObcu.js';
4
- import { D as Database$1, M as Migration, a as MigrationResult, R as RollbackResult } from '../sirannon-BJ8Yd1Uf.js';
5
- export { A as AppliedMigration, b as AppliedMigrationEntry, H as HookDispose, c as HookEvent, d as HookEventContextMap, e as HookHandler, f as HookRegistry, g as MetricsCollector, S as Sirannon, h as SubscribeHookContext, T as Transaction } from '../sirannon-BJ8Yd1Uf.js';
6
-
7
- type SqliteDb$4 = InstanceType<typeof Database>;
8
- declare class BackupManager {
9
- /**
10
- * Creates a one-shot backup of the database using VACUUM INTO.
11
- * Produces a fresh, defragmented copy at the specified destination path.
12
- *
13
- * The destination file must not already exist. Parent directories are
14
- * created automatically when missing.
15
- *
16
- * Note: there is a narrow TOCTOU window between the existence check and
17
- * the VACUUM INTO statement. In the unlikely event that another process
18
- * creates a file at the same path during that window, VACUUM INTO may
19
- * silently overwrite it depending on the SQLite version.
20
- */
21
- backup(db: SqliteDb$4, destPath: string): void;
22
- /**
23
- * Generates a timestamped backup filename.
24
- *
25
- * Format: `backup-YYYY-MM-DDTHH-MM-SS-mmmZ.db`
26
- *
27
- * Colons and dots in the ISO timestamp are replaced with hyphens so the
28
- * filename is safe on every major filesystem.
29
- */
30
- generateFilename(): string;
31
- /**
32
- * Removes old backup files in {@link dir}, keeping the {@link maxFiles}
33
- * most recent entries. Only files matching the `backup-*.db` naming
34
- * convention are considered; other files in the directory are left alone.
35
- *
36
- * When {@link maxFiles} is zero or negative the method is a no-op.
37
- * A non-existent directory is silently ignored.
38
- */
39
- rotate(dir: string, maxFiles: number): void;
40
- }
41
-
42
- type SqliteDb$3 = InstanceType<typeof Database>;
43
- declare class BackupScheduler {
44
- private readonly manager;
45
- constructor(manager?: BackupManager);
46
- /**
47
- * Schedules periodic backups on a cron expression.
48
- *
49
- * Each tick creates a timestamped backup file inside {@link options.destDir}
50
- * and rotates old files so no more than {@link options.maxFiles} (default 5)
51
- * are retained.
52
- *
53
- * Provide {@link options.onError} to receive notification when a scheduled
54
- * backup fails. Without it, errors are silently discarded to prevent
55
- * unhandled exceptions from crashing the process.
56
- *
57
- * The underlying timer is unreferenced so it won't keep the Node.js
58
- * process alive on its own (consistent with the CDC polling timer).
59
- *
60
- * Returns a cancel function that stops the scheduled job immediately.
61
- */
62
- schedule(db: SqliteDb$3, options: BackupScheduleOptions): () => void;
63
- }
1
+ export { B as BackupManager, a as BackupScheduler } from '../index-hXiis3N-.js';
2
+ import { S as SQLiteDriver, a as SQLiteConnection } from '../types-BFSsG77t.js';
3
+ export { D as DriverCapabilities, O as OpenOptions, R as RunResult, b as SQLiteStatement } from '../types-BFSsG77t.js';
4
+ import { D as Database } from '../sirannon-B1oTfebD.js';
5
+ export { H as HookDispose, a as HookEvent, b as HookEventContextMap, c as HookHandler, d as HookRegistry, M as MetricsCollector, S as Sirannon, e as SubscribeHookContext } from '../sirannon-B1oTfebD.js';
6
+ import { D as DatabaseOptions, L as LifecycleConfig, P as Params, E as ExecuteResult } from '../types-DtDutWRU.js';
7
+ export { A as AfterQueryHook, e as BackupScheduleOptions, h as BeforeConnectHook, f as BeforeQueryHook, B as BeforeSubscribeHook, c as CDCMetrics, k as ChangeEvent, l as ChangeOperation, m as ClientOptions, C as ConnectionHookContext, b as ConnectionMetrics, n as CorsOptions, j as DatabaseCloseHook, i as DatabaseOpenHook, H as HookConfig, M as MetricsConfig, O as OnRequestHook, Q as QueryHookContext, a as QueryMetrics, R as RequestContext, o as RequestDenial, S as ServerOptions, g as SirannonOptions, p as Subscription, d as SubscriptionBuilder, W as WSHandlerOptions } from '../types-DtDutWRU.js';
8
+ import { M as Migration, a as MigrationResult, R as RollbackResult } from '../types-DRkJlqex.js';
9
+ export { A as AppliedMigration, b as AppliedMigrationEntry, T as Transaction } from '../types-DRkJlqex.js';
64
10
 
65
11
  interface ConnectionPoolOptions {
12
+ driver: SQLiteDriver;
66
13
  path: string;
67
14
  readOnly?: boolean;
68
15
  readPoolSize?: number;
69
16
  walMode?: boolean;
70
17
  }
71
- type SqliteDb$2 = InstanceType<typeof Database>;
72
18
  declare class ConnectionPool {
73
19
  private readonly writer;
74
20
  private readonly readers;
75
21
  private readerIndex;
76
22
  private closed;
77
- constructor(options: ConnectionPoolOptions);
78
- acquireReader(): SqliteDb$2;
79
- acquireWriter(): SqliteDb$2;
23
+ private constructor();
24
+ static create(options: ConnectionPoolOptions): Promise<ConnectionPool>;
25
+ acquireReader(): SQLiteConnection;
26
+ acquireWriter(): SQLiteConnection;
80
27
  get readerCount(): number;
81
28
  get isReadOnly(): boolean;
82
- loadExtension(extensionPath: string): void;
83
- close(): void;
29
+ close(): Promise<void>;
84
30
  }
85
31
 
32
+ declare function defineDriver(config: SQLiteDriver): SQLiteDriver;
33
+
86
34
  /**
87
35
  * Base class for all sirannon-db errors. Extend this class to create
88
36
  * domain-specific errors that carry a machine-readable {@link code}.
@@ -183,113 +131,53 @@ declare class ExtensionError extends SirannonError {
183
131
  constructor(path: string, cause?: string);
184
132
  }
185
133
 
186
- /**
187
- * Callbacks the LifecycleManager uses to interact with the database registry
188
- * (Sirannon). Injected at construction to avoid circular dependencies.
189
- */
190
134
  interface LifecycleCallbacks {
191
- open: (id: string, path: string, options?: DatabaseOptions) => Database$1;
192
- close: (id: string) => void;
135
+ open: (id: string, path: string, options?: DatabaseOptions) => Promise<Database>;
136
+ close: (id: string) => Promise<void>;
193
137
  count: () => number;
194
138
  has: (id: string) => boolean;
195
139
  }
196
- /**
197
- * Manages automatic database lifecycle: on-demand opening via a resolver,
198
- * idle-timeout based closing, and LRU eviction when the maximum number of
199
- * open databases is reached.
200
- */
201
140
  declare class LifecycleManager {
141
+ #private;
202
142
  private readonly config;
203
143
  private readonly callbacks;
204
144
  private readonly lastAccess;
205
145
  private idleTimer;
206
146
  private _disposed;
207
147
  constructor(config: LifecycleConfig, callbacks: LifecycleCallbacks);
208
- /**
209
- * Attempt to auto-open a database by ID using the configured resolver.
210
- * Returns the opened Database, or `undefined` when no resolver is
211
- * configured or the resolver does not recognise the ID.
212
- *
213
- * Throws {@link MaxDatabasesError} when the registry is at capacity and
214
- * eviction cannot free a slot.
215
- */
216
- resolve(id: string): Database$1 | undefined;
217
- /** Record an access for the given database ID. */
148
+ resolve(id: string): Promise<Database | undefined>;
218
149
  markActive(id: string): void;
219
- /**
220
- * Close every database whose last access was longer ago than the
221
- * configured idle timeout. Also cleans up tracking entries for
222
- * databases that were closed externally.
223
- */
224
- checkIdle(): void;
225
- /**
226
- * Close the least-recently-used tracked database. Called internally
227
- * by {@link resolve} when `maxOpen` capacity is reached.
228
- */
229
- evict(): void;
230
- /** Remove a database from idle tracking (e.g. after an explicit close). */
150
+ checkIdle(): Promise<void>;
151
+ evict(): Promise<void>;
231
152
  untrack(id: string): void;
232
- /** Whether this manager has been disposed. */
233
153
  get disposed(): boolean;
234
- /** The number of databases currently tracked for idle management. */
235
154
  get trackedCount(): number;
236
- /** Shut down the manager: stop the idle timer and clear all state. */
237
155
  dispose(): void;
238
156
  private ensureNotDisposed;
239
157
  }
240
158
 
241
- /**
242
- * Options for {@link createTenantResolver}.
243
- */
244
159
  interface TenantResolverOptions {
245
- /** Base directory where tenant database files are stored. */
246
160
  basePath: string;
247
- /** File extension for database files. Default: `'.db'`. */
248
161
  extension?: string;
249
- /** Default options applied to every auto-opened tenant database. */
250
162
  defaultOptions?: DatabaseOptions;
251
163
  }
252
- /**
253
- * Validate and sanitize a tenant ID. Returns the ID unchanged when
254
- * it passes validation, or `undefined` when it is invalid.
255
- *
256
- * Rules:
257
- * - Must be 1–255 characters long
258
- * - Must start with a letter or digit
259
- * - May contain letters, digits, hyphens, and underscores
260
- * - Must not contain `..` (redundant given the regex, but explicit)
261
- */
262
164
  declare function sanitizeTenantId(id: string): string | undefined;
263
- /**
264
- * Build a filesystem path for a tenant database.
265
- *
266
- * @throws {Error} when the tenant ID fails validation.
267
- */
268
165
  declare function tenantPath(basePath: string, tenantId: string, extension?: string): string;
269
- /**
270
- * Create a resolver function suitable for {@link LifecycleConfig.autoOpen}.
271
- * Maps tenant IDs to database file paths under a common base directory.
272
- *
273
- * Invalid tenant IDs (path traversal, special characters) cause the
274
- * resolver to return `undefined`, preventing the database from opening.
275
- */
276
166
  declare function createTenantResolver(options: TenantResolverOptions): (id: string) => {
277
167
  path: string;
278
168
  options?: DatabaseOptions;
279
169
  } | undefined;
280
170
 
281
- type SqliteDb$1 = InstanceType<typeof Database>;
282
171
  declare class MigrationRunner {
283
- static run(db: SqliteDb$1, input: string | Migration[]): MigrationResult;
284
- static rollback(db: SqliteDb$1, input: string | Migration[], version?: number): RollbackResult;
172
+ static run(conn: SQLiteConnection, migrations: Migration[]): Promise<MigrationResult>;
173
+ static rollback(conn: SQLiteConnection, migrations: Migration[], version?: number): Promise<RollbackResult>;
285
174
  private static validateMigrations;
286
175
  private static getAppliedVersions;
287
176
  }
288
177
 
289
- type SqliteDb = InstanceType<typeof Database>;
290
- declare function query<T = Record<string, unknown>>(db: SqliteDb, sql: string, params?: Params): T[];
291
- declare function queryOne<T = Record<string, unknown>>(db: SqliteDb, sql: string, params?: Params): T | undefined;
292
- declare function execute(db: SqliteDb, sql: string, params?: Params): ExecuteResult;
293
- declare function executeBatch(db: SqliteDb, sql: string, paramsBatch: Params[]): ExecuteResult[];
178
+ declare function query<T = Record<string, unknown>>(conn: SQLiteConnection, sql: string, params?: Params): Promise<T[]>;
179
+ declare function queryOne<T = Record<string, unknown>>(conn: SQLiteConnection, sql: string, params?: Params): Promise<T | undefined>;
180
+ declare function execute(conn: SQLiteConnection, sql: string, params?: Params): Promise<ExecuteResult>;
181
+ declare function executeBatch(conn: SQLiteConnection, sql: string, paramsBatch: Params[]): Promise<ExecuteResult[]>;
294
182
 
295
- export { BackupError, BackupManager, BackupScheduleOptions, BackupScheduler, CDCError, ConnectionPool, ConnectionPoolError, type ConnectionPoolOptions, Database$1 as Database, DatabaseAlreadyExistsError, DatabaseNotFoundError, DatabaseOptions, ExecuteResult, ExtensionError, HookDeniedError, type LifecycleCallbacks, LifecycleConfig, LifecycleManager, MaxDatabasesError, Migration, MigrationError, MigrationResult, MigrationRunner, Params, QueryError, ReadOnlyError, RollbackResult, SirannonError, type TenantResolverOptions, TransactionError, createTenantResolver, execute, executeBatch, query, queryOne, sanitizeTenantId, tenantPath };
183
+ export { BackupError, CDCError, ConnectionPool, ConnectionPoolError, type ConnectionPoolOptions, Database, DatabaseAlreadyExistsError, DatabaseNotFoundError, DatabaseOptions, ExecuteResult, ExtensionError, HookDeniedError, type LifecycleCallbacks, LifecycleConfig, LifecycleManager, MaxDatabasesError, Migration, MigrationError, MigrationResult, MigrationRunner, Params, QueryError, ReadOnlyError, RollbackResult, SQLiteConnection, SQLiteDriver, SirannonError, type TenantResolverOptions, TransactionError, createTenantResolver, defineDriver, execute, executeBatch, query, queryOne, sanitizeTenantId, tenantPath };