TeDB: TypeScript Embedded Database

0.5.1 · active · verified Wed Apr 22

TeDB (TypeScript Embedded Database) is an embedded database designed specifically for TypeScript applications, supporting various environments like Node.js, Electron, and webkit. It's currently at version 0.5.1, with a development cadence that includes regular minor fixes and improvements. A key differentiator is its pluggable storage architecture, allowing developers to implement custom storage drivers for disk persistence, in-memory operations, or even browser-based solutions like IndexedDB. Unlike some other JavaScript embedded databases, TeDB leverages an AVL balanced binary tree to index only document `_id`s and specified indexed field values in memory, preventing potential out-of-memory issues with very large datasets, as the actual document data is managed by the storage driver. All operations are Promise-based, and the library is written entirely in TypeScript, providing strong type safety.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates initializing a TeDB Datastore with a custom in-memory storage driver, inserting, finding, updating, and removing documents, and the recommended `sanitize` operation.

import { Datastore } from 'tedb';

// A minimal in-memory storage driver example
class InMemoryStorageDriver {
  private data: Map<string, any> = new Map();
  constructor(private collectionName: string) {}
  async read(key: string): Promise<any> { return this.data.get(key); }
  async write(key: string, value: any): Promise<boolean> { this.data.set(key, value); return true; }
  async remove(key: string): Promise<boolean> { return this.data.delete(key); }
  async exists(key: string): Promise<boolean> { return this.data.has(key); }
  async getAllKeys(): Promise<string[]> { return Array.from(this.data.keys()); }
}

async function runDbExample() {
  const storageDriver = new InMemoryStorageDriver('myCollection');
  const db = new Datastore({ 
    storage: storageDriver, 
    collection: 'myCollection' 
  });

  // Load any existing data (for persistent drivers)
  await db.loadDatabase();

  // Insert a document
  const doc1 = await db.insert({ name: 'Alice', age: 30 });
  console.log('Inserted doc1:', doc1);

  // Find documents
  const foundDocs = await db.find({ age: { $gt: 25 } }).exec();
  console.log('Found docs (age > 25):', foundDocs);

  // Update a document
  const updatedCount = await db.update({ _id: doc1._id }, { $set: { age: 31 } });
  console.log('Updated documents count:', updatedCount);

  // Find the updated document
  const updatedDoc = await db.findOne({ _id: doc1._id }).exec();
  console.log('Updated doc1:', updatedDoc);

  // Remove a document and sanitize indices
  const removedCount = await db.remove({ _id: doc1._id });
  console.log('Removed documents count:', removedCount);
  await db.sanitize(); // Recommended after remove

  const allDocs = await db.find({}).exec();
  console.log('All remaining docs:', allDocs);
}

runDbExample().catch(console.error);

view raw JSON →