@beenotung/better-sqlite3-helper

中文文档
4.1.10发布于 2026/10/03MITCommonJS近 30 天下载 4998

A wrapper library that eases the work with better-sqlite3 with some new functions and a migration-system

原始使用文档
文档目录15 节
  1. New in Version 4.x
  2. New in Version 3.0
  3. New in Version 2.0
  4. How to install
  5. How to use
  6. One global instance
  7. New Functions
  8. Insert, Update and Replace
  9. Update
  10. Insert and replace
  11. Delete
  12. Try and catch
  13. Migrations
  14. More Documentation of better-sqlite3
  15. License

@beenotung/better-sqlite3-helper

npm Package Version

A nodejs wrapper library for the work with better-sqlite3 ("The fastest and simplest library for SQLite3 in Node.js"). It's intended for simple server-apps for nodejs and offer some new functions and a migration-system.

This package is forked from better-sqlite3-helper by Kauto. Most changes were back-ported but some pull requests are still pending.

New in Version 4.x

  • breaking: forked into @beenotung/better-sqlite3-helper for npm release
  • breaking: better-sqlite3 Version 12 is now used.
  • feat: @types/better-sqlite3 is added as optional and peer dependencies
  • feat: auto setup synchronous pragma when establish connection to database
  • patch: fix type signature for creating new instance of DB
  • chore: no longer depends on mkdirp, using the built-in version from fs instead

New in Version 3.0

better-sqlite3 Version 7 is now used. This means that the option "memory" is removed (use path :memory: instead - worked in version 2 too) and support for Node.js versions < 10 is dropped. For older node versions you can continue using version 2 of this library.

New in Version 2.0

All commands of better-sqlite3 Version 5 (like function and backup) can now be used too. Commands for Version 4 are removed. In addition there is now a TypeScript Declaration File for this library.

How to install

Install it for example with

npm i @beenotung/better-sqlite3-helper

How to use

In every file you want access to a sqlite3 database simply require the library and use it right away.

anyServerFile.js
const DB = require('@beenotung/better-sqlite3-helper')

let row = DB().queryFirstRow('SELECT * FROM users WHERE id=?', userId);
console.log(row.firstName, row.lastName, row.email);

To setup your database, create a sql-file named 001-init.sql in a migrations-directory in the root-directory of your program.

~/migrations/001-init.sql
-- Up
CREATE TABLE `users` (
  id INTEGER PRIMARY KEY,
  firstName TEXT NOT NULL,
  lastName TEXT NOT NULL,
  email TEXT NOT NULL
);

-- Down
DROP TABLE IF EXISTS `users`;

And that's it!

One global instance

A normal, simple application is mostly working with only one database. To make the class management more easy, this library does the access-control for you - mainly as a singleton. (But you can create a new instance to access other databases.)

The database loads lazy. Only when it's used for the first time, the database is read from the file, the migration is started and the journal-mode WAL is set. The default directory of the database is './data/sqlite3.db'.

If you want to change the default-values, you can do this by calling the library once in the beginning of your server-code and thus setting it up:

index.js
const DB = require('@beenotung/better-sqlite3-helper')

// The first call creates the global instance with your settings
DB({
  path: './data/sqlite3.db', // this is the default
  readonly: false, // read only
  fileMustExist: false, // throw error if database not exists
  WAL: true, // automatically enable 'PRAGMA journal_mode = WAL'
  migrate: {  // disable completely by setting `migrate: false`
    force: false, // set to 'last' to automatically reapply the last migration-file
    table: 'migration', // name of the database table that is used to keep track
    migrationsPath: './migrations' // path of the migration-files
  }
})

After that you can use the library without parameter:

anotherAPIFile.js
const DB = require('@beenotung/better-sqlite3-helper')

// a second call directly returns the global instance
let row = DB().queryFirstRow('SELECT * FROM users WHERE id=?', userId);
console.log(row.firstName, row.lastName, row.email);

New Functions

