Tiny Memory DB

0.4.2 · active · verified Wed Apr 22

Tiny Memory DB is a zero-dependency, in-memory database library for JavaScript and TypeScript environments. Currently at version 0.4.2, it is in active development, with a release cadence that reflects its pre-1.0 status, meaning updates occur as features are implemented or bugs are addressed. Its primary differentiators are its minimal footprint and lack of external dependencies, making it ideal for lightweight data management. It provides a structured 'Table' abstraction for defining data schemas with attributes and primary keys, and supports a concise, string-based query language. Unlike persistent databases, Tiny Memory DB operates entirely in RAM, prioritizing speed and simplicity, though users can implement external mechanisms for data persistence if needed. It ships with comprehensive TypeScript types, ensuring type safety and an improved developer experience for TS projects.

Common errors

Warnings

Install

Imports

Quickstart

This example demonstrates creating a typed in-memory table, adding data, querying by exact match and regular expression, updating records, and deleting records.

import { Table } from 'tiny-memory-db';

async function runExample() {
    // Define the shape of our user data for type safety
    type User = { id: number; name: string; email: string; createdAt: number };

    // Create a new table for users, defining its attributes and a unique key
    const usersTable = new Table<User>({
        attrs: 'id name email createdAt',
        funcs: ['id ->'] // 'id' is specified as a unique primary key
    });

    // Add some initial user data to the table
    usersTable.put({ id: 1, name: 'Alice Wonderland', email: 'alice@example.com', createdAt: Date.now() - 100000 });
    usersTable.put({ id: 2, name: 'Bob The Builder', email: 'bob@example.com', createdAt: Date.now() - 50000 });
    usersTable.put({ id: 3, name: 'Charlie Chaplin', email: 'charlie@example.com', createdAt: Date.now() });
    usersTable.put({ id: 4, name: 'David Jones', email: 'david@example.com', createdAt: Date.now() - 20000 });

    console.log('--- All users initially stored ---');
    // Retrieve and log all users in the table
    for (const user of usersTable.scan()) {
        console.log(user);
    }

    // Query for a specific user by their ID
    console.log('
--- User with ID 2 ---');
    const bobResults = await usersTable.query('id=2');
    for (const user of bobResults.scan()) {
        console.log(user);
    }

    // Query for users whose name contains 'Alice' using a regular expression
    console.log('
--- Users with name containing "Alice" (using regex query) ---');
    const usersContainingAlice = await usersTable.query('name~.*Alice.*');
    for (const user of usersContainingAlice.scan()) {
        console.log(user);
    }

    // Update an existing user's email address
    const userToUpdate = (await usersTable.query('id=2')).scan()[0];
    if (userToUpdate) {
        usersTable.put({ ...userToUpdate, email: 'robert.builder@example.com' });
    }
    console.log('\n--- Updated Bob\'s email address ---');
    const updatedBob = (await usersTable.query('id=2')).scan()[0];
    console.log(updatedBob);

    // Remove a user from the table based on their ID
    usersTable.removeMatching('id=1');
    console.log('\n--- Users after deleting Alice Wonderland (ID 1) ---');
    for (const user of usersTable.scan()) {
        console.log(user);
    }

    // Add another new user to demonstrate continued operation
    usersTable.put({ id: 5, name: 'Eve Adams', email: 'eve@example.com', createdAt: Date.now() + 10000 });
    console.log('\n--- All users after adding Eve ---');
    for (const user of usersTable.scan()) {
        console.log(user);
    }
}

runExample();

view raw JSON →