Verida Documentation
  • Welcome
  • Network Overview
  • Start Building
  • Verida Wallet
  • Whitepapers
  • Glossary
  • Protocol
    • Core concepts
      • Design Principles
      • Decentralized Identity
      • Confidential Databases
      • Confidential Compute
      • Accounts and Identity
      • Data Storage
      • Application Contexts
      • Messaging
      • Schemas
      • Data Sharing
    • Client SDK
      • Getting Started
      • How It Works
      • Authentication
      • Data
      • Queries
      • Permissions
      • Messaging
      • Account Profiles
      • Events
      • Command Line Tools
      • Configuration
      • React Native
      • Advanced
    • Verida Connect SDK
      • Getting Started
      • WebUser
      • WalletConnect Support
      • Authentication Server
    • Blockchain Bridge
    • Run a Node
      • Database Node
        • Setup
        • Operations
        • FAQ
      • Compute Node
    • Verida Networks
  • Extensions
    • Credentials
      • Verifiable Credentials Developer SDK
      • cheqd Credential Service
      • Privado ID (fmr Polygon ID)
      • zkPass credentials
      • Reclaim Protocol credential
    • Verida URIs
    • Vue Components
Powered by GitBook
On this page
  • Query data​
  • Pagination​
  • Indexes​

Was this helpful?

  1. Protocol
  2. Client SDK

Queries

PreviousDataNextPermissions

Last updated 9 months ago

Was this helpful?

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)

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)

The following strategies allow 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 received 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.

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

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

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

Pagination

Indexes

You can manually create database indexes by utilizing the underlying :

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

​
PouchDB documentation
schemas
queries/indexes
​
​
PouchDB index API methods
schemas
queries/indexes