Typeorm plugin for Egg.js
Note: This plugin just for integrate Typeorm(used JavaScript) into Egg.js, more documentation please visit https://typeorm.io/.
$ npm i egg-typeorm-js --save
// {app_root}/config/plugin.js
exports.typeorm = {
enable: true,
package: 'egg-typeorm-js',
};
// {app_root}/app/entities/user.js
'use strict'
const EntitySchema = require("typeorm").EntitySchema
module.exports = new EntitySchema({
name: "User",
columns: {
id: {
primary: true,
type: "int",
generated: true
},
member_id: {
type: "int",
unique: true
},
name: {
type: "varchar"
}
}
})
// {app_root}/app/controller/member.js
'use stric'
async find() {
const { ctx } = this
const list = await ctx.repo.User.find() // entity会根据文件名加载到ctx.repo的属性上
ctx.body = list
}
1、单个链接配置
// {app_root}/config/config.default.js
exports.typeorm = {
client: {
"type": "mysql",
"host": "",
"port": 3306,
"username": "",
"password": '',
"database": "",
"synchronize": true,
"logging": false,
}
};
单个数据库配置时,entity的文件在{app_root}/app/entites下
2、多个连接配置
// {app_root}/config/config.default.js
exports.typeorm = {
clients: {
"db1": {
"name": "db1",
"type": "mysql",
"host": "",
"port": 3306,
"username": "",
"password": '',
"database": "",
"synchronize": true,
"logging": false,
"baseDir": "entities/db1"
},
"db2": {
"name": "db2",
"type": "mysql",
"host": "",
"port": 3306,
"username": "",
"password": '',
"database": "",
"synchronize": true,
"logging": false,
"baseDir": "entities/db2"
},
}
};
多个连接配置时,需要制定name和entities的路径,默认在{app_root}/app/entites下
Please open an issue here.
Please open an issue here.