import type BaseRepository from "./baseRepository.ts"; import type { BelongsToManyRelation, BelongsToRelation, HasManyRelation, HasManyThroughRelation, HasOneRelation, MorphManyRelation, MorphOneRelation, MorphToRelation } from "./relationships.ts"; import type { RepositoryQuery } from "./repositoryQuery.ts"; import type { QueryOptions, QueryWhere } from "./types.ts"; type RelatedRecord = { id: unknown; get(key: K): unknown; }; interface RelatedModelClass { name: string; repository(): BaseRepository; create(attributes: Record, forced?: Record): Promise; newFromRecord(record: TRelated, exists?: boolean): RelatedRecord; } interface RelationHost { readonly id: TEntity[PrimaryKey]; get(key: K): TEntity[K]; getRepository(): BaseRepository; } type RelationKind = "hasMany" | "hasOne" | "belongsTo" | "belongsToMany" | "hasManyThrough" | "morphMany" | "morphOne" | "morphTo"; type ExistsClause = { sql: string; params: unknown[]; }; declare class HasManyRelationQuery { private readonly parent; private readonly related; readonly relation: HasManyRelation; readonly kind: RelationKind; private extraWhere; private extraOptions; constructor(parent: RelationHost, related: RelatedModelClass, relation: HasManyRelation); where(where: QueryWhere): this; orderBy(orderBy: QueryOptions["orderBy"]): this; limit(limit: number): this; applyEagerLoad(query: RepositoryQuery, alias: string): void; hydrateEager(row: Record, alias: string): unknown; toExistsClause(parentTable: string): ExistsClause; private scopedQuery; get(): Promise; first(): Promise; count(): Promise; pluck(column: string): Promise; pluck(column: string, keyBy: string): Promise>; value(column: string): Promise; then(onfulfilled?: ((value: RelatedRecord[]) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise; create(attributes?: Record): Promise; save(related: RelatedRecord | (Record & { save?: () => Promise; mergeAttributes?: (patch: Record) => unknown; })): Promise; createMany(records: ReadonlyArray>): Promise; } declare class HasOneRelationQuery { readonly relation: HasOneRelation; readonly kind: RelationKind; private readonly inner; constructor(parent: RelationHost, related: RelatedModelClass, relation: HasOneRelation); where(where: QueryWhere): this; orderBy(orderBy: QueryOptions["orderBy"]): this; applyEagerLoad(query: RepositoryQuery, alias: string): void; hydrateEager(row: Record, alias: string): unknown; toExistsClause(parentTable: string): ExistsClause; get(): Promise; first(): Promise; count(): Promise; pluck(column: string): Promise; pluck(column: string, keyBy: string): Promise>; value(column: string): Promise; then(onfulfilled?: ((value: RelatedRecord | null) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise; create(attributes?: Record): Promise; save(related: RelatedRecord | Record): Promise; } declare class BelongsToRelationQuery { private readonly parent; private readonly related; readonly relation: BelongsToRelation; readonly kind: RelationKind; private extraWhere; private extraOptions; constructor(parent: RelationHost, related: RelatedModelClass, relation: BelongsToRelation); where(where: QueryWhere): this; orderBy(orderBy: QueryOptions["orderBy"]): this; applyEagerLoad(query: RepositoryQuery, alias: string): void; hydrateEager(row: Record, alias: string): unknown; toExistsClause(parentTable: string): ExistsClause; get(): Promise; first(): Promise; pluck(column: string): Promise; pluck(column: string, keyBy: string): Promise>; value(column: string): Promise; private relatedQuery; then(onfulfilled?: ((value: RelatedRecord | null) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise; associate(owner: { id?: unknown; } | RelatedRecord): Promise; dissociate(): Promise; } declare class BelongsToManyRelationQuery { private readonly parent; private readonly related; readonly relation: BelongsToManyRelation; readonly kind: RelationKind; private extraWhere; private extraOptions; private pivotValues; constructor(parent: RelationHost, related: RelatedModelClass, relation: BelongsToManyRelation); where(where: QueryWhere): this; orderBy(orderBy: QueryOptions["orderBy"]): this; applyEagerLoad(query: RepositoryQuery, alias: string): void; withPivotValues(values: Record): this; hydrateEager(row: Record, alias: string): unknown; toExistsClause(parentTable: string): ExistsClause; private connection; get(): Promise; first(): Promise; count(): Promise; pluck(column: string): Promise; pluck(column: string, keyBy: string): Promise>; value(column: string): Promise; private relatedQuery; then(onfulfilled?: ((value: RelatedRecord[]) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise; attach(ids: unknown | readonly unknown[]): Promise; toggle(ids: unknown | readonly unknown[]): Promise; detach(ids?: unknown | readonly unknown[]): Promise; sync(ids: readonly unknown[]): Promise; create(attributes?: Record): Promise; } declare class MorphManyRelationQuery { private readonly parent; private readonly related; readonly relation: MorphManyRelation; readonly kind: RelationKind; private extraWhere; private extraOptions; constructor(parent: RelationHost, related: RelatedModelClass, relation: MorphManyRelation); where(where: QueryWhere): this; applyEagerLoad(query: RepositoryQuery, alias: string): void; hydrateEager(row: Record, alias: string): unknown; toExistsClause(parentTable: string): ExistsClause; private scopedQuery; get(): Promise; first(): Promise; count(): Promise; pluck(column: string): Promise; pluck(column: string, keyBy: string): Promise>; value(column: string): Promise; then(onfulfilled?: ((value: RelatedRecord[]) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise; create(attributes?: Record): Promise; } declare class MorphOneRelationQuery { readonly relation: MorphOneRelation; readonly kind: RelationKind; private readonly inner; constructor(parent: RelationHost, related: RelatedModelClass, relation: MorphOneRelation); where(where: QueryWhere): this; applyEagerLoad(query: RepositoryQuery, alias: string): void; hydrateEager(row: Record, alias: string): unknown; toExistsClause(parentTable: string): ExistsClause; get(): Promise; first(): Promise; count(): Promise; pluck(column: string): Promise; pluck(column: string, keyBy: string): Promise>; value(column: string): Promise; then(onfulfilled?: ((value: RelatedRecord | null) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise; create(attributes?: Record): Promise; } declare class MorphToRelationQuery { private readonly parent; private readonly relatedByType; readonly relation: MorphToRelation; readonly kind: RelationKind; private extraWhere; constructor(parent: RelationHost, relatedByType: Record, "id">>, relation: MorphToRelation); where(where: QueryWhere>): this; applyEagerLoad(query: RepositoryQuery, alias: string): void; hydrateEager(row: Record, alias: string): unknown; toExistsClause(parentTable: string): ExistsClause; get(): Promise; pluck(column: string): Promise; pluck(column: string, keyBy: string): Promise>; value(column: string): Promise; private relatedForCurrentType; private relatedQuery; then(onfulfilled?: ((value: RelatedRecord | null) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise; } declare class HasManyThroughRelationQuery { private readonly parent; private readonly related; readonly relation: HasManyThroughRelation; readonly kind: RelationKind; private extraWhere; private extraOptions; constructor(parent: RelationHost, related: RelatedModelClass, relation: HasManyThroughRelation); where(where: QueryWhere): this; orderBy(orderBy: QueryOptions["orderBy"]): this; limit(limit: number): this; applyEagerLoad(query: RepositoryQuery, alias: string): void; hydrateEager(row: Record, alias: string): unknown; toExistsClause(parentTable: string): ExistsClause; get(): Promise; first(): Promise; count(): Promise; pluck(column: string): Promise; pluck(column: string, keyBy: string): Promise>; value(column: string): Promise; private farRows; then(onfulfilled?: ((value: RelatedRecord[]) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null): Promise; } type AnyRelationQuery = { kind: RelationKind; applyEagerLoad(query: unknown, alias: string): void; hydrateEager(row: Record, alias: string): unknown; get(): Promise; toExistsClause(parentTable: string): ExistsClause; where?(where: Record): unknown; then?: (onfulfilled?: ((value: unknown) => unknown) | null, onrejected?: ((reason: unknown) => unknown) | null) => Promise; }; export type { AnyRelationQuery, RelatedModelClass, RelatedRecord, RelationHost }; export { BelongsToManyRelationQuery, BelongsToRelationQuery, HasManyRelationQuery, HasManyThroughRelationQuery, HasOneRelationQuery, MorphManyRelationQuery, MorphOneRelationQuery, MorphToRelationQuery, };