Registry / database / ruoyi-eggjs-sqlite

ruoyi-eggjs-sqlite

JSON →
library1.1.9jsnpmunverified

An Egg.js plugin for SQLite database operations, built on top of better-sqlite3. Version 1.1.9 supports Egg.js 2.x and 3.x, with multi-instance configuration, optional snake_case-to-camelCase field conversion (since v1.1.6), built-in transaction support with automatic rollback, SQL execution time logging in development, and error messages including the executed SQL. It provides a simple API: select, selects, insert, update, del, run. The plugin uses synchronous better-sqlite3 API wrapped in promises for Egg.js async context. Key differentiators include automatic camelCase conversion (opt-in) and multi-database support via clients config.

npm install ruoyi-eggjs-sqlite
INSTALL
IMPORT
SIG · RUOYI-EGGJS-SQLITE
R
ruoyi-eggjs-sqlite
databasejavascriptv1.1.9
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

sqlite
// In config/plugin.js: exports.sqlite = { enable: true, package: 'ruoyi-eggjs-sqlite' }; // In controller/service: const { app } = this; app.sqlite.select(...)
const sqlite = require('ruoyi-eggjs-sqlite'); // Wrong: plugin is loaded via Egg.js plugin system, not direct require
This is an Egg.js plugin; it must be enabled in plugin.js and accessed via app.sqlite (single instance) or app.sqlite.get(name) (multi-instance). Direct require will not work.
app.sqlite
const { app } = this; await app.sqlite.select('SELECT 1');
app.sqlite.select('SELECT 1').then(...) // No need for .then() because methods return promises; use await
Methods return Promises (async), even though better-sqlite3 is synchronous under the hood.
app.sqlite.get
const db1 = app.sqlite.get('db1'); await db1.select('SELECT * FROM users');
app.sqlite.db1.select(...) // Wrong: use .get() to retrieve named instances
For multi-instance setups, retrieve instances via app.sqlite.get(name). The instance has the same API (select, selects, insert, etc.).

Demonstrates plugin setup in config/plugin.js and config/config.default.js, then uses app.sqlite methods: run, insert, select, selects in a controller.

// config/plugin.js exports.sqlite = { enable: true, package: 'ruoyi-eggjs-sqlite', }; // config/config.default.js const path = require('path'); exports.sqlite = { client: { path: path.join(__dirname, '../database.db'), // or ':memory:' for in-memory options: {}, }, }; // app/controller/home.js const Controller = require('egg').Controller; class HomeController extends Controller { async index() { const { app, ctx } = this; // Create table await app.sqlite.run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)'); // Insert const rowid = await app.sqlite.insert("INSERT INTO users (name, age) VALUES ('Alice', 30)"); // Select single const user = await app.sqlite.select(`SELECT * FROM users WHERE id = ${rowid}`); // Select all const users = await app.sqlite.selects('SELECT * FROM users'); ctx.body = { user, users }; } } module.exports = HomeController;
Debug
Known issues
gotchaSQL injection risk: methods like select() and insert() do not use parameterized queries; passing unsanitized user input directly is dangerous.
fix
Use better-sqlite3 prepared statements via app.sqlite.run() with placeholders? Actually the plugin's API does not expose prepared statement binding. Consider using raw better-sqlite3 via app.sqlite._db for parameterized queries: app.sqlite._db.prepare('SELECT * FROM users WHERE id = ?').get(userInput). Alternatively, escape inputs or use a query builder.
affects: >=1.0.0
gotchaMethods are asynchronous (Promise-based) but the underlying better-sqlite3 API is synchronous. This means SQL runs synchronously in the current microtask, which can block the event loop for large queries.
fix
For heavy queries, consider offloading to a worker or using a truly async SQLite library like sql.js (WebAssembly) or better-sqlite3's synchronous nature is actually a performance benefit for many use cases, but be aware it can block. No direct fix needed, just be mindful.
affects: >=1.0.0
deprecatedcamelCase conversion was introduced in v1.1.6; prior versions return snake_case field names only.
fix
Upgrade to v1.1.6 or later and set camelCase: true in config if you want automatic conversion.
affects: <1.1.6
gotchaMulti-instance config uses 'clients' (plural) object with named entries, while single instance uses 'client' (singular). Misconfiguring can lead to 'Cannot read property of undefined' or using the default client incorrectly.
fix
Ensure you use exactly one of 'client' (single) or 'clients' (multi) in config. Do not set both unless you intend to have a default client AND named clients (not documented).
affects: >=1.0.0
gotchaMemory database with path ':memory:' works, but different 'clients' with ':memory:' create separate in-memory databases. They do not share data.
fix
If you need a shared in-memory database across instances, use a single client with path ':memory:'.
affects: >=1.0.0
Errors
Common errors & fixes
TypeError: app.sqlite.select is not a function
Plugin may not be enabled, or using wrong instance (e.g., app.sqlite.get('name') for multi-instance).
fix
Ensure plugin is enabled in config/plugin.js. If using multi-instance, call app.sqlite.get('dbName').select(...). For single instance, ensure config uses 'client' not 'clients'.
Error: Cannot find module 'better-sqlite3'
better-sqlite3 is a dependency of ruoyi-eggjs-sqlite but may fail to install on some systems (requires native compilation).
fix
Install build tools: `npm install -g node-gyp`. On Windows, install windows-build-tools. Alternatively, use `npm install --build-from-source` or try a different SQLite plugin.
Error: SQLITE_ERROR: no such table: users
Table does not exist; need to create it first.
fix
Run `app.sqlite.run('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT)')` before querying.
Error: Cannot convert undefined or null to object
camelCase: true but result is null/undefined; conversion code expects an object.
fix
Check if the query returned undefined (single row select with no match). Handle null/undefined before conversion, or disable camelCase.
Upgrade
Version history
1.1.9latest on npm
Audit
Dependencies
better-sqlite3requiredCore SQLite3 driver used for all database operations
Agent activity
15 hits · last 30 days
node
12
Meta
1
Resources
ruoyi-eggjs-sqlite — npm install ruoyi-eggjs-sqlite · libregistry