This class implements shorthand methods for better-sqlite3.

// shorthand for db.prepare('SELECT * FROM users').all();
let allUsers = DB().query('SELECT * FROM users');
// result: [{id: 1, firstName: 'a', lastName: 'b', email: '[email protected]'},{},...]
// result for no result: []

// shorthand for db.prepare('SELECT * FROM users WHERE id=?').get(userId);
let row = DB().queryFirstRow('SELECT * FROM users WHERE id=?', userId);
// result: {id: 1, firstName: 'a', lastName: 'b', email: '[email protected]'}
// result for no result: undefined

// shorthand for db.prepare('SELECT * FROM users WHERE id=?').get(999) || {};
let {id, firstname} = DB().queryFirstRowObject('SELECT * FROM users WHERE id=?', userId);
// result: id = 1; firstName = 'a'
// result for no result: id = undefined; firstName = undefined

// shorthand for db.prepare('SELECT * FROM users WHERE id=?').pluck(true).get(userId);
let email = DB().queryFirstCell('SELECT email FROM users WHERE id=?', userId);
// result: '[email protected]'
// result for no result: undefined

// shorthand for db.prepare('SELECT * FROM users').all().map(e => e.email);
let emails = DB().queryColumn('email', 'SELECT email FROM users');
// result: ['[email protected]', '[email protected]', ...]
// result for no result: []

// shorthand for db.prepare('SELECT * FROM users').all().reduce((o, e) => {o[e.lastName] = e.email; return o;}, {});
let emailsByLastName = DB().queryKeyAndColumn('lastName', 'email', 'SELECT lastName, name FROM users');
// result: {b: '[email protected]', c: '[email protected]', ...}
// result for no result: {}

Insert, Update and Replace

There are shorthands for update, insert, replace and delete. They are intended to make programming of CRUD-Rest-API-functions easier. With a blacklist or a whitelist it's even possible to send a request's query (or body) directly into the database.

Update

// const numberOfChangedRows = DB().update(table, data, where, whitelist = undefined)

// simple use with a object as where and no whitelist
DB().update('users', {
  lastName: 'Mustermann',
  firstName: 'Max'
}, {
  email: '[email protected]'
})

// data from a request and a array as a where and only editing of lastName and firstName is allowed
DB().update('users', req.body, ['email = ?', req.body.email], ['lastName', 'firstName'])


// update with blacklist (id and email is not allowed; only valid columns of the table are allowed) and where is a shorthand for ['id = ?', req.body.id]
DB().updateWithBlackList('users', req.body, req.body.id, ['id', 'email'])

Insert and replace

// const lastInsertID = DB().insert(table, datas, whitelist = undefined)
// const lastInsertID = DB().replace(table, datas, whitelist = undefined)

// simple use with an object and no whitelist
DB().insert('users', {
  lastName: 'Mustermann',
  firstName: 'Max',
  email: '[email protected]'
})

// inserting two users
DB().insert('users', [{
  lastName: 'Mustermann',
  firstName: 'Max',
  email: '[email protected]'
}, {
  lastName: 'Mustermann2',
  firstName: 'Max2',
  email: '[email protected]'
}])

// data from a request and only lastName and firstName are set
DB().replace('users', req.body, ['lastName', 'firstName'])


// replace with blacklist (id and email is not allowed; only valid columns of the table are allowed)
DB().replaceWithBlackList('users', req.body, ['id', 'email']) // or insertWithBlackList

Delete

//delete the user with an id of 4
DB().delete('users', {id: 4})

Try and catch

If you want to put invalid values into the database, the functions will throw an error. So don't forget to surround the functions with a try-catch. Here is an example for an express-server:

const { Router } = require('express')
const bodyParser = require('body-parser')
const DB = require('@beenotung/better-sqlite3-helper')

