@@ -17,7 +17,8 @@ import { Remote } from 'comlink';
1717import { AsyncResource } from 'node:async_hooks' ;
1818import { AsyncDatabase , AsyncDatabaseOpener } from './AsyncDatabase.js' ;
1919import { RemoteConnection } from './RemoteConnection.js' ;
20- import { NodeSQLOpenOptions } from './options.js' ;
20+ import { NodeDatabaseImplementation , NodeSQLOpenOptions } from './options.js' ;
21+ import { isBundledToCommonJs } from '../utils/modules.js' ;
2122
2223export type BetterSQLite3LockContext = LockContext & {
2324 executeBatch ( query : string , params ?: any [ ] [ ] ) : Promise < QueryResult > ;
@@ -27,10 +28,15 @@ export type BetterSQLite3Transaction = Transaction & BetterSQLite3LockContext;
2728
2829const READ_CONNECTIONS = 5 ;
2930
31+ const defaultDatabaseImplementation : NodeDatabaseImplementation = {
32+ type : 'better-sqlite3' ,
33+ package : 'better-sqlite3'
34+ } ;
35+
3036/**
3137 * Adapter for better-sqlite3
3238 */
33- export class BetterSQLite3DBAdapter extends BaseObserver < DBAdapterListener > implements DBAdapter {
39+ export class WorkerConnectionPool extends BaseObserver < DBAdapterListener > implements DBAdapter {
3440 private readonly options : NodeSQLOpenOptions ;
3541 public readonly name : string ;
3642
@@ -73,7 +79,7 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
7379 }
7480
7581 const openWorker = async ( isWriter : boolean ) => {
76- const isCommonJsModule = import . meta . isBundlingToCommonJs ?? false ;
82+ const isCommonJsModule = isBundledToCommonJs ;
7783 let worker : Worker ;
7884 const workerName = isWriter ? `write ${ dbFilePath } ` : `read ${ dbFilePath } ` ;
7985
@@ -120,8 +126,12 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
120126 const database = ( await comlink . open ( {
121127 path : dbFilePath ,
122128 isWriter,
123- implementation : this . options . implementation ?? 'better-sqlite3'
129+ implementation : this . options . implementation ?? defaultDatabaseImplementation
124130 } ) ) as Remote < AsyncDatabase > ;
131+ if ( isWriter ) {
132+ await database . execute ( "SELECT powersync_update_hooks('install');" , [ ] ) ;
133+ }
134+
125135 return new RemoteConnection ( worker , comlink , database ) ;
126136 } ;
127137
@@ -192,7 +202,11 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
192202 try {
193203 return await fn ( this . writeConnection ) ;
194204 } finally {
195- const updates = await this . writeConnection . database . collectCommittedUpdates ( ) ;
205+ const serializedUpdates = await this . writeConnection . database . executeRaw (
206+ "SELECT powersync_update_hooks('get');" ,
207+ [ ]
208+ ) ;
209+ const updates = JSON . parse ( serializedUpdates [ 0 ] [ 0 ] as string ) as string [ ] ;
196210
197211 if ( updates . length > 0 ) {
198212 const event : BatchedUpdateNotification = {
0 commit comments