yarn add @shiwangme/mysql
此处给出的传统 Node.js 调用方式示例。同时,您可以在 Typescript 中使用。
const { pool } = require('@shiwangme/mysql');
(async(){
// 传入配置,新建一个Pool,如果已有Pool则直接拿来使用
const cn = await pool({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'test'
}); // 根据当前数据库配置创建一个连接
console.log(await cn.query('SELECT 1'));
})();
TS:
import { pool, PoolConfig } from '@shiwangme/mysql';
const config: PoolConfig = {
host: 'localhost',
user: 'root',
password: 'root'
};
const mysql = pool(config);
mysql.query('select 1').then((x: object) => {
console.log(x);
});
const { connection } = require('@shiwangme/mysql');
(async(){
// 传入配置,新建一个连接,,如果已有连接则直接拿来使用
const cn = await connection({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'test'
});
console.log(await cn.query('SELECT 1'));
})();
const { pool, format } = require('@shiwangme/mysql');
(async(){
const cn = await pool({
host: '127.0.0.1',
user: 'root',
password: 'root',
database: 'test'
});
console.log(await cn.query(format('SELECT 1')));
})();
Apache 2.0