Skip to content

Commit 4b7674d

Browse files
author
Daniele Briggi
committed
feat(database): add isConnected
1 parent 076e1d9 commit 4b7674d

File tree

5 files changed

+51
-80
lines changed

5 files changed

+51
-80
lines changed

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.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@sqlitecloud/drivers",
3-
"version": "1.0.416",
3+
"version": "1.0.417",
44
"description": "SQLiteCloud drivers for Typescript/Javascript in edge, web and node clients",
55
"main": "./lib/index.js",
66
"types": "./lib/index.d.ts",

src/drivers/database.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,13 @@
77
// https://github.com/TryGhost/node-sqlite3
88
// https://github.com/TryGhost/node-sqlite3/blob/master/lib/sqlite3.d.ts
99

10-
import { SQLiteCloudConnection } from './connection'
11-
import { SQLiteCloudRowset } from './rowset'
12-
import { SQLiteCloudConfig, SQLiteCloudError, RowCountCallback, SQLiteCloudArrayType, SQLiteCloudCommand } from './types'
13-
import { popCallback } from './utilities'
14-
import { ErrorCallback, ResultsCallback, RowCallback, RowsCallback } from './types'
1510
import EventEmitter from 'eventemitter3'
16-
import { isBrowser } from './utilities'
11+
import { SQLiteCloudConnection } from './connection'
1712
import { PubSub } from './pubsub'
13+
import { SQLiteCloudRowset } from './rowset'
1814
import { Statement } from './statement'
15+
import { ErrorCallback, ResultsCallback, RowCallback, RowCountCallback, RowsCallback, SQLiteCloudArrayType, SQLiteCloudCommand, SQLiteCloudConfig, SQLiteCloudError } from './types'
16+
import { isBrowser, popCallback } from './utilities'
1917

2018
// Uses eventemitter3 instead of node events for browser compatibility
2119
// https://github.com/primus/eventemitter3
@@ -480,6 +478,13 @@ export class Database extends EventEmitter {
480478
})
481479
}
482480

481+
/**
482+
* Returns true if the database connection is open.
483+
*/
484+
public isConnected(): boolean {
485+
return this.connections?.length > 0 && this.connections[0].connected
486+
}
487+
483488
/**
484489
* PubSub class provides a Pub/Sub real-time updates and notifications system to
485490
* allow multiple applications to communicate with each other asynchronously.

test/connection-tls.test.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@
22
* connection-tls.test.ts - test low level communication protocol with tls sockets and raw commands
33
*/
44

5-
import { SQLiteCloudConnection, SQLiteCloudError, SQLiteCloudRowset } from '../src/index'
65
import { SQLiteCloudTlsConnection } from '../src/drivers/connection-tls'
76
import { anonimizeCommand } from '../src/drivers/utilities'
7+
import { SQLiteCloudError, SQLiteCloudRowset } from '../src/index'
88
import {
99
CHINOOK_DATABASE_URL,
10-
INSECURE_DATABASE_URL,
11-
LONG_TIMEOUT,
12-
getTestingConfig,
13-
getChinookConfig,
10+
EXPECT_SPEED_MS,
11+
EXTRA_LONG_TIMEOUT,
1412
getChinookApiKeyUrl,
13+
getChinookConfig,
1514
getChinookTlsConnection,
15+
getTestingConfig,
16+
INSECURE_DATABASE_URL,
17+
LONG_TIMEOUT,
1618
sendCommandsAsync,
1719
// clearTestingDatabasesAsync,
18-
WARN_SPEED_MS,
19-
EXPECT_SPEED_MS,
20-
EXTRA_LONG_TIMEOUT
20+
WARN_SPEED_MS
2121
} from './shared'
2222

