对Cool-admin的多租户改造记录

cool-admin的多租户只提供了一种最基础的通过typeorm来实现的订阅器自动注入租户ID(tenantId)

/**
   * 查询时添加租户Key条件
   */
  afterSelectQueryBuilder(queryBuilder: SelectQueryBuilder<any>) {
    if (!this.tenant?.enable) return;

    const tenantKey = this.getTenantKey();
    // 忽略列表中的租户不过滤
    if (tenantKey && !this.isIgnoredTenant(tenantKey)) {
      const alias = queryBuilder.alias ? `${queryBuilder.alias}.` : '';
      queryBuilder.andWhere(`${alias}tenantKey = '${tenantKey}'`);
    }
  }

  /**
   * 插入时添加租户Key
   */
  afterInsertQueryBuilder(queryBuilder: InsertQueryBuilder<any>) {
    if (!this.tenant?.enable) return;

    const tenantKey = this.getTenantKey();
    // 忽略列表中的租户不自动填充
    if (tenantKey && !this.isIgnoredTenant(tenantKey)) {
      const values = queryBuilder.expressionMap.valuesSet;
      if (Array.isArray(values)) {
        queryBuilder.values(values.map(item => ({...item, tenantKey})));
      } else if (values && typeof values === 'object') {
        queryBuilder.values({...values, tenantKey});
      }
    }
  }

  /**
   * 更新时添加租户Key条件
   */
  afterUpdateQueryBuilder(queryBuilder: UpdateQueryBuilder<any>) {
    if (!this.tenant?.enable) return;

    const tenantKey = this.getTenantKey();
    // 忽略列表中的租户不过滤
    if (tenantKey && !this.isIgnoredTenant(tenantKey)) {
      queryBuilder.andWhere(`tenantKey = '${tenantKey}'`);
    }
  }

  /**
   * 删除时添加租户Key条件
   */
  afterDeleteQueryBuilder(queryBuilder: DeleteQueryBuilder<any>) {
    if (!this.tenant?.enable) return;

    const tenantKey = this.getTenantKey();
    // 忽略列表中的租户不过滤
    if (tenantKey && !this.isIgnoredTenant(tenantKey)) {
      queryBuilder.andWhere(`tenantKey = '${tenantKey}'`);
    }
  }

对于实际使用来说是完全不够的

在最小侵入的情况下对系统进行扩展,以admin租户为超管租户,开始向下创建新租户

对Cool-admin的多租户改造记录

项目

YANG-ICON支持远程CDN引入的icon图标工具

2025-12-19 10:55:38

开发

TypeOrm配合Redis 实现单体服务旁路缓存Cache Aside Pattern

2025-12-25 2:22:05

0 条回复 A文章作者 M管理员
    暂无讨论,说说你的看法吧
个人中心
购物车
优惠劵
有新私信 私信列表
搜索