LokiJS Adapter
The LokiAdapter is backed by the real LokiJS library — a fast, in-memory document database with optional persistence. It's the simplest adapter — no native dependencies, works in Node.js, browsers, and React Native.
Usage
import { Database, LokiAdapter } from 'pomegranate-db';
const db = new Database({
adapter: new LokiAdapter({ databaseName: 'myapp' }),
models: [Post, Comment],
});
When to Use
- Jest tests — fast, deterministic, no native setup
- Prototyping — get started immediately
- Web applications — with or without persistence
- Development — quick iteration without native builds
Persistence
By default, LokiAdapter runs in-memory and data is lost when the process exits. You can enable persistence by passing a LokiJS persistence adapter:
Browser (IndexedDB):
import LokiIndexedAdapter from 'lokijs/src/loki-indexed-adapter';
const adapter = new LokiAdapter({
databaseName: 'myapp',
persistenceAdapter: new LokiIndexedAdapter(),
autoSave: true,
autoSaveInterval: 5000, // ms
});
Node.js (filesystem):
const LokiFsAdapter = require('lokijs/src/loki-fs-structured-adapter');
const adapter = new LokiAdapter({
databaseName: 'myapp',
persistenceAdapter: new LokiFsAdapter(),
autoSave: true,
});
Limitations
- Not suitable for very large datasets — everything lives in memory (persistence adapters save/load the full DB)
- No encryption — use the
EncryptingAdapterwrapper frompomegranate-db/encryptionfor at-rest encryption
With Encryption
Wrap LokiAdapter with EncryptingAdapter for AES-GCM encryption:
import { LokiAdapter } from 'pomegranate-db';
import { EncryptingAdapter } from 'pomegranate-db/encryption/react-native';
const key = new TextEncoder().encode('0123456789abcdef0123456789abcdef');
const adapter = new EncryptingAdapter(
new LokiAdapter({ databaseName: 'myapp' }),
async () => key,
);
Configuration
interface LokiAdapterConfig {
/** Name of the database (used as filename when persistence is enabled) */
databaseName: string;
/** Optional: provide your own Loki instance */
lokiInstance?: LokiInstance;
/** Optional: LokiJS persistence adapter (e.g. LokiIndexedAdapter, LokiFsAdapter) */
persistenceAdapter?: unknown;
/** Enable auto-save. Default: false */
autoSave?: boolean;
/** Auto-save interval in ms. Default: 5000 */
autoSaveInterval?: number;
}