Skip to content

Commit 2b8828c

Browse files
Merge pull request #212 from sqlitecloud/add-isconnected
feat(database): add isConnected
2 parents 7fa7da8 + c0ffbab commit 2b8828c

File tree

4 files changed

+59
-80
lines changed

4 files changed

+59
-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.

src/drivers/database.ts

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,23 @@
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 {
16+
ErrorCallback,
17+
ResultsCallback,
18+
RowCallback,
19+
RowCountCallback,
20+
RowsCallback,
21+
SQLiteCloudArrayType,
22+
SQLiteCloudCommand,
23+
SQLiteCloudConfig,
24+
SQLiteCloudError
25+
} from './types'
26+
import { isBrowser, popCallback } from './utilities'
1927

2028
// Uses eventemitter3 instead of node events for browser compatibility
2129
// https://github.com/primus/eventemitter3
@@ -480,6 +488,13 @@ export class Database extends EventEmitter {
480488
})
481489
}
482490

491+
/**
492+
* Returns true if the database connection is open.
493+
*/
494+
public isConnected(): boolean {
495+
return this.connections?.length > 0 && this.connections[0].connected
496+
}
497+
483498
/**
484499
* PubSub class provides a Pub/Sub real-time updates and notifications system to
485500
* allow multiple applications to communicate with each other asynchronously.

test/connection-tls.test.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
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,
17-
// clearTestingDatabasesAsync,
18-
WARN_SPEED_MS,
19-
EXPECT_SPEED_MS,
20-
EXTRA_LONG_TIMEOUT
19+
WARN_SPEED_MS
2120
} from './shared'
2221

2322
function getConnection() {

test/database.test.ts

Lines changed: 28 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
@@ -202,9 +187,7 @@ describe('Database.all', () => {
202187
expect(err).toBeNull()
203188
expect(rows).toBeDefined()
204189
expect(rows).toHaveLength(1)
205-
expect(rows[0]).toMatchObject({
206-
'1': 1
207-
})
190+
expect(rows[0]).toMatchObject({ '1': 1 })
208191

209192
chinook.close(error => {
210193
expect(error).toBeNull()
@@ -377,12 +360,7 @@ describe('Database.sql (async)', () => {
377360

378361
const row = results[0]
379362
expect(row).toBeDefined()
380-
expect(row).toMatchObject({
381-
id: 1,
382-
name: 'Emma Johnson',
383-
age: 28,
384-
hobby: 'Collecting clouds'
385-
})
363+
expect(row).toMatchObject({ id: 1, name: 'Emma Johnson', age: 28, hobby: 'Collecting clouds' })
386364
} finally {
387365
await removeDatabaseAsync(database)
388366
}
@@ -396,12 +374,7 @@ describe('Database.sql (async)', () => {
396374
expect(results).toBeDefined()
397375
const row = results[0]
398376
expect(row).toBeDefined()
399-
expect(row).toMatchObject({
400-
id: 1,
401-
name: 'Emma Johnson',
402-
age: 28,
403-
hobby: 'Collecting clouds'
404-
})
377+
expect(row).toMatchObject({ id: 1, name: 'Emma Johnson', age: 28, hobby: 'Collecting clouds' })
405378
} finally {
406379
await removeDatabaseAsync(database)
407380
}
@@ -420,12 +393,7 @@ describe('Database.sql (async)', () => {
420393
let results = await database.sql`SELECT * FROM people WHERE name = ${name}`
421394
// => returns { id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' }
422395

423-
expect(results[0]).toMatchObject({
424-
id: 5,
425-
name: 'Ava Jones',
426-
age: 22,
427-
hobby: 'Time traveling'
428-
})
396+
expect(results[0]).toMatchObject({ id: 5, name: 'Ava Jones', age: 22, hobby: 'Time traveling' })
429397

430398
results = await database.sql`SELECT * FROM people WHERE age < 30`
431399
expect(results).toHaveLength(11)
@@ -466,12 +434,7 @@ describe('Database.sql (async)', () => {
466434
database = await getTestingDatabaseAsync()
467435
const updateSql = "UPDATE people SET name = 'Charlie Brown' WHERE id = 3; UPDATE people SET name = 'David Bowie' WHERE id = 4;"
468436
let results = await database.sql(updateSql)
469-
expect(results).toMatchObject({
470-
lastID: 20,
471-
changes: 1,
472-
totalChanges: 22,
473-
finalized: 1
474-
})
437+
expect(results).toMatchObject({ lastID: 20, changes: 1, totalChanges: 22, finalized: 1 })
475438
} finally {
476439
await removeDatabaseAsync(database)
477440
}
@@ -485,12 +448,7 @@ describe('Database.sql (async)', () => {
485448
database = await getTestingDatabaseAsync()
486449
const insertSql = "INSERT INTO people (name, hobby, age) VALUES ('Barnaby Bumblecrump', 'Rubber Duck Dressing', 42); "
487450
let results = await database.sql(insertSql)
488-
expect(results).toMatchObject({
489-
lastID: 21,
490-
changes: 1,
491-
totalChanges: 21,
492-
finalized: 1
493-
})
451+
expect(results).toMatchObject({ lastID: 21, changes: 1, totalChanges: 21, finalized: 1 })
494452
} finally {
495453
await removeDatabaseAsync(database)
496454
}
@@ -504,12 +462,7 @@ describe('Database.sql (async)', () => {
504462
database = await getTestingDatabaseAsync()
505463
const insertSql = "INSERT INTO people (name, hobby, age) VALUES ('Barnaby Bumblecrump', 'Rubber Duck Dressing', 42); "
506464
let results = await database.sql(insertSql)
507-
expect(results).toMatchObject({
508-
lastID: 21,
509-
changes: 1,
510-
totalChanges: 21,
511-
finalized: 1
512-
})
465+
expect(results).toMatchObject({ lastID: 21, changes: 1, totalChanges: 21, finalized: 1 })
513466
} finally {
514467
await removeDatabaseAsync(database)
515468
}
@@ -522,11 +475,7 @@ describe('Database.sql (async)', () => {
522475
try {
523476
database = await getTestingDatabaseAsync()
524477
let results = await database.sql`SELECT true`
525-
expect(results).toMatchObject([
526-
{
527-
true: 1
528-
}
529-
])
478+
expect(results).toMatchObject([{ true: 1 }])
530479
} finally {
531480
await removeDatabaseAsync(database)
532481
}
@@ -629,3 +578,19 @@ describe('Database.sql (async)', () => {
629578
}
630579
})
631580
})
581+
582+
it('should be connected', async () => {
583+
const database: Database = await new Promise((resolve, rejects) => {
584+
const conn = getChinookDatabase(error => {
585+
if (error) {
586+
rejects(error)
587+
} else {
588+
resolve(conn)
589+
}
590+
})
591+
})
592+
593+
expect(database.isConnected()).toBe(true)
594+
database.close()
595+
expect(database.isConnected()).toBe(false)
596+
})

0 commit comments

Comments
 (0)