tspace-mysql
Version:
Tspace MySQL is a promise-based ORM for Node.js, designed with modern TypeScript and providing type safety for schema databases.
602 lines (601 loc) • 28.9 kB
TypeScript
import { TRawStringQuery } from "../types";
import { Model } from "./Model";
import { type T } from "./UtilityTypes";
declare class RepositoryFactory<TS extends Record<string, any> = Record<string, any>, TR = unknown, M extends Model<TS, TR> = Model<TS, TR>> {
private _model;
constructor(_model: {
new (): Model<TS, TR>;
});
/**
*
* The 'find' method is used to retrieve a single record from a database table by its primary key.
*
* It allows you to retrieve a single record from a database table that meets the specified criteria.
* @type {?Object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {string[]} options.relations
* @property {string[]} options.relationExists
* @property {?{condition,callback}} options.relationQuery
* @property {?boolean} options.debug
* @returns {promise<Object | null>}
*
* @example
* import { Repository } from 'tspace-mysql'
* import { User } from '../Models/User'
*
* const userRepository = Repository(User)
*
* const user = await userRepository.find(1,{
* select : { id: true, name: true }
* })
*
* const user = await userRepository.findOne()
*/
find<S extends T.SelectOptions<M> | undefined = undefined, SR extends T.RelationOptions<M> | undefined = undefined, E extends T.ExceptOptions<M> | undefined = undefined, SRS extends Record<string, TRawStringQuery> | undefined = undefined, G extends Record<string, T.RepositoryGenericTypeOptions> | undefined = {}>(primaryKey: number | string, options?: T.RepositoryOptions<M, S, SR, E, SRS, G>): Promise<T.ResultFiltered<M, S, SR, E, SRS, G> | null>;
/**
*
* The 'findOne' method is used to retrieve the first record that matches the query conditions.
*
* It allows you to retrieve a single record from a database table that meets the specified criteria.
* @type {?Object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {string[]} options.relations
* @property {string[]} options.relationExists
* @property {?{condition,callback}} options.relationQuery
* @property {?boolean} options.debug
* @returns {promise<Object | null>}
*
* @example
* import { Repository } from 'tspace-mysql'
* import { User } from '../Models/User'
*
* const userRepository = Repository(User)
*
* const user = await userRepository.findOne({
* select : { id: true, name: true },
* where : {
* id: 1
* }
* })
*
* const user = await userRepository.findOne()
*/
findOne<S extends T.SelectOptions<M> | undefined = undefined, SR extends T.RelationOptions<M> | undefined = undefined, E extends T.ExceptOptions<M> | undefined = undefined, SRS extends Record<string, TRawStringQuery> | undefined = undefined, G extends Record<string, T.RepositoryGenericTypeOptions> | undefined = {}>(options?: T.RepositoryOptions<M, S, SR, E, SRS, G>): Promise<T.ResultFiltered<M, S, SR, E, SRS, G> | null>;
/**
*
* The 'findMany' method is used to retrieve the get record that matches the query conditions.
*
* It allows you to retrieve a single record from a database table that meets the specified criteria.
* @type {?Object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {string[]} options.relations
* @property {string[]} options.relationExists
* @property {?{condition,callback}} options.relationQuery
* @property {?boolean} options.debug
* @returns {promise<Object>[]}
*
* @example
* import { Repository } from 'tspace-mysql'
* import { User } from '../Models/User'
*
* const userRepository = Repository(User)
*
* const users = await userRepository.get({
* select : { id: true, name: true },
* where : {
* id: 1
* }
* })
*
* const users = await userRepository.get()
*/
findMany<S extends T.SelectOptions<M> | undefined = undefined, SR extends T.RelationOptions<M> | undefined = undefined, E extends T.ExceptOptions<M> | undefined = undefined, SRS extends Record<string, TRawStringQuery> | undefined = undefined, G extends Record<string, T.RepositoryGenericTypeOptions> | undefined = {}>(options?: T.RepositoryOptions<M, S, SR, E, SRS, G>): Promise<T.ResultFiltered<M, S, SR, E, SRS, G>[]>;
/**
*
* The 'paginate' method is used to perform pagination on a set of database query results obtained through the Query Builder.
*
* It allows you to split a large set of query results into smaller, more manageable pages,
* making it easier to display data in a web application and improve user experience.
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?string[]} options.relations
* @property {string[]} options.relationExists
* @property {?{condition,callback}} options.relationQuery
* @property {?boolean} options.debug
* @property {?number} options.page
* @returns {promise<{ meta , data[]}>}
*
* @example
* import { Repository } from 'tspace-mysql'
* import { User } from '../Models/User'
*
* const userRepository = Repository(User)
*
* const users = await userRepository.paginate({
* select : { id: true, name: true },
* where : {
* id: 1
* }
* })
*
* const users = await userRepository.paginate({ page : 1 , limit : 2 })
*/
paginate<S extends T.SelectOptions<M> | undefined = undefined, SR extends T.RelationOptions<M> | undefined = undefined, E extends T.ExceptOptions<M> | undefined = undefined, SRS extends Record<string, TRawStringQuery> | undefined = undefined, G extends Record<string, T.RepositoryGenericTypeOptions> | undefined = {}>(options?: Omit<Partial<T.RepositoryOptions<M, S, SR, E, SRS, G>> & {
page?: number;
}, 'offset'>): Promise<T.PaginateResultFiltered<M, S, SR, E, SRS, G>>;
/**
* The 'exists' method is used to determine if any records exist in the database table that match the query conditions.
*
* It returns a boolean value indicating whether there are any matching records.
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?boolean} options.debug
*
* @example
* import { Repository } from 'tspace-mysql'
* import { User } from '../Models/User'
*
* const userRepository = Repository(User)
*
* const users = await userRepository.exists({
* where : {
* id: 1
* }
* })
*
*/
exists(options: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): Promise<boolean>;
/**
* The 'toString' method is used to retrieve the raw SQL query that would be executed by a query builder instance without actually executing it.
*
* This method is particularly useful for debugging and understanding the SQL queries generated by your application.
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?boolean} options.debug
* @returns {string}
*/
toString(options?: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): string;
/**
* The 'toJSON' method is used to execute a database query and retrieve the result set that matches the query conditions.
*
* It retrieves multiple records from a database table based on the criteria specified in the query.
*
* It returns a JSON formatted.
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?boolean} options.debug
* @property {?number} options.page
* @returns {string} json
*/
toJSON(options?: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): Promise<string>;
/**
* The 'toArray' method is used to execute a database query and retrieve the result set that matches the query conditions.
*
* It retrieves multiple records from a database table based on the criteria specified in the query.
*
* It returns an array formatted.
* @param {string} column
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?boolean} options.debug
* @property {?number} options.page
* @return {promise<any[]>}
*/
toArray<K extends T.ColumnKeys<M> | `${string}.${string}`>(column: K, options?: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): Promise<(K extends keyof T.Result<M> ? T.Result<M>[K] : unknown)[]>;
/**
* The 'count' method is used to retrieve the total number of records that match the specified query conditions.
*
* It returns an integer representing the count of records.
* @param {string} column
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?boolean} options.debug
* @property {?number} options.page
* @return {promise<any[]>}
*/
count(column: T.ColumnKeys<M> | `${string}.${string}`, options?: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): Promise<number>;
/**
* The 'avg' method is used to retrieve the total number of records that match the specified query conditions.
*
* It returns an integer representing the avg of records.
* @param {string} column
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?boolean} options.debug
* @property {?number} options.page
* @return {promise<any[]>}
*/
avg(column: T.ColumnKeys<M> | `${string}.${string}`, options?: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): Promise<number>;
/**
* The 'sum' method is used to retrieve the total number of records that match the specified query conditions.
*
* It returns an integer representing the sum of records.
* @param {string} column
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?boolean} options.debug
* @property {?number} options.page
* @return {promise<any[]>}
*/
sum(column: T.ColumnKeys<M> | `${string}.${string}`, options?: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): Promise<number>;
/**
* The 'max' method is used to retrieve the maximum value of a numeric column in a database table.
*
* It finds the highest value in the specified column among all records that match the query conditions and returns that value.
* @param {string} column
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?boolean} options.debug
* @property {?number} options.page
* @return {promise<any[]>}
*/
max(column: T.ColumnKeys<M> | `${string}.${string}`, options?: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): Promise<number>;
/**
* The 'min' method is used to retrieve the minimum (lowest) value of a numeric column in a database table.
*
* It finds the smallest value in the specified column among all records that match the query conditions and returns that value.
* @param {string} column
* @type {?object} options
* @property {?object} options.select
* @property {?object} options.except
* @property {?object[]} options.orderBy
* @property {?string[]} options.groupBy
* @property {?string} options.having
* @property {?number} options.limit
* @property {?number} options.offset
* @property {?object} options.where
* @property {?string[]} options.whereRaw
* @property {?object} options.whereQuery
* @property {?{condition,callback}} options.when
* @property {?{localKey , referenceKey}[]} options.join
* @property {?{localKey , referenceKey}[]} options.rightJoin
* @property {?{localKey , referenceKey}[]} options.leftJoin
* @property {?boolean} options.debug
* @property {?number} options.page
* @return {promise<any[]>}
*/
min(column: T.ColumnKeys<M> | `${string}.${string}`, options?: Partial<Omit<T.RepositoryOptions<M>, "relations" | "relationQuery">>): Promise<number>;
/**
* The 'create' method is used to insert a new record into a database table associated.
*
* It simplifies the process of creating and inserting records.
* @type {object} options
* @property {object} options.data
* @property {?boolean} options.debug
* @property {?transaction} options.transaction
* @return {promise<T.Result<M>>}
*/
create<NR extends boolean | undefined = false>({ data, debug, transaction, noReturn, }: T.RepositoryCreate<M, NR>): Promise<NR extends true ? undefined : T.Result<M>>;
/**
* The 'createNotExists' method to insert data into a database table while ignoring any duplicate key constraint violations.
*
* This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
* but without raising an error or exception if duplicates are encountered.
*
* @type {object} options
* @property {object} options.data
* @property {object} options.where
* @property {?boolean} options.debug
* @property {?transaction} options.transaction
* @return {promise<T | null>}
*/
createNotExists<NR extends boolean | undefined = false>({ data, where, debug, noReturn, transaction }: T.RepositoryCreateOrThings<M, NR>): Promise<NR extends true ? undefined : T.Result<M> | null>;
/**
* The 'createMultiple' method is used to insert a new records into a database table associated.
*
* It simplifies the process of creating and inserting records with an array.
* @type {object} options
* @property {object[]} options.data
* @property {?boolean} options.debug
* @property {?transaction} options.transaction
* @return {promise<TS[]>}
*/
createMultiple<NR extends boolean | undefined = false>({ data, debug, transaction, noReturn }: T.RepositoryCreateMultiple<M, NR>): Promise<NR extends true ? undefined : T.Result<M>[]>;
/**
* The 'createMany' method is used to insert a new records into a database table associated.
*
* It simplifies the process of creating and inserting records with an array.
* @type {object} options
* @property {object[]} options.data
* @property {?boolean} options.debug
* @property {?transaction} options.transaction
* @return {promise<TS[]>}
*/
createMany<NR extends boolean | undefined = false>({ data, debug, transaction, noReturn }: T.RepositoryCreateMultiple<M, NR>): Promise<NR extends true ? undefined : T.Result<M>[]>;
/**
*
* The 'createOrUpdate' method allows you to update an existing record in a database table if it exists or create a new record if it does not exist.
*
* This method is particularly useful when you want to update a record based on certain conditions and,
* if the record matching those conditions doesn't exist, create a new one with the provided data.
* @type {object} options
* @property {object} options.data
* @property {object} options.where
* @property {?boolean} options.debug
* @return {promise<T.Result<M>>}
*/
createOrUpdate<NR extends boolean | undefined = false>({ data, where, debug, transaction, noReturn }: T.RepositoryCreateOrThings<M, NR>): Promise<NR extends true ? undefined : T.Result<M>>;
/**
*
* The 'createOrSelect' method to insert data into a database table while select any duplicate key constraint violations.
*
* This method is particularly useful when you want to insert records into a table and ensure that duplicates are not inserted,
* but if exists should be returns a result.
* @type {object} options
* @property {object} options.data
* @property {object} options.where
* @property {?boolean} options.debug
* @return {promise<T.Result<M>>}
*/
createOrSelect<NR extends boolean | undefined = false>({ data, where, debug, transaction, noReturn }: T.RepositoryCreateOrThings<M, NR>): Promise<NR extends true ? undefined : T.Result<M> | null>;
/**
* The 'update' method is used to update existing records in a database table that are associated.
*
* It simplifies the process of updating records by allowing you to specify the values to be updated using a single call.
*
* It allows you to remove one record that match certain criteria.
* @type {object} options
* @property {object} options.data
* @property {object} options.where
* @property {?boolean} options.debug
* @property {?transaction} options.transaction
* @return {promise<T.Result<M>>}
*/
update<NR extends boolean | undefined = false>({ data, where, debug, transaction, noReturn }: T.RepositoryUpdate<M, NR>): Promise<NR extends true ? undefined : T.Result<M> | null>;
/**
* The 'updateMany' method is used to update existing records in a database table that are associated.
*
* It simplifies the process of updating records by allowing you to specify the values to be updated using a single call.
*
* It allows you to remove more records that match certain criteria.
* @type {object} options
* @property {object} options.data
* @property {object} options.where
* @property {?boolean} options.debug
* @property {?transaction} options.transaction
* @return {promise<T.Result<M>[]>}
*/
updateMany<NR extends boolean | undefined = false>({ data, where, debug, transaction, noReturn }: T.RepositoryUpdate<M, NR>): Promise<NR extends true ? undefined : T.Result<M>[]>;
/**
* The 'updateCases' method is used to update records in a table based on specific conditions.
*
* This method allows updating multiple rows at once by specifying an array of update cases.
* Each case defines which records to update (`when`) and the new values to apply (`columns`).
*
* @type {object} options
* @property {object} options.data
* @property {object} options.where
* @property {?boolean} options.debug
* @property {?transaction} options.transaction
* @return {promise<TS[]>}
* @example
* const saveUpdateMultiple = await userRepository.updateCases({
* cases : [
* {
* when : {
* id: 1
* },
* columns : {
* name : 'name-edit-in-multiple : id 1 '
* }
* },
* {
* when : {
* id : 2
* },
* columns : {
* name : 'name-edit-in-multiple : id 2'
* }
* }
* ],
* where : {
* id : Operator.in([1,2])
* }
* })
*
*/
updateCases<NR extends boolean | undefined = false>({ cases, debug, transaction, noReturn }: T.RepositoryUpdateMultiple<M, NR>): Promise<NR extends true ? undefined : T.Result<M>[]>;
/**
* The 'delete' method is used to delete records from a database table based on the specified query conditions.
*
* It allows you to remove one record that match certain criteria.
* @type {object} options
* @property {object} options.data
* @property {object} options.where
* @property {?boolean} options.debug
* @property {?transaction} options.transaction
* @return {promise<boolean>}
*/
delete({ where, debug, transaction, }: T.RepositoryDelete<M>): Promise<boolean>;
/**
* The 'deleteMany' method is used to delete records from a database table based on the specified query conditions.
*
* It allows you to remove one record that match certain criteria.
* @type {object} options
* @property {object} options.data
* @property {object} options.where
* @property {?boolean} options.debug
* @property {?transaction} options.transaction
* @return {promise<boolean>}
*/
deleteMany({ where, debug, transaction, }: T.RepositoryDelete<M>): Promise<boolean>;
private _handleRelationQuery;
private _handlerRequest;
}
/**
*
* The 'Repository' is a class that encapsulates all database operations related to a specific model.
*
* It provides methods for querying, inserting, updating, and deleting records in the database associated with the model.
*
* @param {Model} model A class constructor for a model
* @returns {RepositoryFactory<T,R>}
*
* @example
* import { Repository } from 'tspace-mysql'
* import { User } from '../Models/User'
*
* const userRepository = Repository(User)
*
* const user = await userRepository.findOne()
* const users = await userRepository.findMany()
*
*/
export declare const Repository: <M extends Model<any, any>>(model: new () => M) => RepositoryFactory<T.SchemaModel<M>, T.RelationModel<M>, M>;
export default Repository;