Parameters to request records from a server.
const query = {
pageSize: 100,
sort: ["receivedAt", "ascending"],
};
Query
new(query: Query)
: Query
ofKey(key: RecordKey, baseQuery?: SingleQuery)
: Query
Create a new query to fetch the record identified by the provided key.
The query behavior can be tweaked with baseQuery
.
filter(records: Record[], query: Query)
: Record[]
toQueryString(query: Query)
: string
sort
QuerySort optional
Property on which to sort, and sort order.
Defaults to ["version.receivedAt", "descending"]
.
min
QueryDate optional
max
QueryDate optional
page_start
QueryDate optional
pageSize
int optional
Desired number of records.
Accepts values between 1 and 200, inclusive.
Default to 20.
distinct
string optional
sort_property
to deduplicate on.filter
QueryFilter optional
mode
RecordMode optional
Filter records by their mode
(synchronized or local-only).
Only used for local queries.
include_links
string[] optional
Paths of links to include in the response (comma-separated).
Supports the following special values:
entity
: Entity records for all entity links.
existential
: All records for existential record links.
Defaults to entity,existential
.
include_deleted
boolean optional
Whether to include deleted records in the response.
Defaults to false
.
proxy_to
Entity optional
QuerySort
Tuple containing a query’s sort property and sort direction.
type QuerySort = [SortProperty, SortDirection];
SortProperty
string
Property on which to sort.
Possible values:
receivedAt
createdAt
version.receivedAt
version.createdAt
Path of a Tag link marked as sort_property
to sort on.
SortDirection
enum
Sort order in which to return the records.
Possible values:
descending
ascending
QueryDate
Either a Date
, or a Date
and VersionHash
tuple.
type QueryDate = Date | [Date, VersionHash];
Multiple records may have the same value for a query’s sort property. When that happens, these records are then further sorted by their version hash in ASCII order. Providing a VersionHash
makes it possible to have a page boundary in the middle of that group of same-value records.
SingleQuery
Limited query with only parameters used when fetching a single record.
include_links
string[] optional
Paths of links to include in the response (comma-separated).
Supports the following special values:
entity
: Entity records for all entity links.
existential
: All records for existential record links.
Defaults to entity,existential
.
include_deleted
boolean optional
Whether to include deleted records in the response.
Defaults to false
.
proxy_to
Entity optional
The GET records accepts a lot of different parameters to narrow down the records to return. Using a Query
can make it a lot easier to construct the HTTP request.
In this example, we want the first 30 records, sorted by creation date in ascending order, that were created in 2024.
import {Query} from "@baqhub/sdk";
const query = {
sort: ["createdAt", "ascending"],
min: new Date(Date.UTC(2024, 0, 1)),
pageSize: 30,
};
const queryString = Query.toQueryString(query);
const url = "https://baq.run/api/alice/records" + queryString;
const response = await fetch(url);
The same query object used to build HTTP requests can also be used to filter a local collection of Records. Here we want to find the most recent Message record for each conversation.
import {Query} from "@baqhub/sdk";
const query = {
sort: ["receivedAt", "descending"],
distinct: "content.conversation",
};
const allMessageRecords = [...];
const mostRecentByConversation = Query.filter(
allMessageRecords,
query
);