Database

Creating a Database object automatically opens a connection to the SQLite database. When the connection is established the Database object emits an open event and calls the optional provided callback. If the connection cannot be established an error event will be emitted and the optional callback is called with the error information.

Hierarchy

  • EventEmitter

    ↳ Database

Table of contents

Constructors

Properties

Methods

Constructors

constructor

• new Database(config, callback?): Database

Create and initialize a database from a full configuration object, or connection string

Parameters

NameType
configstring | SQLiteCloudConfig
callback?ErrorCallback

Returns

Database

Overrides

EventEmitter.constructor

Defined in

src/drivers/database.ts:33

• new Database(config, mode?, callback?): Database

Parameters

NameType
configstring | SQLiteCloudConfig
mode?number
callback?ErrorCallback

Returns

Database

Overrides

EventEmitter.constructor

Defined in

src/drivers/database.ts:34

Properties

config

• Private config: SQLiteCloudConfig

Configuration used to open database connections

Defined in

src/drivers/database.ts:57


connections

• Private connections: SQLiteCloudConnection[] = []

Database connections

Defined in

src/drivers/database.ts:60


prefixed

â–ª Static prefixed: string | boolean

Inherited from

EventEmitter.prefixed

Defined in

node_modules/eventemitter3/index.d.ts:9

Methods

addListener

â–¸ addListener<T>(event, fn, context?): Database

Type parameters

NameType
Textends string | symbol

Parameters

NameType
eventT
fn(…args: any[]) => void
context?any

Returns

Database

Inherited from

EventEmitter.addListener

Defined in

node_modules/eventemitter3/index.d.ts:45


all

â–¸ all<T>(sql, callback?): Database

Runs the SQL query with the specified parameters and calls the callback with all result rows afterwards. The function returns the Database object to allow for function chaining. The parameters are the same as the Database#run function, with the following differences: The signature of the callback is function(err, rows) {}. rows is an array. If the result set is empty, it will be an empty array, otherwise it will have an object for each result row which in turn contains the values of that row, like the Database#get function. Note that it first retrieves all result rows and stores them in memory. For queries that have potentially large result sets, use the Database#each function to retrieve all rows or Database#prepare followed by multiple Statement#get calls to retrieve a previously unknown amount of rows.

Type parameters

Name
T

Parameters

NameType
sqlstring
callback?RowsCallback<T>

Returns

Database

Defined in

src/drivers/database.ts:273

â–¸ all<T>(sql, params, callback?): Database

Type parameters

Name
T

Parameters

NameType
sqlstring
paramsany
callback?RowsCallback<T>

Returns

Database

Defined in

src/drivers/database.ts:274


close

â–¸ close(callback?): void

If the optional callback is provided, this function will be called when the database was closed successfully or when an error occurred. The first argument is an error object. When it is null, closing succeeded. If no callback is provided and an error occurred, an error event with the error object as the only parameter will be emitted on the database object. If closing succeeded, a close event with no parameters is emitted, regardless of whether a callback was provided or not.

Parameters

NameType
callback?ErrorCallback

Returns

void

Defined in

src/drivers/database.ts:394


configure

â–¸ configure(_option, _value): Database

Set a configuration option for the database

Parameters

NameType
_optionstring
_valueany

Returns

Database

Defined in

src/drivers/database.ts:190


each

â–¸ each<T>(sql, callback?, complete?): Database

Runs the SQL query with the specified parameters and calls the callback once for each result row. The function returns the Database object to allow for function chaining. The parameters are the same as the Database#run function, with the following differences: The signature of the callback is function(err, row) {}. If the result set succeeds but is empty, the callback is never called. In all other cases, the callback is called once for every retrieved row. The order of calls correspond exactly to the order of rows in the result set. After all row callbacks were called, the completion callback will be called if present. The first argument is an error object, and the second argument is the number of retrieved rows. If you specify only one function, it will be treated as row callback, if you specify two, the first (== second to last) function will be the row callback, the last function will be the completion callback. If you know that a query only returns a very limited number of rows, it might be more convenient to use Database#all to retrieve all rows at once. There is currently no way to abort execution.

Type parameters

Name
T

Parameters

NameType
sqlstring
callback?RowCallback<T>
complete?RowCountCallback

Returns

Database

Defined in

src/drivers/database.ts:312

â–¸ each<T>(sql, params, callback?, complete?): Database

Type parameters

