@@ -15,6 +15,8 @@ export default function nodeSqlite3Connector(
1515 opts : ConnectorOptions ,
1616) : Connector < sqlite3 . Database > {
1717 let _db : sqlite3 . Database ;
18+ const _activeStatements = new Set < StatementWrapper > ( ) ;
19+
1820 const getDB = ( ) => {
1921 if ( _db ) {
2022 return _db ;
@@ -47,7 +49,25 @@ export default function nodeSqlite3Connector(
4749 dialect : "sqlite" ,
4850 getInstance : ( ) => getDB ( ) ,
4951 exec : ( sql : string ) => query ( sql ) ,
50- prepare : ( sql ) => new StatementWrapper ( sql , getDB ( ) ) ,
52+ prepare : ( sql ) => {
53+ const stmt = new StatementWrapper ( sql , getDB ( ) ) ;
54+ _activeStatements . add ( stmt ) ;
55+ return stmt ;
56+ } ,
57+ dispose : async ( ) => {
58+ await Promise . all (
59+ [ ..._activeStatements ] . map ( ( s ) =>
60+ s . finalize ( ) . catch ( ( error ) => {
61+ console . warn ( "[db0] [sqlite3] failed to finalize statement" , error ) ;
62+ } ) ,
63+ ) ,
64+ ) ;
65+ _activeStatements . clear ( ) ;
66+ await new Promise < void > ( ( resolve , reject ) =>
67+ _db ?. close ?.( ( error ) => ( error ? reject ( error ) : resolve ( ) ) ) ,
68+ ) ;
69+ _db = undefined as any ;
70+ } ,
5171 } ;
5272}
5373
@@ -90,4 +110,14 @@ class StatementWrapper extends BoundableStatement<sqlite3.Statement> {
90110 } ) ;
91111 return row ;
92112 }
113+
114+ finalize ( ) {
115+ try {
116+ // TODO: Can we await on finalize cb?
117+ this . _statement . finalize ( ) ;
118+ return Promise . resolve ( ) ;
119+ } catch ( error ) {
120+ return Promise . reject ( error ) ;
121+ }
122+ }
93123}
0 commit comments