Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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.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",
Expand Down
17 changes: 11 additions & 6 deletions src/drivers/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@
// 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
Expand Down Expand Up @@ -480,6 +478,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.
Expand Down
16 changes: 8 additions & 8 deletions test/connection-tls.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@
* 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() {
Expand Down
92 changes: 29 additions & 63 deletions test/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

//
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
Expand Down Expand Up @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand All @@ -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)
}
Expand Down Expand Up @@ -627,3 +576,20 @@ 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 {
console.log(conn.isConnected())
resolve(conn)
}
})
})

expect(database.isConnected()).toBe(true)
database.close()
expect(database.isConnected()).toBe(false)
})
Loading