Javascript SDK

Query

Parameters to request records from a server.

const query = {
  pageSize: 100,
  sort: ["receivedAt", "ascending"],
};

Static methods

  • new(query: Query): Query

    • Create a new 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[]

    • Filter an array of records with the provided query.
  • toQueryString(query: Query): string

Properties

  • sort QuerySort optional

    • Property on which to sort, and sort order.

    • Defaults to ["version.receivedAt", "descending"].

  • min QueryDate optional

    • Minimum bound for the sort property, exclusive.
  • max QueryDate optional

    • Maximum bound for the sort property, exclusive.
  • page_start QueryDate optional

    • Starting point of the sort property for the current page, exclusive.
  • pageSize int optional

    • Desired number of records.

    • Accepts values between 1 and 200, inclusive.

    • Default to 20.

  • distinct string optional

    • Path of a Tag link­ marked as sort_property to deduplicate on.
  • filter QueryFilter optional

    • Conditions to filter the records with.
  • 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

    • Other user’s entity to proxy the request to.

Tuple containing a query’s sort property and sort direction.

type QuerySort = [SortProperty, SortDirection];

Members

  • 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

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.

Limited query with only parameters used when fetching a single record.

Properties

  • 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

    • Other user’s entity to proxy the request to.

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
);
© 2024 Quentez