Skip to content

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

typescript
// 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 ParameterDefault typeDescription
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

ts
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
ParameterTypeDescription
callbackReactiveQueryBuilderSubQuery<OM, T, PK, R, H>Function that builds a group of conditions
Returns

this

See
Example
typescript
query
  .where("active", true)
  .andWhere((q) => q.where("role", "admin").where("permissionLevel", ">=", 5));

Call Signature

ts
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
ParameterTypeDescription
conditionsRecord<Extract<keyof T, string>, unknown>Object where keys are column names and values are the expected values
Returns

this

Example
typescript
query.where("active", true).andWhere({
  verified: true,
  accountType: "premium",
});

Call Signature

ts
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
ParameterTypeDescription
keyKColumn name to check
valueT[K]Value to compare against
Returns

this

Example
typescript
query.where("active", true).andWhere("department", "sales");

Call Signature

ts
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
ParameterTypeDescription
keyKColumn 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
valueanyValue to compare against
Returns

this

Example
typescript
query
  .where("active", true)
  .andWhere("experience", ">=", 5)
  .andWhere("rating", ">", 4.5);

Call Signature

ts
andWhere(value: boolean): this;

Add an AND condition using a boolean value. true is equivalent to whereExists, false to whereNotExists.

Parameters
ParameterTypeDescription
valuebooleanboolean condition
Returns

this


andWhereBetween()

ts
andWhereBetween(key: Extract<keyof T>, value: any): this;

Alias for whereBetween that makes queries more readable when chaining conditions.

Parameters

ParameterType
keyExtract<keyof T>
valueany

Returns

this

Example

typescript
const results = await query
  .where("active", true)
  .andWhereBetween("price", [10, 100])
  .fetch();

andWhereExists()

ts
andWhereExists(key: Extract<keyof T>): this;

Alias for whereExists that makes queries more readable when chaining conditions.

Parameters

ParameterTypeDescription
keyExtract<keyof T>StringKeyOf the column name to check for existence

Returns

this

The query builder instance for chaining

Example

typescript
const results = await query
  .where("active", true)
  .andWhereExists("email")
  .fetch();

andWhereILike()

ts
andWhereILike(key: Extract<keyof T>, value?: any): this;

Alias for whereILike that makes queries more readable when chaining conditions.

Parameters

ParameterType
keyExtract<keyof T>
value?any

Returns

this

Example

typescript
const results = await query
  .where("active", true)
  .andWhereILike("tags", "%featured%")
  .fetch();

andWhereIn()

ts
andWhereIn(key: Extract<keyof T>, value: any): this;

Alias for whereIn that makes queries more readable when chaining conditions.

Parameters

ParameterType
keyExtract<keyof T>
valueany

Returns

this

Example

typescript
const results = await query
  .where("active", true)
  .andWhereIn("status", ["pending", "processing"])
  .fetch();

andWhereLike()

ts
andWhereLike(key: Extract<keyof T>, value?: any): this;

Alias for whereLike that makes queries more readable when chaining conditions.

Parameters

ParameterType
keyExtract<keyof T>
value?any

Returns

this

Example

typescript
const results = await query
  .where("active", true)
  .andWhereLike("name", "John%")
  .fetch();

andWhereNot()

Call Signature

ts
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
ParameterType
callbackReactiveQueryBuilderSubQuery<OM, T, PK, R, H>
Returns

this

Example
typescript
// 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

ts
andWhereNot(conditions: Partial<T>): this;

Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.

Parameters
ParameterType
conditionsPartial<T>
Returns

this

Example
typescript
// 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

ts
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
ParameterType
keyK
valueT[K]
Returns

this

Example
typescript
// 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

ts
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
ParameterType
keyK
operator| "is" | "=" | "!=" | "is not" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "<>" | "in" | "not in" | "between" | "not between" | "null" | "not null" | "exists" | "not exists" | "ilike" | "not ilike"
valueany
Returns

this

Example
typescript
// 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

