Local NoSQL Database for Exploration and Education
local-nosql-db is a lightweight, file-based NoSQL database designed exclusively for local development, educational purposes, and exploration. Version 1.1.7 is the current stable release, with an infrequent release cadence given its niche purpose. It stores data in JSON files, mimicking a simplified MongoDB-like API for basic CRUD operations (Create, Read, Update, Delete). Key differentiators include its extreme simplicity, zero external dependencies, and focus on providing a hands-on experience with NoSQL concepts without the overhead of a full database server. It is explicitly not recommended or suitable for production environments due to inherent limitations in scalability, concurrency handling, and data integrity guarantees. Its primary use case is isolated local data persistence for small projects or learning exercises.
Common errors
-
La base de données n'est pas initialisée. Veuillez appeler 'init()' avant d'effectuer des opérations.
cause Attempting to perform database operations (like accessing a collection) before calling the `init()` method on the `Database` instance.fixEnsure you call `await db.init()` after creating a new `Database` instance and await its completion before any other database interactions. -
Chemin de base de données non valide ou inaccessible.
cause The provided path to the `Database` constructor is invalid, does not exist, or the application lacks the necessary file system permissions.fixVerify the `dbPath` string is a valid file system path and that your application has read/write permissions for that directory. Ensure the directory exists before initializing the database or handle its creation programmatically. -
Erreur de lecture du fichier JSON de la collection: [chemin/vers/fichier.json]. Le fichier est corrompu ou mal formé.
cause A collection's JSON data file has become corrupted or contains invalid JSON syntax, preventing the library from parsing it.fixInspect the specified JSON file for syntax errors. If the data is not critical, delete the file (losing data for that collection) or manually repair the JSON structure. Implement robust error handling around file I/O if possible, although the library might abstract this.
Warnings
- breaking This package is explicitly stated as 'Not suitable for production' use. It is intended solely for exploration and educational purposes and lacks features critical for production environments such as robust error handling, concurrency control, data integrity, and performance optimization for large datasets.
- gotcha All internal messages and logs generated by the library are in French. This can be a significant barrier for non-French speakers attempting to debug or understand library output.
- gotcha As a file-based database, concurrent writes or sudden application termination can lead to data corruption or inconsistencies. There are no built-in mechanisms for atomic transactions or robust recovery.
- gotcha Query capabilities are extremely limited, primarily supporting direct field matching. Complex queries, aggregations, indexing, or advanced search patterns common in full NoSQL databases are not supported.
- deprecated The library's development activity is infrequent, reflecting its purpose as an educational tool rather than a continuously evolving product. Users should not expect regular updates, new features, or rapid bug fixes.
Install
-
npm install local-nosql-db -
yarn add local-nosql-db -
pnpm add local-nosql-db
Imports
- Database
const { Database } = require('local-nosql-db');import { Database } from 'local-nosql-db'; - Database (CommonJS)
import { Database } from 'local-nosql-db';const { Database } = require('local-nosql-db'); - IDocument
import type { IDocument } from 'local-nosql-db';
Quickstart
import { Database } from 'local-nosql-db';
import * as path from 'path';
import * as fs from 'fs';
const dbPath = path.join(__dirname, 'local-db-data');
// Ensure the database directory exists, or create it
if (!fs.existsSync(dbPath)) {
fs.mkdirSync(dbPath, { recursive: true });
}
async function runDbOperations() {
const db = new Database(dbPath);
await db.init(); // Initialize the database
const usersCollection = db.collection('users');
// 1. Insert a document
console.log('Inserting a new user...');
const newUser = await usersCollection.insert({
name: 'Alice Smith',
email: 'alice@example.com',
age: 30
});
console.log('Inserted user:', newUser);
// 2. Find documents
console.log('\nFinding all users...');
const allUsers = await usersCollection.find({});
console.log('All users:', allUsers);
console.log('\nFinding user by email...');
const foundUser = await usersCollection.findOne({ email: 'alice@example.com' });
console.log('Found user:', foundUser);
// 3. Update a document
if (foundUser && foundUser._id) {
console.log('\nUpdating user age...');
const updatedUser = await usersCollection.update(
{ _id: foundUser._id },
{ $set: { age: 31, status: 'active' } }
);
console.log('Updated user:', updatedUser);
}
// 4. Delete a document
if (newUser && newUser._id) {
console.log('\nDeleting the new user...');
const deleteResult = await usersCollection.delete({ _id: newUser._id });
console.log('Delete result:', deleteResult);
}
console.log('\nFinal check of users after deletion:');
const remainingUsers = await usersCollection.find({});
console.log('Remaining users:', remainingUsers);
// Clean up the created data directory (optional)
// fs.rmSync(dbPath, { recursive: true, force: true });
// console.log('\nDatabase directory cleaned up.');
}
runDbOperations().catch(console.error);