dbcube
v5.2.10
Published
DBCube ORM: the fastest way to work with MySQL, PostgreSQL, SQLite and MongoDB in Node.js — daemon-powered query engine (sub-millisecond queries), fluent query builder, transactions, eager loading and .cube schema files.
Downloads
2,491
Maintainers
Keywords
Readme
dbcube
The fastest way to work with MySQL, PostgreSQL, SQLite and MongoDB in Node.js.
DBCube runs your queries through a persistent Rust engine (daemon mode) with warm connection pools — sub-millisecond query dispatch instead of paying process-spawn overhead, plus a fluent query builder, real transactions and schema-as-code with .cube files.
Installation
npm install dbcube
npx dbcube init # scaffolds dbcube.config.js + dbcube/ + example schemaConfiguration
dbcube.config.js in your project root:
module.exports = function (config) {
config.set({
databases: {
myapp: {
type: "mysql", // mysql | postgres | sqlite | mongodb
config: {
HOST: process.env.DB_HOST,
USER: process.env.DB_USER,
PASSWORD: process.env.DB_PASSWORD,
DATABASE: process.env.DB_NAME,
PORT: 3306
}
}
}
});
};Usage
const dbcube = require('dbcube');
const db = dbcube.database('myapp');
// Queries
const users = await db.table('users')
.where('status', '=', 'active')
.orderBy('created_at', 'DESC')
.limit(10)
.get();
// Writes (update/delete always require a WHERE)
await db.table('users').insert([{ name: 'Ada', email: '[email protected]' }]);
await db.table('users').where('id', '=', 1).update({ status: 'inactive' });
// Transactions — everything commits or everything rolls back
await db.transaction(async (trx) => {
await trx.table('accounts').where('id', '=', 1).update({ balance: 50 });
await trx.table('accounts').where('id', '=', 2).update({ balance: 150 });
});
// Raw SQL escape hatch (bound parameters)
const rows = await db.raw('SELECT * FROM users WHERE age > ?', [25]);
// Eager loading from your .cube foreign keys — one batched query, no N+1
const usersWithOrders = await db.table('users').with('orders').get();
// Pagination, upsert, atomic counters…
const page = await db.table('posts').paginate(2, 20);
await db.table('settings').upsert([{ key: 'theme', value: 'dark' }], ['key']);
await db.table('products').where('id', '=', 7).increment('stock', 5);Advanced configuration
Tune the connection pool and daemon per database:
myapp: {
type: "mysql",
config: { /* ... */ },
pool: {
maxConnections: 20, // default 5 (sqlite: 10)
minConnections: 4, // default 2
acquireTimeoutMs: 5000, // default 3000
idleTimeoutMs: 600000 // default 3600000
},
daemon: {
enabled: true, // false → one-shot mode (transactions need the daemon)
requestTimeoutMs: 60000 // default 30000
}
}TypeScript
Generate interfaces from your schema and get fully typed queries:
// npx dbcube generate → dbcube/types.ts
import type { User } from './dbcube/types';
const users = await db.table<User>('users').where('age', '>', 25).get(); // User[]
const one = await db.table<User>('users').first(); // User | nullHighlights
- Daemon mode: persistent TCP engine, ~0.3 ms per query (disable with
DBCUBE_DAEMON=0) - Transactions with automatic rollback on error — MySQL, PostgreSQL, SQLite and MongoDB (replica set)
- Schema as code: define tables in
.table.cubefiles, apply withnpx dbcube run table:refresh - Migrations with history and rollback (
.alter.cube+migrate:status/migrate:rollback) - TypeScript types generated from your schema:
npx dbcube generate - Aggregations (
count/sum/avg/max/min),exists(),chunk(),whereIn/whereNotIn,having,selectRaw, computed fields and runtime triggers - Runtime triggers scale horizontally: they run in JS in the process that performs the write, so with N service replicas each operation fires its trigger exactly once — no distributed coordination needed
Documentation
Full docs: https://dbcube.org — CLI reference: npx dbcube help
License
MIT