ts
andWhereNot(value: boolean): this;

Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.

Parameters
ParameterType
valueboolean
Returns

this

Example
typescript
// 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()

ts
andWhereNotBetween(key: Extract<keyof T>, value: any): this;

Alias for whereNotBetween that makes queries more readable when chaining conditions.

Parameters

ParameterType
keyExtract<keyof T>
valueany

Returns

this

Example

typescript
const results = await query
  .where("active", true)
  .andWhereNotBetween("price", [0, 10])
  .fetch();

andWhereNotExists()

ts
andWhereNotExists(key: Extract<keyof T>): this;

Alias for whereNotExists that makes queries more readable when chaining conditions.

Parameters

ParameterTypeDescription
keyExtract<keyof T>StringKeyOf the column name to check for non-existence

Returns

this

The query builder instance for chaining

Example

typescript
const results = await query
  .where("active", true)
  .andWhereNotExists("deletedAt")
  .fetch();

andWhereNotIn()

ts
andWhereNotIn(key: Extract<keyof T>, value: any): this;

Alias for whereNotIn that makes queries more readable when chaining conditions.

Parameters

ParameterType
keyExtract<keyof T>
valueany

Returns

this

Example

typescript
const results = await query
  .where("active", true)
  .andWhereNotIn("category", ["archived", "deleted"])
  .fetch();

andWhereNotNull()

ts
andWhereNotNull(key: Extract<keyof T>): this;

Alias for whereNotNull that makes queries more readable when chaining conditions.

Parameters

ParameterType
keyExtract<keyof T>

Returns

this

Example

typescript
const results = await query
  .where("active", true)
  .andWhereNotNull("verifiedAt")
  .fetch();

andWhereNull()

ts
andWhereNull(key: Extract<keyof T>): this;

Alias for whereNull that makes queries more readable when chaining conditions.

Parameters

ParameterType
keyExtract<keyof T>

Returns

this

Example

typescript
const results = await query
  .where("active", true)
  .andWhereNull("deletedAt")
  .fetch();

clear()

ts
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

typescript
// 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 records

clone()

ts
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

ParameterTypeDescription
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

typescript
// 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()

ts
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

typescript
// Count all records
const total = await query.count();

// Count filtered records
const highScores = await query.where("score", ">", 80).count();

decrement()

ts
decrement(column: StringKeyOf<T>, amount: number): Promise<IT[]>;

Decrements a numeric column value for all matching records. Only affects columns with number values.

Parameters

ParameterTypeDefault valueDescription
columnStringKeyOf<T>undefinedName of the numeric column to decrement
amountnumber1Amount to decrement by (default: 1)

Returns

Promise<IT[]>

Promise resolving to array of updated records

Example

typescript
// 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()

ts
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

typescript
// Delete all inactive users
await query.where("active", false).delete();

// Delete users with specific roles
await query.whereIn("role", ["guest", "blocked"]).delete();

fetch()

ts
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

typescript
// 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()

ts
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

typescript
// Get record with lowest score
const lowest = await query.orderBy("score").first();
// Returns: 'Test1'

forPage()

ts
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

ParameterTypeDefault valueDescription
pagenumberundefinedThe page number (1-based)
perPagenumber20Number of records per page (default: 20)

Returns

Promise<undefined | number | IT | IT[]>

Promise resolving to array of records for the requested page

Example

typescript
// 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()

ts
increment(column: StringKeyOf<T>, amount: number): Promise<IT[]>;

Increments a numeric column value for all matching records. Only affects columns with number values.

Parameters

ParameterTypeDefault valueDescription
columnStringKeyOf<T>undefinedName of the numeric column to increment
amountnumber1Amount to increment by (default: 1)

Returns

Promise<IT[]>

Promise resolving to array of updated records

Example

typescript
// 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()

ts
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

typescript
// Get record with highest score
const highest = await query.orderBy("score").last();
// Returns: 'Test4'

limit()

ts
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

ParameterTypeDescription
limitnumberThe maximum number of records to return

