Libraries
    Preparing search index...

    Interface EntityQuery<TEntity, TResult>

    The typed query handle returned by every DbSet method that initiates a query (e.g. .where(), .include(), .first()).

    Structurally it IS a SchemaQueryBuilder for the entity's schema, with typed .include() / .includeVariant() methods overlaid. All other SchemaQueryBuilder methods are available and this-returning chains preserve the typed wrapper.

    interface EntityQuery<TEntity extends Entity<any, any, any>, TResult> {
        andWhere(
            column: ColumnRef<EntitySchema<TEntity>>,
            operator: string,
            value: any,
        ): this;
        andWhere(column: ColumnRef<EntitySchema<TEntity>>, value: any): this;
        andWhere(record: Record<string, any>): this;
        andWhere(callback: (builder: QueryBuilder) => void): this;
        andWhere(raw: Raw): this;
        apply(fn: (builder: QueryBuilder) => void): this;
        avg(column: Raw<any> | ColumnRef<EntitySchema<TEntity>>): this;
        bulkInsert(
            rows: InferType<
                ReturnType<EntitySchema<TEntity>["makeAllPropsOptional"]>,
            >[],
            opts?: {
                chunkSize?: number;
                conflictColumns?: ColumnRef<EntitySchema<TEntity>>[];
                onConflict?: "ignore" | "merge";
            },
        ): Promise<TResult[]>;
        bulkUpdate(
            updates: readonly {
                set: Partial<InferType<TLocalSchema>>;
                where: Partial<InferType<TLocalSchema>>;
            }[],
        ): Promise<number>;
        bulkUpsert(
            rows: InferType<
                ReturnType<EntitySchema<TEntity>["makeAllPropsOptional"]>,
            >[],
            opts: {
                chunkSize?: number;
                conflictColumns: ColumnRef<EntitySchema<TEntity>>[];
            },
        ): Promise<TResult[]>;
        count(column?: Raw<any> | ColumnRef<EntitySchema<TEntity>>): this;
        countDistinct(column?: Raw<any> | ColumnRef<EntitySchema<TEntity>>): this;
        delete(): Promise<number>;
        distinct(...columns: (Raw<any> | ColumnRef<EntitySchema<TEntity>>)[]): this;
        execute(): Promise<TResult[]>;
        find(
            pk: PrimaryKeyValueOf<EntitySchema<TEntity>>,
        ): Promise<TResult | undefined>;
        findMany(
            pks: readonly PrimaryKeyValueOf<EntitySchema<TEntity>>[],
        ): Promise<TResult[]>;
        findOrFail(pk: PrimaryKeyValueOf<EntitySchema<TEntity>>): Promise<TResult>;
        first(): Promise<TResult | undefined>;
        groupBy(...columns: (Raw<any> | ColumnRef<EntitySchema<TEntity>>)[]): this;
        groupByRaw(sql: string, ...bindings: any[]): this;
        hardDelete(): Promise<number>;
        having(
            column: Raw<any> | ColumnRef<EntitySchema<TEntity>>,
            operator: string,
            value: any,
        ): this;
        havingRaw(sql: string, ...bindings: any[]): this;
        include<K extends string>(
            sel: (t: RelKeyTree<TEntity>) => K,
            customize?: (q: SchemaQueryBuilder<any, any>) => void,
        ): EntityQuery<TEntity, WithIncluded<TEntity, TResult, K>>;
        includeVariant<TVariant extends string, TRel extends string>(
            variantKey: TVariant,
            relationName: TRel,
            customize?: (q: SchemaQueryBuilder<any, any>) => void,
        ): EntityQuery<
            TEntity,
            TRel extends keyof EntityRelations<TEntity> & string
                ? WithVariantIncluded<TEntity, TResult, TVariant, TRel>
                : TResult,
        >;
        insert(
            data: InferType<
                ReturnType<EntitySchema<TEntity>["makeAllPropsOptional"]>,
            >,
        ): Promise<TResult>;
        insertMany(
            data: InferType<
                ReturnType<EntitySchema<TEntity>["makeAllPropsOptional"]>,
            >[],
        ): Promise<TResult[]>;
        joinMany<
            TForeignSchema extends
                ObjectSchemaBuilder<any, any, any, any, any, any, any>,
            TFieldName extends string,
        >(
            spec: JoinManySpec<EntitySchema<TEntity>, TForeignSchema, TFieldName>,
        ): SchemaQueryBuilder<
            EntitySchema<TEntity>,
            WithJoinedMany<TResult, TFieldName, TForeignSchema>,
        >;
        joinOne<
            TForeignSchema extends
                ObjectSchemaBuilder<any, any, any, any, any, any, any>,
            TFieldName extends string,
            TRequired extends boolean = true,
        >(
            spec: JoinOneSpec<
                EntitySchema<TEntity>,
                TForeignSchema,
                TFieldName,
                TRequired,
            >,
        ): SchemaQueryBuilder<
            EntitySchema<TEntity>,
            WithJoinedOne<TResult, TFieldName, TForeignSchema, TRequired>,
        >;
        limit(n: number): this;
        max(column: Raw<any> | ColumnRef<EntitySchema<TEntity>>): this;
        min(column: Raw<any> | ColumnRef<EntitySchema<TEntity>>): this;
        offset(n: number): this;
        onConflict(
            ...conflictColumns: ColumnRef<EntitySchema<TEntity>>[],
        ): OnConflictBuilder<EntitySchema<TEntity>, TResult>;
        onlyDeleted(): this;
        orderBy(
            column: Raw<any> | ColumnRef<EntitySchema<TEntity>>,
            direction?: "asc" | "desc",
        ): this;
        orderByRaw(sql: string, ...bindings: any[]): this;
        orWhere(
            column: ColumnRef<EntitySchema<TEntity>>,
            operator: string,
            value: any,
        ): this;
        orWhere(column: ColumnRef<EntitySchema<TEntity>>, value: any): this;
        orWhere(record: Record<string, any>): this;
        orWhere(callback: (builder: QueryBuilder) => void): this;
        orWhere(raw: Raw): this;
        orWhereIn(
            column: ColumnRef<EntitySchema<TEntity>>,
            values: readonly any[] | QueryBuilder<any, any>,
        ): this;
        orWhereNotIn(
            column: ColumnRef<EntitySchema<TEntity>>,
            values: readonly any[] | QueryBuilder<any, any>,
        ): this;
        orWhereNotNull(column: ColumnRef<EntitySchema<TEntity>>): this;
        orWhereNull(column: ColumnRef<EntitySchema<TEntity>>): this;
        paginate(
            opts: { page: number; pageSize: number },
        ): Promise<PaginationResult<TResult>>;
        paginateAfter(
            opts: {
                column?: ColumnRef<EntitySchema<TEntity>>;
                cursor?: any;
                direction?: "asc" | "desc";
                limit: number;
            },
        ): Promise<CursorPaginationResult<TResult>>;
        pluck<K extends string>(
            column: ColumnRef<EntitySchema<TEntity>>,
        ): Promise<TResult[K][]>;
        projected<K extends string>(
            name: K,
        ): SchemaQueryBuilder<
            EntitySchema<TEntity>,
            Pick<TResult, ProjectionKeysOf<EntitySchema<TEntity>, K> & keyof TResult>,
        >;
        restore(): Promise<TResult[]>;
        scoped<K extends never>(name: K): this;
        select(...columns: (Raw<any> | ColumnRef<EntitySchema<TEntity>>)[]): this;
        select<TSel extends SelectSelector<EntitySchema<TEntity>>>(
            selector: TSel,
        ): SchemaQueryBuilder<
            EntitySchema<TEntity>,
            SelectProjection<ReturnType<TSel>>,
        >;
        selectRaw(sql: string, bindings?: any[]): this;
        selectVariants(keys: string[]): this;
        sum(column: Raw<any> | ColumnRef<EntitySchema<TEntity>>): this;
        then<TReturn1 = TResult[], TReturn2 = never>(
            onfulfilled?:
                | ((value: TResult[]) => TReturn1 | PromiseLike<TReturn1>)
                | null,
            onrejected?: ((reason: any) => TReturn2 | PromiseLike<TReturn2>) | null,
        ): Promise<TReturn1 | TReturn2>;
        toKnexQuery(): QueryBuilder;
        toQuery(): string;
        toString(): string;
        transacting(
            trx: Transaction,
        ): SchemaQueryBuilder<EntitySchema<TEntity>, TResult>;
        unscoped(): this;
        update(data: Partial<InferType<TLocalSchema>>): Promise<TResult[]>;
        upsert(
            data: InferType<
                ReturnType<EntitySchema<TEntity>["makeAllPropsOptional"]>,
            >,
            opts: {
                conflictColumns: ColumnRef<EntitySchema<TEntity>>[];
                updateColumns?: ColumnRef<EntitySchema<TEntity>>[];
            },
        ): Promise<TResult>;
        where(
            column: ColumnRef<EntitySchema<TEntity>>,
            operator: string,
            value: any,
        ): this;
        where(column: ColumnRef<EntitySchema<TEntity>>, value: any): this;
        where(raw: Raw, operator: string, value: any): this;
        where(callback: (builder: QueryBuilder) => void): this;
        where(record: Record<string, any>): this;
        where(raw: Raw): this;
        whereBetween(
            column: ColumnRef<EntitySchema<TEntity>>,
            range: readonly [any, any],
        ): this;
        whereExists(
            callback: QueryBuilder<any, any> | QueryCallback<any, unknown[]>,
        ): this;
        whereILike(column: ColumnRef<EntitySchema<TEntity>>, value: string): this;
        whereIn(
            column: ColumnRef<EntitySchema<TEntity>>,
            values: readonly any[] | QueryBuilder<any, any>,
        ): this;
        whereJsonPath(
            column: ColumnRef<EntitySchema<TEntity>>,
            path: string,
            operator?: string,
            value?: any,
        ): this;
        whereLike(column: ColumnRef<EntitySchema<TEntity>>, value: string): this;
        whereNot(
            column: ColumnRef<EntitySchema<TEntity>>,
            operator: string,
            value: any,
        ): this;
        whereNot(column: ColumnRef<EntitySchema<TEntity>>, value: any): this;
        whereNot(record: Record<string, any>): this;
        whereNot(callback: (builder: QueryBuilder) => void): this;
        whereNot(raw: Raw): this;
        whereNotBetween(
            column: ColumnRef<EntitySchema<TEntity>>,
            range: readonly [any, any],
        ): this;
        whereNotExists(
            callback: QueryBuilder<any, any> | QueryCallback<any, unknown[]>,
        ): this;
        whereNotIn(
            column: ColumnRef<EntitySchema<TEntity>>,
            values: readonly any[] | QueryBuilder<any, any>,
        ): this;
        whereNotNull(column: ColumnRef<EntitySchema<TEntity>>): this;
        whereNull(column: ColumnRef<EntitySchema<TEntity>>): this;
        whereRaw(sql: string, ...bindings: any[]): this;
        whereVariant(
            key: string,
            column: string,
            operator: string,
            value: any,
        ): this;
        withDeleted(): this;
    }

    Type Parameters

    • TEntity extends Entity<any, any, any>
    • TResult

    Hierarchy (View Summary)

    Index

    Methods

    • Parameters

      Returns this

    • Parameters

      Returns this

    • Parameters

      • record: Record<string, any>

      Returns this

    • Parameters

      • callback: (builder: QueryBuilder) => void

      Returns this

    • Parameters

      • raw: Raw

      Returns this

    • Parameters

      • fn: (builder: QueryBuilder) => void

      Returns this

    • Parameters

      Returns Promise<TResult[]>

    • Parameters

      Returns Promise<number>

    • Parameters

      Returns Promise<TResult[]>

    • Returns Promise<number>

    • Returns Promise<TResult[]>

    • Look up a single entity by primary key. Returns undefined when no row matches.

      The PK column(s) are resolved automatically from the entity's schema via the primaryKey / hasPrimaryKey extensions. Composite PKs are passed as a tuple in declaration order:

      await db.todos.find(42);                 // single PK
      await db.userRoles.find([userId, role]); // composite PK

      Any chained .include() / .includeVariant() calls on this query are honoured by the underlying SELECT.

      Returns Promise<TResult | undefined>

    • Returns Promise<TResult | undefined>

    • Parameters

      • sql: string
      • ...bindings: any[]

      Returns this

    • Returns Promise<number>

    • Parameters

      Returns this

    • Parameters

      • sql: string
      • ...bindings: any[]

      Returns this

    • Parameters

      Returns Promise<TResult>

    • Parameters

      Returns Promise<TResult[]>

    • Parameters

      • n: number

      Returns this

    • Parameters

      • n: number

      Returns this

    • Returns this

    • Parameters

      Returns this

    • Parameters

      • sql: string
      • ...bindings: any[]

      Returns this

    • Parameters

      Returns this

    • Parameters

      Returns this

    • Parameters

      • record: Record<string, any>

      Returns this

    • Parameters

      • callback: (builder: QueryBuilder) => void

      Returns this

    • Parameters

      • raw: Raw

      Returns this

    • Parameters

      Returns this

    • Parameters

      Returns this

    • Parameters

      • opts: { page: number; pageSize: number }

      Returns Promise<PaginationResult<TResult>>

    • Returns Promise<TResult[]>

    • Type Parameters

      • K extends never

      Parameters

      • name: K

      Returns this

    • Parameters

      • sql: string
      • Optionalbindings: any[]

      Returns this

    • Parameters

      • keys: string[]

      Returns this

    • Returns QueryBuilder

    • Returns string

    • Returns string

    • Returns this

    • Parameters

      Returns Promise<TResult[]>

    • Parameters

      Returns this

    • Parameters

      Returns this

    • Parameters

      • raw: Raw
      • operator: string
      • value: any

      Returns this

    • Parameters

      • callback: (builder: QueryBuilder) => void

      Returns this

    • Parameters

      • record: Record<string, any>

      Returns this

    • Parameters

      • raw: Raw

      Returns this

    • Parameters

      Returns this

    • Parameters

      • callback: QueryBuilder<any, any> | QueryCallback<any, unknown[]>

      Returns this

    • Parameters

      Returns this

    • Parameters

      Returns this

    • Parameters

      Returns this

    • Parameters

      Returns this

    • Parameters

      • record: Record<string, any>

      Returns this

    • Parameters

      • callback: (builder: QueryBuilder) => void

      Returns this

    • Parameters

      • raw: Raw

      Returns this

    • Parameters

      Returns this

    • Parameters

      • callback: QueryBuilder<any, any> | QueryCallback<any, unknown[]>

      Returns this

    • Parameters

      Returns this

    • Parameters

      • sql: string
      • ...bindings: any[]

      Returns this

    • Parameters

      • key: string
      • column: string
      • operator: string
      • value: any

      Returns this

    • Returns this