mysql数据库操作(二)

express 中使用 TypeORM 连接 mysql 数据库

TypeORM 介绍

TypeORM 是一个采用 TypeScript 编写的用于 Node.js 的优秀 ORM 框架,支持使用 TypeScript 或 Javascript(ES5, ES6, ES7)开发。

express 中使用

安装

yarn add typeorm mysql

初始化
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import express, { Request, Response } from 'express'
import { createConnection } from 'typeorm'

export default createConnection(config.db)
.then(() => {
const app = express()
app.listen(port, () => {
info(
`the server is start at port ${port}, listening on http://localhost:${port}`
)
})
})
.catch((e) => {
console.error('connection mysql error: ', e.message)
})

使用

1
2
3
4
import { getRepository } from 'typeorm'
async function getList() {
const [list, total] = await getRepository(Entity).findAndCount()
}