@ fastify/ misql (中文(简体) )
快速化 MySQL 连接插件; 您可以通过此程序在服务器的每个部分共享相同的 MySQL 连接池 .
盖下 mysql2 (英语) 已使用。 如果你没有使用 connectionString 选项 register 将传递给 MySQL 游泳池建造者.
重要内容: 所有 MySQL2 选项在使用时将被忽略 connectionString,如果想要将额外选项传递给 MySQL2 使用 uri 改为 connectionString。 。 。 .
安装
npm i @fastify/mysql
兼容性
| 插件版本 | 快速化版本 |
|---|---|
>=5.x | ^5.x |
^4.x | ^4.x |
>=2.x <4.x | ^3.x |
>=0.x <2.x | ^2.x |
>=0.x <2.x | ^1.x |
请注意,如果一个快取版本没有支持,那么该插件的相应版本也一样 上表列出. 见 快速化 LTS 政策 更多细节.
使用量
添加到您的项目 register 你们就完了!
此插件将添加 mysql 在您快速化实例中的命名空间,其属性如下:
pool: the pool instance
query: an utility to perform a query without a transaction
execute: an utility to perform a prepared statement without a transaction
getConnection: get a connection from the pool
format: an utility to generate SQL string
escape: an utility to escape query values
escapeId: an utility to escape query identifiers
示例
const fastify = require('fastify')()
fastify.register(require('@fastify/mysql'), {
connectionString: 'mysql://root@localhost/mysql'
})
fastify.get('/user/:id', (req, reply) => {
fastify.mysql.getConnection(onConnect)
function onConnect (err, client) {
if (err) return reply.send(err)
client.query(
'SELECT id, username, hash, salt FROM users WHERE id=?', [req.params.id],
function onResult (err, result) {
client.release()
reply.send(err || result)
}
)
}
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
使用 mysql.query
const fastify = require('fastify')()
fastify.register(require('@fastify/mysql'), {
connectionString: 'mysql://root@localhost/mysql'
})
fastify.get('/user/:id', (req, reply) => {
fastify.mysql.query(
'SELECT id, username, hash, salt FROM users WHERE id=?', [req.params.id],
function onResult (err, result) {
reply.send(err || result)
}
)
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
你可以看到,没有必要关闭客户端,因为它是内部完成的.
集合选项
默认情况下,此插件将创建一个 mysql2 组合。如果你不通过 connectionString,在插件中注册的每个 MySQL 选项都会转发到 mysql2.createPool()数字 :
const fastify = require('fastify')()
fastify.register(require('@fastify/mysql'), {
host: 'localhost',
user: 'root',
database: 'mysql',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
})
fastify.get('/users', (req, reply) => {
fastify.mysql.query(
'SELECT id, username FROM users',
function onResult (err, result) {
reply.send(err || result)
}
)
})
重要编号 :
mysql2使用时忽略集合选项connectionString。如果需要附加选项和连接字符串,请使用uri改为connectionString。 。 。 .
也可以使用相同的集合选项 promise: true;如果是这样的话,池是用 mysql2/promise数字 :
fastify.register(require('@fastify/mysql'), {
promise: true,
host: 'localhost',
user: 'root',
database: 'mysql',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0
})
在注册时支持 Async/await promise 选项 true数字 :
const fastify = require('fastify')()
fastify.register(require('@fastify/mysql'), {
promise: true,
connectionString: 'mysql://root@localhost/mysql'
})
fastify.get('/user/:id', async (req, reply) => {
const connection = await fastify.mysql.getConnection()
const [rows, fields] = await connection.query(
'SELECT id, username, hash, salt FROM users WHERE id=?', [req.params.id],
)
connection.release()
return rows[0]
})
fastify.listen({ port: 3000 }, err => {
if (err) throw err
console.log(`server listening on ${fastify.server.address().port}`)
})
命名实例范围
被命名的客户端由后人Fastify范围继承. 在孩子身上添加名字 范围创建包含继承客户端的本地注册;不添加 给父母或兄弟姐妹的名字。 独立的兄弟姐妹可以重用一个名字,但是 无法替换继承的名字 .
未名客户端可以先注册,再注册名客户端. 名称不得
与现有的客户端属性相撞, 例如 query, (中文(简体) ), pool,或 format。 。 。 .
__proto__, (中文(简体) ), constructor,以及 toString 支持和检查重复,
包括未命名的登记之后.
类型脚本
作为 mysql2 显示四种不同类型的客户端, 我们没有指定您的打字方式 。 您需要按照下面的例子指定类型 .
import { MySQLConnection, MySQLPool, MySQLPromiseConnection, MySQLPromisePool } from '@fastify/mysql'
// if you only pass connectionString
declare module 'fastify' {
interface FastifyInstance {
mysql: MySQLPool
}
}
// if you passed type = 'connection'
declare module 'fastify' {
interface FastifyInstance {
mysql: MySQLConnection
}
}
// if you passed promise = true
declare module 'fastify' {
interface FastifyInstance {
mysql: MySQLPromisePool
}
}
// if you passed promise = true, type = 'connection'
declare module 'fastify' {
interface FastifyInstance {
mysql: MySQLPromiseConnection
}
}
MySQLRow 数据包
使用 mysql2 添加返回数据类型的能力 RowData软件包。 。 。 .
const fastifyMysql, { MySQLRowDataPacket } from '@fastify/mysql'
const app = fastify();
app.register(fastifyMysql, {
connectionString: "mysql://root@localhost/mysql",
});
app.get("/", async () => {
const connection = app.mysql;
// SELECT
const [rows, fields] = await connection.query<MySQLRowDataPacket[]>(
"SELECT 1 + 1 AS `test`;",
);
/**
* @rows: [ { test: 2 } ]
*/
return rows[0];
});
MySQL ResultSet页眉
使用 mysql2 添加返回数据类型的能力 结果列表标题。 。 。 .
const fastifyMysql, { MySQLResultSetHeader } from '@fastify/mysql'
const app = fastify();
app.register(fastifyMysql, {
connectionString: "mysql://root@localhost/mysql",
});
app.get("/", async () => {
const connection = app.mysql;
const result = await connection.query<MySQLResultSetHeader>("SET @1 = 1");
/**
* @result: ResultSetHeader {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
info: '',
serverStatus: 2,
warningStatus: 0,
changedRows: 0
}
*/
return result
});
是 MySQL Pool 语句
检查是否固定装饰的方法, Mysql 是类型 MySQL Pool 游戏
const app = fastify();
app
.register(fastifyMysql, {
connectionString: "mysql://root@localhost/mysql",
})
.after(function (err) {
if (isMySQLPool(app.mysql)) {
const mysql = app.mysql
mysql.getConnection(function (err, con) {
con.release();
});
mysql.pool.end();
}
})
是 MySQLPromise Pool 语句
检查是否固定装饰的方法, Mysql 是类型 MySQLPromise 波尔语
app
.register(fastifyMysql, {
promise: true,
connectionString: "mysql://root@localhost/mysql",
})
.after(async function (err) {
if (isMySQLPromisePool(app.mysql)) {
const mysql = app.mysql
const con = await mysql.getConnection();
con.release();
mysql.pool.end();
}
});
是 MySQLC 连接
检查是否固定装饰的方法, Mysql 是类型 MySQLC 连接
app
.register(fastifyMysql, {
type: "connection",
connectionString: "mysql://root@localhost/mysql",
})
.after(async function (err) {
if (isMySQLConnection(app.mysql)) {
const mysql = app.mysql
mysql.connection.end();
}
});
是 MySQLPromise 连接
检查是否固定装饰的方法, Mysql 是类型 MySQLPromise 连接
app
.register(fastifyMysql, {
type: "connection",
promise: true,
connectionString: "mysql://root@localhost/mysql",
})
.after(async function (err) {
if (isMySQLPromiseConnection(app.mysql)) {
const mysql = app.mysql
mysql.connection.end();
}
});
承认
这个项目由下列各方慷慨赞助:
过去提案国:
- 莱兹
许可证
许可证根据 麻省理工学院。 。 。 .