About Queries
Date | Responsible | Changes |
---|---|---|
July 8, 2024 | @Tomislav Bradaric | Initial version |
This document provides detailed information on how to use query filters for various list endpoints in the ledger service, in order to precisely narrow down records that the api caller is interested in. The filtering mechanism follows (unrelated to the actual DB the ledger uses) the syntax of MongoDB query filters and is implemented through query parameters
Supported Endpoints
Filtering can be applied to all records that have an endpoint that lists them, e.g. /intents
, /wallets
, etc.
Query Operators
At the moment, the following query operators are supported:
Operator | Explanation |
---|---|
$eq | (equal to) |
$gt | (greater than) |
$gte | (greater than or equal to) |
$lt | (less than) |
$lte | (less than or equal to) |
$in | (in array) |
$ne | (not equal to) |
Filtering Syntax
Filters are passed through query parameters. The syntax follows the pattern of some.field.path.$operator=value
. If no operator is listed, $eq
is assumed.
The path of a field is the same as in the response when fetching a record of the same type. So, if intent
has a data
property with a handle
that is a string, the path for filtering by handle is data.handle
If the query params contain multiple filters, they must all be a match for a record to be returned.
Below are some examples to illustrate how filters can be used:
The above filter will match intents where data.handle
equals my-intent
.
The above filter will match wallets the schema of which is bank-wallet
.
The above filter will match wallets that are marked as active.
Using Filters with SDK
The ledger SDK supports filtering with typings to aid with autocomplete and type-checking. Here is an example of how to use filters with the SDK:
Due to technical limitations, the only property type that does not support autocomplete and type-checks is filtering of individual items in array fields like data.claims
in intents. Exact matches may still be filtered and autocompleted with something like the following:
The below section describes the principles for filtering of items contained in arrays.
Array Property Filtering
For array properties like claims
in intents, two types of filtering can be applied:
-
Specific Index Filtering
This filter will match intents where the first claim's
amount
is greater than 20. Equivalent SDK: -
Any Element Filtering
This filter will match intents where any claim's
amount
is greater than 20. Equivalent SDK:
Known Limitations
- Some fields are not filterable, depending on record. If a user attempts to filter by a non-supported field, an error will be returned indicating which fields are not supported.
- Currently, some filtering is done in memory. Future versions of the ledger will optimize this by processing more filters in the database for efficient querying.
- For now, querying is done in query params on
GET
endpoints. Due to syntax limitations, we do not currently support$and
and$or
operators. Everything is assumed to be under one root$and
. In the future, we may introducePOST
endpoints for search, where these operators may be used to build more complex expressions.