diff --git a/package-lock.json b/package-lock.json index 0785b63..a5be444 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.416", + "version": "1.0.417", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@sqlitecloud/drivers", - "version": "1.0.416", + "version": "1.0.417", "license": "MIT", "dependencies": { "buffer": "^6.0.3", diff --git a/package.json b/package.json index f6d3861..33e2640 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@sqlitecloud/drivers", - "version": "1.0.416", + "version": "1.0.417", "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 85a80de..1227c63 100644 --- a/src/drivers/database.ts +++ b/src/drivers/database.ts @@ -7,15 +7,23 @@ // https://github.com/TryGhost/node-sqlite3 // https://github.com/TryGhost/node-sqlite3/blob/master/lib/sqlite3.d.ts -import { SQLiteCloudConnection } from './connection' -import { SQLiteCloudRowset } from './rowset' -import { SQLiteCloudConfig, SQLiteCloudError, RowCountCallback, SQLiteCloudArrayType, SQLiteCloudCommand } from './types' -import { popCallback } from './utilities' -import { ErrorCallback, ResultsCallback, RowCallback, RowsCallback } from './types' import EventEmitter from 'eventemitter3' -import { isBrowser } from './utilities' +import { SQLiteCloudConnection } from './connection' import { PubSub } from './pubsub' +import { SQLiteCloudRowset } from './rowset' import { Statement } from './statement' +import { + ErrorCallback, + ResultsCallback, + RowCallback, + RowCountCallback, + RowsCallback, + SQLiteCloudArrayType, + SQLiteCloudCommand, + SQLiteCloudConfig, + SQLiteCloudError +} from './types' +import { isBrowser, popCallback } from './utilities' // Uses eventemitter3 instead of node events for browser compatibility // https://github.com/primus/eventemitter3 @@ -480,6 +488,13 @@ export class Database extends EventEmitter { }) } + /** + * Returns true if the database connection is open. + */ + public isConnected(): boolean { + return this.connections?.length > 0 && this.connections[0].connected + } + /** * PubSub class provides a Pub/Sub real-time updates and notifications system to * allow multiple applications to communicate with each other asynchronously. diff --git a/test/connection-tls.test.ts b/test/connection-tls.test.ts index f09a694..e0183ce 100644 --- a/test/connection-tls.test.ts +++ b/test/connection-tls.test.ts @@ -2,22 +2,21 @@ * connection-tls.test.ts - test low level communication protocol with tls sockets and raw commands */ -import { SQLiteCloudConnection, SQLiteCloudError, SQLiteCloudRowset } from '../src/index' import { SQLiteCloudTlsConnection } from '../src/drivers/connection-tls' import { anonimizeCommand } from '../src/drivers/utilities' +import { SQLiteCloudError, SQLiteCloudRowset } from '../src/index' import { CHINOOK_DATABASE_URL, - INSECURE_DATABASE_URL, - LONG_TIMEOUT, - getTestingConfig, - getChinookConfig, + EXPECT_SPEED_MS, + EXTRA_LONG_TIMEOUT, getChinookApiKeyUrl, + getChinookConfig, getChinookTlsConnection, + getTestingConfig, + INSECURE_DATABASE_URL, + LONG_TIMEOUT, sendCommandsAsync, - // clearTestingDatabasesAsync, - WARN_SPEED_MS, - EXPECT_SPEED_MS, - EXTRA_LONG_TIMEOUT + WARN_SPEED_MS } from './shared' function getConnection() { diff --git a/test/database.test.ts b/test/database.test.ts index bdaefd2..dc2a884 100644 --- a/test/database.test.ts +++ b/test/database.test.ts @@ -4,7 +4,7 @@ import { describe, expect, it } from '@jest/globals' import { RowCountCallback } from '../src/drivers/types' -import { SQLiteCloudError, SQLiteCloudRow, SQLiteCloudRowset, sanitizeSQLiteIdentifier } from '../src/index' +import { Database, SQLiteCloudError, SQLiteCloudRow, SQLiteCloudRowset, sanitizeSQLiteIdentifier } from '../src/index' import { LONG_TIMEOUT, getChinookDatabase, getTestingDatabase, getTestingDatabaseAsync, removeDatabase, removeDatabaseAsync } from './shared' // @@ -20,12 +20,7 @@ describe('Database.run', () => { // lambda callback would "hide" this function plainCallbackNotALambda(err: Error, results: any) { expect(err).toBeNull() - expect(results).toEqual({ - lastID: 20, - changes: 1, - totalChanges: 22, - finalized: 1 - }) + expect(results).toEqual({ lastID: 20, changes: 1, totalChanges: 22, finalized: 1 }) // Database.run should return number of rows modified and lastID // @ts-expect-error @@ -57,12 +52,7 @@ describe('Database.run', () => { // lambda callback would "hide" this function plainCallbackNotALambdaOne(err: Error, results: any) { expect(err).toBeNull() - expect(results).toEqual({ - lastID: 21, - changes: 1, - totalChanges: 21, - finalized: 1 - }) + expect(results).toEqual({ lastID: 21, changes: 1, totalChanges: 21, finalized: 1 }) // Database.run should return number of rows modified and lastID // @ts-expect-error @@ -78,12 +68,7 @@ describe('Database.run', () => { // lambda callback would "hide" this function plainCallbackNotALambdaTwo(err: Error, results: any) { expect(err).toBeNull() - expect(results).toEqual({ - lastID: 22, - changes: 1, - totalChanges: 22, - finalized: 1 - }) + expect(results).toEqual({ lastID: 22, changes: 1, totalChanges: 22, finalized: 1 }) // Database.run should return number of rows modified and lastID // @ts-expect-error @@ -200,9 +185,7 @@ describe('Database.all', () => { expect(err).toBeNull() expect(rows).toBeDefined() expect(rows).toHaveLength(1) - expect(rows[0]).toMatchObject({ - '1': 1 - }) + expect(rows[0]).toMatchObject({ '1': 1 }) chinook.close(error => { expect(error).toBeNull() @@ -375,12 +358,7 @@ describe('Database.sql (async)', () => { const row = results[0] expect(row).toBeDefined() - expect(row).toMatchObject({ - id: 1, - name: 'Emma Johnson', - age: 28, - hobby: 'Collecting clouds' - }) + expect(row).toMatchObject({ id: 1, name: 'Emma Johnson', age: 28, hobby: 'Collecting clouds' }) } finally { await removeDatabaseAsync(database) } @@ -394,12 +372,7 @@ describe('Database.sql (async)', () => { expect(results).toBeDefined() const row = results[0] expect(row).toBeDefined() - expect(row).toMatchObject({ - id: 1, - name: 'Emma Johnson', - age: 28, - hobby: 'Collecting clouds' - }) + expect(row).toMatchObject({ id: 1, name: 'Emma Johnson', age: 28, hobby: 'Collecting clouds' }) } finally { await removeDatabaseAsync(database) } @@ -418,12 +391,7 @@ describe('Database.sql (async)', () => { let results = await database.sql`SELECT * FROM people WHERE name = ${name}` // => returns { id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' } - expect(results[0]).toMatchObject({ - id: 5, - name: 'Ava Jones', - age: 22, - hobby: 'Time traveling' - }) + expect(results[0]).toMatchObject({ id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' }) results = await database.sql`SELECT * FROM people WHERE age < 30` expect(results).toHaveLength(11) @@ -464,12 +432,7 @@ describe('Database.sql (async)', () => { database = await getTestingDatabaseAsync() const updateSql = "UPDATE people SET name = 'Charlie Brown' WHERE id = 3; UPDATE people SET name = 'David Bowie' WHERE id = 4;" let results = await database.sql(updateSql) - expect(results).toMatchObject({ - lastID: 20, - changes: 1, - totalChanges: 22, - finalized: 1 - }) + expect(results).toMatchObject({ lastID: 20, changes: 1, totalChanges: 22, finalized: 1 }) } finally { await removeDatabaseAsync(database) } @@ -483,12 +446,7 @@ describe('Database.sql (async)', () => { database = await getTestingDatabaseAsync() const insertSql = "INSERT INTO people (name, hobby, age) VALUES ('Barnaby Bumblecrump', 'Rubber Duck Dressing', 42); " let results = await database.sql(insertSql) - expect(results).toMatchObject({ - lastID: 21, - changes: 1, - totalChanges: 21, - finalized: 1 - }) + expect(results).toMatchObject({ lastID: 21, changes: 1, totalChanges: 21, finalized: 1 }) } finally { await removeDatabaseAsync(database) } @@ -502,12 +460,7 @@ describe('Database.sql (async)', () => { database = await getTestingDatabaseAsync() const insertSql = "INSERT INTO people (name, hobby, age) VALUES ('Barnaby Bumblecrump', 'Rubber Duck Dressing', 42); " let results = await database.sql(insertSql) - expect(results).toMatchObject({ - lastID: 21, - changes: 1, - totalChanges: 21, - finalized: 1 - }) + expect(results).toMatchObject({ lastID: 21, changes: 1, totalChanges: 21, finalized: 1 }) } finally { await removeDatabaseAsync(database) } @@ -520,11 +473,7 @@ describe('Database.sql (async)', () => { try { database = await getTestingDatabaseAsync() let results = await database.sql`SELECT true` - expect(results).toMatchObject([ - { - true: 1 - } - ]) + expect(results).toMatchObject([{ true: 1 }]) } finally { await removeDatabaseAsync(database) } @@ -627,3 +576,19 @@ describe('Database.sql (async)', () => { } }) }) + +it('should be connected', async () => { + const database: Database = await new Promise((resolve, rejects) => { + const conn = getChinookDatabase(error => { + if (error) { + rejects(error) + } else { + resolve(conn) + } + }) + }) + + expect(database.isConnected()).toBe(true) + database.close() + expect(database.isConnected()).toBe(false) +})