Loading...
Loading...
Instructions for using the DeepBase multi-driver persistence library. Use when a task requires data persistence, storage abstraction, multi-backend setups, data migration between drivers, or integrating DeepBase into a Node.js project.
npx skill4agent add clasen/deepbase deepbase# Core package (includes JSON driver by default)
npm install deepbase
# Optional drivers — install only what you need
npm install deepbase-sqlite # SQLite (better-sqlite3)
npm install deepbase-mongodb # MongoDB
npm install deepbase-redis # Redis (vanilla)
npm install deepbase-redis-json # Redis Stack (RedisJSON)
npm install deepbase-indexeddb # Browser IndexedDBdeepbasedeepbase-jsonnanoid// ES Modules (recommended)
import DeepBase from 'deepbase';
import { DeepBase, DeepBaseDriver } from 'deepbase';
// CommonJS
const { DeepBase } = require('deepbase');
// Individual drivers
import { JsonDriver } from 'deepbase-json';
import { MongoDriver } from 'deepbase-mongodb';
import { SqliteDriver } from 'deepbase-sqlite';
import { RedisDriver } from 'deepbase-redis';
import { RedisDriver as RedisJsonDriver } from 'deepbase-redis-json';
import { IndexedDBDriver } from 'deepbase-indexeddb';import DeepBase from 'deepbase';
const db = new DeepBase({ path: './data', name: 'app' });
await db.connect();
await db.set('config', 'theme', 'dark');
const theme = await db.get('config', 'theme'); // 'dark'
await db.disconnect();connect()import DeepBase from 'deepbase';
import { JsonDriver } from 'deepbase-json';
import { MongoDriver } from 'deepbase-mongodb';
const db = new DeepBase([
new MongoDriver({ url: 'mongodb://localhost:27017', database: 'myapp', collection: 'data' }),
new JsonDriver({ path: './backup', name: 'fallback' })
], {
writeAll: true, // Write to all drivers (default: true)
readFirst: true, // Read from first available (default: true)
failOnPrimaryError: false // Continue if primary fails
});
await db.connect();
// Writes go to both MongoDB and JSON; reads try MongoDB first, fall back to JSON| Method | Signature | Description |
|---|---|---|
| | Get value at path. Returns |
| | Set value at path. Last argument is the value. |
| | Delete value at path. |
| | Increment numeric value. |
| | Decrement numeric value. |
| | Add item with auto-generated ID. Returns full path array. |
| | Atomic update — passes current value to |
| Method | Signature | Description |
|---|---|---|
| | Get keys at path (array of strings). |
| | Get values at path. |
| | Get |
| | Remove and return the last item. |
| | Remove and return the first item. |
| Method | Description |
|---|---|
| Connect all drivers. Returns |
| Disconnect all drivers. |
| Method | Description |
|---|---|
| Get driver instance by index (default: 0). |
| Get array of all driver instances. |
// Migrate data from driver 0 to driver 1
await db.migrate(0, 1, {
clear: true, // Clear target first (default: true)
batchSize: 100, // Progress callback interval
onProgress: ({ migrated, errors, current }) => console.log(`${migrated} items`)
});
// Sync primary (index 0) to all other drivers
await db.syncAll({ clear: true });new DeepBase(drivers, {
writeAll: true, // Write to all drivers
readFirst: true, // Read from first available driver in order
failOnPrimaryError: true, // Throw if primary driver (index 0) fails
lazyConnect: true, // Auto-connect on first operation
timeout: 0, // Global timeout in ms (0 = disabled)
readTimeout: 0, // Override for read operations
writeTimeout: 0, // Override for write operations
connectTimeout: 0 // Override for connect
});| Driver | Key Options |
|---|---|
| |
| |
| |
| |
| |
| |
package.jsondeepbasenpm installdb.get()db.set()db.connect(){ connected, total }disconnect()add()add()add()await db.get(...userPath)upd()upd()get()set()failOnPrimaryError: falsedeepbase-redis-jsondeepbase-redisawait db.set('users', 'alice', { name: 'Alice', age: 30 });
await db.set('users', 'alice', 'email', 'alice@example.com');
const user = await db.get('users', 'alice');
// { name: 'Alice', age: 30, email: 'alice@example.com' }const userPath = await db.add('users', { name: 'Bob', email: 'bob@example.com' });
// userPath = ['users', 'aB3xK9mL2n']
const user = await db.get(...userPath);await db.set('stats', 'views', 0);
await db.inc('stats', 'views'); // 1
await db.inc('stats', 'views', 10); // 11
await db.dec('stats', 'views', 5); // 6await db.upd('user', 'name', name => name.toUpperCase());const userKeys = await db.keys('users');
const userList = await db.values('users');
const userEntries = await db.entries('users'); // [[id, data], ...]const last = await db.pop('queue'); // Remove and return last item
const first = await db.shift('queue'); // Remove and return first itemimport { JsonDriver } from 'deepbase-json';
import { stringify, parse } from 'flatted';
const db = new DeepBase(new JsonDriver({
path: './data',
name: 'circular',
stringify,
parse
}));const db = new DeepBase([
new MongoDriver({ url: process.env.MONGO_URL, database: 'app' }),
new JsonDriver({ path: './backup' }),
new RedisDriver({ url: process.env.REDIS_URL })
], { writeAll: true, failOnPrimaryError: false });import { DeepBaseDriver } from 'deepbase';
class MyDriver extends DeepBaseDriver {
async connect() { /* ... */ this._connected = true; }
async disconnect() { /* ... */ }
async get(...args) { /* ... */ }
async set(...args) { /* ... */ }
async del(...args) { /* ... */ }
async inc(...args) { /* ... */ }
async dec(...args) { /* ... */ }
async add(...args) { /* ... */ }
async upd(...args) { /* ... */ }
}DeepBaseDriverkeys()values()entries()get()All drivers must extend DeepBaseDrivertimeoutreadTimeoutwriteTimeoutfailOnPrimaryError: trueconnect(){ connected, total }writeAll: truedb.migrate()db.syncAll()db.syncAll()examples/packages/corepackages/driver-jsonpackages/driver-sqlitepackages/driver-mongodbpackages/driver-redispackages/driver-redis-jsonpackages/driver-indexeddb