TwinDB Local Database

1.1.2 · active · verified Wed Apr 22

TwinDB is a lightweight, local, file-based database designed for Node.js applications, currently at version 1.1.2. It stores data in JSON files, making it suitable for small-scale applications, configuration management, or local development environments where a full-fledged database system is overkill. Its key differentiator lies in its straightforward API for manipulating JSON structures directly through object paths, offering methods like `set`, `get`, `delete`, `sum`, `sub`, `concat`, `push`, and `pull`. While it doesn't offer advanced features like transactions or complex querying found in relational or NoSQL databases, it excels in providing a simple, persistent data store. The project appears to have an ad-hoc release cadence, with recent updates indicating active maintenance, focusing on utility and ease of use. It specifically requires an ES module environment.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize a TwinDB database, set and retrieve values using object paths, perform atomic updates like sum and push, and delete entries. It also includes cleanup for a runnable example.

import TwinDB from 'twin-db';
import { readFileSync, unlinkSync } from 'fs';

// Initialize a database named 'mydata'
const database = new TwinDB('mydata');

// Ensure the database file is clean for demonstration
try { unlinkSync('mydata.json'); } catch (e) { /* ignore if not exists */ }

console.log('Database initialized.');

// Set initial values, demonstrating pathing
database.set('user.name', 'Alice');
database.set('user.age', 30);
database.set('items', ['apple', 'banana']);
database.set('metrics.views', 100);

console.log('Initial data:\n', JSON.parse(readFileSync('mydata.json', 'utf8')));

// Get values
const userName = database.get('user.name');
console.log(`\nUser name: ${userName}`); // Alice

// Update values using various methods
database.set('user.age', 31); // Now age is 31
database.sum('metrics.views', 50); // views is 150
database.push('items', 'orange', 'grape'); // Add more items

console.log('\nData after updates:\n', JSON.parse(readFileSync('mydata.json', 'utf8')));

// Delete a value
database.delete('user.age'); // Age is gone

console.log('\nData after deletion:\n', JSON.parse(readFileSync('mydata.json', 'utf8')));

// Clean up the database file
try { unlinkSync('mydata.json'); } catch (e) { /* ignore if not exists */ }
console.log('\nDatabase file cleaned up.');

view raw JSON →