Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

sqlite

kriasoft624kMIT5.1.1TypeScript support: included

SQLite client for Node.js applications with SQL-based migrations API written in Typescript

sqlite, db, database, sql, migrate, migration, migrations, migrator, seed, seeds, seeder, node, node5, babel, es6, async, promise, promises, api

readme

SQLite Client for Node.js Apps

NPM version CircleCI built with typescript JavaScript Style Guide

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

# v4 of sqlite is targeted for nodejs 10 and on.
$ npm install sqlite --save

# If you need a legacy version for an older version of nodejs
# install v3 instead, and look at the v3 branch readme for usage details
$ 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'

// this is a top-level await 
(async () => {
    // open the database
    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) => {
  // do your thing
})

or

import sqlite3 from 'sqlite3'
import { open } from 'sqlite'

// you would have to import / invoke this in another file
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-next:

import sqlite3Offline from 'sqlite3-offline-next'
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


// db is an instance of `sqlite#Database`
// which is a wrapper around `sqlite3#Database`
const db = await open({
  /**
   * Valid values are filenames, ":memory:" for an anonymous in-memory
   * database and an empty string for an anonymous disk-based database.
   * Anonymous databases are not persisted and when closing the database
   * handle, their contents are lost.
   */
  filename: string

  /**
   * One or more of sqlite3.OPEN_READONLY, sqlite3.OPEN_READWRITE and
   * sqlite3.OPEN_CREATE. The default value is OPEN_READWRITE | OPEN_CREATE.
   */
  mode?: number

  /**
   * The database driver. Most will install `sqlite3` and use the `Database` class from it.
   * As long as the library you are using conforms to the `sqlite3` API, you can use it as
   * the driver.
   *
   * @example
   *
   *
  • import sqlite from 'sqlite3' *
  • const driver = sqlite.Database
  • */
    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')

// { col: 'test' }
const result = await db.get('SELECT col FROM tbl WHERE col = ?', ['test'])

// { col: 'test' }
const result = await db.get('SELECT col FROM tbl WHERE col = :test', {
  ':test': 'test'
})

// { col: 'test' }

Getting many rows

const result = await db.all('SELECT col FROM tbl')

// [{ col: 'test' }]

Inserting rows

const result = await db.run(
  'INSERT INTO tbl (col) VALUES (?)',
  'foo'
)

/*
{
  // row ID of the inserted row
  lastID: 1,
  // instance of `sqlite#Statement`
  // which is a wrapper around `sqlite3#Statement`
  stmt: <Statement>
}
*/
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'
)

/*
{
  // number of rows changed
  changes: 1,
  // instance of `sqlite#Statement`
  // which is a wrapper around `sqlite3#Statement`
  stmt: <Statement>
}
*/

Prepared statement

// stmt is an instance of `sqlite#Statement`
// which is a wrapper around `sqlite3#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()
// { col: 'some text' }
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 due to its underlying implementation.

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.
try {
  // You need to wrap this in a try / catch for SQL parse / connection errors
  const rowsCount = await db.each(
    'SELECT col FROM tbl WHERE ROWID = ?',
    [2],
    (err, row) => {
      if (err) {
        // This would be if there is an error specific to the row result
        throw err
      }

      // row = { col: 'other thing' }
    }
  )
} catch (e) {
  throw e
}

// rowsCount = 1

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({    
    /**
    * If true, will force the migration API to rollback and re-apply the latest migration over
    * again each time when Node.js app launches.
    */
    force?: boolean
    /**
    * Migrations table name. Default is 'migrations'
    */
    table?: string
    /**
    * Path to the migrations folder. Default is `path.join(process.cwd(), 'migrations')`
    */
    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

// Assuming you have @types/sqlite3 installed
import sqlite3 from 'sqlite3'

// sqlite3.Database, sqlite3.Statement is the default if no explicit generic is specified
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
}

// result will be of type Row, allowing Typescript supported IDEs to autocomplete on the properties!
const result = await db.get<Row>('SELECT col FROM tbl WHERE col = ?', 'test')

All example

interface Row {
  col: string
}

// Result is an array of rows, you can now have array-autocompletion data
const result = await db.all<Row[]>('SELECT col FROM tbl')

result.each((row) => {
  // row should have type information now!
})

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.
  • Bun sqlite3: bun:sqlite is a high-performance builtin SQLite3 module for bun.js.
  • sql.js: SQLite compiled to Webassembly.
  • sqlite3-offline-next: 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

License

The MIT License © 2020-present Kriasoft / Theo Gravity. All rights reserved.


Made with ♥ by Konstantin Tarkus (@koistya), Theo Gravity and contributors

changelog

5.1.1 - Wed Nov 01 2023 23:59:09

Contributor: Theo Gravity

  • Update vendor typings to use sqlite3 (#176)

5.0.1 - Tue Jul 11 2023 17:36:14

Contributor: Aleksandr Ananin

Breaking Change:

  • Remove params parameter from exec function (#173)

4.2.1 - Tue May 23 2023 00:58:29

Contributor: Blake Embrey

  • Support all SQL template libraries (#172)

  • Support all SQL template libraries

  • Remove sql-template-strings from lib

4.2.0 (4.1.3) - Mon May 01 2023 10:52:06

Contributor: Andarist

  • move types condition to the front (#171)

Package should have published under 4.2.0 but was also published as 4.1.3.

Downgrade to 4.1.2 if you are having issues and raise an issue.

4.1.2 - Tue Jul 19 2022 16:51:14

Contributor: rinsuki

  • Support TS 4.7+ node16/next module mode (#164)

4.1.1 - Sun Apr 24 2022 00:00:52

Contributor: Theo Gravity

  • Have native sqlite errors contain stack traces (#162)

This ensures that errors thrown from the sqlite driver now have stack traces and are of an Error type.

Thanks to @fresheneesz for initial troubleshooting and initial code to help fix the issue.

This is a minor level version update as to not break implementations that may handle errors in their own way prior to this fix.

4.0.25 - Mon Mar 07 2022 08:50:06

Contributor: Theo Gravity

  • Update packages, fix tests for latest jest (#159)

  • Update packages, fix tests for latest jest

  • Update node version on ci

4.0.24 - Sun Mar 06 2022 20:12:24

Contributor: fresheneesz

  • Updating exec to support passed in parameters like the other functions do. (#155)

4.0.23 - Mon May 24 2021 21:17:44

Contributor: dependabot[bot]

  • Bump browserslist from 4.16.3 to 4.16.6 (#145)

Bumps browserslist from 4.16.3 to 4.16.6.

Signed-off-by: dependabot[bot] support@github.com

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

4.0.22 - Sun May 09 2021 19:59:28

Contributor: dependabot[bot]

  • Bump handlebars from 4.7.6 to 4.7.7 (#141)

Bumps handlebars from 4.7.6 to 4.7.7.

Signed-off-by: dependabot[bot] support@github.com

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

4.0.21 - Wed Mar 31 2021 21:22:42

Contributor: Theo Gravity

  • Update packages to latest

4.0.20 - Wed Mar 31 2021 20:54:37

Contributor: dependabot[bot]

  • Bump y18n from 4.0.0 to 4.0.1 (#139)

Bumps y18n from 4.0.0 to 4.0.1.

Signed-off-by: dependabot[bot] support@github.com

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

4.0.19 - Wed Dec 30 2020 02:11:07

Contributor: dependabot[bot]

  • Bump ini from 1.3.5 to 1.3.8 (#135)

Bumps ini from 1.3.5 to 1.3.8.

Signed-off-by: dependabot[bot] support@github.com

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

4.0.18 - Fri Dec 04 2020 21:20:15

Contributor: dependabot[bot]

  • Bump highlight.js from 10.1.2 to 10.4.1 (#133)

Bumps highlight.js from 10.1.2 to 10.4.1.

Signed-off-by: dependabot[bot] support@github.com

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

4.0.17 - Fri Nov 13 2020 19:18:04

Contributor: Tiger Oakes

  • Add strict types for each() (#131)

4.0.16 - Fri Nov 13 2020 19:15:24

Contributor: Tiger Oakes

  • Let migrations be read manually (#132)

4.0.15 - Wed Sep 30 2020 16:06:20

Contributor: Paul Kiddle

  • Make down migrations optional (#129)

4.0.14 - Mon Aug 10 2020 01:08:29

Contributor: Theo Gravity

  • Revert filename optional; update filename checks instead (#125)

The filename property is back to being required, but empty strings are valid values for the purpose of using an anonymous disk-based database.

Values of undefined or null for filename will throw.

4.0.13 - Mon Aug 10 2020 00:57:58

Contributor: Theo Gravity

  • Make filename optional (#124)

This allows for the specification of an anonymous database.

4.0.12 - Mon Jul 20 2020 04:19:01

Contributor: dependabot[bot]

  • Bump lodash from 4.17.15 to 4.17.19 (#122)

Bumps lodash from 4.17.15 to 4.17.19.

Signed-off-by: dependabot[bot] support@github.com

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

4.0.11 - Fri Jun 12 2020 09:43:39

Contributor: Theo Gravity

  • Update readme for inserting rows (#120)

@akc42 pointed out that the docs for db.run says it returns lastId when it should be lastID.

4.0.10 - Wed Jun 03 2020 00:51:41

Contributor: Gustavo Rodrigues

  • Use HTTPS in README badges (#118)

Shields.io badges were being loaded using HTTP causing mixed-content errors in the NPM page.

4.0.9 - Mon May 25 2020 22:50:02

Contributor: Markus Felten

  • feat: provide native esm exports (#117)

4.0.8 - Thu May 21 2020 22:59:57

Contributor: George Corney

  • Move triple-slash reference so it is recognized (#115)

4.0.7 - Sun Apr 12 2020 19:43:31

Contributor: Yamavol

  • Fix typescript defs for sqlite3 loadExtension() (#112)

4.0.6 - Sat Apr 11 2020 04:16:08

Contributor: Theo Gravity

  • Throw an error if two callbacks are defined for #each()

This addresses an issue where the sqlite3 API for each() uses two callbacks, while this library only uses one for each().

An error is now thrown if two callbacks are defined when using the sqlite each() method.

(In sqlite, the second callback of sqlite3 is used to resolve the promise.)

4.0.5 - Sun Apr 05 2020 20:13:43

Contributor: jameswilddev

  • Use non-default imports rather than synthetic default imports. (#110)

4.0.4 - Sun Apr 05 2020 07:44:39

  • Fix Typescript reference issues for sqlite3 if it is not installed (#109)

This allows the usage of the sqlite3-offline; library in Typescript.

4.0.3 - Sun Apr 05 2020 00:01:31

  • Fix Typescript issue where sql-template-strings is a forced requirement (#108)

It should be an optional dependency and is not required for installation.

4.0.2 - Sat Apr 04 2020 22:19:38

  • Add management tools section to readme

4.0.1 - Sat Apr 04 2020 21:46:39

New major version 4!

This version has been written in Typescript with a target of node.js v10 or greater.

If you are using an older version of node < 10, use the 3.x version of the library instead.

What's new:

  • ZERO dependencies!
    • This should make troubleshooting the library vs the sqlite3 driver easier. Most issues relate to the sqlite3 driver vs the library.
    • Allows support for alternative drivers such as sqlite3-offline as long as they match the node-sqlite3 API.
  • Re-written in Typescript.
  • All existing features maintained.
  • Existing unit tests were cleaned up and pass, new tests added.
  • Better documentation. See README.md.
  • New CI process - pull requests merged into master will run tests, build, and auto-publish to NPM.
  • Parity with the node-sqlite3 API.
    • Exceptions are serialize and parallelize (PRs are welcomed!)

Breaking Changes:

  • sqlite3 is no longer a dependency of this project. You must install it first before using this wrapper library.
    • This allows for usage of alternative libraries.
    • This means verbose and cache modes must be enabled before using the library.
    • The README.md file explains how to do this.
  • Opening a new database has changed. See README.md for example.
  • migrations API: force is now a boolean.
  • Can no longer specify a custom Promise library. Uses native Promise instead.
[v3.0.3]

2019-03-22

  • Export class types for flow ##88
[v3.0.2]

2019-02-14

  • Flow: Add Flow typings ##86
[v3.0.1]

2019-01-28

  • Typescript: Add cached option to open function ##81
  • Typescript: Merged declarations for methods that accepts both string and SQLStatements in Database ##80
  • Misc: Fix small typo ##84
  • Apply npm audit fixes (mocha updated to latest)
v3.0.0

2018-08-22

  • Add support for ES6 tagged template strings (##66)
v2.9.3

2018-08-22

  • Change baseline sqlite3 version from 4.0.0 -> ^4.0.0 (##71)
v2.9.2

2018-04-20

  • Bump up baseline sqlite3 version from 3.1.13 -> 4.0.0 (##56)
[v2.9.1]

2018-01-13

  • Expose the sqlite3##configure method (##53)
  • Example fixes (##52)
  • Example fixes (##49)
  • Expose sqlite3 debugging hooksk (##48)
  • Typescript updates (##47)
[v2.9.0]

2017-11-27

  • Move away from global db object in readme (##45)
  • Fix typescript 2.6.1 issue where *.d.ts contains executable code when it is no longer allowed (##47)
  • Fix readme typo (##46)
  • Bump up baseline sqlite3 version from 3.1.8 -> 3.1.13
[v2.8.0]

2017-05-21

  • Match only “down” as separator in migration files (##32)
v2.7.0

2017-05-01

v2.6.0

2017-04-30

  • Migration bug fixed where valid multiple dashes (eg a PEM string) was being removed (##28)
v2.5.0

2017-03-23

  • npm publish'd from the wrong area. 2.4.0 is unpublished, 2.5.0 is the latest.
v2.4.0

2017-03-23

  • Typescript updates - Make ambient declarations more useful for type inference (##26)
v2.3.0

2017-01-31

  • Fix a bug sorting of schemas during migrations (##25)
v2.2.4

2016-12-10

  • Add TypeScript definition (##21)
v2.2.3

2016-11-10

  • Update sqlite3 package to 3.1.8
  • Use external-helpers plugin in Babel config (ref ##12)
v2.2.2

2016-11-02

  • Fix duplicate migration application when db.migrate() is run multiple times (##19)
v2.2.1

2016-10-26

v2.2.0

2016-07-22

v2.0.2

2016-05-21

  • Add SQL-based migrations API: db.migrate({ force: 'last' })
  • Add migration files examples. See /migrations folder.
  • Add support for multiple databases: const db = sqlite.open('db.sqlite')
  • Add async wrapper for sqlite3 Statement object
v1.0.0

2016-05-19

  • The initial release after a couple of preview versions earlier this year
  • Built the project with Babel and Rollup for Node.js v3-5, Node.js v6, Node.js vNext (Harmony Modules)