@@ -100,7 +100,7 @@ export class InterruptibleTransaction {
100100 *
101101 * @example
102102 * ```ts
103- * let tx = await db.executeInterruptibleTransaction ([
103+ * let tx = await db.beginInterruptibleTransaction ([
104104 * ['INSERT INTO users (name) VALUES ($1)', ['Alice']]
105105 * ]);
106106 *
@@ -119,7 +119,7 @@ export class InterruptibleTransaction {
119119 }
120120
121121 /**
122- * **continue **
122+ * **continueWith **
123123 *
124124 * Execute additional statements within this transaction and return a new
125125 * transaction handle.
@@ -129,14 +129,14 @@ export class InterruptibleTransaction {
129129 *
130130 * @example
131131 * ```ts
132- * let tx = await db.executeInterruptibleTransaction ([...]);
133- * tx = await tx.continue ([
132+ * let tx = await db.beginInterruptibleTransaction ([...]);
133+ * tx = await tx.continueWith ([
134134 * ['INSERT INTO users (name) VALUES ($1)', ['Bob']]
135135 * ]);
136136 * await tx.commit();
137137 * ```
138138 */
139- public async continue ( statements : Array < [ string , SqlValue [ ] ?] > ) : Promise < InterruptibleTransaction > {
139+ public async continueWith ( statements : Array < [ string , SqlValue [ ] ?] > ) : Promise < InterruptibleTransaction > {
140140 const token = await invoke < { dbPath : string ; transactionId : string } > (
141141 'plugin:sqlite|transaction_continue' ,
142142 {
@@ -163,7 +163,7 @@ export class InterruptibleTransaction {
163163 *
164164 * @example
165165 * ```ts
166- * let tx = await db.executeInterruptibleTransaction ([...]);
166+ * let tx = await db.beginInterruptibleTransaction ([...]);
167167 * [...]
168168 * await tx.commit();
169169 * ```
@@ -182,7 +182,7 @@ export class InterruptibleTransaction {
182182 *
183183 * @example
184184 * ```ts
185- * let tx = await db.executeInterruptibleTransaction ([...]);
185+ * let tx = await db.beginInterruptibleTransaction ([...]);
186186 * [...]
187187 * await tx.rollback();
188188 * ```
@@ -419,6 +419,61 @@ class ExecuteBuilder implements PromiseLike<WriteQueryResult> {
419419 }
420420}
421421
422+ /**
423+ * Builder for interruptible transaction operations
424+ */
425+ class InterruptibleTransactionBuilder implements PromiseLike < InterruptibleTransaction > {
426+ private readonly _db : Database ;
427+ private readonly _initialStatements : Array < [ string , SqlValue [ ] ?] > ;
428+ private _attached : AttachedDatabaseSpec [ ] ;
429+
430+ public constructor (
431+ db : Database ,
432+ initialStatements : Array < [ string , SqlValue [ ] ?] > ,
433+ attached : AttachedDatabaseSpec [ ] = [ ]
434+ ) {
435+ this . _db = db ;
436+ this . _initialStatements = initialStatements ;
437+ this . _attached = attached ;
438+ }
439+
440+ /**
441+ * Attach databases for cross-database transactions
442+ */
443+ public attach ( specs : AttachedDatabaseSpec [ ] ) : this {
444+ this . _attached = specs ;
445+ return this ;
446+ }
447+
448+ /**
449+ * Make the builder directly awaitable
450+ */
451+ public then < TResult1 = InterruptibleTransaction , TResult2 = never > (
452+ onfulfilled ?: ( ( value : InterruptibleTransaction ) => TResult1 | PromiseLike < TResult1 > ) | null ,
453+ onrejected ?: ( ( reason : unknown ) => TResult2 | PromiseLike < TResult2 > ) | null
454+ ) : PromiseLike < TResult1 | TResult2 > {
455+ return this . _execute ( ) . then ( onfulfilled , onrejected ) ;
456+ }
457+
458+ private async _execute ( ) : Promise < InterruptibleTransaction > {
459+ const token = await invoke < { dbPath : string ; transactionId : string } > (
460+ 'plugin:sqlite|begin_interruptible_transaction' ,
461+ {
462+ db : this . _db . path ,
463+ initialStatements : this . _initialStatements . map ( ( [ query , values ] ) => {
464+ return {
465+ query,
466+ values : values ?? [ ] ,
467+ } ;
468+ } ) ,
469+ attached : this . _attached . length > 0 ? this . _attached : null ,
470+ }
471+ ) ;
472+
473+ return new InterruptibleTransaction ( token . dbPath , token . transactionId ) ;
474+ }
475+ }
476+
422477/**
423478 * Builder for transaction operations
424479 */
@@ -597,7 +652,7 @@ export default class Database {
597652 * **Use this method** when you have a batch of writes to execute and
598653 * don't need to read data mid-transaction. For transactions that
599654 * require reading uncommitted data to decide how to proceed, use
600- * `executeInterruptibleTransaction ()` instead.
655+ * `beginInterruptibleTransaction ()` instead.
601656 *
602657 * The function automatically:
603658 * - Begins a transaction (BEGIN IMMEDIATE)
@@ -765,7 +820,7 @@ export default class Database {
765820 }
766821
767822 /**
768- * **executeInterruptibleTransaction **
823+ * **beginInterruptibleTransaction **
769824 *
770825 * Begins an interruptible transaction for cases where you need to
771826 * **read data mid-transaction to decide how to proceed**. For example,
@@ -788,12 +843,12 @@ export default class Database {
788843 * writer connection is held for the entire duration - keep transactions short.
789844 *
790845 * @param initialStatements - Array of [query, values?] tuples to execute initially
791- * @returns Promise that resolves with an InterruptibleTransaction handle
846+ * @returns Builder for setting up the transaction with optional attached databases
792847 *
793848 * @example
794849 * ```ts
795850 * // Insert an order and read back its ID
796- * let tx = await db.executeInterruptibleTransaction ([
851+ * let tx = await db.beginInterruptibleTransaction ([
797852 * ['INSERT INTO orders (user_id, total) VALUES ($1, $2)', [userId, 0]]
798853 * ]);
799854 *
@@ -805,7 +860,7 @@ export default class Database {
805860 * const orderId = orders[0].id;
806861 *
807862 * // Use the ID in subsequent writes
808- * tx = await tx.continue ([
863+ * tx = await tx.continueWith ([
809864 * [
810865 * 'INSERT INTO order_items (order_id, product_id) VALUES ($1, $2)',
811866 * [ orderId, productId ],
@@ -814,24 +869,29 @@ export default class Database {
814869 *
815870 * await tx.commit();
816871 * ```
872+ *
873+ * @example
874+ * ```ts
875+ * // Transaction with attached database
876+ * let tx = await db.beginInterruptibleTransaction([
877+ * ['DELETE FROM users WHERE archived = 1']
878+ * ]).attach([{
879+ * databasePath: 'archive.db',
880+ * schemaName: 'archive',
881+ * mode: 'readWrite'
882+ * }]);
883+ *
884+ * tx = await tx.continueWith([
885+ * ['INSERT INTO archive.users SELECT * FROM users WHERE archived = 1']
886+ * ]);
887+ *
888+ * await tx.commit();
889+ * ```
817890 */
818- public async executeInterruptibleTransaction (
891+ public beginInterruptibleTransaction (
819892 initialStatements : Array < [ string , SqlValue [ ] ?] >
820- ) : Promise < InterruptibleTransaction > {
821- const token = await invoke < { dbPath : string ; transactionId : string } > (
822- 'plugin:sqlite|execute_interruptible_transaction' ,
823- {
824- db : this . path ,
825- initialStatements : initialStatements . map ( ( [ query , values ] ) => {
826- return {
827- query,
828- values : values ?? [ ] ,
829- } ;
830- } ) ,
831- }
832- ) ;
833-
834- return new InterruptibleTransaction ( token . dbPath , token . transactionId ) ;
893+ ) : InterruptibleTransactionBuilder {
894+ return new InterruptibleTransactionBuilder ( this , initialStatements ) ;
835895 }
836896
837897 /**
0 commit comments