AceBase Realtime Database Client

1.22.3 · active · verified Wed Apr 22

AceBase Client is a JavaScript/TypeScript library designed to connect to and interact with a remote AceBase realtime database server. It provides functionalities for reading, writing, querying, and subscribing to real-time data changes within an AceBase database. The current stable version is 1.22.3, with an active release cadence that frequently addresses bug fixes and introduces minor features, often preceded by release candidates. This client acts as the bridge for applications to leverage AceBase's NoSQL, schemaless, object-tree data model, supporting complex queries and robust authentication mechanisms. Its primary differentiator is its tight integration with the AceBase server ecosystem, enabling efficient data synchronization and real-time event handling for connected applications, both in Node.js and browser environments.

Common errors

Warnings

Install

Imports

Quickstart

Demonstrates connecting to an AceBase server, logging an event, performing a read/write operation, and setting up a real-time subscription for data changes. Includes environment variable usage for configuration.

import { AceBaseClient } from 'acebase-client';

// Replace with your actual AceBase server details
const host = process.env.ACEBASE_HOST ?? 'localhost';
const port = parseInt(process.env.ACEBASE_PORT ?? '5757', 10);
const dbname = process.env.ACEBASE_DBNAME ?? 'mydb';
const https = (process.env.ACEBASE_HTTPS ?? 'false') === 'true';

const db = new AceBaseClient({ host, port, dbname, https });

db.ready(async () => {
    console.log('Connected to AceBase server.');

    // 1. Log an event to the database
    const logRef = db.ref('log');
    await logRef.push({
        user: 'guest_user',
        type: 'quickstart_connect',
        datetime: new Date().toISOString()
    });
    console.log('Logged connection event.');

    // 2. Write and read user data
    const userId = 'quickstart_user';
    const userRef = db.ref(`users/${userId}`);
    await userRef.set({ name: 'Jane Doe', email: 'jane@example.com', createdAt: new Date().toISOString() });
    console.log(`User '${userId}' created.`);

    const snapshot = await userRef.get();
    console.log(`Read user data: ${JSON.stringify(snapshot.val())}`);

    // 3. Subscribe to real-time changes on a 'todo' list
    const todoRef = userRef.child('todo');
    todoRef.on('child_added', (snapshot) => {
        const item = snapshot.val();
        console.log(`[REALTIME] Todo item added: ${item.text} (ID: ${snapshot.key})`);
    });

    // Add an item after a short delay to trigger the subscription
    setTimeout(async () => {
        await todoRef.push({ text: 'Learn AceBase', completed: false });
        console.log('Added a todo item.');
    }, 1000);

    // Optional: Sign in if authentication is enabled on the server
    // try {
    //     const authResult = await db.auth.signInWithEmail('admin@example.com', 'yourpassword');
    //     console.log(`Signed in as ${authResult.user.email}`);
    // } catch (error) {
    //     console.warn('Authentication failed (if server requires it):', error.message);
    // }

});

db.on('error', (error) => {
    console.error('AceBase Client Error:', error);
});

view raw JSON →