Manager and entry to the databases.

Note: if you want to write to a database, don't forget to using it, or data written to the database would lose!

You should NOT construct any of its instances, except for unit tests: there's a app-local singleton app.db.

Usage:

// register the database
db.register('groups', { data: () => { id: number, users: User[] }, sub: { data: () => string } });
// use the databases
await using grpdb = await app.db.db<{ id: number, users: User[] }>('groups');
const grp = grpdb.data[msg.group.id];
grp.id;
await using usrdb = await grpdb.sub<string>(msg.group.id);
const usr = usrdb.data[msg.user.id];

Accessing the same entry concurrently may result in losing data, or conflict between two transactions; here, we choose to copy the entry lazily per database, which is not right, but lock-free, and wouldn't cause any transactions to fail.

Constructors

  • You should ONLY do this in unit tests.

    Construct a manager with given storage implement.

    Parameters

    Returns DBManager

Properties

registry: {
    [name: string]: DBRegistry;
}

Type declaration

  • [name: string]: DBRegistry
storage: DBStorage

Methods

  • Get the database of the name.

    For detailed document, see DBManager.

    Type Parameters

    • T = any

    Parameters

    • name: string

    Returns Promise<DB<T>>

  • Type Parameters

    • T

    Parameters

    • base: (string | number)[]

    Returns Promise<DB<T>>

  • Register a database.

    Note that the structure of the database can't be changed once it's registered.

    Reregistering is a no-op.

    Parameters

    • name: string

      name of the database

    • init: DBInitializer | (() => unknown)[]

      initializer of the database, can also be an array if the explicit initializer is too annoying...

    Returns void

  • Conveniently perform a transaction with the given entry in given path.

    If no writes are needed, use peek_path.

    Equivalent to a chain of calls like:

    // convenient call
    await mgr.with_path<T>(['main', 'sub', ..., 'final sub', 'key'],
    async (val) => { 'do sth' });
    // equivalent to:
    await using subdb = await mgr.db('main').sub('sub')....sub<T>('final sub');
    const val = subdb.data['key'];
    // do sth

    You may return a new value in the transaction to set the entry to the given value.

    Type Parameters

    • T

    Parameters

    Returns Promise<void>

  • Type Parameters

    • T

    Parameters

    Returns Promise<T>

Generated using TypeDoc