Cosmoose
API Reference

QueryBuilder

API reference for QueryBuilder, Cursor, and TokenPaginationResult

Class: QueryBuilder<T, Q>

A fluent, chainable query builder that generates parameterized Cosmos DB SQL queries. Implements PromiseLike so it can be awaited directly.

index.ts
// Obtained from Model methods
const query = User.find({ role: 'admin' });

The type parameter Q determines the return type when the query is awaited.


Chainable Methods

sort(fields)

Signature
sort(fields: Record<string, 1 | -1>): this

Set sort order. 1 for ascending, -1 for descending.

index.ts
User.find().sort({ name: 1, createdAt: -1 });

limit(value)

Signature
limit(value: number): this

Set maximum number of results.


offset(value)

Signature
offset(value: number): this

Set the number of results to skip (for offset-based pagination).


Execution Methods

exec()

Signature
async exec(): Promise<Result>

Executes the query and returns the result. The return type depends on Q:

QReturn Type
'find'Document<T>[]
'findOne'Document<T> | undefined
'findAll'Document<T>[]
'count'number
'findAsCursor'Cursor<T>
'findAsTokenPagination'TokenPaginationResult<T>

then()

Signature
then(onfulfilled?, onrejected?): Promise<Result>

Implements PromiseLike so queries can be awaited directly:

index.ts
const users = await User.find({ role: 'admin' });

buildQuery()

Signature
buildQuery(): SqlQuerySpec

Returns the generated SQL query specification without executing it. Useful for debugging:

index.ts
const spec = User.find({ age: { $gt: 18 } }).buildQuery();
console.log(spec.query);      // SELECT * FROM root r WHERE r.age > @age_0 ...
console.log(spec.parameters); // [{ name: '@age_0', value: 18 }]

Filter Operators

Filters are passed as the first argument to query methods:

OperatorExampleSQL
equality{ role: 'admin' }r.role = @role
$gt{ age: { $gt: 18 } }r.age > @age
$gte{ age: { $gte: 18 } }r.age >= @age
$lt{ age: { $lt: 65 } }r.age < @age
$lte{ age: { $lte: 65 } }r.age <= @age
$in{ role: { $in: ['a', 'b'] } }r.role IN (@r_0, @r_1)
$or{ $or: [{...}, {...}] }(...) OR (...)
$and{ $and: [{...}, {...}] }... AND ...

Interface: Cursor<T>

An iterator for processing large result sets in batches.

types.ts
interface Cursor<T> {
  each(fn: (doc: Document<T>, index: number) => Promise<void> | void): Promise<void>;
}

each(fn)

Iterates over all matching documents, fetching in batches (configurable via batchSize). The callback receives each document and its zero-based index.

index.ts
const cursor = await User.findAsCursor({}, { batchSize: 100 });
await cursor.each(async (doc, index) => {
  // process each document
});

Interface: TokenPaginationResult<T>

Result of a token-based paginated query.

types.ts
interface TokenPaginationResult<T> {
  data: Document<T>[];
  pagination: {
    next: string | undefined;
  };
}
PropertyTypeDescription
dataDocument<T>[]Documents for this page
pagination.nextstring | undefinedContinuation token for the next page, or undefined if no more pages

The token is a base64url-encoded Cosmos DB continuation token.

On this page