1- import path from 'node:path' ;
1+ import * as path from 'node:path' ;
2+ import * as OS from 'node:os' ;
3+ import * as url from 'node:url' ;
24
35import BetterSQLite3Database from 'better-sqlite3' ;
46
@@ -49,7 +51,24 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
4951 dbFilePath = path . join ( options . dbLocation , dbFilePath ) ;
5052 }
5153
54+ const platform = OS . platform ( ) ;
55+ let extensionPath : string ;
56+ if ( platform === "win32" ) {
57+ extensionPath = 'powersync.dll' ;
58+ } else if ( platform === "linux" ) {
59+ extensionPath = 'libpowersync.so' ;
60+ } else if ( platform === "darwin" ) {
61+ extensionPath = 'libpowersync.dylib' ;
62+ }
63+
5264 const baseDB = new BetterSQLite3Database ( dbFilePath ) ;
65+
66+ const loadExtension = ( db : BetterSQLite3Database ) => {
67+ const resolved = url . fileURLToPath ( new URL ( `../${ extensionPath } ` , import . meta. url ) ) ;
68+ db . loadExtension ( resolved , 'sqlite3_powersync_init' ) ;
69+ }
70+
71+ loadExtension ( baseDB ) ;
5372 baseDB . pragma ( 'journal_mode = WAL' ) ;
5473
5574 baseDB . updateHook ( ( _op , _dbName , tableName , _rowid ) => {
@@ -71,6 +90,7 @@ export class BetterSQLite3DBAdapter extends BaseObserver<DBAdapterListener> impl
7190 this . readConnections = [ ] ;
7291 for ( let i = 0 ; i < READ_CONNECTIONS ; i ++ ) {
7392 const baseDB = new BetterSQLite3Database ( dbFilePath ) ;
93+ loadExtension ( baseDB ) ;
7494 baseDB . pragma ( 'query_only = true' ) ;
7595 this . readConnections . push ( new Connection ( baseDB ) ) ;
7696 }
@@ -253,7 +273,7 @@ class Connection implements BetterSQLite3LockContext {
253273 async execute ( query : string , params ?: any [ ] ) : Promise < QueryResult > {
254274 const stmt = this . baseDB . prepare ( query ) ;
255275 if ( stmt . reader ) {
256- const rows = stmt . all ( params ) ;
276+ const rows = stmt . all ( params ?? [ ] ) ;
257277 return {
258278 rowsAffected : 0 ,
259279 rows : {
@@ -263,7 +283,7 @@ class Connection implements BetterSQLite3LockContext {
263283 } ,
264284 } ;
265285 } else {
266- const info = stmt . run ( params ) ;
286+ const info = stmt . run ( params ?? [ ] ) ;
267287 return {
268288 rowsAffected : info . changes ,
269289 insertId : Number ( info . lastInsertRowid ) ,
0 commit comments