A1-Database: Embedded JSON Database

1.8.0 · active · verified Wed Apr 22

a1-database is an embedded, zero-dependency, zero-installation JSON database designed for simplicity and portability. As of version 1.8.0, it stores all data in a single file, making it ideal for development, local applications, or scenarios where a lightweight, persistent data store is required without external database server overhead. It operates directly on JavaScript objects, eliminating the need for an ORM, and returns query results as arrays of objects. The package provides methods that cater to both SQL-like (e.g., `insert`, `update` with `id` keys) and document-like (flexible `save` with custom filter functions for multi-key or heterogeneous data) paradigms. Each database instance is represented by a single file, facilitating easy backup and data dumping. Its primary release cadence is not explicitly stated, but ongoing development is implied by the version number, focusing on a straightforward API for common CRUD operations like `save`, `find`, and `delete`.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates database connection, saving data (including upsert behavior with IDs), finding records, checking existence, and deleting entries, along with proper cleanup of the database file.

const database = require('a1-database');
const fs = require('fs/promises'); // For cleanup

async function runExample() {
  const dbPath = 'users.db';

  try {
    // Ensure cleanup of previous runs for a fresh start
    try { await fs.unlink(dbPath); } catch (e) { /* ignore if file doesn't exist */ }

    const db = await database.get(dbPath);
    console.log('Database connected to', dbPath);

    // Save initial data
    await db.save({ name: 'Juan', email: 'juan@example.com' });
    await db.save({ name: 'Maria', email: 'maria@example.com' });
    console.log('Initial users saved.');

    // Save with an ID, demonstrating upsert behavior
    await db.save([{ id: 100, value: 'old test data' }]);
    console.log('Saved item with id 100 (old).');
    await db.save([{ id: 100, value: 'new test data', timestamp: Date.now() }]); // item with id, so old items are removed
    console.log('Saved item with id 100 (new), overwriting old data.');

    // Find operations
    const juan = await db.findOne(el => el.name === 'Juan');
    console.log('Found Juan:', juan);

    const allUsers = await db.find(el => el.name === 'Juan' || el.name === 'Maria');
    console.log('Found all initial users:', allUsers);

    // Demonstrate existence check
    const existsJuan = await db.exists(el => el.name === 'Juan');
    console.log('Does Juan exist?', !!existsJuan);

    // Delete operation
    const deletedCount = await db.delete(el => el.email === 'maria@example.com');
    console.log(`Deleted ${deletedCount} user(s).`);

    const remainingUsers = await db.find(el => true);
    console.log('Remaining items after delete:', remainingUsers);

    // Disconnect from the database
    await database.disconnect(db);
    console.log('Database disconnected.');

  } catch (error) {
    console.error('An error occurred:', error);
  } finally {
    // Optional: Clean up the database file after the example
    try { await fs.unlink(dbPath); } catch (e) { /* ignore */ }
    console.log('Cleaned up database file.');
  }
}

runExample().catch(console.error);

view raw JSON →