Javascript SDK

QueryFilter

Record conditions for queries

const filter = Q.and(
  Q.author("alice.baq.run"),
  Q.tag("content.category", "books")
);

Recursive record condition, represented as a tuple.
Matches the filter­ parameter expected by the server.
Also exported as Q for ease of use.

Values

  • [“link”, link: QueryLink]

    • Require records to have a set value for a given link.
  • [“and”, filters: QueryFilter[]]

    • Require all provided filters to match.

    • An empty filters array validates the condition.

  • [“or”, filters: QueryFilter[]]

    • Require at least one of the provided filters to match.

    • An empty filters array does not validate the condition.

Static builders

Filter builders are provided for common use-cases.

  • link(queryLink: QueryLink): QueryFilter

    • Create a new filter on a link.
  • and(…filters: QueryFilter[]): QueryFilter

    • Combine multiple filters in a single one.

    • All of the provided filters must match to validate the condition.

  • or(…filters: QueryFilter[]): QueryFilter

    • Combine multiple filters in a single one.

    • At least one of the provided filters must match to validate the condition.

  • tag(path: string, tag: TagLink): QueryFilter

    • Create a new filter on a tag link at the given path.
  • entity(path: string, entity: Entity): QueryFilter

    • Create a new filter on an entity link at the given path.
  • record(path: string, recordLink: RecordLink): QueryFilter

    • Create a new filter on a record link at the given path.
  • version(path: string, versionLink: VersionLink): QueryFilter

    • Create a new filter on a version link at the given path.
  • id(recordId: RecordId): QueryFilter

    • Create a new filter on a record’s ID.
  • source(source: RecordSource): QueryFilter

    • Create a new filter on a record’s source.
  • author(entity: Entity): QueryFilter

    • Create a new filter on a record’s author.
  • type(recordType: RecordType): QueryFilter

    • Create a new filter on a record’s type.
  • empty(path: string): QueryFilter

    • Create a new filter on no link having a value at the given path.

Other static methods

  • isMatch<T>(record: Record, filter: QueryFilter<T>): boolean

    • Check whether a record matches the given filter.

    • Acts as a type guard for record as T.

Single record condition on a link, represented as a tuple.

Values

  • [“link”, value: QueryLinkValue]

    • Require any of the record’s links to have the specified value.
  • [“path_link”, path: string, value: QueryLinkValue]

    • Require a link at the given path to have the specified value.
  • [“empty_path_link”, path: string]

    • Require no value for the link at the given path.

Static builders

  • link(value: QueryLinkValue): QueryLink

    • Create a new link condition at any path for the given value.
  • pathLink(path: string, value: QueryLinkValue): QueryLink

    • Create a new link condition at a path for the given value.
  • emptyPathLink(path: string): QueryLink

    • Create a new empty link condition a the given path.

Link value for a record condition, represented as a tuple.

Values

  • [“tag”, value: TagLink]

    • Require a tag link of the specified value.
  • [“entity”, value: EntityLink]

    • Require an entity link of the specified value.
  • [“record”, value: RecordLink]

    • Require a record link of the specified value.
  • [“version”, value: VersionLink]

    • Require a version link of the specified value.

Static builders

  • tag(tag: TagLink): QueryLinkValue

    • Create a new tag link condition value.
  • entity(entity: Entity): QueryLinkValue

    • Create a new entity link condition value.
  • record(recordLink: RecordLink): QueryLinkValue

    • Create a new record link condition value.
  • version(versionLink: VersionLink): QueryLinkValue

    • Create a new version link condition value.

Filters don’t need to be combined nor wrapped to work in a Query­. Here we want all records that have user Natalie as publisher entity link in their content and only need the one filter to make it work.

import {Q} from "@baqhub/sdk";

const query = {
  filter: Q.entity("content.publisher", "natalie.baq.run");
}

A common use-case for a query filter is to restrict the records by author and type. In this example, we want all of the File records­ created by user Alice.

import {Q} from "@baqhub/sdk";
import {FileRecord} from "./baq/fileRecord.js";

const filter = Q.and(
  Q.author("alice.baq.run"),
  Q.type(FileRecord)
);

// Next step:
// Use the filter in a Query.

Filter conditions can be combined through multiple levels of AND and OR grouping, similar to an SQL query.

Here we want all files­ and folders­ in a given parent folder. We filter by author and type, but also require the link at content.parent to either be empty (root folder) or to point to the provided folder record.

import {Q} from "@baqhub/sdk";
import {FolderRecord} from "./baq/folderRecord.js";
import {FileRecord} from "./baq/fileRecord.js";

function listFolderContent(folderLink) {
  const filter = Q.and(
    Q.author("alice.baq.run"),
    Q.or(Q.type(FolderRecord), Q.type(FileRecord)),
    folderLink
      ? Q.record("content.parent", folderLink)
      : Q.empty("content.parent")
  );

  // Next step:
  // Use the filter in a Query.
}
© 2024 Quentez