Skip to content

Commit 5667ad5

Browse files
Move shared test utilities to shared.ts
1 parent 0f4e130 commit 5667ad5

File tree

11 files changed

+517
-371
lines changed

11 files changed

+517
-371
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,5 @@ lib/
122122
.DS_Store
123123
.env
124124
test/assets/testing.db
125+
126+
tools/

jest.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module.exports = {
22
preset: 'ts-jest',
33
testEnvironment: 'node',
44
testMatch: ['**/test/**/*.test.ts'],
5+
testTimeout: 30 * 1000,
56
maxWorkers: 8,
67
collectCoverageFrom: ['<rootDir>/src/**/*.ts', '!<rootDir>/src/index.ts', '!<rootDir>/src/types/**/*.ts'],
78
transform: {

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/database.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,23 @@ import { SQLiteCloudConfig, SQLiteCloudError, RowCountCallback, SQLiteCloudArray
1616
import { prepareSql, popCallback } from './utilities'
1717
import { Statement } from './statement'
1818
import { 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

Comments
 (0)