Name
T

Parameters

NameType
sqlstring
paramsany
callback?RowCallback<T>
complete?RowCountCallback

Returns

Database

Defined in

src/drivers/database.ts:313


emit

â–¸ emit<T>(event, ...args): boolean

Calls each of the listeners registered for a given event.

Type parameters

NameType
Textends string | symbol

Parameters

NameType
eventT
...argsany[]

Returns

boolean

Inherited from

EventEmitter.emit

Defined in

node_modules/eventemitter3/index.d.ts:32


emitEvent

â–¸ emitEvent(event, ...args): void

Emits given event with optional arguments on the next tick so callbacks can complete first

Parameters

NameType
eventstring
...argsany[]

Returns

void

Defined in

src/drivers/database.ts:160


eventNames

â–¸ eventNames(): (string | symbol)[]

Return an array listing the events for which the emitter has registered listeners.

Returns

(string | symbol)[]

Inherited from

EventEmitter.eventNames

Defined in

node_modules/eventemitter3/index.d.ts:15


exec

â–¸ exec(sql, callback?): Database

Runs all SQL queries in the supplied string. No result rows are retrieved. The function returns the Database object to allow for function chaining. If a query fails, no subsequent statements will be executed (wrap it in a transaction if you want all or none to be executed). When all statements have been executed successfully, or when an error occurs, the callback function is called, with the first parameter being either null or an error object. When no callback is provided and an error occurs, an error event will be emitted on the database object.

Parameters

NameType
sqlstring
callback?ErrorCallback

Returns

Database

Defined in

src/drivers/database.ts:368


get

â–¸ get<T>(sql, callback?): Database

Runs the SQL query with the specified parameters and calls the callback with a subsequent result row. The function returns the Database object to allow for function chaining. The parameters are the same as the Database#run function, with the following differences: The signature of the callback is function(err, row) {}. If the result set is empty, the second parameter is undefined, otherwise it is an object containing the values for the first row. The property names correspond to the column names of the result set. It is impossible to access them by column index; the only supported way is by column name.

Type parameters

Name
T

Parameters

NameType
sqlstring
callback?RowCallback<T>

Returns

Database

Defined in

src/drivers/database.ts:235

â–¸ get<T>(sql, params, callback?): Database

Type parameters

Name
T

Parameters

NameType
sqlstring
paramsany
callback?RowCallback<T>

Returns

Database

Defined in

src/drivers/database.ts:236


getConfiguration

â–¸ getConfiguration(): SQLiteCloudConfig

Returns the configuration with which this database was opened. The configuration is readonly and cannot be changed as there may be multiple connections using the same configuration.

Returns

SQLiteCloudConfig

A configuration object

Defined in

src/drivers/database.ts:176


getConnection

â–¸ getConnection(callback): void

Returns first available connection from connection pool

Parameters

NameType
callbackResultsCallback<SQLiteCloudConnection>

Returns

void

Defined in

src/drivers/database.ts:67


handleError

â–¸ handleError(connection, error, callback?): void

Handles an error by closing the connection, calling the callback and/or emitting an error event

Parameters

NameType
connectionnull | SQLiteCloudConnection
errorError
callback?ErrorCallback

Returns

void

Defined in

src/drivers/database.ts:117


interrupt

â–¸ interrupt(): void

Allows the user to interrupt long-running queries. Wrapper around sqlite3_interrupt and causes other data-fetching functions to be passed an err with code = sqlite3.INTERRUPT. The database must be open to use this function.

Returns

void

Defined in

src/drivers/database.ts:429


listenerCount

â–¸ listenerCount(event): number

Return the number of listeners listening to a given event.

Parameters

NameType
eventstring | symbol

Returns

number

Inherited from

EventEmitter.listenerCount

Defined in

node_modules/eventemitter3/index.d.ts:27


listeners

▸ listeners<T>(event): (…args: any[]) => void[]

Return the listeners registered for a given event.

Type parameters

NameType
Textends string | symbol

Parameters

NameType
eventT

Returns

(…args: any[]) => void[]

Inherited from

EventEmitter.listeners

Defined in

node_modules/eventemitter3/index.d.ts:20


loadExtension

â–¸ loadExtension(_path, callback?): Database

Loads a compiled SQLite extension into the database connection object.

Parameters

