npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

fastify-sqlite

v2.0.0

Published

Fastify plugin to connect to a SQLite database using the built-in node:sqlite module

Readme

fastify-sqlite

ci npm neostandard javascript style

Fastify plugin to connect to a SQLite database.

Under the hood, this plugin uses the built-in node:sqlite module that ships with Node.js. It works on Node.js >=22.5.0 where node:sqlite is available as an experimental feature.

Install

npm install fastify-sqlite

Compatibility

| Plugin version | Fastify version | Node.js version | | ------------- |:---------------:|:---------------:| | ^2.0.0 | ^5.0.0 | >=22.5.0 | | ^1.0.0 | ^4.0.0 | >=18 |

Usage

const Fastify = require('fastify')
const fastifySqlite = require('fastify-sqlite')

async function main () {
  const app = Fastify()
  app.register(fastifySqlite, {
    dbFile: 'foo.db'
  })
  await app.ready()

  // node:sqlite exposes a synchronous API.
  // Prepare it, then run the query.
  const rows = app.sqlite.prepare('SELECT * FROM myTable').all()
  console.log(rows)
}

main()

Check the node:sqlite documentation to see all the methods available on DatabaseSync and StatementSync.

Options

You can pass the following options to the plugin:

await app.register(require('fastify-sqlite'), {
  dbFile: ':memory:', // path to the DB file. Default ':memory:'
  name: 'mydb',       // optional decorator name. Default null
  readOnly: true,     // open the database in read-only mode. Default false
  verbose: true       // log every SQL statement via fastify.log.trace. Default false
})

When the name option is set, the database is exposed at fastify.sqlite[name] instead of fastify.sqlite:

await app.register(require('fastify-sqlite'), { name: 'main' })
app.sqlite.main.prepare('SELECT 1').get()

Verbose mode

When verbose: true, every SQL statement is forwarded to fastify.log.trace({ sql }):

app.register(fastifySqlite, { verbose: true })
app.sqlite.exec('CREATE TABLE foo (id INT)')
// fastify.log.trace receives { sql: 'CREATE TABLE foo (id INT)' }

Both db.exec(sql) and db.prepare(sql).run() / .all() / .get() / .iterate() are traced.

Migrations

The plugin decorates fastify.migrateSqlite(db, options). Pass the database instance to migrate (typically app.sqlite, or app.sqlite.mydb when the name option is used) and the directory holding the migration files:

const ran = app.migrateSqlite(app.sqlite, {
  migrationsPath: './migrations'
})
console.log(ran) // ['001-init.sql', '002-data.sql']
  • Migration files must end with .sql and are executed in alphabetical order.
  • Each file may include a -- Down marker to separate the rollback section from the upgrade section; only the upgrade section is executed.
  • A _migrations table tracks which files have already run, so calling migrateSqlite again is a no-op.
  • Each migration runs in its own transaction. If a migration fails, only that one is rolled back; previously committed migrations stay committed.
  • The call returns the array of files that have been executed (synchronously).

TypeScript

The plugin ships with index.d.ts. The database decorator is typed as DatabaseSync | Record<string, DatabaseSync> | undefined and the options as SqlitePluginOptions:

import Fastify from 'fastify'
import sqlitePlugin, { SqlitePluginOptions } from 'fastify-sqlite'

const app = Fastify()
app.register(sqlitePlugin, {
  dbFile: 'foo.db',
  name: 'main',
  readOnly: false
} satisfies SqlitePluginOptions)

License

Copyright Manuel Spigolon, Licensed under MIT.