Record conditions for queries
const filter = Q.and(
Q.author("alice.baq.run"),
Q.tag("content.category", "books")
);
QueryFilter
Recursive record condition, represented as a tuple.
Matches the filter
parameter expected by the server.
Also exported as Q
for ease of use.
[“link”, link: QueryLink]
[“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.
Filter builders are provided for common use-cases.
link(queryLink: QueryLink)
: QueryFilter
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
entity(path: string, entity: Entity)
: QueryFilter
record(path: string, recordLink: RecordLink)
: QueryFilter
version(path: string, versionLink: VersionLink)
: QueryFilter
id(recordId: RecordId)
: QueryFilter
source(source: RecordSource)
: QueryFilter
author(entity: Entity)
: QueryFilter
type(recordType: RecordType)
: QueryFilter
empty(path: string)
: QueryFilter
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
.
QueryLink
Single record condition on a link, represented as a tuple.
[“link”, value: QueryLinkValue]
[“path_link”, path: string, value: QueryLinkValue]
[“empty_path_link”, path: string]
link(value: QueryLinkValue)
: QueryLink
pathLink(path: string, value: QueryLinkValue)
: QueryLink
emptyPathLink(path: string)
: QueryLink
QueryLinkValue
Link value for a record condition, represented as a tuple.
[“tag”, value: TagLink]
[“entity”, value: EntityLink]
[“record”, value: RecordLink]
[“version”, value: VersionLink]
tag(tag: TagLink)
: QueryLinkValue
entity(entity: Entity)
: QueryLinkValue
record(recordLink: RecordLink)
: QueryLinkValue
version(versionLink: VersionLink)
: QueryLinkValue
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.
}