一. 配置多数据库源,多个mysql数据库,这里用的是typeorm
1.修改配置文件:src/config/config.local.ts
第一个数据库一定要用default, 第二数据库别名随意,我这里写test
import { CoolConfig } from '@cool-midway/core';
import { MidwayConfig } from '@midwayjs/core';
export default {
orm: {
default: {
type: 'mysql',
host: '127.0.0.1',
port: 3309,
username: 'root',
password: 'dzh123456',
database: 'dzh3094',
synchronize: true,
logging: false,
charset: 'utf8mb4',
},
test: {
type: 'mysql',
host: '127.0.0.1',
port: 3309,
username: 'root',
password: 'dzh123456',
database: 'cool',
synchronize: true,
logging: false,
charset: 'utf8mb4',
},
},
cool: {
initDB: true,
} as CoolConfig,
} as MidwayConfig;
2.添加实体类,我这里放在我自定义的模块member下,你可以放到你随意的模块, 我新增一个名称叫test的测试实体类。 路径:src/modules/member/entity/test.ts
@EntityModel注解, 第一个参数写第二个数据库的表名(配置文件test下设置 synchronize: true,所以会自动生成数据库表,若为 synchronize: false,则需手动建表) 第二个参数写第二个数据库的别名:test(对应配置文件里第二个数据库的别名)
import { EntityModel } from '@midwayjs/orm';
import { BaseEntity } from '@cool-midway/core';
import { Column } from 'typeorm';
@EntityModel('dzh_member_test',{
connectionName: 'test'
})
export class DzhMemberTestEntity extends BaseEntity {
@Column({ comment: '会员账号' })
username: string;
@Column({ comment: '会员密码',default:888888 })
password: string;
@Column({ comment: '会员昵称', nullable:true })
nickname: string;
@Column({ comment: '会员等级', default:1 })
role_id: number;
@Column({ comment: '备注', type:'text', nullable:true })
remark: string;
@Column({ comment: '状态 0:禁用 1:正常', default: 1, type: 'tinyint'})
status: number
}
3.新建控制器,app文件夹是应用端,admin文件夹是后台,这里我写在应用端控制器,方便浏览器打开接口测试, 因为写在admin端需要登录权限才能调式接口。路径:src/modules/member/controller/app/user.ts
新建一个test方法,添加Get注解
import { Body, Get, Inject, Post, Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from '@cool-midway/core';
import { DzhMemberUserEntity } from '../../entity/user';
import { DzhMemberUserService } from '../../service/app/user';
@Provide()
@CoolController({
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
entity: DzhMemberUserEntity,
})
export class DzhMemberAccountController extends BaseController {
@Inject()
dzhMemberUserService: DzhMemberUserService;
@Get('/test', { summary: '测试' })
async test() {
const testInfo = await this.dzhMemberUserService.test()
return this.ok(testInfo)
}
@Post('/login', { summary: '会员登录' })
async login(
@Body('userInfo') userInfo: { username: string; password: string }
) {
const res = await this.dzhMemberUserService.login(userInfo);
return this.ok(res);
}
}
4.在app文件夹下新增user服务类,路径:src/modules/member/service/app/user.ts @ InjectEntityModel 注解, 第一个参数写实体类,第二参数写第二个数据库的别名(对应配置文件里第二个数据库的别名)
import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { DzhMemberTestEntity } from '../../entity/test';
@Provide()
export class DzhMemberUserService extends BaseService {
@InjectEntityModel(DzhMemberTestEntity, 'test')
dzhMemberTestEntity:Repository<DzhMemberTestEntity>;
async test() {
return this.dzhMemberTestEntity.find()
}
}
测试效果,已经读取到第二个数据库的数据了
二. 配置多数据库源,mysql+MongoDB,这里用的是typeorm
修改配置文件:src/config/config.local.ts
第二个数据库下的type写上mongodb,默认端口是27017, 在这之前我已经用Navicat连接新建了一个cool-test数据库 运行后会连接上MongoDB并且自动建表
import { CoolConfig } from '@cool-midway/core';
import { MidwayConfig } from '@midwayjs/core';
export default {
orm: {
default: {
type: 'mysql',
host: '127.0.0.1',
port: 3309,
username: 'root',
password: 'dzh123456',
database: 'dzh3094',
synchronize: true,
logging: false,
charset: 'utf8mb4',
},
test: {
type: 'mongodb',
host: '127.0.0.1',
port: 27017,
database: 'cool-test',
logging: false,
charset: 'utf8mb4',
synchronize: true,
},
},
cool: {
initDB: true,
} as CoolConfig,
} as MidwayConfig;
控制器调用 路径:src/modules/member/controller/app/user.ts
import { Get, Inject, Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from '@cool-midway/core';
import { DzhMemberUserService } from '../../service/app/user';
@Provide()
@CoolController({
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
entity: DzhMemberUserEntity,
})
export class DzhMemberAccountController extends BaseController {
@Inject()
dzhMemberUserService: DzhMemberUserService;
@Get('/test', { summary: '测试' })
async test() {
const testInfo = await this.dzhMemberUserService.test()
return this.ok(testInfo)
}
}
服务类 路径:src/modules/member/service/app/user.ts
import { Inject, Provide } from '@midwayjs/decorator';
import { BaseService } from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/orm';
import { Repository } from 'typeorm';
import { DzhMemberTestEntity } from '../../entity/test';
@Provide()
export class DzhMemberUserService extends BaseService {
@InjectEntityModel(DzhMemberTestEntity, 'test')
dzhMemberTestEntity:Repository<DzhMemberTestEntity>;
async test() {
return this.dzhMemberTestEntity.create({
username:"我是MongoDB"
})
}
}
浏览器或者api测试工具测试接口:http://localhost:8001/app/member/user/test
已经写入到数据库了
|