Options
All
  • Public
  • Public/Protected
  • All
Menu

Class Database<$$>

A Jine Database.

The purpose of a Database is to store data. The data is organized into several item [[Stores]], which are queryable by Indexes. The shape of the database, i.e. what stores it contains and what indexes those contain, are defined during migrations.

Type parameters

  • $$

Hierarchy

  • Database

Index

Constructors

constructor

  • new Database(name: string, args: { migrations: Array<Migration>; types?: Array<UserCodec> }): Database
  • Creates a database with the given name and according to the given migrations and custom types.

    See Example for example database construction.

    Parameters

    • name: string

      The database name. Must be unique.

    • args: { migrations: Array<Migration>; types?: Array<UserCodec> }

      The database migrations as well as codecs for custom datatypes.

      • migrations: Array<Migration>
      • Optional types?: Array<UserCodec>

    Returns Database

Properties

Readonly $

$: $$

The database shorthand object.

An operation such as

await db.$.myStore.add(myitem)

Will add an item to the database store called myStore.

Also see Example.

Readonly initialized

initialized: Promise<void>

Resolves when the database is finished initializing.

When the database is created, it will immediately begin running migrations. All database operations will wait for these migrations to finish before actually taking the action. However, this means that before awaiting a database operation, you are not guaranteed about the state of the database. Weird things can happen if, say, you open a new database with the same name while an existing one is still initializing.

If there is an issue when migrating, this value will never resolve, and an error will be printed into the console.

So, if you need to gaurantee that the database is initialized, but don't want to do any operations to do this, you can await this attribute.

Readonly name

name: string

The name of the database. Database names are unique.

Readonly version

version: Promise<number>

The database version. This is an integer equal to the number of migrations given.

Methods

connect

  • connect<R>(callback: (conn: Connection<$$>) => Promise<R>): Promise<R>
  • Create a Connection to a database and run some code.

    This will create a new Connection to the database, run the given callback, and then close the connection once the callback has completed.

    Type parameters

    • R

    Parameters

    Returns Promise<R>

newConnection

transact

  • Perform a Transaction on a database.

    This will create a new [[Connection] to the database, create a new Transaction on that connection, run the given callback, close the transaction, and then close the connection once the callback has completed.

    Type parameters

    • R

    Parameters

    Returns Promise<R>

Generated using TypeDoc