Returns

ReactiveQueryBuilder<OM, T, PK, R, H, IT>

The query builder instance for chaining

Example

typescript
// Get only the first 2 records
const records = await query.orderBy("score").limit(2).fetch();
// Returns: ['Test1', 'Test2']

offset()

ts
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

ParameterTypeDescription
offsetnumberThe number of records to skip

Returns

ReactiveQueryBuilder<OM, T, PK, R, H, IT>

The query builder instance for chaining

Example

typescript
// Skip first 2 records and get next 2
const records = await query.orderBy("score").offset(2).limit(2).fetch();
// Returns: ['Test3', 'Test4']

orderBy()

ts
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

ParameterTypeDefault valueDescription
keyExtract<keyof T>undefinedThe 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

typescript
// 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

ts
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
ParameterTypeDescription
callbackReactiveQueryBuilderSubQuery<OM, T, PK, R, H>Function that builds a group of conditions
Returns

this

Example
typescript
query
  .where("status", "active")
  .orWhere((q) => q.where("role", "admin").where("permissionLevel", ">=", 5)); // status = 'active' OR (role = 'admin' AND permissionLevel >= 5)

Call Signature

ts
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
ParameterTypeDescription
conditionsRecord<Extract<keyof T, string>, unknown>Object where keys are column names and values are the expected values
Returns

this

Example
typescript
query.where("status", "active").orWhere({
  role: "admin",
  verified: true,
}); // status = 'active' OR (role = 'admin' AND verified = true)

Call Signature

ts
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
ParameterTypeDescription
keyKColumn name to check
valueT[K]Value to compare against
Returns

this

Example
typescript
query.where("role", "user").orWhere("department", "IT"); // role = 'user' OR department = 'IT'

Call Signature

ts
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
ParameterTypeDescription
keyKColumn 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
valueanyValue to compare against
Returns

this

Example
typescript
query
  .where("status", "active")
  .orWhere("score", ">=", 100)
  .orWhere("rating", ">", 4.5); // status = 'active' OR score >= 100 OR rating > 4.5

Call Signature

ts
orWhere(value: boolean): this;

Add an OR condition using a boolean value. true is equivalent to orWhereExists, false to orWhereNotExists.

Parameters
ParameterTypeDescription
valuebooleanboolean condition
Returns

this


orWhereBetween()

ts
orWhereBetween(key: Extract<keyof T>, value: any): this;

Adds an OR condition checking if a column's value is between two values.

Parameters

ParameterType
keyExtract<keyof T>
valueany

Returns

this

Example

typescript
const results = await query
  .where("category", "electronics")
  .orWhereBetween("rating", [4, 5])
  .fetch();

orWhereExists()

ts
orWhereExists(key: Extract<keyof T>): this;

Adds an OR condition filtering records where a column's value exists.

Parameters

ParameterTypeDescription
keyExtract<keyof T>StringKeyOf the column name to check for existence

Returns

this

The query builder instance for chaining

See

orWhere

Example

typescript
const results = await query
  .where("status", "pending")
  .orWhereExists("processedAt")
  .fetch();

orWhereILike()

ts
orWhereILike(key: Extract<keyof T>, value?: any): this;

Adds an OR condition checking if a column matches a pattern (case-insensitive).

Parameters

ParameterType
keyExtract<keyof T>
value?any

Returns

this

Example

typescript
const results = await query
  .where("category", "electronics")
  .orWhereILike("brand", "%apple%")
  .fetch();

orWhereIn()

ts
orWhereIn(key: Extract<keyof T>, value: any): this;

Adds an OR condition checking if a column's value is in the given array.

Parameters

ParameterType
keyExtract<keyof T>
valueany

Returns

this

Example

typescript
const results = await query
  .where("department", "sales")
  .orWhereIn("role", ["manager", "lead"])
  .fetch();

orWhereLike()

ts
orWhereLike(key: Extract<keyof T>, value?: any): this;

