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
- addListener
- all
- close
- configure
- each
- emit
- emitEvent
- eventNames
- exec
- get
- getConfiguration
- getConnection
- handleError
- interrupt
- listenerCount
- listeners
- loadExtension
- off
- on
- once
- prepare
- processContext
- removeAllListeners
- removeListener
- run
- sql
- verbose
Constructors
constructor
• new Database(config
, callback?
): Database
Create and initialize a database from a full configuration object, or connection string
Parameters
Name | Type |
---|---|
config | string | SQLiteCloudConfig |
callback? | ErrorCallback |
Returns
Overrides
EventEmitter.constructor
Defined in
• new Database(config
, mode?
, callback?
): Database
Parameters
Name | Type |
---|---|
config | string | SQLiteCloudConfig |
mode? | number |
callback? | ErrorCallback |
Returns
Overrides
EventEmitter.constructor
Defined in
Properties
config
• Private
config: SQLiteCloudConfig
Configuration used to open database connections
Defined in
connections
• Private
connections: SQLiteCloudConnection
[] = []
Database connections
Defined in
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
Name | Type |
---|---|
T | extends string | symbol |
Parameters
Name | Type |
---|---|
event | T |
fn | (…args : any []) => void |
context? | any |
Returns
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
Name | Type |
---|---|
sql | string |
callback? | RowsCallback <T > |
Returns
Defined in
â–¸ all<T
>(sql
, params
, callback?
): Database
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
sql | string |
params | any |
callback? | RowsCallback <T > |
Returns
Defined in
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
Name | Type |
---|---|
callback? | ErrorCallback |
Returns
void
Defined in
configure
â–¸ configure(_option
, _value
): Database
Set a configuration option for the database
Parameters
Name | Type |
---|---|
_option | string |
_value | any |
Returns
Defined in
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
Name | Type |
---|---|
sql | string |
callback? | RowCallback <T > |
complete? | RowCountCallback |
Returns
Defined in
â–¸ each<T
>(sql
, params
, callback?
, complete?
): Database
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
sql | string |
params | any |
callback? | RowCallback <T > |
complete? | RowCountCallback |
Returns
Defined in
emit
â–¸ emit<T
>(event
, ...args
): boolean
Calls each of the listeners registered for a given event.
Type parameters
Name | Type |
---|---|
T | extends string | symbol |
Parameters
Name | Type |
---|---|
event | T |
...args | any [] |
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
Name | Type |
---|---|
event | string |
...args | any [] |
Returns
void
Defined in
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
Name | Type |
---|---|
sql | string |
callback? | ErrorCallback |
Returns
Defined in
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
Name | Type |
---|---|
sql | string |
callback? | RowCallback <T > |
Returns
Defined in
â–¸ get<T
>(sql
, params
, callback?
): Database
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
sql | string |
params | any |
callback? | RowCallback <T > |
Returns
Defined in
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
A configuration object
Defined in
getConnection
â–¸ getConnection(callback
): void
Returns first available connection from connection pool
Parameters
Name | Type |
---|---|
callback | ResultsCallback <SQLiteCloudConnection > |
Returns
void
Defined in
handleError
â–¸ handleError(connection
, error
, callback?
): void
Handles an error by closing the connection, calling the callback and/or emitting an error event
Parameters
Name | Type |
---|---|
connection | null | SQLiteCloudConnection |
error | Error |
callback? | ErrorCallback |
Returns
void
Defined in
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
listenerCount
â–¸ listenerCount(event
): number
Return the number of listeners listening to a given event.
Parameters
Name | Type |
---|---|
event | string | 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
Name | Type |
---|---|
T | extends string | symbol |
Parameters
Name | Type |
---|---|
event | T |
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
Name | Type | Description |
---|---|---|
_path | string | - |
callback? | ErrorCallback | If 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
Defined in
off
â–¸ off<T
>(event
, fn?
, context?
, once?
): Database
Type parameters
Name | Type |
---|---|
T | extends string | symbol |
Parameters
Name | Type |
---|---|
event | T |
fn? | (…args : any []) => void |
context? | any |
once? | boolean |
Returns
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
Name | Type |
---|---|
T | extends string | symbol |
Parameters
Name | Type |
---|---|
event | T |
fn | (…args : any []) => void |
context? | any |
Returns
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
Name | Type |
---|---|
T | extends string | symbol |
Parameters
Name | Type |
---|---|
event | T |
fn | (…args : any []) => void |
context? | any |
Returns
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
Name | Type |
---|---|
T | any |
Parameters
Name | Type |
---|---|
sql | string |
...params | any [] |
Returns
Statement
<T
>
Defined in
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
Name | Type | Description |
---|---|---|
results? | any | Results 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
removeAllListeners
â–¸ removeAllListeners(event?
): Database
Remove all listeners, or those of the specified event.
Parameters
Name | Type |
---|---|
event? | string | symbol |
Returns
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
Name | Type |
---|---|
T | extends string | symbol |
Parameters
Name | Type |
---|---|
event | T |
fn? | (…args : any []) => void |
context? | any |
once? | boolean |
Returns
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
Name | Type |
---|---|
sql | string |
callback? | ResultsCallback <T > |
Returns
Defined in
â–¸ run<T
>(sql
, params
, callback?
): Database
Type parameters
Name |
---|
T |
Parameters
Name | Type |
---|---|
sql | string |
params | any |
callback? | ResultsCallback <T > |
Returns
Defined in
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
Name | Type | Description |
---|---|---|
sql | string | TemplateStringsArray | A sql string or a template string in backticks format |
...values | any [] | - |
Returns
Promise
<any
>
An array of rows in case of selections or an object with metadata in case of insert, update, delete.
Defined in
verbose
â–¸ verbose(): Database
Enable verbose mode