router.patch('/user/:id', bodyParser.json(), function (req, res, next) {
  try {
    if (!req.params.id) {
      res.status(400).json({error: 'missing id'})
      return
    }
    DB().updateWithBlackList(
      'users',
      req.body,
      req.params.id,
      ['id']
    )

    res.statusCode(200)
  } catch (e) {
    console.error(e)
    res.status(503).json({error: e.message})
  }
})

Migrations

The migration in this library mimics the migration system of the excellent sqlite by Kriasoft.

To use this feature you have to create a migrations-directory in your root. Inside you create sql-files that are separated in a up- and a down-part:

migrations/001-initial-schema.sql
-- Up
CREATE TABLE Category (id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE Post (id INTEGER PRIMARY KEY, categoryId INTEGER, title TEXT,
  CONSTRAINT Post_fk_categoryId FOREIGN KEY (categoryId)
    REFERENCES Category (id) ON UPDATE CASCADE ON DELETE CASCADE);
INSERT INTO Category (id, name) VALUES (1, 'Business');
INSERT INTO Category (id, name) VALUES (2, 'Technology');

-- Down
DROP TABLE Category
DROP TABLE Post;
migrations/002-missing-index.sql
-- Up
CREATE INDEX Post_ix_categoryId ON Post (categoryId);

-- Down
DROP INDEX Post_ix_categoryId;

The files need to be numbered. They are automatically executed before the first use of the database.

NOTE: For the development environment, while working on the database schema, you may want to set force: 'last' (default false) that will force the migration API to rollback and re-apply the latest migration over again each time when Node.js app launches. See "Global Instance".

You can also give an array of changes.

const DB = require('@beenotung/better-sqlite3-helper')

const db = new DB({
  migrate: {
    migrations: [
      `-- Up
      CREATE TABLE Setting (
        key TEXT NOT NULL UNIQUE,
        value BLOB,
        type INT NOT NULL DEFAULT 0,
        PRIMARY KEY(key)
      );
      CREATE INDEX IF NOT EXISTS Setting_index_key ON Setting (key);

      -- Down
      DROP INDEX IF EXISTS Setting_index_key;
      DROP TABLE IF EXISTS Setting;
      `,
      `-- Up
      INSERT INTO Setting (key, value, type) VALUES ('test', 'now', 0);
      INSERT INTO Setting (key, value, type) VALUES ('testtest', 'nownow', 6);

      -- Down
      DELETE FROM Setting WHERE key = 'test';
      DELETE FROM Setting WHERE key = 'testtest';
      `
    ]
  }
})

More Documentation of better-sqlite3

License

MIT

版本与发布记录

@beenotung/better-sqlite3-helper 共发布 15 个版本,首次发布于 2023/03/13;过去 12 个月发布了 2 个版本。

版本发布日期距今
4.1.10latest2026/10/031 个月前
4.1.92026/01/269 个月前
4.1.82025/10/071.1 年前
4.1.72024/06/182 年前
4.1.62024/06/122 年前
4.1.52024/05/272 年前

关于 @beenotung/better-sqlite3-helper 的常见问题

@beenotung/better-sqlite3-helper 是什么?

A wrapper library that eases the work with better-sqlite3 with some new functions and a migration-system

如何安装 @beenotung/better-sqlite3-helper?

在项目目录运行 npm install @beenotung/better-sqlite3-helper,或使用 pnpm add / yarn add / bun add。

@beenotung/better-sqlite3-helper 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/beenotung__better-sqlite3-helper 是否存在。

@beenotung/better-sqlite3-helper 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@beenotung/better-sqlite3-helper 可以商用吗?

@beenotung/better-sqlite3-helper 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@beenotung/better-sqlite3-helper 的下载量有多少?

根据 npm 官方下载统计,@beenotung/better-sqlite3-helper 在 2026/10/08 – 2026/11/06 这 30 天内被下载 4,998 次,日均约 167 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@beenotung/better-sqlite3-helper 还在维护吗?

最新版本 4.1.10 发布于 2026/10/03(1 个月前);至今共发布 15 个版本,其中过去 12 个月发布了 2 个;首次发布于 2023/03/13。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。