Class: ReactiveQueryBuilder<OM, T, PK, R, H, IT>
A fluent query builder for ReactiveModel that provides SQL-like query functionality.
Remarks
This class provides a chainable API for building complex database queries including:
- Where clauses with various operators (
=,!=,>,<,LIKE, etc.) - Logical combinations (AND, OR)
- Ordering and pagination
- Relationship eager loading
- Data modifications (update, delete, increment, decrement)
Example
// Basic query with where clause
const users = await User.query().where("age", ">", 18).orderBy("name").fetch();
// Complex query with relationships
const posts = await Post.query()
.where("published", true)
.whereBetween("views", [100, 1000])
.with("author")
.forPage(1, 20);Type Parameters
| Type Parameter | Default type | Description |
|---|---|---|
OM extends BaseObjectMap | - | - |
T extends PlainObject | - | The type of the model being queried |
PK extends StringKeyOf<T> | - | The type of the primary key of the model |
R extends Record<string, RelationshipConfiguration> | - | The type of relationships configuration |
H extends Required<ReactiveDatabaseOptions<any>["hooks"]> | - | - |
IT extends ReactiveModel<T, PK, R> | InstanceType<ReactiveModelConstructor<OM, T, PK, R, H>> | - |
Implements
PromiseLike<any>
Methods
andWhere()
Call Signature
andWhere(callback: ReactiveQueryBuilderSubQuery<OM, T, PK, R, H>): this;Add an AND condition using a callback function for grouped conditions. Allows for complex nested queries with multiple conditions.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | ReactiveQueryBuilderSubQuery<OM, T, PK, R, H> | Function that builds a group of conditions |
Returns
this
See
- ReactiveQueryBuilderSubQuery for callback function type
- where for base query building
Example
query
.where("active", true)
.andWhere((q) => q.where("role", "admin").where("permissionLevel", ">=", 5));Call Signature
andWhere(conditions: Record<Extract<keyof T, string>, unknown>): this;Add an AND condition using an object of key-value pairs. All conditions in the object are combined with AND logic.
Parameters
| Parameter | Type | Description |
|---|---|---|
conditions | Record<Extract<keyof T, string>, unknown> | Object where keys are column names and values are the expected values |
Returns
this
Example
query.where("active", true).andWhere({
verified: true,
accountType: "premium",
});Call Signature
andWhere<K>(key: K, value: T[K]): this;Add an AND condition comparing a column value for equality. Shorthand for andWhere(key, '=', value).
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | K | Column name to check |
value | T[K] | Value to compare against |
Returns
this
Example
query.where("active", true).andWhere("department", "sales");Call Signature
andWhere<K>(
key: K,
operator:
| "is"
| "="
| "!="
| "is not"
| "<"
| "<="
| ">"
| ">="
| "like"
| "not like"
| "<>"
| "in"
| "not in"
| "between"
| "not between"
| "null"
| "not null"
| "exists"
| "not exists"
| "ilike"
| "not ilike",
value: any): this;Add an AND condition using a comparison operator. Supports various operators like >, <, >=, <=, !=, like, etc.
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | K | Column name to check |
operator | | "is" | "=" | "!=" | "is not" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "<>" | "in" | "not in" | "between" | "not between" | "null" | "not null" | "exists" | "not exists" | "ilike" | "not ilike" | Comparison operator |
value | any | Value to compare against |
Returns
this
Example
query
.where("active", true)
.andWhere("experience", ">=", 5)
.andWhere("rating", ">", 4.5);Call Signature
andWhere(value: boolean): this;Add an AND condition using a boolean value. true is equivalent to whereExists, false to whereNotExists.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | boolean | boolean condition |
Returns
this
andWhereBetween()
andWhereBetween(key: Extract<keyof T>, value: any): this;Alias for whereBetween that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value | any |
Returns
this
Example
const results = await query
.where("active", true)
.andWhereBetween("price", [10, 100])
.fetch();andWhereExists()
andWhereExists(key: Extract<keyof T>): this;Alias for whereExists that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | StringKeyOf the column name to check for existence |
Returns
this
The query builder instance for chaining
Example
const results = await query
.where("active", true)
.andWhereExists("email")
.fetch();andWhereILike()
andWhereILike(key: Extract<keyof T>, value?: any): this;Alias for whereILike that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value? | any |
Returns
this
Example
const results = await query
.where("active", true)
.andWhereILike("tags", "%featured%")
.fetch();andWhereIn()
andWhereIn(key: Extract<keyof T>, value: any): this;Alias for whereIn that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value | any |
Returns
this
Example
const results = await query
.where("active", true)
.andWhereIn("status", ["pending", "processing"])
.fetch();andWhereLike()
andWhereLike(key: Extract<keyof T>, value?: any): this;Alias for whereLike that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value? | any |
Returns
this
Example
const results = await query
.where("active", true)
.andWhereLike("name", "John%")
.fetch();andWhereNot()
Call Signature
andWhereNot(callback: ReactiveQueryBuilderSubQuery<OM, T, PK, R, H>): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Parameters
| Parameter | Type |
|---|---|
callback | ReactiveQueryBuilderSubQuery<OM, T, PK, R, H> |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();Call Signature
andWhereNot(conditions: Partial<T>): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Parameters
| Parameter | Type |
|---|---|
conditions | Partial<T> |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();Call Signature
andWhereNot<K>(key: K, value: T[K]): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type |
|---|---|
key | K |
value | T[K] |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();Call Signature
andWhereNot<K>(
key: K,
operator:
| "is"
| "="
| "!="
| "is not"
| "<"
| "<="
| ">"
| ">="
| "like"
| "not like"
| "<>"
| "in"
| "not in"
| "between"
| "not between"
| "null"
| "not null"
| "exists"
| "not exists"
| "ilike"
| "not ilike",
value: any): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type |
|---|---|
key | K |
operator | | "is" | "=" | "!=" | "is not" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "<>" | "in" | "not in" | "between" | "not between" | "null" | "not null" | "exists" | "not exists" | "ilike" | "not ilike" |
value | any |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();Call Signature
andWhereNot(value: boolean): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Parameters
| Parameter | Type |
|---|---|
value | boolean |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();andWhereNotBetween()
andWhereNotBetween(key: Extract<keyof T>, value: any): this;Alias for whereNotBetween that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value | any |
Returns
this
Example
const results = await query
.where("active", true)
.andWhereNotBetween("price", [0, 10])
.fetch();andWhereNotExists()
andWhereNotExists(key: Extract<keyof T>): this;Alias for whereNotExists that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | StringKeyOf the column name to check for non-existence |
Returns
this
The query builder instance for chaining
Example
const results = await query
.where("active", true)
.andWhereNotExists("deletedAt")
.fetch();andWhereNotIn()
andWhereNotIn(key: Extract<keyof T>, value: any): this;Alias for whereNotIn that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value | any |
Returns
this
Example
const results = await query
.where("active", true)
.andWhereNotIn("category", ["archived", "deleted"])
.fetch();andWhereNotNull()
andWhereNotNull(key: Extract<keyof T>): this;Alias for whereNotNull that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
Returns
this
Example
const results = await query
.where("active", true)
.andWhereNotNull("verifiedAt")
.fetch();andWhereNull()
andWhereNull(key: Extract<keyof T>): this;Alias for whereNull that makes queries more readable when chaining conditions.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
Returns
this
Example
const results = await query
.where("active", true)
.andWhereNull("deletedAt")
.fetch();clear()
clear(): ReactiveQueryBuilder<OM, T, PK, R, H, IT>;Clears all query conditions and clauses, resetting the query builder to its initial state. Removes all where conditions, ordering, limits, and offsets.
Returns
ReactiveQueryBuilder<OM, T, PK, R, H, IT>
The query builder instance for chaining
Example
// Build a complex query
const query = Model.query().where("score", ">", 80).orderBy("name").limit(10);
// Clear all conditions and start fresh
query.clear();
const count = await query.count(); // counts all recordsclone()
clone(introspector?: ReactiveQueryBuilderIntrospector<OM, T, PK, R, H>): ReactiveQueryBuilder<OM, T, PK, R, H, ReactiveModel<T, PK, R>>;Creates a copy of the current query builder with all its conditions and clauses. Useful when you want to reuse a base query but add different conditions.
Parameters
| Parameter | Type | Description |
|---|---|---|
introspector? | ReactiveQueryBuilderIntrospector<OM, T, PK, R, H> | Optional introspector for testing and debugging |
Returns
ReactiveQueryBuilder<OM, T, PK, R, H, ReactiveModel<T, PK, R>>
A new query builder instance with the same conditions
Example
// Create a base query
const baseQuery = Model.query().where("active", true);
// Clone and add more conditions
const adminQuery = baseQuery.clone().where("role", "admin");
const userQuery = baseQuery.clone().where("role", "user");
// Original query remains unchanged
const activeRecords = await baseQuery.fetch();count()
count(): Promise<number>;Gets the total number of records that match the query conditions. Can be used with where clauses to count filtered results.
Returns
Promise<number>
Promise resolving to the count of matching records
Example
// Count all records
const total = await query.count();
// Count filtered records
const highScores = await query.where("score", ">", 80).count();decrement()
decrement(column: StringKeyOf<T>, amount: number): Promise<IT[]>;Decrements a numeric column value for all matching records. Only affects columns with number values.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
column | StringKeyOf<T> | undefined | Name of the numeric column to decrement |
amount | number | 1 | Amount to decrement by (default: 1) |
Returns
Promise<IT[]>
Promise resolving to array of updated records
Example
// Decrement lives by 1 for all active players
const updated = await query.where("active", true).decrement("lives");
// Decrement stock by 5 for specific products
const updated = await query
.whereIn("productId", ["A1", "B2"])
.decrement("stock", 5);delete()
delete(): Promise<void>;Deletes all records that match the current query conditions.
Returns
Promise<void>
Promise that resolves when all matching records have been deleted
DANGER
This operation cannot be undone.
Example
// Delete all inactive users
await query.where("active", false).delete();
// Delete users with specific roles
await query.whereIn("role", ["guest", "blocked"]).delete();fetch()
fetch(): Promise<undefined | number | IT | IT[]>;Executes the query and returns the results. Used internally by pagination methods like forPage(), first(), and last().
Returns
Promise<undefined | number | IT | IT[]>
Promise resolving to an array of matching records
Example
// Basic fetch of all records
const all = await query.fetch();
// Fetch with filtering and sorting
const filtered = await query.where("score", ">", 80).orderBy("name").fetch();first()
first(): Promise<undefined | IT>;Gets the first record that matches the query. Commonly used with orderBy() to get the record with the lowest value.
Returns
Promise<undefined | IT>
Promise resolving to the first matching record or undefined if none found
Example
// Get record with lowest score
const lowest = await query.orderBy("score").first();
// Returns: 'Test1'forPage()
forPage(page: number, perPage: number): Promise<undefined | number | IT | IT[]>;Paginates through records by applying offset and limit internally. Returns a subset of records for the requested page.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
page | number | undefined | The page number (1-based) |
perPage | number | 20 | Number of records per page (default: 20) |
Returns
Promise<undefined | number | IT | IT[]>
Promise resolving to array of records for the requested page
Example
// Get first page with 2 records per page
const page1 = await query.orderBy("score").forPage(1, 2);
// Returns first 2 records: ['Test1', 'Test2']
// Get second page with 2 records per page
const page2 = await query.orderBy("score").forPage(2, 2);
// Returns next 2 records: ['Test3', 'Test4']increment()
increment(column: StringKeyOf<T>, amount: number): Promise<IT[]>;Increments a numeric column value for all matching records. Only affects columns with number values.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
column | StringKeyOf<T> | undefined | Name of the numeric column to increment |
amount | number | 1 | Amount to increment by (default: 1) |
Returns
Promise<IT[]>
Promise resolving to array of updated records
Example
// Increment score by 1 for all active users
const updated = await query.where("active", true).increment("score");
// Increment points by 10 for high scorers
const updated = await query.where("score", ">", 90).increment("points", 10);last()
last(): Promise<undefined | IT>;Gets the last record that matches the query. Commonly used with orderBy() to get the record with the highest value.
Returns
Promise<undefined | IT>
Promise resolving to the last matching record or undefined if none found
Example
// Get record with highest score
const highest = await query.orderBy("score").last();
// Returns: 'Test4'limit()
limit(limit: number): ReactiveQueryBuilder<OM, T, PK, R, H, IT>;Limits the number of records returned by the query. Used internally by forPage() for pagination.
Parameters
| Parameter | Type | Description |
|---|---|---|
limit | number | The maximum number of records to return |
Returns
ReactiveQueryBuilder<OM, T, PK, R, H, IT>
The query builder instance for chaining
Example
// Get only the first 2 records
const records = await query.orderBy("score").limit(2).fetch();
// Returns: ['Test1', 'Test2']offset()
offset(offset: number): ReactiveQueryBuilder<OM, T, PK, R, H, IT>;Skips a specified number of records before starting to return results. Used internally by forPage() for pagination.
Parameters
| Parameter | Type | Description |
|---|---|---|
offset | number | The number of records to skip |
Returns
ReactiveQueryBuilder<OM, T, PK, R, H, IT>
The query builder instance for chaining
Example
// Skip first 2 records and get next 2
const records = await query.orderBy("score").offset(2).limit(2).fetch();
// Returns: ['Test3', 'Test4']orderBy()
orderBy(key: Extract<keyof T>, direction: "asc" | "desc"): ReactiveQueryBuilder<OM, T, PK, R, H, IT>;Orders the query results by a specified column. Uses Dexie's native orderBy functionality for better performance when possible.
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
key | Extract<keyof T> | undefined | The column name to sort by |
direction | "asc" | "desc" | 'asc' | The sort direction ('asc' or 'desc') |
Returns
ReactiveQueryBuilder<OM, T, PK, R, H, IT>
The query builder instance for chaining
Example
// Sort by score ascending
const records = await query.orderBy("score").fetch();
// Returns: ['Test1', 'Test2', 'Test3', 'Test4']
// Sort by score descending
const records = await query.orderBy("score", "desc").fetch();
// Returns: ['Test4', 'Test3', 'Test2', 'Test1']orWhere()
Call Signature
orWhere(callback: ReactiveQueryBuilderSubQuery<OM, T, PK, R, H>): this;Add an OR condition using a callback function for grouped conditions. Allows for complex nested queries with multiple conditions.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | ReactiveQueryBuilderSubQuery<OM, T, PK, R, H> | Function that builds a group of conditions |
Returns
this
Example
query
.where("status", "active")
.orWhere((q) => q.where("role", "admin").where("permissionLevel", ">=", 5)); // status = 'active' OR (role = 'admin' AND permissionLevel >= 5)Call Signature
orWhere(conditions: Record<Extract<keyof T, string>, unknown>): this;Add an OR condition using an object of key-value pairs. All conditions in the object are combined with AND logic, then joined with OR.
Parameters
| Parameter | Type | Description |
|---|---|---|
conditions | Record<Extract<keyof T, string>, unknown> | Object where keys are column names and values are the expected values |
Returns
this
Example
query.where("status", "active").orWhere({
role: "admin",
verified: true,
}); // status = 'active' OR (role = 'admin' AND verified = true)Call Signature
orWhere<K>(key: K, value: T[K]): this;Add an OR condition comparing a column value for equality. Shorthand for orWhere(key, '=', value).
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | K | Column name to check |
value | T[K] | Value to compare against |
Returns
this
Example
query.where("role", "user").orWhere("department", "IT"); // role = 'user' OR department = 'IT'Call Signature
orWhere<K>(
key: K,
operator:
| "is"
| "="
| "!="
| "is not"
| "<"
| "<="
| ">"
| ">="
| "like"
| "not like"
| "<>"
| "in"
| "not in"
| "between"
| "not between"
| "null"
| "not null"
| "exists"
| "not exists"
| "ilike"
| "not ilike",
value: any): this;Add an OR condition using a comparison operator. Supports various operators like >, <, >=, <=, !=, like, etc.
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | K | Column name to check |
operator | | "is" | "=" | "!=" | "is not" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "<>" | "in" | "not in" | "between" | "not between" | "null" | "not null" | "exists" | "not exists" | "ilike" | "not ilike" | Comparison operator |
value | any | Value to compare against |
Returns
this
Example
query
.where("status", "active")
.orWhere("score", ">=", 100)
.orWhere("rating", ">", 4.5); // status = 'active' OR score >= 100 OR rating > 4.5Call Signature
orWhere(value: boolean): this;Add an OR condition using a boolean value. true is equivalent to orWhereExists, false to orWhereNotExists.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | boolean | boolean condition |
Returns
this
orWhereBetween()
orWhereBetween(key: Extract<keyof T>, value: any): this;Adds an OR condition checking if a column's value is between two values.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value | any |
Returns
this
Example
const results = await query
.where("category", "electronics")
.orWhereBetween("rating", [4, 5])
.fetch();orWhereExists()
orWhereExists(key: Extract<keyof T>): this;Adds an OR condition filtering records where a column's value exists.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | StringKeyOf the column name to check for existence |
Returns
this
The query builder instance for chaining
See
Example
const results = await query
.where("status", "pending")
.orWhereExists("processedAt")
.fetch();orWhereILike()
orWhereILike(key: Extract<keyof T>, value?: any): this;Adds an OR condition checking if a column matches a pattern (case-insensitive).
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value? | any |
Returns
this
Example
const results = await query
.where("category", "electronics")
.orWhereILike("brand", "%apple%")
.fetch();orWhereIn()
orWhereIn(key: Extract<keyof T>, value: any): this;Adds an OR condition checking if a column's value is in the given array.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value | any |
Returns
this
Example
const results = await query
.where("department", "sales")
.orWhereIn("role", ["manager", "lead"])
.fetch();orWhereLike()
orWhereLike(key: Extract<keyof T>, value?: any): this;Adds an OR condition checking if a column matches a pattern (case-sensitive).
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value? | any |
Returns
this
Example
const results = await query
.where("category", "phones")
.orWhereLike("description", "%wireless%")
.fetch();orWhereNot()
Call Signature
orWhereNot(callback: ReactiveQueryBuilderSubQuery<OM, T, PK, R, H>): this;Adds an OR where clause that negates the condition. Matches records that satisfy either the previous conditions OR do NOT match this condition.
Parameters
| Parameter | Type |
|---|---|
callback | ReactiveQueryBuilderSubQuery<OM, T, PK, R, H> |
Returns
this
Example
// Find active users OR non-basic accounts
const users = await query
.where("active", true)
.orWhereNot("accountType", "basic")
.fetch();
// Complex OR NOT conditions
const products = await query
.where("featured", true)
.orWhereNot((query) => query.where("price", "<", 100).where("rating", "<", 4))
.fetch();Call Signature
orWhereNot(conditions: Partial<T>): this;Adds an OR where clause that negates the condition. Matches records that satisfy either the previous conditions OR do NOT match this condition.
Parameters
| Parameter | Type |
|---|---|
conditions | Partial<T> |
Returns
this
Example
// Find active users OR non-basic accounts
const users = await query
.where("active", true)
.orWhereNot("accountType", "basic")
.fetch();
// Complex OR NOT conditions
const products = await query
.where("featured", true)
.orWhereNot((query) => query.where("price", "<", 100).where("rating", "<", 4))
.fetch();Call Signature
orWhereNot<K>(key: K, value: T[K]): this;Adds an OR where clause that negates the condition. Matches records that satisfy either the previous conditions OR do NOT match this condition.
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type |
|---|---|
key | K |
value | T[K] |
Returns
this
Example
// Find active users OR non-basic accounts
const users = await query
.where("active", true)
.orWhereNot("accountType", "basic")
.fetch();
// Complex OR NOT conditions
const products = await query
.where("featured", true)
.orWhereNot((query) => query.where("price", "<", 100).where("rating", "<", 4))
.fetch();Call Signature
orWhereNot<K>(
key: K,
operator:
| "is"
| "="
| "!="
| "is not"
| "<"
| "<="
| ">"
| ">="
| "like"
| "not like"
| "<>"
| "in"
| "not in"
| "between"
| "not between"
| "null"
| "not null"
| "exists"
| "not exists"
| "ilike"
| "not ilike",
value: any): this;Adds an OR where clause that negates the condition. Matches records that satisfy either the previous conditions OR do NOT match this condition.
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type |
|---|---|
key | K |
operator | | "is" | "=" | "!=" | "is not" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "<>" | "in" | "not in" | "between" | "not between" | "null" | "not null" | "exists" | "not exists" | "ilike" | "not ilike" |
value | any |
Returns
this
Example
// Find active users OR non-basic accounts
const users = await query
.where("active", true)
.orWhereNot("accountType", "basic")
.fetch();
// Complex OR NOT conditions
const products = await query
.where("featured", true)
.orWhereNot((query) => query.where("price", "<", 100).where("rating", "<", 4))
.fetch();Call Signature
orWhereNot(value: boolean): this;Adds an OR where clause that negates the condition. Matches records that satisfy either the previous conditions OR do NOT match this condition.
Parameters
| Parameter | Type |
|---|---|
value | boolean |
Returns
this
Example
// Find active users OR non-basic accounts
const users = await query
.where("active", true)
.orWhereNot("accountType", "basic")
.fetch();
// Complex OR NOT conditions
const products = await query
.where("featured", true)
.orWhereNot((query) => query.where("price", "<", 100).where("rating", "<", 4))
.fetch();orWhereNotBetween()
orWhereNotBetween(key: Extract<keyof T>, value: any): this;Adds an OR condition checking if a column's value is NOT between two values.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value | any |
Returns
this
Example
const results = await query
.where("category", "premium")
.orWhereNotBetween("rating", [1, 3])
.fetch();orWhereNotExists()
orWhereNotExists(key: Extract<keyof T>): this;Adds an OR condition filtering records where a column's value does not exist.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | StringKeyOf the column name to check for non-existence |
Returns
this
The query builder instance for chaining
See
Example
const results = await query
.where("status", "pending")
.orWhereNotExists("processedAt")
.fetch();orWhereNotIn()
orWhereNotIn(key: Extract<keyof T>, value: any): this;Adds an OR condition checking if a column's value is NOT in the given array.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
value | any |
Returns
this
Example
const results = await query
.where("department", "sales")
.orWhereNotIn("status", ["inactive", "suspended"])
.fetch();orWhereNotNull()
orWhereNotNull(key: Extract<keyof T>): this;Adds an OR condition checking if a column's value is NOT NULL.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
Returns
this
Example
const results = await query
.where("status", "pending")
.orWhereNotNull("processedAt")
.fetch();orWhereNull()
orWhereNull(key: Extract<keyof T>): this;Adds an OR condition checking if a column's value is NULL.
Parameters
| Parameter | Type |
|---|---|
key | Extract<keyof T> |
Returns
this
Example
const results = await query
.where("status", "pending")
.orWhereNull("processedAt")
.fetch();reactive()
reactive(): {
count: () => Promise<ReactiveQueryResult<T, PK, R, H, number>>;
fetch: () => Promise<ReactiveQueryCollection<T, PK, R, H, ReactiveModel<T, PK, R>>>;
first: () => Promise<ReactiveQueryResult<T, PK, R, H, ReactiveModel<T, PK, R>>>;
forPage: (page: number, perPage: number) => Promise<ReactiveQueryCollection<T, PK, R, H, ReactiveModel<T, PK, R>>>;
last: () => Promise<ReactiveQueryResult<T, PK, R, H, ReactiveModel<T, PK, R>>>;
};Returns a set of reactive query response factories for this query builder.
The returned object provides methods to create reactive query responses for fetching collections, single results (first/last), and paginated results.
Each method returns a new instance of a reactive response class that will automatically update when relevant changes occur in the underlying data.
Returns
{
count: () => Promise<ReactiveQueryResult<T, PK, R, H, number>>;
fetch: () =>
Promise<ReactiveQueryCollection<T, PK, R, H, ReactiveModel<T, PK, R>>>;
first: () =>
Promise<ReactiveQueryResult<T, PK, R, H, ReactiveModel<T, PK, R>>>;
forPage: (page: number, perPage: number) =>
Promise<ReactiveQueryCollection<T, PK, R, H, ReactiveModel<T, PK, R>>>;
last: () =>
Promise<ReactiveQueryResult<T, PK, R, H, ReactiveModel<T, PK, R>>>;
}An object with methods to create reactive query responses:
fetch: Returns a reactive collection response for the current query.first: Returns a reactive response for the first matching record.last: Returns a reactive response for the last matching record.forPage: Returns a reactive collection response for a specific page.count: Returns a reactive response for the count of matching records.
| Name | Type | Description |
|---|---|---|
count() | () => Promise<ReactiveQueryResult<T, PK, R, H, number>> | Creates a reactive response for the count of matching records. |
fetch() | () => Promise<ReactiveQueryCollection<T, PK, R, H, ReactiveModel<T, PK, R>>> | Creates a reactive response for the current query collection. |
first() | () => Promise<ReactiveQueryResult<T, PK, R, H, ReactiveModel<T, PK, R>>> | Creates a reactive response for the first matching record. |
forPage() | (page: number, perPage: number) => Promise<ReactiveQueryCollection<T, PK, R, H, ReactiveModel<T, PK, R>>> | Creates a reactive response for a specific page of results. |
last() | () => Promise<ReactiveQueryResult<T, PK, R, H, ReactiveModel<T, PK, R>>> | Creates a reactive response for the last matching record. |
Example
// Get a reactive collection
const collection = await query.where('active', true).reactive().fetch()
collection.value // Array of matching records
collection.on('next', users => { ... })
// Get a reactive first result
const firstUser = await query.where('active', true).reactive().first()
firstUser.value // First matching record or undefined
firstUser.on('next', user => { ... })
// Get a reactive paginated collection
const page1 = await query.where('active', true).reactive().forPage(1, 10)
page1.value // First 10 matching records
page1.on('next', users => { ... })update()
update(data: Partial<T>): Promise<IT[]>;Updates all records that match the current query conditions with the given data. Only modifies the specified fields, leaving others unchanged.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | Partial<T> | Partial data containing fields to update |
Returns
Promise<IT[]>
Promise resolving to array of updated records
Example
// Update status of all active users
const updated = await query
.where("active", true)
.update({ status: "verified" });
// Update multiple fields with conditions
const updated = await query.where("score", ">", 90).update({
rank: "expert",
verified: true,
});where()
Call Signature
where(callback: ReactiveQueryBuilderSubQuery<OM, T, PK, R, H>): this;Filter records using a callback function for grouped conditions. Allows for complex nested queries with multiple conditions.
Parameters
| Parameter | Type | Description |
|---|---|---|
callback | ReactiveQueryBuilderSubQuery<OM, T, PK, R, H> | Function that builds a group of conditions |
Returns
this
Example
query.where((q) =>
q.where("role", "admin").orWhere("permissions", "includes", "manage_users"),
);Call Signature
where(conditions: Record<Extract<keyof T, string>, unknown>): this;Filter records using an object of key-value pairs. All conditions are combined with AND logic.
Parameters
| Parameter | Type | Description |
|---|---|---|
conditions | Record<Extract<keyof T, string>, unknown> | Object where keys are column names and values are the expected values |
Returns
this
Example
query.where({
status: "active",
type: "premium",
verified: true,
});Call Signature
where<K>(key: K, value: T[K]): this;Filter records by comparing a column value for equality. Shorthand for where(key, '=', value).
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | K | Column name to check |
value | T[K] | Value to compare against |
Returns
this
Example
query.where("status", "active");Call Signature
where<K>(
key: K,
operator:
| "is"
| "="
| "!="
| "is not"
| "<"
| "<="
| ">"
| ">="
| "like"
| "not like"
| "<>"
| "in"
| "not in"
| "between"
| "not between"
| "null"
| "not null"
| "exists"
| "not exists"
| "ilike"
| "not ilike",
value: any): this;Filter records using a comparison operator. Supports various operators like >, <, >=, <=, !=, like, etc.
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type | Description |
|---|---|---|
key | K | Column name to check |
operator | | "is" | "=" | "!=" | "is not" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "<>" | "in" | "not in" | "between" | "not between" | "null" | "not null" | "exists" | "not exists" | "ilike" | "not ilike" | Comparison operator |
value | any | Value to compare against |
Returns
this
Example
query.where("age", ">=", 18);
query.where("name", "like", "John%");Call Signature
where(value: boolean): this;Filter records using a boolean value. true is equivalent to whereExists, false to whereNotExists.
Parameters
| Parameter | Type | Description |
|---|---|---|
value | boolean | boolean condition |
Returns
this
whereBetween()
whereBetween(key: Extract<keyof T>, value: [any, any]): this;Filters records where a column's value is between two values (inclusive). Uses Dexie's native between method for better performance when possible.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | The column name to check |
value | [any, any] | Array containing [min, max] values |
Returns
this
The query builder instance for chaining
Example
// Find users with age between 18 and 30
const users = await query.whereBetween("age", [18, 30]).fetch();
// Find orders within a date range
const orders = await query
.whereBetween("createdAt", [startDate, endDate])
.fetch();whereExists()
whereExists(key: Extract<keyof T>): this;Filters records where a column's value exists (is not null or undefined).
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | StringKeyOf the column name to check for existence |
Returns
this
The query builder instance for chaining
See
Example
// Find records where 'email' field exists
const results = await query.whereExists("email").fetch();whereILike()
whereILike(key: Extract<keyof T>, value?: any): this;Filters records where a column's value matches a pattern (case-insensitive). Supports SQL LIKE patterns: % for any characters, _ for single character.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | The column name to check |
value? | any | The pattern to match against |
Returns
this
The query builder instance for chaining
Example
// Find users with name containing 'john' (any case)
const users = await query.whereILike("name", "%john%").fetch();
// Find products with description containing 'premium'
const products = await query.whereILike("description", "%premium%").fetch();whereIn()
whereIn(key: Extract<keyof T>, value: any[]): this;Filters records where a column's value is in the given array. Uses Dexie's native anyOf method for better performance when possible.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | The column name to check |
value | any[] | Array of values to match against |
Returns
this
The query builder instance for chaining
Example
// Find users with specific roles
const admins = await query.whereIn("role", ["admin", "superadmin"]).fetch();
// Find products in specific categories
const products = await query.whereIn("categoryId", [1, 2, 3]).fetch();whereLike()
whereLike(key: Extract<keyof T>, value: any): this;Filters records where a column's value matches a pattern (case-sensitive). Supports SQL LIKE patterns: % for any characters, _ for single character.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | The column name to check |
value | any | The pattern to match against |
Returns
this
The query builder instance for chaining
Example
// Find users with email ending in @gmail.com
const users = await query.whereLike("email", "%@gmail.com").fetch();
// Find products with names starting with 'iPhone'
const products = await query.whereLike("name", "iPhone%").fetch();whereNot()
Call Signature
whereNot(callback: ReactiveQueryBuilderSubQuery<OM, T, PK, R, H>): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Parameters
| Parameter | Type |
|---|---|
callback | ReactiveQueryBuilderSubQuery<OM, T, PK, R, H> |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();Call Signature
whereNot(conditions: Partial<T>): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Parameters
| Parameter | Type |
|---|---|
conditions | Partial<T> |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();Call Signature
whereNot<K>(key: K, value: T[K]): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type |
|---|---|
key | K |
value | T[K] |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();Call Signature
whereNot<K>(
key: K,
operator:
| "is"
| "="
| "!="
| "is not"
| "<"
| "<="
| ">"
| ">="
| "like"
| "not like"
| "<>"
| "in"
| "not in"
| "between"
| "not between"
| "null"
| "not null"
| "exists"
| "not exists"
| "ilike"
| "not ilike",
value: any): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Type Parameters
| Type Parameter |
|---|
K extends string |
Parameters
| Parameter | Type |
|---|---|
key | K |
operator | | "is" | "=" | "!=" | "is not" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "<>" | "in" | "not in" | "between" | "not between" | "null" | "not null" | "exists" | "not exists" | "ilike" | "not ilike" |
value | any |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();Call Signature
whereNot(value: boolean): this;Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.
Parameters
| Parameter | Type |
|---|---|
value | boolean |
Returns
this
Example
// Find non-admin users
const users = await query.whereNot("role", "admin").fetch();
// Complex negation
const products = await query
.whereNot((query) =>
query.where("status", "discontinued").orWhere("stock", "<=", 0),
)
.fetch();
// Multiple conditions
const orders = await query
.whereNot({
status: "cancelled",
paymentFailed: true,
})
.fetch();whereNotBetween()
whereNotBetween(key: Extract<keyof T>, value: any): this;Filters records where a column's value is NOT between two values. Useful for finding records outside a specific range.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | The column name to check |
value | any | Array containing [min, max] values to exclude |
Returns
this
The query builder instance for chaining
Example
// Find users with age outside 18-30
const users = await query.whereNotBetween("age", [18, 30]).fetch();
// Find orders outside a date range
const orders = await query
.whereNotBetween("createdAt", [startDate, endDate])
.fetch();whereNotExists()
whereNotExists(key: Extract<keyof T>): this;Filters records where a column's value does not exist (is null or undefined).
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | StringKeyOf the column name to check for non-existence |
Returns
this
The query builder instance for chaining
See
Example
// Find records where 'deletedAt' field is not set
const results = await query.whereNotExists("deletedAt").fetch();whereNotIn()
whereNotIn(key: Extract<keyof T>, value: any): this;Filters records where a column's value is NOT in the given array. Uses Dexie's native noneOf method for better performance when possible.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | The column name to check |
value | any | Array of values to exclude |
Returns
this
The query builder instance for chaining
Example
// Find users excluding specific roles
const users = await query.whereNotIn("role", ["guest", "blocked"]).fetch();
// Find orders excluding certain statuses
const orders = await query
.whereNotIn("status", ["cancelled", "refunded"])
.fetch();whereNotNull()
whereNotNull(key: Extract<keyof T>): this;Filters records where a column's value is NOT NULL. Useful for finding records with populated or set values.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | The column name to check for NOT NULL |
Returns
this
The query builder instance for chaining
Example
// Find users with email set
const users = await query.whereNotNull("email").fetch();
// Find completed orders
const orders = await query.whereNotNull("completedAt").fetch();whereNull()
whereNull(key: Extract<keyof T>): this;Filters records where a column's value is NULL. Useful for finding records with missing or unset values.
Parameters
| Parameter | Type | Description |
|---|---|---|
key | Extract<keyof T> | The column name to check for NULL |
Returns
this
The query builder instance for chaining
Example
// Find users with no email set
const users = await query.whereNull("email").fetch();
// Find orders with no completion date
const orders = await query.whereNull("completedAt").fetch();with()
with(relationship: Extract<keyof R>, ...relationships: Extract<keyof R, string>[]): ReactiveQueryBuilder<OM, T, PK, R, H, IT>;Preload the relationship(s) for the model when the results are fetched.
Parameters
| Parameter | Type | Description |
|---|---|---|
relationship | Extract<keyof R> | The relationship to preload. |
...relationships | Extract<keyof R, string>[] | - |
Returns
ReactiveQueryBuilder<OM, T, PK, R, H, IT>
the query builder instance for chaining.
withAll()
withAll(): ReactiveQueryBuilder<OM, T, PK, R, H, IT>;Preload all relationships for the model when the results are fetched.
Returns
ReactiveQueryBuilder<OM, T, PK, R, H, IT>
the query builder instance for chaining.