Using MySQL2 with TypeScript
Installation
npm install --save mysql2
npm install --save-dev @types/node
The
@types/node
ensure the proper interaction between TypeScript and the Node.js modules used by MySQL2 (net, events, stream, tls, etc.).
信息
Requires TypeScript >=4.5.2
.
Usage
You can import MySQL2 in two ways:
- By setting the
esModuleInterop
option totrue
intsconfig.json
import mysql from 'mysql2';
import mysql from 'mysql2/promise';
- By setting the
esModuleInterop
option tofalse
intsconfig.json
import * as mysql from 'mysql2';
import * as mysql from 'mysql2/promise';
Connection
import mysql, { ConnectionOptions } from 'mysql2';
const access: ConnectionOptions = {
user: 'test',
database: 'test',
};
const conn = mysql.createConnection(access);
Pool Connection
import mysql, { PoolOptions } from 'mysql2';
const access: PoolOptions = {
user: 'test',
database: 'test',
};
const conn = mysql.createPool(access);
Query and Execute
A simple query
conn.query('SELECT 1 + 1 AS `test`;', (_err, rows) => {
/**
* @rows: [ { test: 2 } ]
*/
});
conn.execute('SELECT 1 + 1 AS `test`;', (_err, rows) => {
/**
* @rows: [ { test: 2 } ]
*/
});
The rows
output will be these possible types:
RowDataPacket[]
RowDataPacket[][]
ResultSetHeader
ResultSetHeader[]
ProcedureCallPacket
In this example, you need to manually check the output types
Type Specification
RowDataPacket[]
An array with the returned rows, for example:
import mysql, { RowDataPacket } from 'mysql2';
const conn = mysql.createConnection({
user: 'test',
database: 'test',
});
// SELECT
conn.query<RowDataPacket[]>('SELECT 1 + 1 AS `test`;', (_err, rows) => {
console.log(rows);
/**
* @rows: [ { test: 2 } ]
*/
});
// SHOW
conn.query<RowDataPacket[]>('SHOW TABLES FROM `test`;', (_err, rows) => {
console.log(rows);
/**
* @rows: [ { Tables_in_test: 'test' } ]
*/
});
Using rowsAsArray
option as true
:
import mysql, { RowDataPacket } from 'mysql2';
const conn = mysql.createConnection({
user: 'test',
database: 'test',
rowsAsArray: true,
});
// SELECT
conn.query<RowDataPacket[]>(
'SELECT 1 + 1 AS test, 2 + 2 AS test;',
(_err, rows) => {
console.log(rows);
/**
* @rows: [ [ 2, 4 ] ]
*/
}
);
// SHOW
conn.query<RowDataPacket[]>('SHOW TABLES FROM `test`;', (_err, rows) => {
console.log(rows);
/**
* @rows: [ [ 'test' ] ]
*/
});
RowDataPacket[][]
Using multipleStatements
option as true
with multiple queries:
import mysql, { RowDataPacket } from 'mysql2';
const conn = mysql.createConnection({
user: 'test',
database: 'test',
multipleStatements: true,
});
const sql = `
SELECT 1 + 1 AS test;
SELECT 2 + 2 AS test;
`;
conn.query<RowDataPacket[][]>(sql, (_err, rows) => {
console.log(rows);
/**
* @rows: [ [ { test: 2 } ], [ { test: 4 } ] ]
*/
});
ResultSetHeader
History
Version | Changes |
---|---|
v3.5.1 | Please use ResultSetHeader instead. Please use affectedRows instead. |
For INSERT
, UPDATE
, DELETE
, TRUNCATE
, etc.:
import mysql, { ResultSetHeader } from 'mysql2';
const conn = mysql.createConnection({
user: 'test',
database: 'test',
});
const sql = `
SET @1 = 1;
`;
conn.query<ResultSetHeader>(sql, (_err, result) => {
console.log(result);
/**
* @result: ResultSetHeader {
fieldCount: 0,
affectedRows: 0,
insertId: 0,
info: '',
serverStatus: 2,
warningStatus: 0,
changedRows: 0
}
*/
});