egg-ts-typeorm
TypeORM plugin for Egg.js.
安装
$ npm install -S egg-ts-typeorm
使用
插件启用
// {app_root}/config/plugin.ts
配置ormconfig.yml
在egg项目根目录下新建ormconfig.yml文件;内容如下
default: # 默认连接 entitiesdir: "app/entities"
该文件表示数据库的实体文件存放的路径;相当于connection-options中entities配置项为['app/entity/**/*.{js,ts}']
配置config.{env}.ts
// {app_root}/config/config.default.tsconfig.typeorm =
多数据库连接配置
// {app_root}/config/config.default.tsconfig.typeorm =
ormconfig.yml如下:
model1: # config.default.ts中对应的name entitiesdir: "app/entity/db1" model2: # 同上 entitiesdir: "app/entity/db2"
创建实体
├── controller│ └── home.ts├── entity ├── Post.ts └── User.ts
实体文件
// app/entity/User.ts
使用查找
// in controller
所有实体会加载在ctx.entities中, 所有仓库会加载到ctx.repo; 多数据库时加载在对应的ctx.entities[connectName]与ctx.repo[connectionName]上; 详见typings/typeorm.d.ts文件
使用QueryBuilder
// in controller