2323
function getConnection() {

test/database.test.ts

Lines changed: 29 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import { describe, expect, it } from '@jest/globals'
66
import { RowCountCallback } from '../src/drivers/types'
7-
import { SQLiteCloudError, SQLiteCloudRow, SQLiteCloudRowset, sanitizeSQLiteIdentifier } from '../src/index'
7+
import { Database, SQLiteCloudError, SQLiteCloudRow, SQLiteCloudRowset, sanitizeSQLiteIdentifier } from '../src/index'
88
import { LONG_TIMEOUT, getChinookDatabase, getTestingDatabase, getTestingDatabaseAsync, removeDatabase, removeDatabaseAsync } from './shared'
99

1010
//
@@ -20,12 +20,7 @@ describe('Database.run', () => {
2020
// lambda callback would "hide" this
2121
function plainCallbackNotALambda(err: Error, results: any) {
2222
expect(err).toBeNull()
23-
expect(results).toEqual({
24-
lastID: 20,
25-
changes: 1,
26-
totalChanges: 22,
27-
finalized: 1
28-
})
23+
expect(results).toEqual({ lastID: 20, changes: 1, totalChanges: 22, finalized: 1 })
2924

3025
// Database.run should return number of rows modified and lastID
3126
// @ts-expect-error
@@ -57,12 +52,7 @@ describe('Database.run', () => {
5752
// lambda callback would "hide" this
5853
function plainCallbackNotALambdaOne(err: Error, results: any) {
5954
expect(err).toBeNull()
60-
expect(results).toEqual({
61-
lastID: 21,
62-
changes: 1,
63-
totalChanges: 21,
64-
finalized: 1
65-
})
55+
expect(results).toEqual({ lastID: 21, changes: 1, totalChanges: 21, finalized: 1 })
6656

6757
// Database.run should return number of rows modified and lastID
6858
// @ts-expect-error
@@ -78,12 +68,7 @@ describe('Database.run', () => {
7868
// lambda callback would "hide" this
7969
function plainCallbackNotALambdaTwo(err: Error, results: any) {
8070
expect(err).toBeNull()
81-
expect(results).toEqual({
82-
lastID: 22,
83-
changes: 1,
84-
totalChanges: 22,
85-
finalized: 1
86-
})
71+
expect(results).toEqual({ lastID: 22, changes: 1, totalChanges: 22, finalized: 1 })
8772

8873
// Database.run should return number of rows modified and lastID
8974
// @ts-expect-error
@@ -200,9 +185,7 @@ describe('Database.all', () => {
200185
expect(err).toBeNull()
201186
expect(rows).toBeDefined()
202187
expect(rows).toHaveLength(1)
203-
expect(rows[0]).toMatchObject({
204-
'1': 1
205-
})
188+
expect(rows[0]).toMatchObject({ '1': 1 })
206189

207190
chinook.close(error => {
208191
expect(error).toBeNull()
@@ -375,12 +358,7 @@ describe('Database.sql (async)', () => {
375358

376359
const row = results[0]
377360
expect(row).toBeDefined()
378-
expect(row).toMatchObject({
379-
id: 1,
380-
name: 'Emma Johnson',
381-
age: 28,
382-
hobby: 'Collecting clouds'
383-
})
361+
expect(row).toMatchObject({ id: 1, name: 'Emma Johnson', age: 28, hobby: 'Collecting clouds' })
384362
} finally {
385363
await removeDatabaseAsync(database)
386364
}
@@ -394,12 +372,7 @@ describe('Database.sql (async)', () => {
394372
expect(results).toBeDefined()
395373
const row = results[0]
396374
expect(row).toBeDefined()
397-
expect(row).toMatchObject({
398-
id: 1,
399-
name: 'Emma Johnson',
400-
age: 28,
401-
hobby: 'Collecting clouds'
402-
})
375+
expect(row).toMatchObject({ id: 1, name: 'Emma Johnson', age: 28, hobby: 'Collecting clouds' })
403376
} finally {
404377
await removeDatabaseAsync(database)
405378
}
@@ -418,12 +391,7 @@ describe('Database.sql (async)', () => {
418391
let results = await database.sql`SELECT * FROM people WHERE name = ${name}`
419392
// => returns { id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' }
420393

421-
expect(results[0]).toMatchObject({
422-
id: 5,
423-
name: 'Ava Jones',
424-
age: 22,
425-
hobby: 'Time traveling'
426-
})
394+
expect(results[0]).toMatchObject({ id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' })
427395

428396
results = await database.sql`SELECT * FROM people WHERE age < 30`
429397
expect(results).toHaveLength(11)
@@ -464,12 +432,7 @@ describe('Database.sql (async)', () => {
464432
database = await getTestingDatabaseAsync()
465433
const updateSql = "UPDATE people SET name = 'Charlie Brown' WHERE id = 3; UPDATE people SET name = 'David Bowie' WHERE id = 4;"
466434
let results = await database.sql(updateSql)
467-
expect(results).toMatchObject({
468-
lastID: 20,
469-
changes: 1,
470-
totalChanges: 22,
471-
finalized: 1
472-
})
435+
expect(results).toMatchObject({ lastID: 20, changes: 1, totalChanges: 22, finalized: 1 })
473436
} finally {
474437
await removeDatabaseAsync(database)
475438
}
@@ -483,12 +446,7 @@ describe('Database.sql (async)', () => {
483446
database = await getTestingDatabaseAsync()
484447
const insertSql = "INSERT INTO people (name, hobby, age) VALUES ('Barnaby Bumblecrump', 'Rubber Duck Dressing', 42); "
485448
let results = await database.sql(insertSql)
486-
expect(results).toMatchObject({
487-
lastID: 21,
488-
changes: 1,
489-
totalChanges: 21,
490-
finalized: 1
491-
})
449+
expect(results).toMatchObject({ lastID: 21, changes: 1, totalChanges: 21, finalized: 1 })
492450
} finally {
493451
await removeDatabaseAsync(database)
494452
}
@@ -502,12 +460,7 @@ describe('Database.sql (async)', () => {
502460
database = await getTestingDatabaseAsync()
503461
const insertSql = "INSERT INTO people (name, hobby, age) VALUES ('Barnaby Bumblecrump', 'Rubber Duck Dressing', 42); "
504462
let results = await database.sql(insertSql)
505-
expect(results).toMatchObject({
506-
lastID: 21,
507-
changes: 1,
508-
totalChanges: 21,
509-
finalized: 1
510-
})
463+
expect(results).toMatchObject({ lastID: 21, changes: 1, totalChanges: 21, finalized: 1 })
511464
} finally {
512465
await removeDatabaseAsync(database)
513466
}
@@ -520,11 +473,7 @@ describe('Database.sql (async)', () => {
520473
try {
521474
database = await getTestingDatabaseAsync()
522475
let results = await database.sql`SELECT true`
523-
expect(results).toMatchObject([
524-
{
525-
true: 1
526-
}
527-
])
476+
expect(results).toMatchObject([{ true: 1 }])
528477
} finally {
529478
await removeDatabaseAsync(database)
530479
}
@@ -627,3 +576,20 @@ describe('Database.sql (async)', () => {
627576
}
628577
})
629578
})
579+
580+
it('should be connected', async () => {
581+
const database: Database = await new Promise((resolve, rejects) => {
582+
const conn = getChinookDatabase(error => {
583+
if (error) {
584+
rejects(error)
585+
} else {
586+
console.log(conn.isConnected())
587+
resolve(conn)
588+
}
589+
})
590+
})
591+
592+
expect(database.isConnected()).toBe(true)
593+
database.close()
594+
expect(database.isConnected()).toBe(false)
595+
})

0 commit comments

Comments
 (0)