【问题标题】:NestJS How I make query to views in an databaseNestJS 如何查询数据库中的视图
【发布时间】:2020-06-23 03:22:59
【问题描述】:

我有一个小问题,我的问题是如何查询数据库中的视图?我基于这个例子enter link description here

  • 数据库名称是bue(mysql)
  • 我的视图是 v1
  • 我用的是 TYPEORM

我的代码是一个实体:

import { ViewEntity, Connection } from 'typeorm';

@ViewEntity({
    expression: (connection: Connection) => connection.createQueryBuilder()
    .select('id')
    .from(V1, 'v1'), }) 

}
export class V1 {

}

我的错误记录在控制台中:

【问题讨论】:

    标签: database typescript nestjs typeorm


    【解决方案1】:

    您可以使用查看实体,如更新的文档中所述: https://typeorm.io/#/view-entities

    例如:

    @ViewEntity({ 
        expression: (connection: Connection) => connection.createQueryBuilder()
            .select("post.id", "id")
            .addSelect("post.name", "name")
            .addSelect("category.name", "categoryName")
            .from(Post, "post")
            .leftJoin(Category, "category", "category.id = post.categoryId")
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-15
      • 2022-01-26
      • 1970-01-01
      • 2011-10-31
      • 2019-07-31
      • 2013-09-26
      • 2021-09-23
      相关资源
      最近更新 更多