UNPKG

@techmmunity/symbiosis

Version:

Symbiosis - The Ultimate OM For All Databases

356 lines (355 loc) 14.1 kB
import type { EntityManager } from "../entity-manager"; import type { Logger } from "../logger"; import type { AfterCountParams } from "./methods/count/after"; import type { BeforeCountInput, BeforeCountOutput } from "./methods/count/before"; import type { AfterDeleteParams } from "./methods/delete/after"; import type { BeforeDeleteInput, BeforeDeleteOutput } from "./methods/delete/before"; import type { AfterFindOneParams } from "./methods/find-one/after"; import type { BeforeFindOneInput, BeforeFindOneOutput } from "./methods/find-one/before"; import type { AfterFindParams } from "./methods/find/after"; import type { BeforeFindInput, BeforeFindOutput } from "./methods/find/before"; import type { AfterInsertParams } from "./methods/insert/after"; import type { BeforeInsertInput, BeforeInsertOutput } from "./methods/insert/before"; import type { AfterPerformativeCountParams } from "./methods/performative-count/after"; import type { BeforePerformativeCountInput, BeforePerformativeCountOutput } from "./methods/performative-count/before"; import type { AfterRecoverParams } from "./methods/recover/after"; import type { BeforeRecoverInput, BeforeRecoverOutput } from "./methods/recover/before"; import type { AfterSaveParams } from "./methods/save/after"; import type { BeforeSaveInput, BeforeSaveOutput } from "./methods/save/before"; import type { AfterSoftDeleteParams } from "./methods/soft-delete/after"; import type { BeforeSoftDeleteInput, BeforeSoftDeleteOutput } from "./methods/soft-delete/before"; import type { AfterUpdateParams } from "./methods/update/after"; import type { BeforeUpdateInput, BeforeUpdateOutput } from "./methods/update/before"; import type { AfterUpsertParams } from "./methods/upsert/after"; import type { BeforeUpsertInput, BeforeUpsertOutput } from "./methods/upsert/before"; import type { BaseExtraMetadata } from "../types/extra-metadata"; import type { FindConditions } from "./types/find-conditions"; import type { FindOneOptions, FindOptions } from "./types/find-options"; import type { BaseCountOutput } from "./types/methods-outputs/count"; import type { BaseDeleteOutput } from "./types/methods-outputs/delete"; import type { BaseFindOutput } from "./types/methods-outputs/find"; import type { BaseFindOneOutput } from "./types/methods-outputs/find-one"; import type { BaseInsertOutput } from "./types/methods-outputs/insert"; import type { BasePerformativeCountOutput } from "./types/methods-outputs/performative-count"; import type { BaseRecoverOutput } from "./types/methods-outputs/recover"; import type { BaseSaveOutput } from "./types/methods-outputs/save"; import type { BaseSoftDeleteOutput } from "./types/methods-outputs/soft-delete"; import type { BaseUpdateOutput } from "./types/methods-outputs/update"; import type { BaseUpsertOutput } from "./types/methods-outputs/upsert"; import type { BaseQueryOptions } from "./types/query-options"; import type { SingleSaveData, ArraySaveData } from "./types/save-conditions"; export declare abstract class BaseRepository<Entity = any, ExtraMetadata extends BaseExtraMetadata = any> { protected readonly entityManager: EntityManager<ExtraMetadata>; protected readonly logger: Logger; protected readonly entity: Entity; readonly tableName: string; constructor(entityManager: EntityManager<ExtraMetadata>, logger: Logger, entity: any); /** * -------------------------------------------------- * * Insert / Update * * -------------------------------------------------- */ /** * Make an "upsert" operation based on the primary keys. * * This is an more performative way to make an "upsert", * and most of the databases supports this method. */ abstract save(data: SingleSaveData<Entity>, options?: BaseQueryOptions): Promise<BaseSaveOutput<Entity>>; abstract save(data: ArraySaveData<Entity>, options?: BaseQueryOptions): Promise<BaseSaveOutput<Array<Entity>>>; /** * Inserts a record on the database and fail if it's already exist. */ abstract insert(data: SingleSaveData<Entity>, options?: BaseQueryOptions): Promise<BaseInsertOutput<Entity>>; abstract insert(data: ArraySaveData<Entity>, options?: BaseQueryOptions): Promise<BaseInsertOutput<Array<Entity>>>; /** * Updates a record based on a query and fail if it's not exist. */ abstract update(conditions: FindConditions<Entity>, data: SingleSaveData<Entity>, options?: BaseQueryOptions): Promise<BaseUpdateOutput<Entity>>; /** * Make an "upsert" operation based on a query. */ abstract upsert(conditions: FindConditions<Entity>, data: SingleSaveData<Entity>, options?: BaseQueryOptions): Promise<BaseUpsertOutput<Entity>>; /** * -------------------------------------------------- * * Find * * -------------------------------------------------- */ /** * Find many records based on a query. */ abstract find(conditions: FindOptions<Entity>, options?: BaseQueryOptions): Promise<BaseFindOutput<Entity>>; /** * Find one record based on a query. */ abstract findOne(conditions: FindOneOptions<Entity>, options?: BaseQueryOptions): Promise<BaseFindOneOutput<Entity>>; /** * -------------------------------------------------- * * Delete * * -------------------------------------------------- */ /** * Delete a record based on a query condition. */ abstract delete(where: FindConditions<Entity>, options?: BaseQueryOptions): Promise<BaseDeleteOutput>; /** * Soft delete a record based on a query condition. * * **WARN:** To use this method, the entity must have * a column decorated with `DeleteDateColumn`. */ abstract softDelete(where: FindConditions<Entity>, options?: BaseQueryOptions): Promise<BaseSoftDeleteOutput>; /** * Recovers a record that was sof-deleted. * * **WARN:** To use this method, the entity must have * a column decorated with `DeleteDateColumn`. */ abstract recover(where: FindConditions<Entity>, options?: BaseQueryOptions): Promise<BaseRecoverOutput>; /** * -------------------------------------------------- * * Count * * -------------------------------------------------- */ /** * Count the records returned by a query condition. */ abstract count(where: FindConditions<Entity>, options?: BaseQueryOptions): Promise<BaseCountOutput>; /** * Some databases, like PostgreSQL, have methods that allow * you to make a count operation in a more performative way, * but it's not very precise. * * This methods allow you to make a count operator, that * in some cases can be imprecise, but is more performative */ abstract performativeCount(where: FindConditions<Entity>, options?: BaseQueryOptions): Promise<BasePerformativeCountOutput>; /** * -------------------------------------------------- * * BEFORE & AFTER save * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeSave(params: BeforeSaveInput<Entity>): BeforeSaveOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterSave(params: AfterSaveParams): Array<Entity> | Entity; /** * -------------------------------------------------- * * BEFORE & AFTER insert * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeInsert(params: BeforeInsertInput<Entity>): BeforeInsertOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterInsert(params: AfterInsertParams): Array<Entity> | Entity; /** * -------------------------------------------------- * * BEFORE & AFTER update * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeUpdate(params: BeforeUpdateInput<Entity>): BeforeUpdateOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterUpdate(params: AfterUpdateParams<Entity>): Array<Entity>; /** * -------------------------------------------------- * * BEFORE & AFTER upsert * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeUpsert(params: BeforeUpsertInput<Entity>): BeforeUpsertOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterUpsert(params: AfterUpsertParams<Entity>): Array<Entity>; /** * -------------------------------------------------- * * BEFORE & AFTER find * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeFind(params: BeforeFindInput<Entity>): BeforeFindOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterFind(params: AfterFindParams<Entity>): Array<Entity>; /** * -------------------------------------------------- * * BEFORE & AFTER findOne * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeFindOne(params: BeforeFindOneInput<Entity>): BeforeFindOneOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterFindOne(params: AfterFindOneParams<Entity>): Entity | undefined; /** * -------------------------------------------------- * * BEFORE & AFTER delete * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeDelete(params: BeforeDeleteInput<Entity>): BeforeDeleteOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterDelete(params: AfterDeleteParams<Entity>): number; /** * -------------------------------------------------- * * BEFORE & AFTER softDelete * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeSoftDelete(params: BeforeSoftDeleteInput<Entity>): BeforeSoftDeleteOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterSoftDelete(params: AfterSoftDeleteParams<Entity>): number; /** * -------------------------------------------------- * * BEFORE & AFTER recover * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeRecover(params: BeforeRecoverInput<Entity>): BeforeRecoverOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterRecover(params: AfterRecoverParams<Entity>): number; /** * -------------------------------------------------- * * BEFORE & AFTER count * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforeCount(params: BeforeCountInput<Entity>): BeforeCountOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterCount(params: AfterCountParams<Entity>): number; /** * -------------------------------------------------- * * BEFORE & AFTER performativeCount * * -------------------------------------------------- */ /** * Handles the data before the start of the function * * Does things like auto-generate values and format the * data to the database format */ protected beforePerformativeCount(params: BeforePerformativeCountInput<Entity>): BeforePerformativeCountOutput; /** * Handles the data after the end of the function * * Does things like format the data to the entity format */ protected afterPerformativeCount(params: AfterPerformativeCountParams<Entity>): number; }