cool-admin框架后端使用-node版本,使用事务装饰器来创建和事务回滚

  • 这里以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,//这里一定要写,不然服务类重写update无效

})
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
    );
    //这里因为接收到的param不是实体类类型,而save方法入参要求实体类类型,所以这里转换一下
    const dzhSgChatAcount = new DzhSgChatAcountEntity()
    let obj = Object.assign(dzhSgChatAcount, param);
    await queryRunner.manager.save<DzhSgChatAcountEntity>(obj);
    
    return param.id;
  }
}  
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值