UNPKG

@megaorm/mysql

Version:

This package provides a simple, high-level, unified API for interacting with MySQL databases.

224 lines (223 loc) 6.95 kB
import { MegaDriver } from '@megaorm/driver'; import { MegaConnection } from '@megaorm/driver'; export interface SslOptions { /** * A string or buffer holding the PFX or PKCS12 encoded private key, certificate and CA certificates */ pfx?: string; /** * Either a string/buffer or list of strings/Buffers holding the PEM encoded private key(s) to use */ key?: string | string[] | Buffer | Buffer[]; /** * A string of passphrase for the private key or pfx */ passphrase?: string; /** * A string/buffer or list of strings/Buffers holding the PEM encoded certificate(s) */ cert?: string | string[] | Buffer | Buffer[]; /** * Either a string/Buffer or list of strings/Buffers of PEM encoded CA certificates to trust. */ ca?: string | string[] | Buffer | Buffer[]; /** * Either a string or list of strings of PEM encoded CRLs (Certificate Revocation List) */ crl?: string | string[]; /** * A string describing the ciphers to use or exclude */ ciphers?: string; /** * You can also connect to a MySQL server without properly providing the appropriate CA to trust. You should not do this. */ rejectUnauthorized?: boolean; /** * Configure the minimum supported version of SSL, the default is TLSv1.2. */ minVersion?: string; /** * Configure the maximum supported version of SSL, the default is TLSv1.3. */ maxVersion?: string; /** * You can verify the server name identity presented on the server certificate when connecting to a MySQL server. * You should enable this but it is disabled by default right now for backwards compatibility. */ verifyIdentity?: boolean; } export interface MySQLOptions { /** * The MySQL user to authenticate as */ user?: string; /** * The password of that MySQL user */ password?: string; /** * Alias for the MySQL user password. Makes a bit more sense in a multifactor authentication setup (see * "password2" and "password3") */ password1?: string; /** * 2nd factor authentication password. Mandatory when the authentication policy for the MySQL user account * requires an additional authentication method that needs a password. * https://dev.mysql.com/doc/refman/8.0/en/multifactor-authentication.html */ password2?: string; /** * 3rd factor authentication password. Mandatory when the authentication policy for the MySQL user account * requires two additional authentication methods and the last one needs a password. * https://dev.mysql.com/doc/refman/8.0/en/multifactor-authentication.html */ password3?: string; /** * Name of the database to use for this connection */ database?: string; /** * The charset for the connection. This is called 'collation' in the SQL-level of MySQL (like utf8_general_ci). * If a SQL-level charset is specified (like utf8mb4) then the default collation for that charset is used. * (Default: 'UTF8_GENERAL_CI') */ charset?: string; /** * The hostname of the database you are connecting to. (Default: localhost) */ host?: string; /** * The port number to connect to. (Default: 3306) */ port?: number; /** * The source IP address to use for TCP connection */ localAddress?: string; /** * The path to a unix domain socket to connect to. When used host and port are ignored */ socketPath?: string; /** * List of connection flags to use other than the default ones. It is also possible to blacklist default ones */ flags?: Array<string>; /** * object with ssl parameters or a string containing name of ssl profile */ ssl?: string | SslOptions; /** * Tells if you want to retrive BIGINT values as string */ bigNumberStrings?: boolean; } /** * MySQL driver responsible for creating MySQL connections. * @implements `MegaDriver` interface. * @example * * // Create a new MySQL driver * const driver = new MySQL({ * database: 'main', // Your db name * password: 'root', // Your db password, * user: 'root', // Your db user name * host: 'localhost', // Your db host * }); * * // Create connection * const connection = await driver.create(); * * // Execute your queries * const result = await connection.query(sql, values); * console.log(result); * * // Begin a transaction * await connection.beginTransaction(); * * // Commit transaction * await connection.commit(); * * // Rollback transaction * await connection.rollback(); * * // Close connection * await connection.close(); */ export declare class MySQL implements MegaDriver { /** * Unique identifier for the driver instance. */ id: Symbol; /** * MySQL driver configuration options. */ private options; /** * Constructs a MySQL driver with the given options. * @param options - Configuration options for the MySQL driver. * @example * * // Create a new MySQL driver * const driver = new MySQL({ * database: 'main', // Your db name * password: 'root', // Your db password, * user: 'root', // Your db user name * host: 'localhost', // Your db host * }); * * // Create connection * const connection = await driver.create(); * * // Execute your queries * const result = await connection.query(sql, values); * console.log(result); * * // Begin a transaction * await connection.beginTransaction(); * * // Commit transaction * await connection.commit(); * * // Rollback transaction * await connection.rollback(); * * // Close connection * await connection.close(); */ constructor(options: MySQLOptions); /** * Creates a new MySQL connection. * @returns A `Promise` that resolves with a new MySQL connection. * @throws `CreateConnectionError` If connection creation fails. * @example * * // Create a new MySQL driver * const driver = new MySQL({ * database: 'main', // Your db name * password: 'root', // Your db password, * user: 'root', // Your db user name * host: 'localhost', // Your db host * }); * * // Create connection * const connection = await driver.create(); * * // Execute your queries * const result = await connection.query(sql, values); * console.log(result); * * // Begin a transaction * await connection.beginTransaction(); * * // Commit transaction * await connection.commit(); * * // Rollback transaction * await connection.rollback(); * * // Close connection * await connection.close(); */ create(): Promise<MegaConnection>; }