Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
27 changes: 16 additions & 11 deletions src/drivers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export class Database extends EventEmitter {
})
.catch(error => {
this.handleError(error, callback)
this.close()
done(error)
})
})
Expand All @@ -116,6 +117,7 @@ export class Database extends EventEmitter {
})
.catch(error => {
this.handleError(error, callback)
this.close()
done(error)
})
})
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
})
}

/**
Expand Down
12 changes: 5 additions & 7 deletions test/connection-ws.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
Loading