123456789101112131415161718192021222324252627282930 |
- import { createPool } from 'mysql2/promise';
- const pool = createPool({
- host: '127.0.0.1',
- user: 'root',
- password: 'Jing@123',
- database: 'shopping',
- waitForConnections: true,
- connectionLimit: 10,
- maxIdle: 10, // max idle connections, the default value is the same as `connectionLimit`
- idleTimeout: 60000, // idle connections timeout, in milliseconds, the default value 60000
- queueLimit: 0,
- });
- export async function query(sql, values) {
- try {
- let [res] = await pool.query(sql, values);
- return res;
- } catch (error) {
- console.log('====', error);
- return Promise.reject(error);
- }
- }
- // 定义一个数据库的中间件:作用使得每个中间件函数的ctx对象都可以具有query方法
- export async function DBMiddleware(ctx, next) {
- ctx.app.context.execute = query;
- await next();
- }
- export default pool;
|