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 🙏

© 2025 – Pkg Stats / Ryan Hefner

nestjs-better-sqlite3

v0.0.1

Published

NestJS module for Better SQLite3

Downloads

7

Readme

NestJS Better SQLite3

Minimal SQLite3 module for NestJS with type safety.

Motivation: ORM's are great at abstracting a lot of complexity but often introduce bugs and produce slow queries; In addition, I rarely find myself wanting to switch between different databases, so I figured I write a lightweight wrapper around better-sqlite3.

Features

  • Some query helpers (create, findOne, findAll, update, delete, count)
  • Migrations (SQL)
  • Type safe
  • Prevent SQL Injections

Setup

npm install nestjs-better-sqlite3

Configuration

// Basic
@Module({
    imports: [Sqlite3Module.forRoot({ dataSource: 'sqlite.db' })]
})

// With options
@Module({
    imports: [Sqlite3Module.forRoot({
        dataSource: 'app.db',
        enableWal: true,
        runMigrations: true,
        migrationsDir: './migrations',
        entities: [User, Post]
    })]
})

Migrations

Migrations are organized in folders with up.sql and down.sql files:

migrations/
├── 1732471234-create-users/
│   ├── up.sql
│   └── down.sql
└── 1732471456-create-posts/
    ├── up.sql
    └── down.sql

Example migration files:

-- migrations/1732471234-create-users/up.sql
CREATE TABLE users (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    email TEXT UNIQUE NOT NULL
);
-- migrations/1732471234-create-users/down.sql
DROP TABLE users;

CLI Commands

# Generate new migration
npx sqlite3-migrate migration:generate create-users

# Run pending migrations
npx sqlite3-migrate migration:run

# Revert last migration
npx sqlite3-migrate migration:revert

# Custom paths
npx sqlite3-migrate migration:run --db app.db --migrations ./db/migrations

Usage

Entities

@Entity('users')
class User {
    id: number
    name: string
    email: string
}

Repository Methods

const repo = this.db.getRepository(User)

// CRUD operations
repo.create({ name: 'John', email: '[email protected]' })
repo.findAll({ limit: 10, offset: 5, orderBy: { column: 'name', direction: 'ASC' } })
repo.findOne({ email: '[email protected]' })
repo.update({ id: 1 }, { name: 'Jane' })
repo.delete({ id: 1 })
repo.count({ active: true })

Raw Queries

// Type-safe raw queries
this.db.query<User>('SELECT * FROM users WHERE age > ?', [18])
this.db.queryOne<User>('SELECT * FROM users WHERE id = ?', [1])
this.db.execute('INSERT INTO users (name) VALUES (?)', ['Alice'])

Transactions

this.db.transaction((db) => {
    const stmt = db.prepare('INSERT INTO users (name) VALUES (?)')
    stmt.run('User 1')
    stmt.run('User 2')
})

Tests

npm run test

TODO

  • [ ] The Better SQLite 3 logic should be part of a generic library