CamaDB: TypeScript Embedded NoSQL Database

2.0.0 · active · verified Wed Apr 22

CamaDB is an embedded NoSQL database written in pure TypeScript, supporting Node.js, Electron, and browser environments. Its current stable version is 2.0.0 (January 2023), and it is under active development. CamaDB provides a MongoDB-style API for querying (SiftJS), updating (Obop), and aggregation (Mingo, with some limitations), offering full TypeScript support. It differentiates itself by providing frictionless integration across runtimes, handling native JavaScript data types, and aiming for fast performance on datasets up to 1 million rows, bypassing common issues with native database bindings in environments like Electron.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a CamaDB instance and a collection with a type interface, then inserting a document, and performing basic find and update operations.

import { Cama } from 'camadb';
// Ensure reflect-metadata is imported if decorators are used, often at the top level
// import 'reflect-metadata'; 

interface User { 
  _id: string;
  name: string;
  email: string;
  registrationDate: Date;
  preferences: { 
    theme: string;
    notifications: boolean;
  };
}

async function setupCamaDB() {
  // Initialize the database instance with filesystem persistence
  const database = new Cama({
    path: './.cama-data',
    persistenceAdapter: 'fs', // Use 'indexeddb' or 'localstorage' for browser environments
    logLevel: 'info' // Can be 'debug' for more verbose logging
  });

  // Initialize a collection named 'users' with a specific date column for proper type handling
  const usersCollection = await database.initCollection<User>('users', {
    columns: [{
      type: 'date',
      title: 'registrationDate' // Essential for CamaDB to correctly store and retrieve Date objects
    }],
    indexes: [], // Indexes are not yet implemented but are part of the configuration API
  });

  // Insert a new user document into the collection
  const newUser: User = {
    _id: 'user_001',
    name: 'Alice Wonderland',
    email: 'alice@example.com',
    registrationDate: new Date(),
    preferences: {
      theme: 'dark',
      notifications: true
    }
  };
  await usersCollection.insertOne(newUser);

  console.log('User inserted successfully:', newUser._id);

  // Example of finding a user by _id
  const foundUser = await usersCollection.findMany({ _id: 'user_001' });
  console.log('Found user:', foundUser);

  // Example of updating a user's preferences
  await usersCollection.updateMany({
    _id: 'user_001'
  }, {
    $set: {
      'preferences.theme': 'light'
    }
  });
  console.log('User preferences updated.');
}

setupCamaDB().catch(console.error);

view raw JSON →