@iarayan/ch-orm
Version:
A Developer-First ClickHouse ORM with Powerful CLI Tools
215 lines • 7.36 kB
TypeScript
import { Connection } from "../connection/Connection";
import { Model } from "../model/Model";
import { QueryBuilder } from "../query/QueryBuilder";
/**
* Base class for all relationship types
* Provides common functionality for relationship management
*/
export declare abstract class Relationship<T extends Model, R extends Model> {
/**
* The model instance that owns this relationship
*/
protected ownerModel: T;
/**
* The related model class
*/
protected relatedModelClass: new () => R;
/**
* Local key on the owner model
*/
protected localKey: string;
/**
* Constructor for relationship
* @param ownerModel - The model instance that owns this relationship
* @param relatedModelClass - The related model class
* @param localKey - The local key (default: primary key of owner model)
*/
constructor(ownerModel: T, relatedModelClass: new () => R, localKey?: string);
/**
* Get owner model primary key
*/
protected getOwnerPrimaryKey(): string;
/**
* Get related model primary key
*/
protected getRelatedPrimaryKey(): string;
/**
* Get the related model table name
*/
protected getRelatedTableName(): string;
/**
* Get the owner model table name
*/
protected getOwnerTableName(): string;
/**
* Get the connection from a model instance
*/
protected getConnection(): Connection;
/**
* Get the query builder for the related model
*/
protected getQueryBuilder(): QueryBuilder;
/**
* Abstract method to get related records
*/
abstract get(): Promise<R[]>;
/**
* Abstract method to eager load related records for a collection of models
*/
abstract eagerLoad(models: T[]): Promise<Map<any, R[]>>;
}
/**
* HasOne relationship
* Represents a one-to-one relationship from the owner model to the related model
*/
export declare class HasOne<T extends Model, R extends Model> extends Relationship<T, R> {
/**
* Foreign key on the related model
*/
private foreignKey;
/**
* Constructor for HasOne relationship
* @param ownerModel - The model instance that owns this relationship
* @param relatedModelClass - The related model class
* @param foreignKey - The foreign key on the related model
* @param localKey - The local key (default: primary key of owner model)
*/
constructor(ownerModel: T, relatedModelClass: new () => R, foreignKey: string, localKey?: string);
/**
* Get the related record
*/
get(): Promise<R[]>;
/**
* Get the first related record (convenience method)
*/
first(): Promise<R | null>;
/**
* Eager load related records for a collection of models
*/
eagerLoad(models: T[]): Promise<Map<any, R[]>>;
}
/**
* HasMany relationship
* Represents a one-to-many relationship from the owner model to the related model
*/
export declare class HasMany<T extends Model, R extends Model> extends Relationship<T, R> {
/**
* Foreign key on the related model
*/
private foreignKey;
/**
* Constructor for HasMany relationship
* @param ownerModel - The model instance that owns this relationship
* @param relatedModelClass - The related model class
* @param foreignKey - The foreign key on the related model
* @param localKey - The local key (default: primary key of owner model)
*/
constructor(ownerModel: T, relatedModelClass: new () => R, foreignKey: string, localKey?: string);
/**
* Get the related records
*/
get(): Promise<R[]>;
/**
* Eager load related records for a collection of models
*/
eagerLoad(models: T[]): Promise<Map<any, R[]>>;
}
/**
* BelongsTo relationship
* Represents an inverse one-to-one or one-to-many relationship
*/
export declare class BelongsTo<T extends Model, R extends Model> extends Relationship<T, R> {
/**
* Foreign key on the owner model
*/
private foreignKey;
/**
* Constructor for BelongsTo relationship
* @param ownerModel - The model instance that owns this relationship
* @param relatedModelClass - The related model class
* @param foreignKey - The foreign key on the owner model
* @param ownerKey - The referenced key on the related model (default: primary key)
*/
constructor(ownerModel: T, relatedModelClass: new () => R, foreignKey: string, ownerKey?: string);
/**
* Get the related record
*/
get(): Promise<R[]>;
/**
* Get the first related record (convenience method)
*/
first(): Promise<R | null>;
/**
* Eager load related records for a collection of models
*/
eagerLoad(models: T[]): Promise<Map<any, R[]>>;
}
/**
* ManyToMany relationship
* Represents a many-to-many relationship through a pivot table
*/
export declare class ManyToMany<T extends Model, R extends Model> extends Relationship<T, R> {
private relatedKey?;
/**
* Pivot table name
*/
private pivotTable;
/**
* Foreign key on the pivot table for the owner model
*/
private foreignPivotKey;
/**
* Foreign key on the pivot table for the related model
*/
private relatedPivotKey;
/**
* Constructor for ManyToMany relationship
* @param ownerModel - The model instance that owns this relationship
* @param relatedModelClass - The related model class
* @param pivotTable - The pivot table name
* @param foreignPivotKey - The foreign key on the pivot table for the owner model
* @param relatedPivotKey - The foreign key on the pivot table for the related model
* @param localKey - The local key on the owner model (default: primary key)
* @param relatedKey - The local key on the related model (default: primary key)
*/
constructor(ownerModel: T, relatedModelClass: new () => R, pivotTable: string, foreignPivotKey: string, relatedPivotKey: string, localKey?: string, relatedKey?: string | undefined);
/**
* Get the related records
*/
get(): Promise<R[]>;
/**
* Eager load related records for a collection of models
*/
eagerLoad(models: T[]): Promise<Map<any, R[]>>;
/**
* Attach related models to the owner model
* @param relatedIds - IDs of related models to attach
*/
attach(relatedIds: any | any[]): Promise<void>;
/**
* Detach related models from the owner model
* @param relatedIds - Optional IDs of related models to detach. Detaches all if not provided.
*/
detach(relatedIds?: any | any[]): Promise<void>;
/**
* Toggle the attachment status of the given related models
* @param relatedIds - IDs of related models to toggle
*/
toggle(relatedIds: any | any[]): Promise<void>;
}
/**
* Relationship decorator options
*/
export interface RelationshipOptions {
/**
* Make the relationship lazy-loaded
* @default false
*/
lazy?: boolean;
}
/**
* Class decorator to register relationships for a model
* @param relationships - Map of relationship property names to relationship definitions
*/
export declare function Relationships(relationships: Record<string, (model: any) => Relationship<any, any>>): ClassDecorator;
//# sourceMappingURL=ModelRelationships.d.ts.map