Node.js SQLite storage adapter for ShareDB using better-sqlite3.
This package provides a Node.js implementation of ShareDB's DurableStorage interface using SQLite. It builds on the shared @sharesync/sharedb-storage-sqlite library and adds Node.js-specific adapters for better-sqlite3.
- ✅ ShareDB DurableStorage - Implements ShareDB's offline storage interface
- ✅ Better-SQLite3 - High-performance synchronous SQLite for Node.js
- ✅ Multiple Schema Strategies - Choose how documents are organized in SQLite
- ✅ Database Attachments - Support for multi-database architectures
- ✅ Projection Support - Automatic materialization of arrays into relational tables
- ✅ Field Encryption - Encrypt specific document fields
- ✅ Production Ready - Comprehensive error handling and testing
npm install @sharesync/sharedb-storage-node-sqlitePeer Dependencies:
@sharesync/sharedb >= 6.0.0better-sqlite3 >= 8.0.0
const SqliteStorage = require('@sharesync/sharedb-storage-node-sqlite');
const { BetterSqliteAdapter } = SqliteStorage;
// Create adapter for your SQLite database
const adapter = new BetterSqliteAdapter('./myapp.db');
// Create storage with schema strategy
const storage = new SqliteStorage({
adapter: adapter,
schemaStrategy: new SqliteStorage.CollectionPerTableStrategy()
});
// Initialize and use with ShareDB
await storage.initialize();const { Connection } = require('@sharesync/sharedb/lib/client');
const SqliteStorage = require('@sharesync/sharedb-storage-node-sqlite');
// Create storage
const storage = new SqliteStorage({
adapter: new SqliteStorage.BetterSqliteAdapter('./sharedb.db')
});
// Create ShareDB connection
const connection = new Connection(websocket);
// Enable offline-first DurableStore
connection.useDurableStore({ storage });For multi-database architectures (e.g., read-only reference data + user data):
const { AttachedBetterSqliteAdapter } = SqliteStorage;
const adapter = new AttachedBetterSqliteAdapter(
'./user-data.db', // Primary database
{
attachments: [
{ path: './reference-data.db', alias: 'ref' }
]
}
);
// Now you can query across both databases
// Tables in attached database are prefixed with alias (e.g., ref.products)Creates separate tables for each collection with optimized indexes:
const strategy = new SqliteStorage.CollectionPerTableStrategy({
collectionConfig: {
products: {
indexes: ['payload.name', 'payload.category'],
encryptedFields: ['payload.price']
}
}
});Simple strategy using two tables (docs and meta):
const strategy = new SqliteStorage.DefaultSchemaStrategy();For use with attached databases:
const strategy = new SqliteStorage.AttachedCollectionPerTableStrategy({
attachmentAlias: 'ref'
});const adapter = new BetterSqliteAdapter(dbPath, options);Options:
readonly(boolean): Open database in read-only modeverbose(function): Log all SQL queriesfileMustExist(boolean): Fail if database doesn't exist
const adapter = new AttachedBetterSqliteAdapter(primaryDbPath, {
attachments: [
{ path: './other.db', alias: 'other' }
]
});npm testMIT
- @sharesync/sharedb-storage-sqlite - Shared components
- @sharesync/sharedb-storage-expo-sqlite - React Native implementation
- @sharesync/sharedb - ShareDB with DurableStore support