Options
All
  • Public
  • Public/Protected
  • All
Menu

DB is a snapshot of your current database state. It helps you reading data via get, table and query.

const db = new DB(state);

db.get('enableAwesomeThing'); // true

const things = db.table('things'); // if things is not defined, you would get an error here
things.all; // returns Thing[]
things.find('12'); // find by id
things.where({ name: 'tool' }); // simple equality based where queries
things.where(thing => thing.name.length == 4); // function based where queries

Type parameters

  • State: StateDefining

    The state type of your database schema.

Hierarchy

  • DB

Index

Constructors

Properties

Context Methods

Key-Value Storage Methods

Other Methods

Reset Methods

Table Storage Methods

Transactions Methods

Constructors

constructor

  • new DB(state: State, options?: { context?: undefined | string }): DB
  • Create a snapshot from the given state. Can either be used in conjunction with redux or implicitly by calling mutableDB.snapshot.

    Parameters

    • state: State
    • Default value options: { context?: undefined | string } = {}
      • Optional context?: undefined | string

    Returns DB

Properties

Private Optional currentContext

currentContext: undefined | string

Private state

state: State

Context Methods

commit

  • commit<K>(table?: K, ids?: RowIdentififying): CommitContextAction
  • Commit changes in a context to its parent. You can either commit individual ids, full tables or the whole context.

    Type parameters

    • K: RowKeyOf<State>

    Parameters

    • Optional table: K
    • Optional ids: RowIdentififying

    Returns CommitContextAction

context

  • context(context: string): DB<State>
  • Access a named context.

    const draftDB = db.context('draft');
    store.dispatch(draftDB.table('things').update('1', { name: 'Updated Thing' }));
    // retrieve your state again from the store
    db.table('things').first.name; // this is still 'First Thing'
    draftDB.table('things').first.name; // this is updated to 'Updated Thing'

    Parameters

    • context: string

    Returns DB<State>

revert

  • revert<K>(table?: K, ids?: RowIdentififying): RevertContextAction
  • Revert changes in a context. You can either revert individual ids, full tables or the whole context.

    Type parameters

    • K: RowKeyOf<State>

    Parameters

    • Optional table: K
    • Optional ids: RowIdentififying

    Returns RevertContextAction

Key-Value Storage Methods

get

  • get<K>(name: K): SettingsType<State, K>
  • Get the value from the key-value store.

    Type parameters

    • K: SettingsKey<State>

    Parameters

    • name: K

      A key of your key-value schema

    Returns SettingsType<State, K>

    the value of the supplied key, undefined if no value is set yet

set

  • set<K, U>(name: K, value: U): SettingsUpdateAction
  • Set a value in your key-value storage. This action does not mutate your state but returns an action to be dispatched.

    Type parameters

    • K: SettingsKey<State>

    • U: SettingsType<State, K>

    Parameters

    • name: K

      The key to update.

    • value: U

      The value to set for this key.

    Returns SettingsUpdateAction

    Send this value to your dispatch function.

Other Methods

Private changeSetsOfContext

  • changeSetsOfContext(table: string, context?: undefined | string): Array<ContextChanges<Row>>
  • Parameters

    • table: string
    • Optional context: undefined | string

    Returns Array<ContextChanges<Row>>

Reset Methods

reset

  • reset(type?: "all" | "tables" | "settings"): ResetAction
  • Resets the given type back to the initial state. If you want to have empty tables, use truncate instead.

    • all: Reset settings and tables back to initial state.
    • tables: Reset tables, but keeps current settings.
    • settings: Reset settings, but keeps all tables.

    Parameters

    • Default value type: "all" | "tables" | "settings" = "all"

    Returns ResetAction

truncate

  • truncate(): TruncateAction
  • Truncates all tables. After this action, all tables are empty. This will not change settings. If you would like to reset settings instead, use reset.

    Returns TruncateAction

Table Storage Methods

query

  • query<Key>(type: Key): Query<State, Key, RowType<State, Key>>
  • Start a composable query. Queries allow chaining together different queries, joins and transformations.

    const things = db.query('things').where({name: 'tool'}).embed('user', 'users', 'userId).order({name: 'desc'}).all;

    Type parameters

    • Key: RowKeyOf<State>

    Parameters

    • type: Key

      Name of the table to start the query on.

    Returns Query<State, Key, RowType<State, Key>>

table

  • table<K>(type: K): Table<RowType<State, K>>
  • Retrieve an immutable table by name. If you plan to compose a complex query, use query instead.

    const things = db.table('things');
    things.first

    Type parameters

    • K: RowKeyOf<State>

    Parameters

    • type: K

      The name of the table to retrieve

    Returns Table<RowType<State, K>>

    An immutable Table instance.

Transactions Methods

transaction

  • transaction(execute: (dispatch: DBDispatch) => void): TransactionAction
  • Start a transaction to group updates. This is mostly used to prevent rerenders if you're planning to update multiple values and tables.

    store.dispatch(
      db.transaction(dispatch => {
        dispatch(things.insert({ name: 'First Thing' }));
        dispatch(things.insert({ name: 'Second Thing' }));
      })
    );

    Parameters

    • execute: (dispatch: DBDispatch) => void

      Your execution function

        • (dispatch: DBDispatch): void
        • Parameters

          • dispatch: DBDispatch

          Returns void

    Returns TransactionAction

    The action that you should send to your reducer.

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Type alias with type parameter
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc