UNPKG

slim-node-mysql

Version:

MySQL database class to abstract pooling and prepared statements

63 lines (62 loc) 3.08 kB
import { PoolOptions } from 'mysql2'; import { ExecuteResult } from './models/ExecuteResult'; import { PreparedStatementParameters } from './models/PreparedStatementParameters'; /** * The main class for SlimNodeMySQL to create a new connection to the database and perform queries. */ export declare class SlimNodeMySQL { private pool; private config; private otherPoolOptions; constructor(config: string | PoolOptions, otherPoolOptions?: PoolOptions); /** * Check if there is an open pool. * @returns true if the pool is open. */ hasOpenPool(): boolean; /** * Re-connect to the database if the close method has been called. * The constructor auto-connects to the database so this method is only needed if the close method has been called. */ connect(): void; /** * Executes a create, update, or delete SQL query and returns the result. * @param sql SQL to run * @param parameters Prepared statement parameters to to replace in the SQL (keys should match the "@" in the SQL) * @returns An ExecuteResult object. */ execute(sql: string, parameters?: PreparedStatementParameters): Promise<ExecuteResult>; /** * Query the database for a list of records. * @param sql The SQL to run to get records. * @param parameters The prepared statement parameters to replace in the SQL (keys should match the "@" in the SQL) * @returns A list of records. */ query<TableModel>(sql: string, parameters?: PreparedStatementParameters): Promise<TableModel[]>; /** * Return a single record from the database or null if no record is found. For large queries, use a LIMIT in the SQL for better performance. * @param sql The SQL to run to get a single record. * @param parameters The prepared statement parameters to replace in the SQL (keys should match the "@" in the SQL) * @returns the record or null. */ getOne<TableModel>(sql: string, parameters?: PreparedStatementParameters): Promise<TableModel>; /** * Get a single value from a record. For large queries, use a LIMIT in the SQL for better performance. * @param column the column of the record to return. * @param sql the SQL to run to get the record. * @param parameters the prepared statement parameters to replace in the SQL (keys should match the "@" in the SQL) * @returns the value of the column or null if no record is found. */ getValue<TableModel, K extends keyof TableModel>(column: K, sql: string, parameters?: PreparedStatementParameters): Promise<TableModel[K] | null>; /** * Check if a given record exists. * @param sql SQL to run to check if a record exists. * @param parameters Prepared statement parameters to to replace in the SQL (keys should match the "@" in the SQL) * @returns boolean value if the record exists. */ exists<TableModel>(sql: string, parameters?: PreparedStatementParameters): Promise<boolean>; /** * Close the database pool. */ close(): Promise<void>; }