@@ -16,30 +16,23 @@ import { SQLiteCloudConfig, SQLiteCloudError, RowCountCallback, SQLiteCloudArray
1616import { prepareSql , popCallback } from './utilities'
1717import { Statement } from './statement'
1818import { ErrorCallback , ResultsCallback , RowCallback , RowsCallback } from './types'
19+ import EventEmitter from 'eventemitter3'
1920
2021/**
2122 * Creating a Database object automatically opens a connection to the SQLite database.
2223 * When the connection is established the Database object emits an open event and calls
2324 * the optional provided callback. If the connection cannot be established an error event
2425 * will be emitted and the optional callback is called with the error information.
2526 */
26- export class Database {
27+ export class Database extends EventEmitter {
2728 /** Create and initialize a database from a full configuration object, or connection string */
2829 constructor ( config : SQLiteCloudConfig | string , callback ?: ErrorCallback ) {
30+ super ( )
2931 this . config = typeof config === 'string' ? { connectionString : config } : config
32+ this . connections = [ ]
3033
3134 // opens first connection to the database automatically
32- const connection = new SQLiteCloudConnection ( this . config )
33- this . connections = [ connection ]
34-
35- // get a connection for the only purpose of opening the database
36- this . getConnection ( error => {
37- if ( error ) {
38- this . handleError ( null , error , callback )
39- } else {
40- callback ?. call ( this , null )
41- }
42- } )
35+ this . getConnection ( callback as ResultsCallback )
4336 }
4437
4538 /** Configuration used to open database connections */
@@ -59,11 +52,11 @@ export class Database {
5952 callback ?. call ( this , null , this . connections [ 0 ] )
6053 } else {
6154 const connection = new SQLiteCloudConnection ( this . config )
55+ this . connections . push ( connection )
6256 connection . connect ( error => {
6357 if ( error ) {
6458 this . handleError ( connection , error , callback )
6559 } else {
66- this . connections . push ( connection )
6760 callback ?. call ( this , null )
6861 this . emitEvent ( 'open' )
6962 }
@@ -82,19 +75,10 @@ export class Database {
8275 if ( callback ) {
8376 callback . call ( this , error )
8477 } else {
85- // TODO sqlitecloud-js / implement database error handling #12
8678 this . emitEvent ( 'error' , error )
8779 }
8880 }
8981
90- /** Emits given event with optional arguments */
91- private emitEvent ( event : string , ...args : any [ ] ) : void {
92- // TODO sqlitecloud-js / database emit event #16
93- if ( this . config . verbose ) {
94- console . log ( `Database.emitEvent - '${ event } '` , ...args )
95- }
96- }
97-
9882 /**
9983 * Some queries like inserts or updates processed via run or exec may generate
10084 * an empty result (eg. no data was selected), but still have some metadata.
@@ -123,6 +107,16 @@ export class Database {
123107 return undefined
124108 }
125109
110+ /** Emits given event with optional arguments on the next tick so callbacks can complete first */
111+ private emitEvent ( event : string , ...args : any [ ] ) : void {
112+ process . nextTick ( ( ) => {
113+ if ( this . config . verbose ) {
114+ console . log ( `Database.emitEvent - emitted '${ event } '` , ...args )
115+ }
116+ this . emit ( event , ...args )
117+ } )
118+ }
119+
126120 //
127121 // public methods
128122 //
0 commit comments