import { AbstractDB } from "./Abstracts/AbstractDB"; import type { T } from "./UtilityTypes"; import type { TConstant, TClone, TDump, TPoolConnected, TConnectionOptions, TConnection, TRawStringQuery, TFreezeStringQuery, TPoolEvent } from "../types"; /** * The 'DB' class is a component of the database system * @param {string?} table table name * @example * new DB('users').findMany().then(results => console.log(results)) */ declare class DB extends AbstractDB { constructor(table?: string); /** * The 'instance' method is used get instance. * @override * @static * @returns {DB} instance of the DB */ static get instance(): DB; /** * The 'initialize' method is used to initialize the database, * and check if it is connected properly. * * @returns {promise} */ initialize(): Promise; /** * The 'initialize' method is used to initialize the database, * and check if it is connected properly. * * @returns {promise} */ static initialize(): Promise; /** * This 'event' method ensures the pool is initialized before attaching * the given callback to the specified pool event. * * @async * @template TPoolEvent * @param {TPoolEvent} event - The name of the pool event to listen for. * @param {(data: any) => any} callback - A callback function invoked when the event is emitted. * @returns {Promise} A promise that resolves once the event listener is registered. */ event(event: TPoolEvent, callback: (data: any) => any): Promise; /** * * This 'event' method ensures the pool is initialized before attaching * the given callback to the specified pool event. * * @async * @template TPoolEvent * @param {TPoolEvent} event - The name of the pool event to listen for. * @param {(data: any) => any} callback - A callback function invoked when the event is emitted. * @returns {Promise} A promise that resolves once the event listener is registered. */ static event(event: TPoolEvent, callback: (data: any) => any): Promise; /** * The 'query' method is used to execute sql statement * * @param {string} sql * @param {Record} parameters * @returns {Promise} * * @example * Execute a query without parameters: * ```ts * const users = await db.query( * 'SELECT * FROM users' * ); * ``` * * @example * Execute a query with named parameters: * ```ts * const user = await db.query( * 'SELECT * FROM users WHERE id = :id', * { id: 1 } * ); * ``` * * @example * Execute a query with multiple parameters: * ```ts * const users = await db.query( * 'SELECT * FROM users WHERE status = :status AND age >= :age', * { * status: 'active', * age: 18, * } * ); * ``` * * @example * Use an array parameter: * ```ts * const users = await db.query( * 'SELECT * FROM users WHERE id IN :ids', * { * ids: [1, 2, 3], * } * ); * ``` * * @example * Positional parameters using `?` placeholders: * ```ts * const users = await db.query( * 'SELECT * FROM users WHERE id = ? AND status = ?', * [1, 'active'] * ); * ``` */ query(sql: string, parameters?: (boolean | number | string | any[] | null)[] | Record): Promise; /** * The 'query' method is used to execute sql statement * * @static * @param {string} sql * @param {Record} parameters * @returns {Promise} * * @example * Execute a query without parameters: * ```ts * const users = await db.query( * 'SELECT * FROM users' * ); * ``` * * @example * Execute a query with named parameters: * ```ts * const user = await db.query( * 'SELECT * FROM users WHERE id = :id', * { id: 1 } * ); * ``` * * @example * Execute a query with multiple parameters: * ```ts * const users = await db.query( * 'SELECT * FROM users WHERE status = :status AND age >= :age', * { * status: 'active', * age: 18, * } * ); * ``` * * @example * Use an array parameter: * ```ts * const users = await db.query( * 'SELECT * FROM users WHERE id IN :ids', * { * ids: [1, 2, 3], * } * ); * ``` * * @example * Positional parameters using `?` placeholders: * ```ts * const users = await db.query( * 'SELECT * FROM users WHERE id = ? AND status = ?', * [1, 'active'] * ); * ``` */ static query(sql: string, parameters?: (boolean | number | string | any[] | null)[] | Record): Promise; /** * The 'from' method is used to define the from table name. * * @static * @param {string} table table name * @returns {this} this */ static from(table: string): DB; /** * The 'table' method is used to define the table name. * * @static * @param {string} table table name * @returns {DB} DB */ static table(table: string): DB; /** * The 'alias' method is used to set the table name. * * @static * @param {string} sql raw sql from make a new alias for this table * @param {string} alias alias name * @returns {DB} DB */ static alias(sql: string, alias: string): DB; /** * * @static * @override * @returns {this} this */ static dd(debug?: boolean): DB; /** * The 'jsonObject' method is used to specify select data to JSON objects. * @param {string} object table name * @param {string} alias * @returns {string} string */ jsonObject(object: Record, alias: string): string; /** * The 'jsonObject' method is used to specify select data to JSON objects. * * @static * @param {string} object table name * @param {string} alias * @returns {string} string */ static jsonObject(object: Record, alias: string): string; /** * The 'JSONObject' method is used to specify select data to JSON objects. * * @param {string} object table name * @param {string} alias * @returns {string} string */ JSONObject(object: Record, alias: string): string; /** * The 'JSONObject' method is used to specify select data to JSON objects. * * @static * @param {string} object table name * @param {string} alias * @returns {string} string */ static JSONObject(object: Record, alias: string): string; /** * The 'constants' method is used to return constants with key or none in 'DB' or 'Model'. * @param {string} key * @returns {string | object} string || object */ constants(key?: keyof TConstant): string | object; /** * The 'constants' method is used to return constants with key or none in 'DB' or 'Model'. * * @static * @param {string} key * @returns {string | object} string || object */ static constants(key?: keyof TConstant): string | Record; /** * cases query * @param {arrayObject} cases array object {when , then } * @param {string?} final else condition * @returns {string} string */ caseUpdate(cases: { when: string; then: string; }[], final?: string): string | []; /** * select by cases * * @static * @param {arrayObject} cases array object {when , then } * @param {string?} final else condition * @returns {this} */ static caseUpdate(cases: { when: string; then: string; }[], final?: string): string | []; /** * The 'generateUUID' methid is used to generate a universal unique identifier (UUID). * * @param options - Options for UUID generation. * @param options.v - UUID version to generate: 4 (random) or 7 (time-ordered). Defaults to 7. * @returns {string} string */ generateUUID({ v }?: { v?: 4 | 7; }): string; /** * The 'generateUUID' methid is used to generate a universal unique identifier (UUID). * * @param options - Options for UUID generation. * @param options.v - UUID version to generate: 4 (random) or 7 (time-ordered). Defaults to 7. * @static * @returns {string} string */ static generateUUID({ v }?: { v?: 4 | 7; }): string; /** * The 'snakeCase' methid is used to covert value to snakeCase pattern. * @returns {string} string */ snakeCase(value: string): string; /** * The 'snakeCase' methid is used to covert value to snake_case pattern. * @returns {string} string */ static snakeCase(value: string): string; /** * The 'camelCase' methid is used to covert value to camelCase pattern. * @returns {string} string */ camelCase(value: string): string; /** * The 'camelCase' methid is used to covert value to camelCase pattern. * @returns {string} string */ static camelCase(value: string): string; /** * The 'escape' methid is used to escaping SQL injections. * @returns {string} string */ escape(value: string): string; /** * The 'escape' methid is used to escaping SQL injections. * * @static * @returns {string} string */ static escape(value: string): string; /** * The 'escapeXSS' methid is used to escaping XSS characters. * @returns {string} string */ escapeXSS(value: string): string; /** * The 'escapeXSS' methid is used to escaping XSS characters. * * @static * @returns {string} string */ static escapeXSS(value: string): string; /** * The 'raw' methid is used to allow for raw sql queries to some method in 'DB' or 'Model'. * Creates a raw SQL query with optional parameter bindings. * * @param sql - The raw SQL string. Use `?` placeholders for parameters. * @param parameters - Values to bind to the placeholders. * @returns A raw SQL query object. * * @example * // Select a computed column using CONCAT * const users = await DB * .from('users') * .select( * 'id', * DB.raw( * `CONCAT(firstName, ' - ', lastName) AS fullName` * // `CONCAT(?, ' - ', ?) AS fullName,['firstName', 'lastName']` * ) * ) * .findOne(); */ raw(sql: string, parameters?: (boolean | number | string | any[] | null)[] | Record): TRawStringQuery; /** * The 'raw' methid is used to allow for raw sql queries to some method in 'DB' or 'Model'. * Creates a raw SQL query with optional parameter bindings. * * @static * @param sql - The raw SQL string. Use `?` placeholders for parameters. * @param parameters - Values to bind to the placeholders. * @returns A raw SQL query object. * * @example * // Select a computed column using CONCAT * const users = await DB * .from('users') * .select( * 'id', * DB.raw( * `CONCAT(firstName, ' - ', lastName) AS fullName` * // `CONCAT(?, ' - ', ?) AS fullName,['firstName', 'lastName']` * ) * ) * .findOne(); */ static raw(sql: string, parameters?: (boolean | number | string | any[] | null)[] | Record): TRawStringQuery; /** * The 'freeze' methid is used to freeze the column without any pattern. * * @param {string} column * @returns {string} string */ freeze(column: string): TFreezeStringQuery; /** * The 'freeze' methid is used to freeze the column without any pattern. * * @static * @param {string} column * @returns {string} string */ static freeze(column: string): TFreezeStringQuery; /** * The 'getConnection' method is used to get a pool connection. * @param {Object} options options for connection database with credentials * @property {string} option.driver * @property {string} option.host * @property {number} option.port * @property {string} option.database * @property {string} option.username * @property {string} option.password * @returns {Connection} */ getConnection(options?: TConnectionOptions): TPoolConnected; /** * The 'getConnection' method is used to get a pool connection. * @param {Object} options options for connection database with credentials * @property {string} option.driver * @property {string} option.host * @property {number} option.port * @property {string} option.database * @property {string} option.username * @property {string} option.password * @returns {Connection} */ static getConnection(options?: TConnectionOptions): TPoolConnected; /** * The 'beginTransaction' is a method used to initiate a database transaction within your application's code. * * A database transaction is a way to group multiple database operations (such as inserts, updates, or deletes) into a single unit of work. * * Transactions are typically used when you want to ensure that a series of database operations either all succeed or all fail together, * ensuring data integrity. * * @param {TPoolConnected} pool * @returns {ConnectionTransaction} object - Connection for the transaction * @type {object} connection * @property {function} connection.query - execute query sql then release connection to pool * @property {function} connection.startTransaction - start transaction of query * @property {function} connection.commit - commit transaction of query * @property {function} connection.rollback - rollback transaction of query */ beginTransaction(pool?: TPoolConnected): Promise; /** * The 'beginTransaction' is a method used to initiate a database transaction within your application's code. * * A database transaction is a way to group multiple database operations (such as inserts, updates, or deletes) into a single unit of work. * * Transactions are typically used when you want to ensure that a series of database operations either all succeed or all fail together, * ensuring data integrity. * @static * * @param {TPoolConnected} pool * @returns {ConnectionTransaction} object - Connection for the transaction * @type {object} connection * @property {function} connection.query - execute query sql then release connection to pool * @property {function} connection.startTransaction - start transaction of query * @property {function} connection.commit - commit transaction of query * @property {function} connection.rollback - rollback transaction of query */ static beginTransaction(pool?: TPoolConnected): Promise; /** * The 'transaction' is a method used to Execute a database transaction. * * This method will: * - Acquire a connection from the pool * - Start a transaction * - Execute the provided handler * - Commit if successful * - Rollback if any error occurs * * @template T * @param {(conn: TConnection) => Promise} handler - Async function that receives the transaction connection. * All queries inside this handler MUST use the provided `conn` instance. * * @returns {Promise} The result returned from the handler. * */ transaction(handler: (conn: TConnection) => Promise, { pool }?: { pool?: TPoolConnected; }): Promise; /** * The 'transaction' is a method used to Execute a database transaction. * * This method will: * - Acquire a connection from the pool * - Start a transaction * - Execute the provided handler * - Commit if successful * - Rollback if any error occurs * * @static * @template T * @param {(conn: TConnection) => Promise} handler - Async function that receives the transaction connection. * All queries inside this handler MUST use the provided `conn` instance. * * @returns {Promise} The result returned from the handler. * */ static transaction(handler: (conn: TConnection) => Promise, { pool }?: { pool?: TPoolConnected; }): Promise; /** * The 'getActiveConnections' method is used to return active connections * * @returns {Promise} this */ getActiveConnections(): Promise; /** * The 'getActiveConnections' method is used to return active connections * * @static * @returns {Promise} this */ static getActiveConnections(): Promise; /** * The 'getMaxConnections' method is used to return max connections * * @returns {Promise} this */ getMaxConnections(): Promise; /** * The 'getMaxConnections' method is used to return max connections * * @static * @returns {Promise} this */ static getMaxConnections(): Promise; /** * This'clone' method is used to clone the current database into a new database * on the same server or another server. * * @property {string} database - Target database name. * @property {string[]} excludes - Tables to exclude from the clone. * @property {object} to - Target server connection. * @property {string} to.host - Target server host. * @property {number} to.port - Target server port. * @property {string} to.username - Target server username. * @property {string} to.password - Target server password. * @returns {Promise} */ clone({ database, excludes, only, value, to }: TClone): Promise; /** * This'clone' method is used to clone the current database into a new database * on the same server or another server. * * @static * @property {string} database - Target database name. * @property {string[]} excludes - Tables to exclude from the clone. * @property {object} to - Target server connection. * @property {string} to.host - Target server host. * @property {number} to.port - Target server port. * @property {string} to.username - Target server username. * @property {string} to.password - Target server password. * @returns {Promise} */ static clone({ database, only, excludes, value, to }: TClone): Promise; /** * * This 'dump' method is used to Dumps a database into a SQL file * @type {Object} opt * @property {string?} opt.database * @property {string} opt.filePath * @property {string[]} opt.excludes * @property {boolean?} opt.value * @returns {Promise} */ dump({ filePath, excludes, only, database, value }: TDump): Promise; /** * This 'dump' method is used to Dumps a database into a SQL file * * @static * @type {Object} opt * @property {string?} opt.database * @property {string} opt.filePath * @property {string[]} opt.excludes * @property {boolean?} opt.value * @returns {Promise} */ static dump({ filePath, database, excludes, only, value, }: TDump): Promise; /** * This 'session' method is used to manage database session variables. * * Provides methods to get and set session variables for the current * database connection. * * Supported drivers: * - MySQL * - MariaDB * - PostgreSQL * - SQLite * * MongoDB is not supported. * * @example * ```ts * await DB.session().set('db', 'primary'); * * const value = await db.session().get('db'); * console.log(value) // primary * * ``` */ session(): { get(name: string): Promise; set(name: string, value: string): Promise; delete(name: string): Promise; }; /** * This 'session' method is used to manage database session variables. * * Provides methods to get and set session variables for the current * database connection. * * Supported drivers: * - MySQL * - MariaDB * - PostgreSQL * - SQLite * * MongoDB is not supported. * * @static * * @example * ```ts * await DB.session().set('db', 'primary'); * * const value = await db.session().get('db'); * console.log(value) // primary * * ``` */ static session(): { get(name: string): Promise; set(name: string, value: string): Promise; delete(name: string): Promise; }; /** * This 'timestamp' method is used to convert a date into a timestamp. * * @type {string | Date} dateString * @returns {T.DateTime} */ timestamp(dateString?: string | Date): T.DateTime; /** * This 'timestamp' method is used to convert a date into a timestamp. * * @static * @type {string | Date} dateString * @returns {T.DateTime} */ static timestamp(dateString?: string | Date): T.DateTime; private _initialDB; } export { DB }; export default DB;