Local NoSQL Database for Exploration and Education

1.1.7 · maintenance · verified Wed Apr 22

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to initialize the local NoSQL database, create a collection, and perform basic CRUD operations (insert, find, update, delete) on documents. It also shows how to manage the persistence directory.

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);

view raw JSON →