@atscript/db-sqlite 0.1.176 → 0.1.178

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/index.d.cts CHANGED
@@ -354,6 +354,8 @@ declare class SqliteAdapter extends BaseDbAdapter {
354
354
  private _vectorPartitionFields;
355
355
  /** Per-driver transaction gate (shared by every adapter over this driver). */
356
356
  private readonly _gate;
357
+ /** Open {@link _atomically} units of the current (synchronous) call. */
358
+ private _unitDepth;
357
359
  private readonly _txOptions;
358
360
  /** `ftsTokenizers` — the FTS5 tokenizer per fulltext language (since 0.1.175). */
359
361
  private readonly _ftsTokenizers;
@@ -363,8 +365,30 @@ declare class SqliteAdapter extends BaseDbAdapter {
363
365
  private readonly _hasBucketFn;
364
366
  constructor(driver: TSqliteDriver, options?: SqliteAdapterOptions);
365
367
  onFieldScanned(field: string, _type: unknown, metadata: TMetadataMap<AtscriptMetadata>): void;
368
+ /**
369
+ * A vector field (since 0.1.178): with sqlite-vec a Float32 BLOB
370
+ * ({@link float32Blob}), the column type `typeMapper` gives it — the mapper
371
+ * hands the formatter JSON text, converted when it holds an array of
372
+ * numbers (anything else is stored as is); without sqlite-vec, JSON text.
373
+ * Rows written before hold JSON text and are not rewritten; a vector
374
+ * changed by an array patch operator is stored as JSON text too
375
+ * (`resolveArrayOps` writes without `toStorage`). Reads take both forms.
376
+ * The extension is probed per value, not here — metadata is built where a
377
+ * busy probe must not run (see {@link _detectVectorSupport}) — so a busy
378
+ * probe fails the write with `LOCK_TIMEOUT`, never stores JSON text.
379
+ *
380
+ * Any other JSON `number[]` column reads a BLOB the same way: a view column
381
+ * reading a vector (the annotation is not passed to a referring field), or
382
+ * a field whose annotation was removed.
383
+ */
366
384
  formatValue(field: TDbFieldMeta): TValueFormatterPair | undefined;
367
385
  isVectorSearchable(): boolean;
386
+ /**
387
+ * Whether sqlite-vec is loaded: the driver's `hasVectorExt`, else a probe,
388
+ * answered once. A busy probe answers nothing — it throws the retryable
389
+ * `LOCK_TIMEOUT` (the caller's write, search or sync fails, the next call
390
+ * probes again).
391
+ */
368
392
  private _detectVectorSupport;
369
393
  private _waitOpts;
370
394
  /** Every adapter over this driver shares one transaction — and one gate (since 0.1.128). */
@@ -376,7 +400,7 @@ declare class SqliteAdapter extends BaseDbAdapter {
376
400
  * exclusive hold) it runs immediately; otherwise it waits until no
377
401
  * transaction is open on the connection. The `held` check and the statement
378
402
  * run in the same synchronous segment, so they cannot interleave with a
379
- * `BEGIN` from another context.
403
+ * `BEGIN` from another context. A busy statement rejects with `LOCK_TIMEOUT`.
380
404
  */
381
405
  private _stmt;
382
406
  /**
@@ -388,7 +412,8 @@ declare class SqliteAdapter extends BaseDbAdapter {
388
412
  * request's transaction (PRAGMA changes are no-ops inside one). Inside our
389
413
  * own transaction `fn` runs directly; re-entrant for the same async
390
414
  * context; nested `withTransaction` calls inside `fn` still BEGIN/COMMIT
391
- * on the held connection.
415
+ * on the held connection. A busy step rejects with `LOCK_TIMEOUT`, after
416
+ * `fn`'s own `finally` blocks restored the connection's PRAGMAs.
392
417
  */
393
418
  protected _withExclusiveConnection<T>(fn: () => Promise<T>): Promise<T>;
394
419
  protected _beginTransaction(): Promise<SqliteTxState>;
@@ -479,6 +504,8 @@ declare class SqliteAdapter extends BaseDbAdapter {
479
504
  /**
480
505
  * Seeds the sqlite_sequence table for auto-increment fields that have a start value.
481
506
  * Only applies once per adapter instance (idempotent via INSERT OR IGNORE + flag).
507
+ * The flag is set once the seed ran: a failed (busy) seed is retried by the
508
+ * next `ensureTable`.
482
509
  */
483
510
  private _seedIncrementStart;
484
511
  ensureView(): Promise<void>;
@@ -522,6 +549,12 @@ declare class SqliteAdapter extends BaseDbAdapter {
522
549
  */
523
550
  getExistingColumnsForTable(tableName: string): Promise<TExistingColumn[]>;
524
551
  syncIndexes(): Promise<TIndexChange[]>;
552
+ /**
553
+ * The plan reads the live index state (`PRAGMA index_list` / `index_info`)
554
+ * through the gate like {@link syncIndexes}: never inside another context's
555
+ * open transaction, and a busy read rejects with `LOCK_TIMEOUT`.
556
+ */
557
+ planIndexes(): Promise<TIndexChange[]>;
525
558
  protected _syncIndexes(dryRun: boolean): Promise<TIndexChange[]>;
526
559
  getSearchIndexes(): TSearchIndexInfo[];
527
560
  search(text: string, query: DbQuery, indexName?: string): Promise<Array<Record<string, unknown>>>;
@@ -593,10 +626,46 @@ declare class SqliteAdapter extends BaseDbAdapter {
593
626
  private _probeFtsTokenizer;
594
627
  /** Whether an existing FTS5 table has exactly the index's text columns, in order. */
595
628
  private _ftsColumnsMatch;
596
- /** Creates an FTS5 virtual table with sync triggers and rebuilds the index. */
629
+ /**
630
+ * Creates an FTS5 virtual table with sync triggers and rebuilds the index —
631
+ * all or nothing (since 0.1.178, see {@link _atomically}).
632
+ */
597
633
  private _createFtsTable;
598
- /** Drops an FTS5 virtual table and its sync triggers. */
634
+ /** Drops an FTS5 virtual table and its sync triggers — all or nothing (since 0.1.178). */
599
635
  private _dropFtsTable;
636
+ /**
637
+ * Runs the synchronous `fn` as one all-or-nothing unit (since 0.1.178) —
638
+ * the FTS5 / vec0 shadow-table steps: a statement failing between a
639
+ * `CREATE VIRTUAL TABLE` and its triggers left the table without them, and
640
+ * the next sync, seeing the table, kept it: the index silently stopped
641
+ * following writes.
642
+ *
643
+ * - The unit opens the transaction itself (none is open, no unit around
644
+ * it): `BEGIN IMMEDIATE` … `COMMIT`, and on any failure — a `COMMIT`
645
+ * refused while another connection reads (rollback-journal mode)
646
+ * included — a full `ROLLBACK`, which ends the transaction. IMMEDIATE
647
+ * takes the write lock up front, waiting on `busy_timeout`: a read-only
648
+ * first statement (`DROP TRIGGER IF EXISTS` of a missing trigger) cannot
649
+ * pin a WAL snapshot that a later write then fails on without waiting
650
+ * (`SQLITE_BUSY_SNAPSHOT`).
651
+ * - Inside a transaction (the adapter's own: `recreateTable`, a caller's
652
+ * `withTransaction` around a schema call) or another unit (the FTS
653
+ * rebuild wraps a drop and a create): a `SAVEPOINT`, rolled back to on
654
+ * failure. Nothing commits there, so nothing waits on another connection.
655
+ *
656
+ * "Open" is the adapter's view — its transaction state and the unit depth;
657
+ * the driver interface has no in-transaction flag. Every caller runs under
658
+ * `_stmt` or `_withExclusiveConnection`, so another context's transaction
659
+ * is never open on the connection meanwhile; one opened behind the
660
+ * adapter's back makes `BEGIN IMMEDIATE` fail without running the unit.
661
+ *
662
+ * Errors propagate as thrown: the caller's boundary maps SQLITE_BUSY —
663
+ * at `BEGIN`, `COMMIT` or any statement — to `LOCK_TIMEOUT`. A failing
664
+ * rollback never replaces the original error; it is logged as an error,
665
+ * because the connection may then still be in the transaction (the next
666
+ * `BEGIN` fails, autocommit statements would join it).
667
+ */
668
+ private _atomically;
600
669
  /** Lists FTS5/vec0 shadow virtual tables for a content table. */
601
670
  private _listShadowTables;
602
671
  /** {@link _listShadowTables} with each virtual table's `CREATE` statement. */
@@ -630,7 +699,9 @@ declare class SqliteAdapter extends BaseDbAdapter {
630
699
  * Geo search is available when SQLite has math functions
631
700
  * (`SQLITE_ENABLE_MATH_FUNCTIONS` — on by default in better-sqlite3 builds).
632
701
  * Distance is computed per row via haversine; declared geo indexes have no
633
- * physical artifact on SQLite.
702
+ * physical artifact on SQLite. Probed once; a busy probe throws the
703
+ * retryable `LOCK_TIMEOUT` and the next call probes again (a `false` would
704
+ * turn into a non-retryable `GEO_NOT_SUPPORTED`).
634
705
  */
635
706
  isGeoSearchable(): boolean;
636
707
  private _detectMathFns;
@@ -650,7 +721,7 @@ declare class SqliteAdapter extends BaseDbAdapter {
650
721
  private _syncVecIndexes;
651
722
  /** Creates a vec0 virtual shadow table with sync triggers and seeds it from existing rows. */
652
723
  private _createVecTable;
653
- /** Drops a vec0 virtual table and its sync triggers. */
724
+ /** Drops a vec0 virtual table and its sync triggers — all or nothing (since 0.1.178). */
654
725
  private _dropVecTable;
655
726
  /** Drops all vec0 virtual shadow tables and triggers for a content table. */
656
727
  private _dropAllVecTables;
package/dist/index.d.mts CHANGED
@@ -354,6 +354,8 @@ declare class SqliteAdapter extends BaseDbAdapter {
354
354
  private _vectorPartitionFields;
355
355
  /** Per-driver transaction gate (shared by every adapter over this driver). */
356
356
  private readonly _gate;
357
+ /** Open {@link _atomically} units of the current (synchronous) call. */
358
+ private _unitDepth;
357
359
  private readonly _txOptions;
358
360
  /** `ftsTokenizers` — the FTS5 tokenizer per fulltext language (since 0.1.175). */
359
361
  private readonly _ftsTokenizers;
@@ -363,8 +365,30 @@ declare class SqliteAdapter extends BaseDbAdapter {
363
365
  private readonly _hasBucketFn;
364
366
  constructor(driver: TSqliteDriver, options?: SqliteAdapterOptions);
365
367
  onFieldScanned(field: string, _type: unknown, metadata: TMetadataMap<AtscriptMetadata>): void;
368
+ /**
369
+ * A vector field (since 0.1.178): with sqlite-vec a Float32 BLOB
370
+ * ({@link float32Blob}), the column type `typeMapper` gives it — the mapper
371
+ * hands the formatter JSON text, converted when it holds an array of
372
+ * numbers (anything else is stored as is); without sqlite-vec, JSON text.
373
+ * Rows written before hold JSON text and are not rewritten; a vector
374
+ * changed by an array patch operator is stored as JSON text too
375
+ * (`resolveArrayOps` writes without `toStorage`). Reads take both forms.
376
+ * The extension is probed per value, not here — metadata is built where a
377
+ * busy probe must not run (see {@link _detectVectorSupport}) — so a busy
378
+ * probe fails the write with `LOCK_TIMEOUT`, never stores JSON text.
379
+ *
380
+ * Any other JSON `number[]` column reads a BLOB the same way: a view column
381
+ * reading a vector (the annotation is not passed to a referring field), or
382
+ * a field whose annotation was removed.
383
+ */
366
384
  formatValue(field: TDbFieldMeta): TValueFormatterPair | undefined;
367
385
  isVectorSearchable(): boolean;
386
+ /**
387
+ * Whether sqlite-vec is loaded: the driver's `hasVectorExt`, else a probe,
388
+ * answered once. A busy probe answers nothing — it throws the retryable
389
+ * `LOCK_TIMEOUT` (the caller's write, search or sync fails, the next call
390
+ * probes again).
391
+ */
368
392
  private _detectVectorSupport;
369
393
  private _waitOpts;
370
394
  /** Every adapter over this driver shares one transaction — and one gate (since 0.1.128). */
@@ -376,7 +400,7 @@ declare class SqliteAdapter extends BaseDbAdapter {
376
400
  * exclusive hold) it runs immediately; otherwise it waits until no
377
401
  * transaction is open on the connection. The `held` check and the statement
378
402
  * run in the same synchronous segment, so they cannot interleave with a
379
- * `BEGIN` from another context.
403
+ * `BEGIN` from another context. A busy statement rejects with `LOCK_TIMEOUT`.
380
404
  */
381
405
  private _stmt;
382
406
  /**
@@ -388,7 +412,8 @@ declare class SqliteAdapter extends BaseDbAdapter {
388
412
  * request's transaction (PRAGMA changes are no-ops inside one). Inside our
389
413
  * own transaction `fn` runs directly; re-entrant for the same async
390
414
  * context; nested `withTransaction` calls inside `fn` still BEGIN/COMMIT
391
- * on the held connection.
415
+ * on the held connection. A busy step rejects with `LOCK_TIMEOUT`, after
416
+ * `fn`'s own `finally` blocks restored the connection's PRAGMAs.
392
417
  */
393
418
  protected _withExclusiveConnection<T>(fn: () => Promise<T>): Promise<T>;
394
419
  protected _beginTransaction(): Promise<SqliteTxState>;
@@ -479,6 +504,8 @@ declare class SqliteAdapter extends BaseDbAdapter {
479
504
  /**
480
505
  * Seeds the sqlite_sequence table for auto-increment fields that have a start value.
481
506
  * Only applies once per adapter instance (idempotent via INSERT OR IGNORE + flag).
507
+ * The flag is set once the seed ran: a failed (busy) seed is retried by the
508
+ * next `ensureTable`.
482
509
  */
483
510
  private _seedIncrementStart;
484
511
  ensureView(): Promise<void>;
@@ -522,6 +549,12 @@ declare class SqliteAdapter extends BaseDbAdapter {
522
549
  */
523
550
  getExistingColumnsForTable(tableName: string): Promise<TExistingColumn[]>;
524
551
  syncIndexes(): Promise<TIndexChange[]>;
552
+ /**
553
+ * The plan reads the live index state (`PRAGMA index_list` / `index_info`)
554
+ * through the gate like {@link syncIndexes}: never inside another context's
555
+ * open transaction, and a busy read rejects with `LOCK_TIMEOUT`.
556
+ */
557
+ planIndexes(): Promise<TIndexChange[]>;
525
558
  protected _syncIndexes(dryRun: boolean): Promise<TIndexChange[]>;
526
559
  getSearchIndexes(): TSearchIndexInfo[];
527
560
  search(text: string, query: DbQuery, indexName?: string): Promise<Array<Record<string, unknown>>>;
@@ -593,10 +626,46 @@ declare class SqliteAdapter extends BaseDbAdapter {
593
626
  private _probeFtsTokenizer;
594
627
  /** Whether an existing FTS5 table has exactly the index's text columns, in order. */
595
628
  private _ftsColumnsMatch;
596
- /** Creates an FTS5 virtual table with sync triggers and rebuilds the index. */
629
+ /**
630
+ * Creates an FTS5 virtual table with sync triggers and rebuilds the index —
631
+ * all or nothing (since 0.1.178, see {@link _atomically}).
632
+ */
597
633
  private _createFtsTable;
598
- /** Drops an FTS5 virtual table and its sync triggers. */
634
+ /** Drops an FTS5 virtual table and its sync triggers — all or nothing (since 0.1.178). */
599
635
  private _dropFtsTable;
636
+ /**
637
+ * Runs the synchronous `fn` as one all-or-nothing unit (since 0.1.178) —
638
+ * the FTS5 / vec0 shadow-table steps: a statement failing between a
639
+ * `CREATE VIRTUAL TABLE` and its triggers left the table without them, and
640
+ * the next sync, seeing the table, kept it: the index silently stopped
641
+ * following writes.
642
+ *
643
+ * - The unit opens the transaction itself (none is open, no unit around
644
+ * it): `BEGIN IMMEDIATE` … `COMMIT`, and on any failure — a `COMMIT`
645
+ * refused while another connection reads (rollback-journal mode)
646
+ * included — a full `ROLLBACK`, which ends the transaction. IMMEDIATE
647
+ * takes the write lock up front, waiting on `busy_timeout`: a read-only
648
+ * first statement (`DROP TRIGGER IF EXISTS` of a missing trigger) cannot
649
+ * pin a WAL snapshot that a later write then fails on without waiting
650
+ * (`SQLITE_BUSY_SNAPSHOT`).
651
+ * - Inside a transaction (the adapter's own: `recreateTable`, a caller's
652
+ * `withTransaction` around a schema call) or another unit (the FTS
653
+ * rebuild wraps a drop and a create): a `SAVEPOINT`, rolled back to on
654
+ * failure. Nothing commits there, so nothing waits on another connection.
655
+ *
656
+ * "Open" is the adapter's view — its transaction state and the unit depth;
657
+ * the driver interface has no in-transaction flag. Every caller runs under
658
+ * `_stmt` or `_withExclusiveConnection`, so another context's transaction
659
+ * is never open on the connection meanwhile; one opened behind the
660
+ * adapter's back makes `BEGIN IMMEDIATE` fail without running the unit.
661
+ *
662
+ * Errors propagate as thrown: the caller's boundary maps SQLITE_BUSY —
663
+ * at `BEGIN`, `COMMIT` or any statement — to `LOCK_TIMEOUT`. A failing
664
+ * rollback never replaces the original error; it is logged as an error,
665
+ * because the connection may then still be in the transaction (the next
666
+ * `BEGIN` fails, autocommit statements would join it).
667
+ */
668
+ private _atomically;
600
669
  /** Lists FTS5/vec0 shadow virtual tables for a content table. */
601
670
  private _listShadowTables;
602
671
  /** {@link _listShadowTables} with each virtual table's `CREATE` statement. */
@@ -630,7 +699,9 @@ declare class SqliteAdapter extends BaseDbAdapter {
630
699
  * Geo search is available when SQLite has math functions
631
700
  * (`SQLITE_ENABLE_MATH_FUNCTIONS` — on by default in better-sqlite3 builds).
632
701
  * Distance is computed per row via haversine; declared geo indexes have no
633
- * physical artifact on SQLite.
702
+ * physical artifact on SQLite. Probed once; a busy probe throws the
703
+ * retryable `LOCK_TIMEOUT` and the next call probes again (a `false` would
704
+ * turn into a non-retryable `GEO_NOT_SUPPORTED`).
634
705
  */
635
706
  isGeoSearchable(): boolean;
636
707
  private _detectMathFns;
@@ -650,7 +721,7 @@ declare class SqliteAdapter extends BaseDbAdapter {
650
721
  private _syncVecIndexes;
651
722
  /** Creates a vec0 virtual shadow table with sync triggers and seeds it from existing rows. */
652
723
  private _createVecTable;
653
- /** Drops a vec0 virtual table and its sync triggers. */
724
+ /** Drops a vec0 virtual table and its sync triggers — all or nothing (since 0.1.178). */
654
725
  private _dropVecTable;
655
726
  /** Drops all vec0 virtual shadow tables and triggers for a content table. */
656
727
  private _dropAllVecTables;