Gathering detailed insights and metrics for sqlite
Gathering detailed insights and metrics for sqlite
Gathering detailed insights and metrics for sqlite
Gathering detailed insights and metrics for sqlite
SQLite client wrapper around sqlite3 for Node.js applications with SQL-based migrations API written in Typescript
npm install sqlite
99
Supply Chain
100
Quality
80
Maintenance
100
Vulnerability
100
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
905 Stars
235 Commits
93 Forks
16 Watching
3 Branches
41 Contributors
Updated on 18 Nov 2024
Minified
Minified + Gzipped
TypeScript (91.5%)
JavaScript (5.34%)
Shell (3.15%)
Cumulative downloads
Total Downloads
Last day
2%
27,468
Compared to previous day
Last week
-0.3%
157,951
Compared to previous week
Last month
41.8%
636,815
Compared to previous month
Last year
62.1%
7,176,166
Compared to previous year
27
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.
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
sqlite
1# v4 of sqlite is targeted for nodejs 10 and on. 2$ npm install sqlite --save 3 4# If you need a legacy version for an older version of nodejs 5# install v3 instead, and look at the v3 branch readme for usage details 6$ npm install sqlite@3 --save
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()
).
1import sqlite3 from 'sqlite3' 2import { open } from 'sqlite' 3 4// this is a top-level await 5(async () => { 6 // open the database 7 const db = await open({ 8 filename: '/tmp/database.db', 9 driver: sqlite3.Database 10 }) 11})()
or
1import sqlite3 from 'sqlite3' 2import { open } from 'sqlite' 3 4open({ 5 filename: '/tmp/database.db', 6 driver: sqlite3.Database 7}).then((db) => { 8 // do your thing 9})
or
1import sqlite3 from 'sqlite3' 2import { open } from 'sqlite' 3 4// you would have to import / invoke this in another file 5export async function openDb () { 6 return open({ 7 filename: '/tmp/database.db', 8 driver: sqlite3.Database 9 }) 10}
If you want to enable the database object cache
1import sqlite3 from 'sqlite3' 2import { open } from 'sqlite' 3 4(async () => { 5 const db = await open({ 6 filename: '/tmp/database.db', 7 driver: sqlite3.cached.Database 8 }) 9})()
1import sqlite3 from 'sqlite3' 2 3sqlite3.verbose()
For more info, see this doc.
1db.on('trace', (data) => { 2 3})
You can use an alternative library to sqlite3
as long as it conforms to the sqlite3
API.
For example, using sqlite3-offline-next
:
1import sqlite3Offline from 'sqlite3-offline-next' 2import { open } from 'sqlite' 3 4(async () => { 5 const db = await open({ 6 filename: '/tmp/database.db', 7 driver: sqlite3Offline.Database 8 }) 9})()
1import sqlite3 from 'sqlite3' 2import { open } from 'sqlite' 3 4(async () => { 5 const [db1, db2] = await Promise.all([ 6 open({ 7 filename: '/tmp/database.db', 8 driver: sqlite3.Database 9 }), 10 open({ 11 filename: '/tmp/database2.db', 12 driver: sqlite3.Database 13 }), 14 ]) 15 16 await db1.migrate({ 17 migrationsPath: '...' 18 }) 19 20 await db2.migrate({ 21 migrationsPath: '...' 22 }) 23})()
open
config params1 2// db is an instance of `sqlite#Database` 3// which is a wrapper around `sqlite3#Database` 4const db = await open({ 5 /** 6 * Valid values are filenames, ":memory:" for an anonymous in-memory 7 * database and an empty string for an anonymous disk-based database. 8 * Anonymous databases are not persisted and when closing the database 9 * handle, their contents are lost. 10 */ 11 filename: string 12 13 /** 14 * One or more of sqlite3.OPEN_READONLY, sqlite3.OPEN_READWRITE and 15 * sqlite3.OPEN_CREATE. The default value is OPEN_READWRITE | OPEN_CREATE. 16 */ 17 mode?: number 18 19 /** 20 * The database driver. Most will install `sqlite3` and use the `Database` class from it. 21 * As long as the library you are using conforms to the `sqlite3` API, you can use it as 22 * the driver. 23 * 24 * @example 25 * 26 * ``` 27 * import sqlite from 'sqlite3' 28 * 29 * const driver = sqlite.Database 30 * ``` 31 */ 32 driver: any 33})
src/**/__tests__
directory for more example usagesdocs/
directory for full documentation.sqlite3
library API docs1await db.exec('CREATE TABLE tbl (col TEXT)') 2await db.exec('INSERT INTO tbl VALUES ("test")')
1const result = await db.get('SELECT col FROM tbl WHERE col = ?', 'test') 2 3// { col: 'test' }
1const result = await db.get('SELECT col FROM tbl WHERE col = ?', ['test']) 2 3// { col: 'test' }
1const result = await db.get('SELECT col FROM tbl WHERE col = :test', { 2 ':test': 'test' 3}) 4 5// { col: 'test' }
1const result = await db.all('SELECT col FROM tbl') 2 3// [{ col: 'test' }]
1const result = await db.run( 2 'INSERT INTO tbl (col) VALUES (?)', 3 'foo' 4) 5 6/* 7{ 8 // row ID of the inserted row 9 lastID: 1, 10 // instance of `sqlite#Statement` 11 // which is a wrapper around `sqlite3#Statement` 12 stmt: <Statement> 13} 14*/
1const result = await db.run('INSERT INTO tbl(col) VALUES (:col)', { 2 ':col': 'something' 3})
1const result = await db.run( 2 'UPDATE tbl SET col = ? WHERE col = ?', 3 'foo', 4 'test' 5) 6 7/* 8{ 9 // number of rows changed 10 changes: 1, 11 // instance of `sqlite#Statement` 12 // which is a wrapper around `sqlite3#Statement` 13 stmt: <Statement> 14} 15*/
1// stmt is an instance of `sqlite#Statement` 2// which is a wrapper around `sqlite3#Statement` 3const stmt = await db.prepare('SELECT col FROM tbl WHERE 1 = ? AND 5 = ?5') 4await stmt.bind({ 1: 1, 5: 5 }) 5let result = await stmt.get() 6// { col: 'some text' }
1const stmt = await db.prepare( 2 'SELECT col FROM tbl WHERE 13 = @thirteen ORDER BY col DESC' 3) 4 5const 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 return1try { 2 // You need to wrap this in a try / catch for SQL parse / connection errors 3 const rowsCount = await db.each( 4 'SELECT col FROM tbl WHERE ROWID = ?', 5 [2], 6 (err, row) => { 7 if (err) { 8 // This would be if there is an error specific to the row result 9 throw err 10 } 11 12 // row = { col: 'other thing' } 13 } 14 ) 15} catch (e) { 16 throw e 17} 18 19// rowsCount = 1
Useful if you need to call methods that are not supported yet.
1const rawDb = db.getDatabaseInstance() 2const rawStatement = stmt.getStatementInstance()
1await db.close()
This module is compatible with sql-template-strings.
1import SQL from 'sql-template-strings' 2 3const book = 'harry potter'; 4const author = 'J. K. Rowling'; 5 6const data = await db.all(SQL`SELECT author FROM books WHERE name = ${book} AND author = ${author}`);
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.
1await db.migrate({ 2 /** 3 * If true, will force the migration API to rollback and re-apply the latest migration over 4 * again each time when Node.js app launches. 5 */ 6 force?: boolean 7 /** 8 * Migrations table name. Default is 'migrations' 9 */ 10 table?: string 11 /** 12 * Path to the migrations folder. Default is `path.join(process.cwd(), 'migrations')` 13 */ 14 migrationsPath?: string 15})
import { ISqlite, IMigrate } from 'sqlite'
See the definitions for more details.
1// Assuming you have @types/sqlite3 installed 2import sqlite3 from 'sqlite3' 3 4// sqlite3.Database, sqlite3.Statement is the default if no explicit generic is specified 5await open<sqlite3.Database, sqlite3.Statement>({ 6 filename: ':memory' 7})
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.
1 2interface Row { 3 col: string 4} 5 6// result will be of type Row, allowing Typescript supported IDEs to autocomplete on the properties! 7const result = await db.get<Row>('SELECT col FROM tbl WHERE col = ?', 'test')
1interface Row { 2 col: string 3} 4 5// Result is an array of rows, you can now have array-autocompletion data 6const result = await db.all<Row[]>('SELECT col FROM tbl') 7 8result.each((row) => { 9 // row should have type information now! 10})
See the docs
directory for full documentation.
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:
bun:sqlite
is a high-performance builtin SQLite3 module for bun.js
.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.
The MIT License © 2020-present Kriasoft / Theo Gravity. All rights reserved.
Made with ♥ by Konstantin Tarkus (@koistya), Theo Gravity and contributors
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
7 existing vulnerabilities detected
Details
Reason
Found 5/26 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-25
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More