Skip to main content

Basics

Learn to use Fireproof in a few simple steps.

Install Fireproof with NPM:

npm install use-fireproof

or Yarn:

yarn add use-fireproof

The default package import provides a concise way to access Fireproof, this quick sketch will give you a sense of how the API works:

import { fireproof } from 'use-fireproof';

We cache the database instance so you can call this on every render.

const db = fireproof('my-db')

Document API

Here's how to put a document and get an 'ok' response with an id.

const ok = await db.put({ _id: 'my-doc', hello: 'world' })

To get a document, use the document's id. The returned document's _id field will be 'my-doc'. You can put again to update.

const doc = await db.get('my-doc')
doc.hello = 'universe'
const ok2 = await db.put(doc)

To delete a document, use the document's id.

const ok3 = await db.del('my-doc')

You can query for documents with a specific field. In this case, we're querying for documents with a 'hello' field.

const { rows } = await db.query('hello', { range: ['a', 'z'] })

You can also query for changes since an operation's clock.

const { rows : chs } = await db.changes(ok.clock)

To subscribe to live updates, use the subscribe method. The updates parameter is an array of documents.

const unsub = db.subscribe((updates) => {
// updates is an array of documents
})

You can read more about the database API and the document API in the API docs. For getting started with React, see the React tutorial. Fireproof runs almost anywhere. Read about framework and bundler support.