@coasys/ad4m / Exports / ModelQueryBuilder
Class: ModelQueryBuilder<T>
Query builder for Ad4mModel queries. Allows building queries with a fluent interface and either running them once or subscribing to updates.
Example
const builder = Recipe.query(perspective)
.where({ category: "Dessert" })
.order({ rating: "DESC" })
.limit(10);
// Run once
const recipes = await builder.run();
// Or subscribe to updates
await builder.subscribe(recipes => {
console.log("Updated recipes:", recipes);
});
Type parameters
Name | Type |
---|---|
T | extends Ad4mModel |
Table of contents
Constructors
Properties
Methods
- collections
- count
- countSubscribe
- get
- limit
- offset
- order
- overrideModelClassName
- paginate
- paginateSubscribe
- properties
- source
- subscribe
- where
Constructors
constructor
• new ModelQueryBuilder<T
>(perspective
, ctor
, query?
)
Type parameters
Name | Type |
---|---|
T | extends Ad4mModel <T > |
Parameters
Name | Type |
---|---|
perspective | PerspectiveProxy |
ctor | typeof Ad4mModel |
query? | Query |
Defined in
model/Ad4mModel.ts:828
Properties
ctor
• Private
ctor: typeof Ad4mModel
Defined in
model/Ad4mModel.ts:826
modelClassName
• Private
modelClassName: string
= null
Defined in
model/Ad4mModel.ts:825
perspective
• Private
perspective: PerspectiveProxy
Defined in
model/Ad4mModel.ts:823
queryParams
• Private
queryParams: Query
= {}
Defined in
model/Ad4mModel.ts:824
Methods
collections
▸ collections(collections
): ModelQueryBuilder
<T
>
Specifies which collections to include in the results.
Parameters
Name | Type | Description |
---|---|---|
collections | string [] | Array of collection names to include |
Returns
The query builder for chaining
Example
.collections(["ingredients", "steps"])
Defined in
model/Ad4mModel.ts:946
count
▸ count(): Promise
<number
>
Gets the total count of matching entities.
Returns
Promise
<number
>
Total count
Example
const totalDesserts = await Recipe.query(perspective)
.where({ category: "Dessert" })
.count();
Defined in
model/Ad4mModel.ts:1021
countSubscribe
▸ countSubscribe(callback
): Promise
<number
>
Subscribes to count updates for matching entities.
Parameters
Name | Type | Description |
---|---|---|
callback | (count : number ) => void | Function to call with updated count |
Returns
Promise
<number
>
Initial count
Example
await Recipe.query(perspective)
.where({ status: "active" })
.countSubscribe(count => {
console.log("Active items:", count);
});
Defined in
model/Ad4mModel.ts:1042
get
▸ get(): Promise
<T
[]>
Executes the query once and returns the results.
Returns
Promise
<T
[]>
Array of matching entities
Example
const recipes = await Recipe.query(perspective)
.where({ category: "Dessert" })
.get();
Defined in
model/Ad4mModel.ts:968
limit
▸ limit(limit
): ModelQueryBuilder
<T
>
Sets the maximum number of results to return.
Parameters
Name | Type | Description |
---|---|---|
limit | number | Maximum number of results |
Returns
The query builder for chaining
Example
.limit(10)
Defined in
model/Ad4mModel.ts:882
offset
▸ offset(offset
): ModelQueryBuilder
<T
>
Sets the number of results to skip.
Parameters
Name | Type | Description |
---|---|---|
offset | number | Number of results to skip |
Returns
The query builder for chaining
Example
.offset(20) // Skip first 20 results
Defined in
model/Ad4mModel.ts:898
order
▸ order(orderBy
): ModelQueryBuilder
<T
>
Sets the order for the query results.
Parameters
Name | Type | Description |
---|---|---|
orderBy | Order | The ordering criteria |
Returns
The query builder for chaining
Example
.order({ createdAt: "DESC" })
Defined in
model/Ad4mModel.ts:866
overrideModelClassName
▸ overrideModelClassName(className
): ModelQueryBuilder
<T
>
Parameters
Name | Type |
---|---|
className | string |
Returns
Defined in
model/Ad4mModel.ts:951
paginate
▸ paginate(pageSize
, pageNumber
): Promise
<PaginationResult
<T
>>
Gets a page of results with pagination metadata.
Parameters
Name | Type | Description |
---|---|---|
pageSize | number | Number of items per page |
pageNumber | number | Which page to retrieve (1-based) |
Returns
Promise
<PaginationResult
<T
>>
Paginated results with metadata
Example
const page = await Recipe.query(perspective)
.where({ category: "Main" })
.paginate(10, 1);
console.log(`Page ${page.pageNumber}, ${page.results.length} of ${page.totalCount}`);
Defined in
model/Ad4mModel.ts:1070
paginateSubscribe
▸ paginateSubscribe(pageSize
, pageNumber
, callback
): Promise
<PaginationResult
<T
>>
Subscribes to paginated results updates.
Parameters
Name | Type | Description |
---|---|---|
pageSize | number | Number of items per page |
pageNumber | number | Which page to retrieve (1-based) |
callback | (results : PaginationResult <T >) => void | Function to call with updated pagination results |
Returns
Promise
<PaginationResult
<T
>>
Initial pagination results
Example
await Recipe.query(perspective)
.where({ category: "Main" })
.paginateSubscribe(10, 1, page => {
console.log("Updated page:", page.results);
});
Defined in
model/Ad4mModel.ts:1095
properties
▸ properties(properties
): ModelQueryBuilder
<T
>
Specifies which properties to include in the results.
Parameters
Name | Type | Description |
---|---|---|
properties | string [] | Array of property names to include |
Returns
The query builder for chaining
Example
.properties(["name", "description", "rating"])
Defined in
model/Ad4mModel.ts:930
source
▸ source(source
): ModelQueryBuilder
<T
>
Sets the source filter for the query.
Parameters
Name | Type | Description |
---|---|---|
source | string | The source to filter by |
Returns
The query builder for chaining
Example
.source("ad4m://self")
Defined in
model/Ad4mModel.ts:914
subscribe
▸ subscribe(callback
): Promise
<T
[]>
Subscribes to the query and receives updates when results change. Also returns initial results immediately.
Parameters
Name | Type | Description |
---|---|---|
callback | (results : T []) => void | Function to call with updated results |
Returns
Promise
<T
[]>
Initial results array
Example
await Recipe.query(perspective)
.where({ status: "cooking" })
.subscribe(recipes => {
console.log("Currently cooking:", recipes);
});
Defined in
model/Ad4mModel.ts:991
where
▸ where(conditions
): ModelQueryBuilder
<T
>
Adds where conditions to the query.
Parameters
Name | Type | Description |
---|---|---|
conditions | Where | The conditions to filter by |
Returns
The query builder for chaining
Example
.where({
category: "Dessert",
rating: { gt: 4 },
tags: ["vegan", "quick"],
published: true
})
Defined in
model/Ad4mModel.ts:850