Adds an OR condition checking if a column matches a pattern (case-sensitive).

Parameters

ParameterType
keyExtract<keyof T>
value?any

Returns

this

Example

typescript
const results = await query
  .where("category", "phones")
  .orWhereLike("description", "%wireless%")
  .fetch();

orWhereNot()

Call Signature

ts
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
ParameterType
callbackReactiveQueryBuilderSubQuery<OM, T, PK, R, H>
Returns

this

Example
typescript
// 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

ts
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
ParameterType
conditionsPartial<T>
Returns

this

Example
typescript
// 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

ts
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
ParameterType
keyK
valueT[K]
Returns

this

Example
typescript
// 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

ts
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
ParameterType
keyK
operator| "is" | "=" | "!=" | "is not" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "<>" | "in" | "not in" | "between" | "not between" | "null" | "not null" | "exists" | "not exists" | "ilike" | "not ilike"
valueany
Returns

this

Example
typescript
// 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

ts
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
ParameterType
valueboolean
Returns

this

Example
typescript
// 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()

ts
orWhereNotBetween(key: Extract<keyof T>, value: any): this;

Adds an OR condition checking if a column's value is NOT between two values.

Parameters

ParameterType
keyExtract<keyof T>
valueany

Returns

this

Example

typescript
const results = await query
  .where("category", "premium")
  .orWhereNotBetween("rating", [1, 3])
  .fetch();

orWhereNotExists()

ts
orWhereNotExists(key: Extract<keyof T>): this;

Adds an OR condition filtering records where a column's value does not exist.

Parameters

ParameterTypeDescription
keyExtract<keyof T>StringKeyOf the column name to check for non-existence

Returns

this

The query builder instance for chaining

See

orWhereNot

Example

typescript
const results = await query
  .where("status", "pending")
  .orWhereNotExists("processedAt")
  .fetch();

orWhereNotIn()

ts
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

ParameterType
keyExtract<keyof T>
valueany

Returns

this

Example

typescript
const results = await query
  .where("department", "sales")
  .orWhereNotIn("status", ["inactive", "suspended"])
  .fetch();

orWhereNotNull()

ts
orWhereNotNull(key: Extract<keyof T>): this;

Adds an OR condition checking if a column's value is NOT NULL.

Parameters

ParameterType
keyExtract<keyof T>

Returns

this

Example

typescript
const results = await query
  .where("status", "pending")
  .orWhereNotNull("processedAt")
  .fetch();

orWhereNull()

ts
orWhereNull(key: Extract<keyof T>): this;

Adds an OR condition checking if a column's value is NULL.

Parameters

ParameterType
keyExtract<keyof T>

Returns

this

Example

typescript
const results = await query
  .where("status", "pending")
  .orWhereNull("processedAt")
  .fetch();

reactive()

ts
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

