Skip to main content

Query APIs

WeaveDB queries cover most of the things Firestore can do with syntactic sugar inspired by Firestore Sweet.

getNonce

To get the next nonce for an address. Nonces are internally used for signature verification to write data.

await db.getNonce("address")

get / cget

get only returns data, whereas cget returns metadata of the docs too.

{ id, setter, data, block: { height, timestamp } }

The metadata returned with cget functions as a cursor for pagination.

Get a doc

await db.get("collection_name", "doc_id")
await db.cget("collection_name", "doc_id")

Get a collection

await db.get("collection_name")
await db.cget("collection_name")

Arbitrary length of document nesting is possible.

await db.get("collection_name", "doc_id", "sub_collection_name_1", "sub_doc_id_1", "sub_collection_name_2", "sub_doc_id_2")

Limit the number of docs

await db.get("collection_name", 5)

Sort

await db.get("collection_name", [ "age" ])
await db.get("collection_name", [ "age", "desc" ])
await db.get("collection_name", [ "age", "desc" ], [ "name", "asc" ])

Where

await db.get("collection_name", ["age"], [ "age", ">", 20 ])

= > >= < <= != in not-in array-contains array-contains-any are supported.

Skip

await db.get("collection_name", [ "age" ], [ "startAfter", 20 ], [ "endAt", 60 ])
await db.get("collection_name", [ "age" ], [ "name", "desc" ], [ "startAfter", 20, "Bob" ])

startAt startAfter endAt endAfter are supported.

Pagination

const docs_page1 = db.cget("collection_name", [ "age" ])
const docs_page2 = db.cget("collection_name", [ "age" ], ["startAfter", docs_page1[docs_page1.length - 1]])

add

Add a doc

await db.add({ "age": 20, "name": "Bob" }, "collection_name")

The doc id will be randomly yet deterministically assigned.

getIds

To get the last added doc id, use getIds.

const tx = await db.add({ "age": 20, "name": "Bob" }, "collection_name")
const doc_id = await db.getIds(tx))[0]

set

Set a doc

await db.set({ "age": 20, "name": "Bob" }, "collection_name", "doc_id")

upsert

Upsert a doc

await db.upsert({ "age": 20, "name": "Bob" }, "collection_name", "doc_id")

update

Update a doc

await db.update({ "age": 25 }, "collection_name", "doc_id")

Delete a field

await db.update({ "age": db.del() }, "collection_name", "doc_id")

Increase/Decrease a field

await db.update({ "age": db.inc(5) }, "collection_name", "doc_id")
await db.update({ "age": db.inc(-5) }, "collection_name", "doc_id")

Array union

await db.update({ "chars": db.union([ "a", "b", "c", "d" ]) }, "collection_name", "doc_id")

Array remove

await db.update({ "chars": db.union([ "b", "c" ]) }, "collection_name", "doc_id")

Set block timestamp

await db.update({ "date": db.ts() }, "collection_name", "doc_id")

Set signer Ethereum address

await db.update({ "address": db.signer() }, "collection_name", "doc_id")

delete

Delete a doc

await db.delete("collection_name", "doc_id")

batch

Atomic batch write

await db.batch([
["set", { name: "Bob" }, "people", "Bob"],
["upsert", { name: "Alice" }, "people", "Alice"],
["delete", "John"]
])