Fireproof

Fireproof stores data in IndexedDB and provides a Merkle clock. This is the main class for saving and loading JSON and other documents with the database. You can find additional examples and usage guides in the repository README.

Constructor

new Fireproof(blocks, clock, configopt, authCtxopt)

Parameters:
NameTypeAttributesDescription
blocksBlockstore

The block storage instance to use documents and indexes

clockArray.<CID>

The Merkle clock head to use for the Fireproof instance.

configobject<optional>

Optional configuration options for the Fireproof instance.

authCtxobject<optional>

Optional authorization context object to use for any authentication checks.

Methods

changesSince(eventopt) → {Object.<{rows: Array.<Object>, clock: Array.<CID>}>}

Returns the changes made to the Fireproof instance since the specified event.

Parameters:
NameTypeAttributesDescription
eventArray.<CID><optional>

The clock head to retrieve changes since. If null or undefined, retrieves all changes.

Returns:

An object containing the rows and the head of the instance's clock.

Type: 
Object.<{rows: Array.<Object>, clock: Array.<CID>}>

(async) del(id) → {Object.<{id: string, clock: Array.<CID>}>}

Deletes a document from the database

Parameters:
NameTypeDescription
idstring

the document ID

Returns:
  • The result of deleting the document from the database
Type: 
Object.<{id: string, clock: Array.<CID>}>

(async) get(key, optsopt) → {Object.<{_id: string, ...doc: Object}>}

Retrieves the document with the specified ID from the database

Parameters:
NameTypeAttributesDescription
keystring

the ID of the document to retrieve

optsObject<optional>

options

Returns:
  • the document with the specified ID
Type: 
Object.<{_id: string, ...doc: Object}>

(async) put(doc) → {Object.<{id: string, clock: Array.<CID>}>}

Adds a new document to the database, or updates an existing document. Returns the ID of the document and the new clock head.

Parameters:
NameTypeDescription
docObject

the document to be added

Properties
NameTypeDescription
_idstring

the document ID. If not provided, a random ID will be generated.

*Object

the document data to be added

Returns:
  • The result of adding the document to the database
Type: 
Object.<{id: string, clock: Array.<CID>}>

(async) setClock(clock) → {Promise.<void>}

Move the current instance to a new point in time. This triggers a notification to all listeners of the Fireproof instance so they can repaint UI, etc.

Parameters:
NameTypeDescription
clockArray.<CID>

Clock to use for the snapshot.

Returns:
Type: 
Promise.<void>

snapshot(clock) → {Fireproof}

Returns a snapshot of the current Fireproof instance as a new instance.

Parameters:
NameTypeDescription
clockArray.<CID>

The Merkle clock head to use for the snapshot.

Returns:

A new Fireproof instance representing the snapshot.

Type: 
Fireproof

toJSON() → {Object}

Renders the Fireproof instance as a JSON object.

Returns:
  • The JSON representation of the Fireproof instance. Includes clock heads for the database and its indexes.
Type: 
Object

(static) registerListener(listener) → {function}

Registers a Listener to be called when the Fireproof instance's clock is updated. Recieves live changes from the database after they are committed.

Parameters:
NameTypeDescription
listenerfunction

The listener to be called when the clock is updated.

Returns:
  • A function that can be called to unregister the listener.
Type: 
function