SQLite Client for Node.js Apps
A wrapper library written in Typescript with ZERO dependencies that adds ES6 promises
and SQL-based migrations API to sqlite3 (docs).
note v4 of sqlite
has breaking changes compared to v3! Please see CHANGELOG.md
for more details.
Installation
Install sqlite3
Most people who use this library will use sqlite3
as the database driver.
Any library that conforms to the sqlite3
(API)
should also work.
$ npm install sqlite3 --save
Install sqlite
$ npm install sqlite --save
$ npm install sqlite@3 --save
Usage
This module has the same API as the original sqlite3
library (docs),
except that all its API methods return ES6 Promises and do not accept callback arguments (with the exception of each()
).
Opening the database
Without caching
import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
(async () => {
const db = await open({
filename: '/tmp/database.db',
driver: sqlite3.Database
})
})()
or
import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
open({
filename: '/tmp/database.db',
driver: sqlite3.Database
}).then((db) => {
})
or
import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
export async function openDb () {
return open({
filename: '/tmp/database.db',
driver: sqlite3.Database
})
}
With caching
If you want to enable the database object cache
import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
(async () => {
const db = await open({
filename: '/tmp/database.db',
driver: sqlite3.cached.Database
})
})()
Enable verbose / debug mode
import sqlite3 from 'sqlite3'
sqlite3.verbose()
Tracing SQL errors
For more info, see this doc.
db.on('trace', (data) => {
})
With a custom driver
You can use an alternative library to sqlite3
as long as it conforms to the sqlite3
API.
For example, using sqlite3-offline
:
import sqlite3Offline from 'sqlite3-offline'
import { open } from 'sqlite'
(async () => {
const db = await open({
filename: '/tmp/database.db',
driver: sqlite3Offline.Database
})
})()
Opening multiple databases
import sqlite3 from 'sqlite3'
import { open } from 'sqlite'
(async () => {
const [db1, db2] = await Promise.all([
open({
filename: '/tmp/database.db',
driver: sqlite3.Database
}),
open({
filename: '/tmp/database2.db',
driver: sqlite3.Database
}),
])
await db1.migrate({
migrationsPath: '...'
})
await db2.migrate({
migrationsPath: '...'
})
})()
open
config params
const db = await open({
filename: string
mode?: number
driver: any
})
Examples
- See the
src/**/__tests__
directory for more example usages - See the
docs/
directory for full documentation. - Also visit the
sqlite3
library API docs
Creating a table and inserting data
await db.exec('CREATE TABLE tbl (col TEXT)')
await db.exec('INSERT INTO tbl VALUES ("test")')
Getting a single row
const result = await db.get('SELECT col FROM tbl WHERE col = ?', 'test')
const result = await db.get('SELECT col FROM tbl WHERE col = ?', ['test'])
const result = await db.get('SELECT col FROM tbl WHERE col = :test', {
':test': 'test'
})
Getting many rows
const result = await db.all('SELECT col FROM tbl')
Inserting rows
const result = await db.run(
'INSERT INTO tbl (col) VALUES (?)',
'foo'
)
const result = await db.run('INSERT INTO tbl(col) VALUES (:col)', {
':col': 'something'
})
Updating rows
const result = await db.run(
'UPDATE tbl SET col = ? WHERE col = ?',
'foo',
'test'
)
Prepared statement
const stmt = await db.prepare('SELECT col FROM tbl WHERE 1 = ? AND 5 = ?5')
await stmt.bind({ 1: 1, 5: 5 })
let result = await stmt.get()
const stmt = await db.prepare(
'SELECT col FROM tbl WHERE 13 = @thirteen ORDER BY col DESC'
)
const result = await stmt.all({ '@thirteen': 13 })
each()
each()
is a bit different compared to the other operations.
The function signature looks like this:
async each (sql, [...params], callback)
callback(err, row)
is triggered when the database has a row to return- The promise resolves when all rows have returned with the number of rows returned.
const rowsCount = await db.each(
'SELECT col FROM tbl WHERE ROWID = ?',
[2],
(err, row) => {
if (err) {
throw err
}
}
)
Get the driver instance
Useful if you need to call methods that are not supported yet.
const rawDb = db.getDatabaseInstance()
const rawStatement = stmt.getStatementInstance()
Closing the database
await db.close()
ES6 tagged template strings
This module is compatible with sql-template-strings.
import SQL from 'sql-template-strings'
const book = 'harry potter';
const author = 'J. K. Rowling';
const data = await db.all(SQL`SELECT author FROM books WHERE name = ${book} AND author = ${author}`);
Migrations
This module comes with a lightweight migrations API that works with SQL-based migration files
With default configuration, you can create a migrations/
directory in your project with SQL files,
and call the migrate()
method to run the SQL in the directory against the database.
See this project's migrations/
folder for examples.
await db.migrate({
force?: boolean
table?: string
migrationsPath?: string
})
Typescript tricks
Import interfaces from sqlite
import { ISqlite, IMigrate } from 'sqlite'
See the definitions for more details.
Specify typings for a specific database driver
import sqlite3 from 'sqlite3'
await open<sqlite3.Database, sqlite3.Statement>({
filename: ':memory'
})
Use generics to get better typings on your rows
Most methods allow for the use of generics
to specify the data type of your returned data. This allows your IDE to perform better autocomplete
and the typescript compiler to perform better static type analysis.
Get example
interface Row {
col: string
}
const result = await db.get<Row>('SELECT col FROM tbl WHERE col = ?', 'test')
All example
interface Row {
col: string
}
const result = await db.all<Row[]>('SELECT col FROM tbl')
result.each((row) => {
})
API Documentation
See the docs
directory for full documentation.
Management Tools
- Beekeeper Studio: Open Source SQL Editor and Database Manager
- DB Browser for SQLite: Desktop-based browser.
- datasette: Datasette is a tool for exploring and publishing
data. Starts up a server that provides a web interface to your SQLite data.
- SQLite Studio: A free, open source, multi-platform SQLite database manager written in C++, with use of Qt framework.
- HeidiSQL: Full-featured database editor.
- DBeaver: Full-featured multi-platform database tool and designer.
Alternative SQLite libraries
This library and the library it primarily supports, sqlite3
, may not be the best library that
fits your use-case. You might want to try these other SQLite libraries:
- better-sqlite3: Totes itself as the fastest and
simplest library for SQLite3 in Node.js.
- sql.js: SQLite compiled to Webassembly.
- sqlite3-offline: Offers pre-compiled
sqlite3
binaries if your machine cannot compile it. Should be mostly compatible with this library.
If you know of any others, feel free to open a PR to add them to the list.
References
Support
- Join #node-sqlite chat room on Gitter to stay up to date regarding the project
- Join #sqlite IRC chat room on Freenode about general discussion about SQLite
License
The MIT License © 2020-present Kriasoft / Theo Gravity. All rights reserved.
Made with ♥ by Konstantin Tarkus (@koistya), Theo Gravity and contributors