NameTypeDescription
_pathstring-
callback?ErrorCallbackIf provided, this function will be called when the extension was loaded successfully or when an error occurred. The first argument is an error object. When it is null, loading succeeded. If no callback is provided and an error occurred, an error event with the error object as the only parameter will be emitted on the database object.

Returns

Database

Defined in

src/drivers/database.ts:413


off

â–¸ off<T>(event, fn?, context?, once?): Database

Type parameters

NameType
Textends string | symbol

Parameters

NameType
eventT
fn?(…args: any[]) => void
context?any
once?boolean

Returns

Database

Inherited from

EventEmitter.off

Defined in

node_modules/eventemitter3/index.d.ts:69


on

â–¸ on<T>(event, fn, context?): Database

Add a listener for a given event.

Type parameters

NameType
Textends string | symbol

Parameters

NameType
eventT
fn(…args: any[]) => void
context?any

Returns

Database

Inherited from

EventEmitter.on

Defined in

node_modules/eventemitter3/index.d.ts:40


once

â–¸ once<T>(event, fn, context?): Database

Add a one-time listener for a given event.

Type parameters

NameType
Textends string | symbol

Parameters

NameType
eventT
fn(…args: any[]) => void
context?any

Returns

Database

Inherited from

EventEmitter.once

Defined in

node_modules/eventemitter3/index.d.ts:54


prepare

â–¸ prepare<T>(sql, ...params): Statement<T>

Prepares the SQL statement and optionally binds the specified parameters and calls the callback when done. The function returns a Statement object. When preparing was successful, the first and only argument to the callback is null, otherwise it is the error object. When bind parameters are supplied, they are bound to the prepared statement before calling the callback.

Type parameters

NameType
Tany

Parameters

NameType
sqlstring
...paramsany[]

Returns

Statement<T>

Defined in

src/drivers/database.ts:353


processContext

â–¸ processContext(results?): undefined | Record<string, any>

Some queries like inserts or updates processed via run or exec may generate an empty result (eg. no data was selected), but still have some metadata. For example the server may pass the id of the last row that was modified. In this case the callback results should be empty but the context may contain additional information like lastID, etc.

Parameters

NameTypeDescription
results?anyResults received from the server

Returns

undefined | Record<string, any>

A context object if one makes sense, otherwise undefined

See

https://github.com/TryGhost/node-sqlite3/wiki/API#runsql—param---callback

Defined in

src/drivers/database.ts:141


removeAllListeners

â–¸ removeAllListeners(event?): Database

Remove all listeners, or those of the specified event.

Parameters

NameType
event?string | symbol

Returns

Database

Inherited from

EventEmitter.removeAllListeners

Defined in

node_modules/eventemitter3/index.d.ts:79


removeListener

â–¸ removeListener<T>(event, fn?, context?, once?): Database

Remove the listeners of a given event.

Type parameters

NameType
Textends string | symbol

Parameters

NameType
eventT
fn?(…args: any[]) => void
context?any
once?boolean

Returns

Database

Inherited from

EventEmitter.removeListener

Defined in

node_modules/eventemitter3/index.d.ts:63


run

â–¸ run<T>(sql, callback?): Database

Runs the SQL query with the specified parameters and calls the callback afterwards. The callback will contain the results passed back from the server, for example in the case of an update or insert, these would contain the number of rows modified, etc. It does not retrieve any result data. The function returns the Database object for which it was called to allow for function chaining.

Type parameters

Name
T

Parameters

NameType
sqlstring
callback?ResultsCallback<T>

Returns

Database

Defined in

src/drivers/database.ts:202

â–¸ run<T>(sql, params, callback?): Database

Type parameters

Name
T

Parameters

NameType
sqlstring
paramsany
callback?ResultsCallback<T>

Returns

Database

Defined in

src/drivers/database.ts:203


sql

â–¸ sql(sql, ...values): Promise<any>

Sql is a promise based API for executing SQL statements. You can pass a simple string with a SQL statement or a template string using backticks and parameters in ${parameter} format. These parameters will be properly escaped and quoted like when using a prepared statement.

Parameters

NameTypeDescription
sqlstring | TemplateStringsArrayA sql string or a template string in backticks format
...valuesany[]-

Returns

Promise<any>

An array of rows in case of selections or an object with metadata in case of insert, update, delete.

Defined in

src/drivers/database.ts:447


verbose

â–¸ verbose(): Database

Enable verbose mode

Returns

Database

Defined in

src/drivers/database.ts:181