Skip to main content
Version: v10.0.0

createDb

Creates the db that queries and writes go through. The names given to it are the names on the db, as in db.post . Requests carry their Client and listeners carry their Socket, so it needs neither.


Import
import { createDb } from "@hyper-fetch/db"

Usage​

const db = createDb({ post: Post, user: User });

Parameters​

createDb<Collections, Transactions>(collections: Collections & { [ Name in (onError | ready | clear | destroy | transaction | dehydrate | inspect | onChange)]: never }, options: Notation & { codecs?: readonly DbCodec[]; gcTime?: number; offline?: (boolean | DbOfflineOptions); persistence?: DbPersistenceOptions; state?: DehydratedDb; syncTimeout?: number; tabs?: (false | { channel?: DbChannel; locks?: DbLockManager }) } & { links?: readonly readonly [(keyof Collections & string), (keyof Collections & string)][]; transactions?: (Transactions & Record<string, TransactionConfig>) })
Parameters
NameTypeDescription
collections
Collections & { [ Name in (onError | ready | clear | destroy | transaction | dehydrate | inspect | onChange)]: never }
options
Notation & { codecs?: readonly DbCodec[]; gcTime?: number; offline?: (boolean | DbOfflineOptions); persistence?: DbPersistenceOptions; state?: DehydratedDb; syncTimeout?: number; tabs?: (false | { channel?: DbChannel; locks?: DbLockManager }) } & { links?: readonly readonly [(keyof Collections & string), (keyof Collections & string)][]; transactions?: (Transactions & Record<string, TransactionConfig>) }

Returns​

type Db<Collections,Transactions> = { [ Name in keyof Collections]: HandleOf<Collections[Name]> } & DbMethods & { transaction: TransactionMethod<Collections, Transactions> };
createDb
NameTypeDescription
ready
Promise<void>

Resolves once persisted rows and the outbox are restored

transaction
TransactionMethod<Collections, Transactions>
clear
() => Promise<void>

Removes every row, loaded subset and pending write, and the persisted data of the scope, for example on sign-out

dehydrate
() => DehydratedDb

The server rows and loaded subsets, as type-tagged JSON, for server rendering. Optimistic layers, the outbox and syncs are never included. Pass it to createDb(collections, { state }) on the client.

destroy
() => void

Stops every sync, timer and tab channel

inspect
() => DbInspection

What the db holds right now: collections with their layers and subsets, live queries and the outbox

onChange
(listener: () => void) => () => void

Listens to every change the db makes, once per batch of writes, load or outbox change

onError
(listener: (event: DbErrorEvent) => void) => () => void

Listens to failed writes, rejected validation, outbox and persistence errors