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

smart-sqlite3-filter

v1.0.0

Published

Query a sqlite3 database using node:sqlite in a way like 'firstName:luc'.

Readme

smart-sqlite3-filter

NPM version build status Test coverage npm download

This package allows querying a sqlite3 database using the built-in node:sqlite module in a way like 'firstName:luc'.

Installation

$ npm i smart-sqlite3-filter

The database is a DatabaseSync instance from Node.js's built-in node:sqlite module, so there is no native dependency to compile. This requires Node.js 22.13 or later, the first version exposing node:sqlite without the --experimental-sqlite flag.

Usage

import { DatabaseSync } from 'node:sqlite';

import { search } from 'smart-sqlite3-filter';

const db = new DatabaseSync(':memory:');
const sql = `
  CREATE TABLE IF NOT EXISTS names (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    year INTEGER NOT NULL,
    age REAL NOT NULL
  );

  INSERT INTO names (name, year, age) VALUES
    ('John', 1990, 30.1),
    ('Jane', 1985, 29.7),
    ('Alice', 2000, 25),
    ('Bob', 1990, 43);
`;
db.exec(sql);

// We don't search in a specific column, it will search everywhere
// For string column it is case insensitive and should startWith, for number it should be the exact value
search('John', db); // [{name: 'John', year: 1990}]

search('1990', db); // [{name: 'John', year: 1990}, {name: 'Bob', year: 1990}]

search('J', db); // [{name: 'John', year: 1990}, {name: 'Jane', year: 1985}]

// we search in a specific field and use the operator '>'. For numbers the following operators can be used: '>', '>=', '<', '<=', '<>', '!=', '='. Default to 'starts with'
search('year:>1990', db); // [{name: 'Alice', year: 2000}]
search('year:!=1990,2000', db); // [{name: 'Jane', year: 1985}]
search('year:<>1990,2000', db); // [{name: 'Jane', year: 1985}]

search('age:30', db); // by default we take into account significative digits and it will search between 29.5 and 30.5
search('age:=30', db); // must be exactly 30, no hit
search('age:=25', db); // must be exactly 25, 1 hit

// when searching for a string we can use the following operators: '^' (starts with), '$' (ends with), '~' (contains), '='. Default to contains. When searching for '=' it is case sensitive otherwise it is not.
search('name:~o', db); // [{name: 'John', year: 1990}, {name: 'Bob', year: 1990}]
search('name:$e', db); // [{name: 'Jane', year: 1985}, {name: 'Alice', year: 2000}]
search('name:^J', db); // [{name: 'John', year: 1990}, {name: 'Jane', year: 1985}]

// A field may have various values separated by ','
search('year:1990,2000', db); // [{name: 'John', year: 1990}, {name: 'Bob', year: 1990}, {name: 'Alice', year: 2000}]
search('year:1990,2000 name:$e,n', db); // [{name: 'John', year: 1990}, {name: 'Alice', year: 2000}]
search('year:!=1990,2000', db); // [{name: 'Jane', year: 1985}]

// It is possible by searching for a range of values using '..'
search('year:1980..1987', db); // [{name: 'Jane', year: 1985}]

Pagination, columns and totals

search accepts limit (default 1000), from to skip results, and columns to avoid reading columns you don't need. count returns the number of matching entries, ignoring limit / from.

import { count, search } from 'smart-sqlite3-filter';

search('year:>1985', db, { orderBy: 'name', limit: 2, from: 2 }); // second page
search('year:>1985', db, { columns: ['name', 'year'] }); // no SELECT *
count('year:>1985', db); // 3

Composing with your own statement

buildWhere returns the WHERE condition and its bound parameters without running anything, so the filter can go inside a statement search cannot express: your own joins, a subquery, a COUNT(*), a window function, …

The emitted parameters are named, so they merge with your own without colliding on position.

import { buildWhere } from 'smart-sqlite3-filter';

const { where, values } = buildWhere('name:~ester mw:100..500', db, {
  tableName: 'molecules',
});

// one page, plus the exact total
const page = db
  .prepare(
    `SELECT id, name, mw FROM molecules WHERE ${where}
     ORDER BY mw DESC LIMIT :limit OFFSET :offset`,
  )
  .all({ ...values, limit: 50, offset: 0 });

const { total } = db
  .prepare(`SELECT COUNT(*) AS total FROM molecules WHERE ${where}`)
  .get(values);

// or as a subquery driving a join — nothing is materialised
db.prepare(
  `SELECT s.* FROM (SELECT id FROM molecules WHERE ${where}) c
   JOIN some_index s ON s.entry_id = c.id`,
).all(values);

where is an empty string when the query string carries no criteria, so guard it with `${where || '1 = 1'}` when the query may be empty.

Usage with BSON

import { DatabaseSync } from 'node:sqlite';

import { search } from 'smart-sqlite3-filter';

const db = new DatabaseSync(':memory:');

// create data and add some dummy data
const sql = `
    CREATE TABLE IF NOT EXISTS bsons (
      id INTEGER PRIMARY KEY AUTOINCREMENT,
      name TEXT NOT NULL,
      year INTEGER NOT NULL,
      bson BLOB NOT NULL
    );

    INSERT INTO bsons (name, year, bson) VALUES
    ('John', 1990, jsonb('{"name": "John", "year": 1990}')),
    ('Jane', 1985, jsonb('{"name": "Jane", "year": 1985}')),
    ('Alice', 2000, jsonb('{"name": "Alice", "year": 2000}')),
    ('Bob', 1990, jsonb('{"name": "Bob", "year": 1990}'));
  `;

db.exec(sql);

search('name:$e,n', db); // 3 hits
search('year:1990,2000 name:$e,n', db); // 2 hits
search('bson.year:1990', db); // 2 hits
search('bson.name:$e', db); // 2 hits
search('bson.name:$e,n', db); // 3 hits
search('bson.year:1990,2000 bson.name:$e,n', db); // 2 hits

License

MIT