promise-mysql is a Node.js library that provides a promise-based wrapper around the `mysqljs/mysql` driver. It leverages the Bluebird promise library to transform the traditional callback-based API of `node-mysql` into an asynchronous, promise-returning interface, supporting `async/await` patterns. Currently at version 5.2.0, this package has not seen a major release in over four years, with its core dependency, `mysqljs/mysql`, also being largely unmaintained. While it differentiates itself by offering promise support for the widely used `node-mysql` driver, modern applications might opt for `mysql2/promise` for native promise support and better performance and features. Its release cadence has been infrequent, indicating a mature but largely static project.
npm install promise-mysqlVerified import paths — ran on the pinned version, not inferred.
Demonstrates connecting to a MySQL database, performing DDL and DML operations (create table, insert, select, delete), and properly closing the connection using async/await with promise-mysql. Environment variables are used for credentials for security.
For new projects, evaluate `mysql2` which has built-in promise support and is actively maintained. If migrating an existing `promise-mysql` project, be aware of potential API differences with `mysql2`.
Ensure consistent promise usage across your codebase. If using `async/await`, the integration is usually seamless, but be mindful of Bluebird-specific promise methods if you delve into advanced promise patterns.
Ensure that calls to `mysql.createPool()` are `await`ed or chained with `.then()`, as they now return a Promise that resolves to the pool object. For example, `const pool = await mysql.createPool(options);`.
Always use a `try...finally` block when acquiring a connection from a pool to ensure `connection.release()` is called, regardless of whether operations succeed or fail. Example: `let connection; try { connection = await pool.getConnection(); /* ... */ } finally { if (connection) connection.release(); }`.Install the underlying MySQL driver: `npm install mysql` or `yarn add mysql`.
Ensure your MySQL server is stable and accessible. Check server logs for connection errors. For long-running applications, utilize connection pools (`mysql.createPool`) rather than individual connections (`mysql.createConnection`), as pools inherently handle connection re-establishment and rotation. If `reconnect` is explicitly set to `false`, consider enabling it.
Verify that `mysql.createConnection()` or `pool.getConnection()` were successfully `await`ed and resolved to a valid connection object. Ensure you are calling `query` on the object returned by these methods. Also, avoid mixing `promise-mysql` with the callback-based `mysqljs/mysql` API directly.