db-sql-toolkit
Version:
Helps with SQL statements, database migration, and bulk execution.
52 lines (51 loc) • 2.6 kB
TypeScript
import { type Database } from "./Database";
import type { DefaultParamType } from "./sql";
interface BulkStatementParams<TData, TParam = DefaultParamType> {
statement: string;
getParameters: (data: TData) => TParam[];
}
interface BulkExecuteStatementParams<TParam = DefaultParamType> {
statement: string;
parameters: TParam[];
bulkParameters: TParam[];
bulkParametersIndex: number;
}
/**
* Inserts data into a database in as few operations as possible.
* @param database The database.
* @param entities The collection of data to insert.
* @param param2 A tuple consisting of the "INSERT INTO" SQL statement a function to get the parameters for one entity. The number of parameters must be equal for all entities.
*/
declare function bulkInsertEntities<TData, TParam = DefaultParamType>(database: Database<TParam>, entities: TData[], { statement, getParameters }: BulkStatementParams<TData, TParam>): Promise<void>;
/**
* Executes a statement in as few operations as possible.
* @param database The database.
* @param executeParams A tuple consisting of the SQL statement, the parameters of the "WHERE" clause, and the parameters of the "IN" clause.
*/
declare function bulkExecuteCommand<TParam = DefaultParamType>(database: Database<TParam>, executeParams: BulkExecuteStatementParams<TParam>): Promise<void>;
/**
* Loads data from a database in as few operations as possible.
* @param database The database.
* @param executeParams The tuple consisting of the SQL statement, the parameters of the "WHERE" clause, and the parameters of the "IN" clause.
*/
declare function bulkGetRows<TData, TParam = DefaultParamType>(database: Database<TParam>, executeParams: BulkExecuteStatementParams<TParam>): Promise<TData[]>;
/**
* Gets the total count of rows from a database in as few operations as possible.
* @param database The database.
* @param executeParams The tuple consisting of the SQL statement, the parameters of the "WHERE" clause, and the parameters of the "IN" clause.
* @returns The total count of rows.
* @example
* ```ts
* const priorities = ["1", "2", "3"];
* const statement = sql`
* SELECT COUNT(*)
* FROM process
* WHERE
* priority IN (${priorities})
* `;
* const count = await bulkGetCount(database, statement)
* ```
*/
declare function bulkGetCount<TParam = DefaultParamType>(database: Database<TParam>, executeParams: BulkExecuteStatementParams<TParam>): Promise<number>;
export type { BulkExecuteStatementParams, BulkStatementParams };
export { bulkExecuteCommand, bulkGetCount, bulkGetRows, bulkInsertEntities };