$ cnpm install @goodware/mysql
npm i --save @goodware/mysql
All runtime dependencies in version 3 were changed to use peer dependencies.
If you're missing a dependency, you have three options:
npm i --save @goodware/mysql@2
Or, in package.json dependencies:
"@goodware/mysql": "^2.0.0"
npm i -g npm@7
The options provided by the constructor and all other methods accept an optional 'logger' function or object. If an object is provided, it must have the method log().
interface Logger {
/**
* @param tags Typically includes a logging level name, like info or debug.
* @param message An object with at least a message property
*/
log(tags: string[] | string, message: Record<string, unknown>): void;
}
The following program outputs 'success' to the console.
const mysql = require('@goodware/mysql');
const config = {
// host: '0.0.0.0', // This is the default
// port: 3306, // This is the default
// user: 'root', // This is the default
// database: 'mysql', // This is the default
password: 'password',
usePool: true, // Defaults to 10 connections (see connectionLimit in constructor options)
};
async () => {
const connector = new mysql(config, console.log); // The second parameter is a logger function
// Acquire a database connection
const result = await connector.execute( async (connection) => {
// Perform database operations
const [results] = await connection.query(`select 'success' AS status`);
// The Promise resolves to 'success'
return results[0].status;
});
// Close all database connections in the pool
await connector.stop();
// The Promise resolves to 'success'
return result;
}().then(console.info, console.error);
Copyright 2014 - 2017 © taobao.org |