Skip to main content

Queries

The majority of decentralized data solutions in the Web3 space provide simple key / value storage options as querying distributed and / or encrypted databases is a significantly hard problem.

The Verida Client SDK architecture has the following features that facilitate querying of encrypted data in a consistent manner across many distributed databases:

  • Using CouchDBcompatible database synchronization and merging
  • Using common JSON schemas to ensure data consistency between distributed applications
  • Encrypt / Decrypt data on the fly between multiple CouchDB compliant database backends, with different encryption keys for each

As a result, all applications implementing the Verida Client SDK support querying user data as you would expect from a typical NoSQL database.

Example: Fetch all runs a user has completed in 2018

const employmentData = database.getMany("https://common.schemas.verida.io/health/activity/v0.1.0/schema.json", {
type: "run",
date: {
"$gte": "2018-01-01"
}
});

Query data

Databases and datastores support a full range of query functionality. This includes; filters, limit, offset and sort:

const filter = {
organization: 'Google'
}

const options = {
limit: 20,
skip: 0,
sort: [{'firstName': 'desc'}]
}

const results = contacts.getMany(filter, options)
console.log(results)

You can learn more about the query functionality in the official PouchDB documentation.

Here's another example using a regular expression to match all records with a name that has ri:

const filter = {
name: {
$regex: ".*ri.*"
}
}
const results = contacts.getMany(filter, options)
tip

Sorting only works if an index has been defined for the field being sorted. Indexes are defined in the datastore schema. See schemas and queries / indexes for more details.

Pagination

The following strategies allows you to paginate data for a user based on a name field:

// Configure our page size (10) and start position (lastId), then sort documents by name

let lastId = null
const currentPageResults = await datastore.getMany({
_id: {$gt: lastId}
}, {
limit: 10,
sort:['name']
})

We receive the first 10 documents sorted by name. We can continue paginating by using the last value as our next starting point. At any given point in time, we will only have 10 documents stored in the memory, which is great for performance.

Indexes

You can manually create database indexes by utilizing the underlying PouchDB index API methods:

const veridaDb = await context.openDatabase('test_db')
const pouchDb = await veridaDb.getDb()
await pouchDb.createIndex({
index: {
fields: ['foo']
}
})
tip

Datastore indexes are defined in the underlying JSON schema document for the datastore. These indexes are automatically managed by Verida Datastore. See schemas and queries / indexes for more details.