Skip to content

Commit b5d3f2b

Browse files
feat: mysql2 and typeorm test
1 parent c582bee commit b5d3f2b

File tree

13 files changed

+251
-0
lines changed

13 files changed

+251
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,7 @@
3838

3939
[Nest 项目如何构建 docker 镜像](./nest-dockerfile)
4040

41+
[通过 mysql2 操作 mysql](./mysql2-test)
42+
43+
[通过 typeorm 操作 mysql](./typeorm-test)
44+

mysql2-test/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# mysql2 test
2+
3+
npm install
4+
5+
node index.js

mysql2-test/index.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
const mysql = require('mysql2');
2+
3+
const connection = mysql.createConnection({
4+
host: 'localhost',
5+
port: 3306,
6+
user: 'root',
7+
password: 'guang',
8+
database: 'practice'
9+
});
10+
11+
// connection.query(
12+
// 'SELECT * FROM customers',
13+
// function(err, results, fields) {
14+
// console.log(results);
15+
// console.log(fields.map(item => item.name));
16+
// }
17+
// );
18+
19+
// connection.query(
20+
// 'SELECT * FROM customers WHERE name LIKE ?',
21+
// ['李%'],
22+
// function(err, results, fields) {
23+
// console.log(results);
24+
// console.log(fields.map(item => item.name));
25+
// }
26+
// );
27+
28+
// connection.execute('INSERT INTO customers (name) VALUES (?)',
29+
// ['光'],
30+
// (err, results, fields) => {
31+
// console.log(err);
32+
// });
33+
34+
// connection.execute('UPDATE customers SET name="guang" where name="光"',
35+
// (err) => {
36+
// console.log(err);
37+
// });
38+
39+
40+
// connection.execute('DELETE FROM customers where name=?',
41+
// ['guang'],
42+
// (err) => {
43+
// console.log(err);
44+
// }
45+
// );

mysql2-test/index2.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const mysql = require('mysql2/promise');
2+
3+
4+
(async function() {
5+
6+
const connection = await mysql.createConnection({
7+
host: 'localhost',
8+
port: 3306,
9+
user: 'root',
10+
password: 'guang',
11+
database: 'practice'
12+
});
13+
14+
const [results, fields] = await connection.query('SELECT * FROM customers');
15+
16+
console.log(results);
17+
console.log(fields.map(item => item.name));
18+
19+
})();
20+
21+
22+
23+
// connection.query(
24+
// 'SELECT * FROM customers WHERE name LIKE ?',
25+
// ['李%'],
26+
// function(err, results, fields) {
27+
// console.log(results);
28+
// console.log(fields.map(item => item.name));
29+
// }
30+
// );
31+
32+
// connection.execute('INSERT INTO customers (name) VALUES (?)',
33+
// ['光'],
34+
// (err, results, fields) => {
35+
// console.log(err);
36+
// });
37+
38+
// connection.execute('UPDATE customers SET name="guang" where name="光"',
39+
// (err) => {
40+
// console.log(err);
41+
// });
42+
43+
44+
// connection.execute('DELETE FROM customers where name=?',
45+
// ['guang'],
46+
// (err) => {
47+
// console.log(err);
48+
// }
49+
// );

mysql2-test/index3.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const mysql = require('mysql2/promise');
2+
3+
(async function() {
4+
const pool = mysql.createPool({
5+
host: 'localhost',
6+
user: 'root',
7+
password: 'guang',
8+
database: 'practice',
9+
waitForConnections: true,
10+
connectionLimit: 10,
11+
maxIdle: 10,
12+
idleTimeout: 60000,
13+
queueLimit: 0,
14+
enableKeepAlive: true,
15+
keepAliveInitialDelay: 0
16+
});
17+
18+
// const [results] = await pool.query('select * from customers');
19+
// console.log(results);
20+
21+
const connection = await pool.getConnection();
22+
23+
const [results] = await connection.query('select * from orders');
24+
console.log(results);
25+
})();

mysql2-test/package.json

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"name": "mysql2-test",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "index.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"keywords": [],
10+
"author": "",
11+
"license": "ISC",
12+
"dependencies": {
13+
"mysql2": "^3.3.3"
14+
}
15+
}

typeorm-mysql-test/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.idea/
2+
.vscode/
3+
node_modules/
4+
build/
5+
tmp/
6+
temp/

typeorm-mysql-test/README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# typeorm test
2+
3+
npm install
4+
5+
修改用户名密码和连接的数据库
6+
7+
npm run start

typeorm-mysql-test/package.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "typeorm-mysql-test",
3+
"version": "0.0.1",
4+
"description": "Awesome project developed with TypeORM.",
5+
"type": "commonjs",
6+
"devDependencies": {
7+
"@types/node": "^16.11.10",
8+
"ts-node": "10.7.0",
9+
"typescript": "4.5.2"
10+
},
11+
"dependencies": {
12+
"mysql": "^2.14.1",
13+
"mysql2": "^3.3.3",
14+
"reflect-metadata": "^0.1.13",
15+
"typeorm": "0.3.16"
16+
},
17+
"scripts": {
18+
"start": "ts-node src/index.ts",
19+
"typeorm": "typeorm-ts-node-commonjs"
20+
}
21+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import "reflect-metadata"
2+
import { DataSource } from "typeorm"
3+
import { User } from "./entity/User"
4+
5+
export const AppDataSource = new DataSource({
6+
type: "mysql",
7+
host: "localhost",
8+
port: 3306,
9+
username: "root",
10+
password: "guang",
11+
database: "practice",
12+
synchronize: true,
13+
logging: true,
14+
entities: [User],
15+
migrations: [],
16+
subscribers: [],
17+
connectorPackage: 'mysql2',
18+
extra: {
19+
authPlugin: 'sha256_password',
20+
}
21+
})

0 commit comments

Comments
 (0)