UNPKG

@sqb/connect

Version:

Multi-dialect database connection framework written with TypeScript

214 lines (213 loc) 10.6 kB
import { LogicalOperator } from '@sqb/builder'; import { PartialDTO, PatchDTO, RequiredSome, StrictOmit, Type } from 'ts-gems'; import { FieldInfoMap } from '../client/field-info-map.js'; import type { SqbClient } from '../client/sqb-client.js'; import type { SqbConnection } from '../client/sqb-connection.js'; import { QueryRequest, TransactionFunction } from '../client/types.js'; import { EntityMetadata } from './model/entity-metadata.js'; interface Projection { projection?: string | string[]; } interface Filtering { filter?: LogicalOperator | LogicalOperator[] | object | object[]; params?: any; } export declare namespace Repository { type TransformRowFunction = (fields: FieldInfoMap, row: object, obj: object) => void; interface CommandOptions { connection?: SqbConnection; prettyPrint?: boolean; } interface CreateOptions extends CommandOptions, Projection { } interface CountOptions extends CommandOptions, Filtering { } interface ExistsOptions extends CommandOptions, Filtering { } interface DeleteOptions extends CommandOptions, Filtering { } interface DeleteManyOptions extends CommandOptions, Filtering { } interface FindOptions extends CommandOptions, Projection, Filtering { } interface FindOneOptions extends FindOptions { sort?: string[]; offset?: number; } interface FindManyOptions extends FindOneOptions { limit?: number; distinct?: boolean; maxEagerFetch?: number; maxSubQueries?: number; onTransformRow?: TransformRowFunction; } interface UpdateOptions extends CommandOptions, Projection, Filtering { } interface UpdateOnlyOptions extends CommandOptions, Filtering { } interface UpdateManyOptions extends CommandOptions, Filtering { } } interface RepositoryEvents { execute: (request: QueryRequest) => void; error: (error: Error) => void; acquire: (connection: SqbConnection) => Promise<void>; } declare const Repository_base: Type<import("strict-typed-events").TypedEventEmitter<any, RepositoryEvents, RepositoryEvents>>; /** * @class Repository * @template T - The data type class type of the record */ export declare class Repository<T> extends Repository_base { private readonly _executor; private readonly _entity; private readonly _schema?; constructor(entityDef: EntityMetadata, executor: SqbClient | SqbConnection, schema?: string); get entity(): EntityMetadata; get type(): Type<T>; /** * Creates a new resource * * @param {PartialDTO<T>} input - The input data * @param {Repository.CreateOptions} [options] - The options object * @returns {Promise<PartialDTO<T>>} A promise that resolves to the created resource * @throws {Error} if an unknown error occurs while creating the resource */ create(input: PartialDTO<T>, options: RequiredSome<Repository.CreateOptions, 'projection'>): Promise<PartialDTO<T>>; create(input: PartialDTO<T>, options?: Repository.CreateOptions): Promise<T>; /** * Creates a new resource but returns nothing * * @param {PartialDTO<T>} input - The input data * @param {Repository.CreateOptions} [options] - The options object * @returns {Promise<any>} A promise that resolves key value(s) of the created record * @throws {Error} if an unknown error occurs while creating the resource */ createOnly(input: PartialDTO<T>, options?: StrictOmit<Repository.CreateOptions, keyof Projection>): Promise<any>; /** * Returns the count of records based on the provided options * * @param {Repository.CountOptions} options - The options for the count operation. * @return {Promise<number>} - A promise that resolves to the count of records */ count(options?: Repository.CountOptions): Promise<number>; /** * Deletes a record from the collection. * * @param {any} keyValue - The ID of the resource to delete. * @param {Repository.DeleteOptions} [options] - Optional delete options. * @return {Promise<boolean>} - A Promise that resolves true or false. True when resource deleted. */ delete(keyValue: any | Record<string, any>, options?: Repository.DeleteOptions): Promise<boolean>; /** * Deletes multiple documents from the collection that meet the specified filter criteria. * * @param {Repository.DeleteManyOptions} options - The options for the delete operation. * @return {Promise<number>} - A promise that resolves to the number of resources deleted. */ deleteMany(options?: Repository.DeleteManyOptions): Promise<number>; /** * Checks if a record with the given id exists. * * @param {any} keyValue - The id of the object to check. * @param {Repository.ExistsOptions} [options] - The options for the query (optional). * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not. */ exists(keyValue: any | Record<string, any>, options?: Repository.ExistsOptions): Promise<boolean>; /** * Checks if a record with the given arguments exists. * * @param {Repository.ExistsOptions} [options] - The options for the query (optional). * @return {Promise<boolean>} - A Promise that resolves to a boolean indicating whether the record exists or not. */ existsOne(options?: Repository.ExistsOptions): Promise<boolean>; /** * Finds a record by ID. * * @param {any} keyValue - The ID of the record. * @param {Repository.FindOneOptions} [options] - The options for the find query. * @return {Promise<PartialDTO<T | undefined>>} - A promise resolving to the found document, or undefined if not found. */ findById(keyValue: any | Record<string, any>, options: RequiredSome<Repository.FindOptions, 'projection'>): Promise<PartialDTO<T> | undefined>; findById(keyValue: any | Record<string, any>, options?: Repository.FindOptions): Promise<T | undefined>; /** * Finds a record in the collection that matches the specified options. * * @param {Repository.FindOneOptions} options - The options for the query. * @return {Promise<PartialDTO<T> | undefined>} A promise that resolves with the found document or undefined if no document is found. */ findOne(options: RequiredSome<Repository.FindOneOptions, 'projection'>): Promise<PartialDTO<T> | undefined>; findOne(options?: Repository.FindOneOptions): Promise<T | undefined>; /** * Finds multiple records in collection. * * @param {Repository.FindManyOptions} options - The options for the find operation. * @return A Promise that resolves to an array of partial outputs of type T. */ findMany(options: RequiredSome<Repository.FindManyOptions, 'projection'>): Promise<PartialDTO<T>[]>; findMany(options?: Repository.FindManyOptions): Promise<T[]>; /** * Updates a record with the given id in the collection. * * @param {any} keyValue - The id of the document to update. * @param {PatchDTO<T>} input - The partial input object containing the fields to update. * @param {Repository.UpdateOptions} [options] - The options for the update operation. * @returns {Promise<PartialDTO<T> | undefined>} A promise that resolves to the updated document or * undefined if the document was not found. */ update(keyValue: any | Record<string, any>, input: PatchDTO<T>, options: RequiredSome<Repository.UpdateOptions, 'projection'>): Promise<PartialDTO<T> | undefined>; update(keyValue: any | Record<string, any>, input: PatchDTO<T>, options?: Repository.UpdateOptions): Promise<T | undefined>; /** * Updates a record in the collection with the specified ID and returns updated record count * * @param {any} keyValue - The ID of the document to update. * @param {PatchDTO<T>} input - The partial input data to update the document with. * @param {Repository.UpdateOptions} options - The options for updating the document. * @returns {Promise<number>} - A Promise that resolves true or false. True when resource updated. */ updateOnly(keyValue: any | Record<string, any>, input: PatchDTO<T>, options?: Repository.UpdateOnlyOptions): Promise<boolean>; /** * Updates multiple records in the collection based on the specified input and options. * * @param {PatchDTO<T>} input - The partial input to update the documents with. * @param {Repository.UpdateManyOptions} options - The options for updating the documents. * @return {Promise<number>} - A promise that resolves to the number of documents matched and modified. */ updateMany(input: PartialDTO<T>, options?: Repository.UpdateManyOptions): Promise<number>; protected _execute(fn: TransactionFunction, opts?: Repository.CommandOptions): Promise<any>; protected _create(values: PartialDTO<T>, options: Repository.CreateOptions & { connection: SqbConnection; returning?: boolean; }): Promise<any>; protected _exists(keyValue: any | Record<string, any>, options: Repository.ExistsOptions & { connection: SqbConnection; }): Promise<boolean>; protected _existsOne(options: Repository.ExistsOptions & { connection: SqbConnection; }): Promise<boolean>; protected _count(options: Repository.CountOptions & { connection: SqbConnection; }): Promise<number>; protected _findMany(options: Repository.FindManyOptions & { connection: SqbConnection; }): Promise<PartialDTO<T>[]>; protected _findOne(options: Repository.FindOneOptions & { connection: SqbConnection; }): Promise<PartialDTO<T> | undefined>; protected _find(keyValue: any | Record<string, any>, options: Repository.FindOptions & { connection: SqbConnection; }): Promise<PartialDTO<T> | undefined>; protected _delete(keyValue: any | Record<string, any>, options: Repository.DeleteOptions & { connection: SqbConnection; }): Promise<boolean>; protected _deleteMany(options: Repository.DeleteManyOptions & { connection: SqbConnection; }): Promise<number>; protected _update(keyValue: any | Record<string, any>, values: PatchDTO<T>, options: Repository.UpdateOptions & { connection: SqbConnection; }): Promise<Record<string, any> | undefined>; protected _updateMany(values: PartialDTO<T>, options: Repository.UpdateManyOptions & { connection: SqbConnection; }): Promise<number>; } export {};