ts
{
  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.
NameTypeDescription
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

typescript
// 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()

ts
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

ParameterTypeDescription
dataPartial<T>Partial data containing fields to update

Returns

Promise<IT[]>

Promise resolving to array of updated records

Example

typescript
// 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

ts
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
ParameterTypeDescription
callbackReactiveQueryBuilderSubQuery<OM, T, PK, R, H>Function that builds a group of conditions
Returns

this

Example
typescript
query.where((q) =>
  q.where("role", "admin").orWhere("permissions", "includes", "manage_users"),
);

Call Signature

ts
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
ParameterTypeDescription
conditionsRecord<Extract<keyof T, string>, unknown>Object where keys are column names and values are the expected values
Returns

this

Example
typescript
query.where({
  status: "active",
  type: "premium",
  verified: true,
});

Call Signature

ts
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
ParameterTypeDescription
keyKColumn name to check
valueT[K]Value to compare against
Returns

this

Example
typescript
query.where("status", "active");

Call Signature

ts
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
ParameterTypeDescription
keyKColumn 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
valueanyValue to compare against
Returns

this

Example
typescript
query.where("age", ">=", 18);
query.where("name", "like", "John%");

Call Signature

ts
where(value: boolean): this;

Filter records using a boolean value. true is equivalent to whereExists, false to whereNotExists.

Parameters
ParameterTypeDescription
valuebooleanboolean condition
Returns

this


whereBetween()

ts
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

ParameterTypeDescription
keyExtract<keyof T>The column name to check
value[any, any]Array containing [min, max] values

Returns

this

The query builder instance for chaining

Example

typescript
// 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()

ts
whereExists(key: Extract<keyof T>): this;

Filters records where a column's value exists (is not null or undefined).

Parameters

ParameterTypeDescription
keyExtract<keyof T>StringKeyOf the column name to check for existence

Returns

this

The query builder instance for chaining

See

where

Example

typescript
// Find records where 'email' field exists
const results = await query.whereExists("email").fetch();

whereILike()

ts
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

ParameterTypeDescription
keyExtract<keyof T>The column name to check
value?anyThe pattern to match against

Returns

this

The query builder instance for chaining

Example

typescript
// 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()

ts
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

ParameterTypeDescription
keyExtract<keyof T>The column name to check
valueany[]Array of values to match against

Returns

this

The query builder instance for chaining

Example

typescript
// 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()

ts
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

ParameterTypeDescription
keyExtract<keyof T>The column name to check
valueanyThe pattern to match against

Returns

this

The query builder instance for chaining

Example

typescript
// 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

ts
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
ParameterType
callbackReactiveQueryBuilderSubQuery<OM, T, PK, R, H>
Returns

this

Example
typescript
// 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

ts
whereNot(conditions: Partial<T>): this;

Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.

Parameters
ParameterType
conditionsPartial<T>
Returns

this

Example
typescript
// 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

ts
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
ParameterType
keyK
valueT[K]
Returns

this

Example
typescript
// 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

ts
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
ParameterType
keyK
operator| "is" | "=" | "!=" | "is not" | "<" | "<=" | ">" | ">=" | "like" | "not like" | "<>" | "in" | "not in" | "between" | "not between" | "null" | "not null" | "exists" | "not exists" | "ilike" | "not ilike"
valueany
Returns

this

Example
typescript
// 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

ts
whereNot(value: boolean): this;

Adds a where clause that negates the condition. Useful for finding records that don't match specific criteria.

Parameters
ParameterType
valueboolean
Returns

this

Example
typescript
// 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()

ts
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

ParameterTypeDescription
keyExtract<keyof T>The column name to check
valueanyArray containing [min, max] values to exclude

Returns

this

The query builder instance for chaining

Example

typescript
// 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()

ts
whereNotExists(key: Extract<keyof T>): this;

Filters records where a column's value does not exist (is null or undefined).

Parameters

ParameterTypeDescription
keyExtract<keyof T>StringKeyOf the column name to check for non-existence

Returns

this

The query builder instance for chaining

See

whereNot

Example

typescript
// Find records where 'deletedAt' field is not set
const results = await query.whereNotExists("deletedAt").fetch();

whereNotIn()

ts
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

ParameterTypeDescription
keyExtract<keyof T>The column name to check
valueanyArray of values to exclude

Returns

this

The query builder instance for chaining

Example

typescript
// 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()

ts
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

ParameterTypeDescription
keyExtract<keyof T>The column name to check for NOT NULL

Returns

this

The query builder instance for chaining

Example

typescript
// Find users with email set
const users = await query.whereNotNull("email").fetch();

// Find completed orders
const orders = await query.whereNotNull("completedAt").fetch();

whereNull()

ts
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

ParameterTypeDescription
keyExtract<keyof T>The column name to check for NULL

Returns

this

The query builder instance for chaining

Example

typescript
// 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()

ts
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

ParameterTypeDescription
relationshipExtract<keyof R>The relationship to preload.
...relationshipsExtract<keyof R, string>[]-

Returns

ReactiveQueryBuilder<OM, T, PK, R, H, IT>

the query builder instance for chaining.


withAll()

ts
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.