二、Midway 增删改查的封装及工具类

阅读本文前,需要提前阅读前置内容:

一、Midway 增删改查
二、Midway 增删改查的封装及工具类
三、Midway 接口安全认证
四、Midway 集成 Swagger 以及支持JWT bearer
五、Midway 中环境变量的使用

样例源码
DEMO LIVE

问题

  • 大多数情况,所有实体类都有统一字段,需要抽取实体模型的基类;
  • 需要将Service的基本操作封装起来;
  • 需要将Controller的基本操作封装起来

抽取Entity基类

  • 创建目录common;
  • 创建基类src/common/BaseEntity.ts;
// src/common/BaseEntity.ts
import {
    Column, CreateDateColumn, PrimaryColumn, UpdateDateColumn } from 'typeorm';

export class BaseEntity {
   
  @PrimaryColumn({
    type: 'bigint' })
  id: number;

  @Column({
    type: 'bigint' })
  updaterId: number;

  @Column({
    type: 'bigint' })
  createrId: number;

  @CreateDateColumn()
  createTime: Date;

  @UpdateDateColumn()
  updateTime: Date;
}
  • 调整实体类src/entity/user.ts;

继承BaseEntity,并删除user.ts中的通用字段。

// src/entity/user.ts
import {
    EntityModel } from '@midwayjs/orm';
import {
    Column } from 'typeorm';
import {
    BaseEntity } from '../common/BaseEntity';

@EntityModel('user')
export class User extends BaseEntity {
   
  @Column({
    length: 100, nullable: true })
  avatarUrl: string;

  @Column({
    length: 20, unique: true })
  username: string;

  @Column({
    length: 200 })
  password: string;

  @Column({
    length: 20 })
  phoneNum: string;

  @Column()
  regtime: Date;

  @Column({
    type: 'int', default: 1 })
  status: number;
}

抽取Service基类

创建基类src/common/BaseService.ts;
// src/common/BaseService.ts
import {
    In, Repository } from 'typeorm';
import {
    BaseEntity } from './BaseEntity';
import {
    FindOptionsWhere } from 'typeorm/find-options/FindOptionsWhere';

export abstract class BaseService<T extends BaseEntity> {
   

  abstract getModel(): Repository<T>;

  async save(o: T) {
   
    if (!o.id) o.id = new Date().getTime();
    return this.getModel().save(o);
  }

  async delete(id: number) {
   
    return this.getModel().delete(id);
  }

  async findById(id: number): Promise<T> {
   
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    return this.getModel().findOneBy({
    id });
  }

  async findByIds(ids: number[]): Promise<T[]> {
   
    // eslint-disable-next-line @typescript-eslint/ban-ts-comment
    // @ts-ignore
    return this.getModel().findBy({
    id: In(ids) });
  }

  async findOne(where: FindOptionsWhere<T>): Promise<T> {
   
    return this.getModel().findOne({
    where });
  }

}
  • 基类定义为抽象类abstract,并添加抽象接口abstract getModel()
  • <T extends BaseEntity>泛型用法,定义TBaseEntity的子类;
调整src/service/user.service.ts;
import {
    Provide } from '@midwayjs/decorator';
import {
    User } from '../eneity/user';
import {
    InjectEntityModel } from '@midwayjs/orm';
import {
    Repository } from 'typeorm';
import {
    BaseService } from
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值