diff --git a/package-lock.json b/package-lock.json index ecda972..b1e7e3d 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.437", + "version": "1.0.438", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sqlitecloud/drivers", - "version": "1.0.437", + "version": "1.0.438", "license": "MIT", "dependencies": { "buffer": "^6.0.3", diff --git a/package.json b/package.json index 00f6f78..df49d59 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.437", + "version": "1.0.438", "description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients", "main": "./lib/index.js", "types": "./lib/index.d.ts", diff --git a/src/drivers/database.ts b/src/drivers/database.ts index 797a221..80aa1f5 100644 --- a/src/drivers/database.ts +++ b/src/drivers/database.ts @@ -96,6 +96,7 @@ export class Database extends EventEmitter { }) .catch(error => { this.handleError(error, callback) + this.close() done(error) }) }) @@ -116,6 +117,7 @@ export class Database extends EventEmitter { }) .catch(error => { this.handleError(error, callback) + this.close() done(error) }) }) @@ -128,21 +130,20 @@ export class Database extends EventEmitter { // we don't wont to silently open a new connection after a disconnession if (this.connection && this.connection.connected) { - this.connection.sendCommands(command, callback) + this.connection.sendCommands(command, (error, results) => { + callback?.call(this, error, results) + done(error) + }) } else { error = new SQLiteCloudError('Connection unavailable. Maybe it got disconnected?', { errorCode: 'ERR_CONNECTION_NOT_ESTABLISHED' }) - this.handleError(error, callback) + callback?.call(this, error, null) + done(error) } - - done(error) }) } /** Handles an error by closing the connection, calling the callback and/or emitting an error event */ private handleError(error: Error, callback?: ConnectionCallback): void { - // an errored connection is thrown out - this.connection?.close() - if (callback) { callback.call(this, error) } else { @@ -382,11 +383,15 @@ export class Database extends EventEmitter { * parameters is emitted, regardless of whether a callback was provided or not. */ public close(callback?: ConnectionCallback): void { - this.operations.clear() - this.connection?.close() + this.operations.enqueue(done => { + this.connection?.close() + + callback?.call(this, null) + this.emitEvent('close') - callback?.call(this, null) - this.emitEvent('close') + this.operations.clear() + done(null) + }) } /** diff --git a/test/connection-ws.test.ts b/test/connection-ws.test.ts index 2772fd3..3cf2fd5 100644 --- a/test/connection-ws.test.ts +++ b/test/connection-ws.test.ts @@ -2,19 +2,17 @@ * connection-ws.test.ts - test connection via socket.io based gateway */ -import { SQLiteCloudError } from '../src/index' import { SQLiteCloudConnection } from '../src/drivers/connection' import { SQLiteCloudWebsocketConnection } from '../src/drivers/connection-ws' +import { SQLiteCloudCommand } from '../src/drivers/types' +import { SQLiteCloudError } from '../src/index' import { - // - CHINOOK_DATABASE_URL, - LONG_TIMEOUT, + EXPECT_SPEED_MS, getChinookConfig, getChinookWebsocketConnection, - WARN_SPEED_MS, - EXPECT_SPEED_MS + LONG_TIMEOUT, + WARN_SPEED_MS } from './shared' -import { SQLiteCloudCommand } from '../src/drivers/types' describe('connection-ws', () => { let chinook: SQLiteCloudConnection diff --git a/test/database.test.ts b/test/database.test.ts index ef46f81..ace2400 100644 --- a/test/database.test.ts +++ b/test/database.test.ts @@ -246,6 +246,33 @@ describe('Database.get', () => { }) }) }) + + it('close() is executed after the previous commands', done => { + // the database enqueue the close command + const chinook = getChinookDatabase() + chinook.get('SELECT * FROM tracks', (err: Error, row?: SQLiteCloudRow) => { + expect(err).toBeNull() + expect(row).toBeDefined() + expect(row).toMatchObject({ + AlbumId: 1, + Bytes: 11170334, + Composer: 'Angus Young, Malcolm Young, Brian Johnson', + GenreId: 1, + MediaTypeId: 1, + Milliseconds: 343719, + Name: 'For Those About To Rock (We Salute You)', + TrackId: 1, + UnitPrice: 0.99 + }) + }) + + // call close() right after the execution + // of the query not in its callback + chinook.close(error => { + expect(error).toBeNull() + done() + }) + }) }) describe('Database.each', () => {