- 这里以cool-admin官方封装到依赖@CoolTransaction来实现
- 演示为重写 控制器update这个CRUD
创建实体类DzhSgChatAcountEntity,路径:src/modules/sg/entity/chata_account.ts
import { EntityModel } from '@midwayjs/orm';
import { BaseEntity } from '@cool-midway/core';
import { Column } from 'typeorm';
@EntityModel('dzh_sg_chat_acount')
export class DzhSgChatAcountEntity extends BaseEntity {
@Column({ comment: '会员' })
uid: number;
@Column({ comment: '唯一值' })
unique: string
@Column({ comment: '备注', type:'text', nullable: true,})
remark: string
@Column({ comment: '状态 0:禁用 1:正常', default: 1, type: 'tinyint'})
status: number
}
创建第二个实体类DzhSgDomainEntity,路径:src/modules/sg/entity/domain.ts
import { EntityModel } from '@midwayjs/orm';
import { BaseEntity } from '@cool-midway/core';
import { Column } from 'typeorm';
@EntityModel('dzh_sg_domain')
export class DzhSgDomainEntity extends BaseEntity {
@Column({ comment: '会员' })
uid: number;
@Column({ comment: '域名', type: 'text' })
domain: string
@Column({ comment: '备注', type:'text', nullable: true })
remark: string
@Column({ comment: '状态 0:禁用 1:正常', default: 1, type: 'tinyint' })
status: number
}
控制器DzhSgChatAcountController,路径:src/modules/sg/controller/admin/chat_account.ts
- service: DzhSgChatAccountService,这里一定要写,不然服务类重写update无效
import { Provide } from '@midwayjs/decorator';
import { CoolController, BaseController } from '@cool-midway/core';
import { DzhSgChatAcountEntity } from '../../entity/chata_account';
import { DzhSgChatAccountService } from '../../service/admin/chat_account';
@Provide()
@CoolController({
api: ['add', 'delete', 'update', 'info', 'list', 'page'],
entity: DzhSgChatAcountEntity,
service: DzhSgChatAccountService,
})
export class DzhSgChatAcountController extends BaseController {}
服务类DzhSgChatAccountService,路径:src/modules/sg/service/admin/chat_account.ts
import { Inject, Provide } from '@midwayjs/decorator';
import {
BaseService,
CoolCommException,
CoolTransaction,
} from '@cool-midway/core';
import { InjectEntityModel } from '@midwayjs/orm';
import { QueryRunner, Repository } from 'typeorm';
import { DzhSgChatAcountEntity } from '../../entity/chata_account';
import { DzhSgDomainEntity } from '../../entity/domain';
@Provide()
export class DzhSgChatAccountService extends BaseService {
@CoolTransaction({ isolation: 'SERIALIZABLE' })
async update(param, queryRunner?: QueryRunner) {
const dzhSgDomain= new DzhSgDomainEntity();
dzhSgDomain.uid = "我是uid";
dzhSgDomain.domain = '我是domain';
await queryRunner.manager.insert<DzhSgDomainEntity>(
DzhSgDomainEntity,
dzhSgDomain
);
const dzhSgChatAcount = new DzhSgChatAcountEntity()
let obj = Object.assign(dzhSgChatAcount, param);
await queryRunner.manager.save<DzhSgChatAcountEntity>(obj);
return param.id;
}
}