Skip to content

Common Table Expressions #1116

Closed
Closed

Description

@sylvainlap

Hello,

This is more a question than an issue, but how can I build a CTE (with) with TypeORM ? Is there a built in feature to do this, or should I use the raw sql query builder ?

Thanks.

Activity

  1. pleerock commented on Oct 30, 2017

    @pleerock
    Member

    Hi, no, there is no, you probably need to execute raw sql query using query method. But if orm would have such functionality, how do you see it there?

  2. pleerock commented on Nov 1, 2017

    @pleerock
    Member

    Sorry but I can't imagine how this can be integrated into typeorm's query builder semantics. TypeORM's QueryBuilder purpose is to select some model and return it. Can you create a feature proposal on how this can be implemented in our QueryBuilder and how it will work.

  3. pleerock commented on Nov 20, 2017

    @pleerock
    Member

    @sylvainlap did you have a chance to take a look on this issue deeply?

  4. sylvainlap commented on Nov 20, 2017

    @sylvainlap
    Author

    Actually, it was more a question than a feature request. I can close the issue.

  5. jtmthf commented on Nov 29, 2017

    @jtmthf

    I have a use case where a with CTE would be useful and I think it could work with the existing API. What I'm trying to do is get a list of items by ids and return the results in the order of the ids. The following raw query works with postgres 9.6+.

    with x (id_list) as (
      values (array[42,48,43])
    )
    select c.*
    from comments c, x
    where id = any (x.id_list)
    order by array_position(x.id_list, c.id);

    However I'd ideally want to avoid using raw queries due to the lack of type-safety and the difficulty in mapping the results back to entities. What I'd like to do is to use an API similar to below.

    const comments = await getRepository(Comment)
        .createQueryBuilder('comment')
        .with('x(id_list)', 'values(:ids)')
        .addFrom('x', 'x')
        .where('comment.id = any(x.id_list)')
        .orderBy('array_position(x.id_list, comment.id)')
        .setParameter('ids', [42, 48, 43])
        .getMany();

    Understandably this is also possible with the following simpler query builder expression:

    const ids = [42, 48, 43];
    const comments = await getRepository(Comment)
        .createQueryBuilder('comment')
        .whereInIds(ids)
        .orderBy('array_position(:ids, comment.id)')
        .setParameter({ids})
        .getMany()

    However I'd like not to send the array of ids twice especially if there is a large number of them. A possible API for with could be similar to Knex and be with(alias: string, query: ((qb: this) => string) | string): this. Some other examples from Knex would be written as:

    Knex

    knex.with('with_alias', knex.raw('select * from "books" where "author" = ?', 'Test')).select('*').from('with_alias')

    TypeORM

    const books = await getRepository(Book)
        .createQueryBuilder()
        .with('with_alias', 'select * from "books" where "author" = :author')
        .select()
        .from('with_alias')
        .setParameter('author', 'Test')
        .getMany();

    Knex

    knex.with('with_alias', (qb) => {
      qb.select('*').from('books').where('author', 'Test')
    }).select('*').from('with_alias') 

    TypeORM

    const books = await getRepository(Book)
        .createQueryBuilder()
        .with('with_alias', qb => qb.select().from('books').where('author', 'Test').getQuery())
        .select()
        .from('with_alias')
        .getMany();
  6. added this to the 0.3.0 milestone on Mar 29, 2018
  7. 12 remaining items

  8. dustin-rcg commented on Jan 30, 2021

    @dustin-rcg

    Can we have a querybuilder that takes at a top level, any WITHs and then an array of UNION or UNION ALL subqueries? That can be a SuperSelectQueryBuilder that has like with, addWith, union, addUnion or similar. Then redesign SelecQueryBuilder to take a scopeIndex parameter, such that the param names become (in mssql for example) @<scopeIndex>_<paramIndex>. Then we have a model that includes a way to combine multiple queries and their parameters as UNION or UNION ALL, plus the WITHs. Having SelectQueryBuilder not aware that its parameters need to be scoped is too limiting. Thanks.

    For that matter, scopes could be recursive, if necessary. So SelectQueryBuilder takes an array of scope indexes when it produces the actual SQL and parameters. This would be great, since it would mean that params in subqueries wouldn't have to be registered manually in the parent query. Just use nested scopes to produce the actual parameter names. Visitor pattern.

  9. dustin-rcg commented on May 19, 2021

    @dustin-rcg

    Here's an implementation I have working. Subclassing SelectQueryBuilder works fine it seems.

    import { Connection, QueryRunner, SelectQueryBuilder } from "typeorm";
    
    export class CteSelectQueryBuilder<T> extends SelectQueryBuilder<T> {
      protected commonTableExprs: Record<string, string> = {};
    
      constructor(connection: Connection, queryRunner?: QueryRunner) {
        super(connection, queryRunner);
      }
    
      addCommonTableExpr(
        name: string,
        expr: string | ((qb: SelectQueryBuilder<any>) => SelectQueryBuilder<any>)
      ) {
        if (typeof expr == "function") {
          const subQueryBuilder = expr(this.subQuery());
          this.setParameters(subQueryBuilder.getParameters());
          this.commonTableExprs[name] = subQueryBuilder.getQuery();
        } else {
          this.commonTableExprs[name] = expr;
        }
      }
    
      getQuery() {
        const ctes = Object.entries(this.commonTableExprs);
        if (ctes.length) {
          return [
            "WITH",
            ctes.map(([name, expr]) => `${name} AS ${expr}`).join(", "),
            super.getQuery(),
          ].join(" ");
        }
        return super.getQuery();
      }
    }
    
  10. added a commit that references this issue on Mar 23, 2022
    7cc1848
  11. timfrans commented on Apr 6, 2022

    @timfrans

    @Ginden
    Are there any plans to make this available for 0.2.x versions?

  12. Ginden commented on Apr 6, 2022

    @Ginden
    Contributor
  13. mopcweb commented on Apr 21, 2022

    @mopcweb

    @dustin-rcg thx so much for your approach.

    To make it work properly with native TypeORM's queryBuilder for all methods (which may call getQuery deeply nested) i found out that it is necessary to patch it. Also, in order to make commonTableExpressions be encapsulated separately for each specific query, it is necessary to patch QueryExpressionMap.

    Note Since [email protected] it is already implemented ability to use CTE, still for other versions this will be useful.

    So the solution which works for me for all native methods such as getManyAndCount etc:

    // patchTypeOrmQueryBuilderAddCommonTableExpressionsSupport.ts
    import { SelectQueryBuilder, UpdateQueryBuilder, DeleteQueryBuilder, InsertQueryBuilder, RelationQueryBuilder } from 'typeorm';
    import { QueryExpressionMap } from 'typeorm/query-builder/QueryExpressionMap';
    
    export function patchTypeOrmQueryBuilderAddCommonTableExpressionsSupport(
      queryBuilder: OptionableQueryBuilder = require('typeorm').SelectQueryBuilder,
    ): void {
      const queryExpressionMap: typeof QueryExpressionMap = require('typeorm/query-builder/QueryExpressionMap').QueryExpressionMap;
      const queryExpressionMapPrototype = queryExpressionMap.prototype;
      const originalClone = queryExpressionMapPrototype.clone;
    
      const qbPrototype = queryBuilder.prototype;
      const originalGetQuery = qbPrototype.getQuery;
    
      Object.defineProperty(queryExpressionMapPrototype, 'clone', {
        value: function clone() {
          const context = this as QueryExpressionMap; // For typization only
          const result = originalClone.call(context);
          result.commonTableExpressions = { ...context.commonTableExpressions };
          return result;
        },
      });
    
      Object.defineProperty(qbPrototype, 'getQuery', {
        value: function getQuery(): string {
          const context = this as SelectQueryBuilder<unknown>; // For typization only
          const ctes = Object.entries(context.expressionMap.commonTableExpressions ?? { });
    
          if (ctes.length && !context.expressionMap.subQuery) {
            return [
              'WITH',
              ctes.map(([name, expression]) => `${context.escape(name)} AS (${expression})`).join(', '),
              originalGetQuery.call(context),
            ].join(' ');
          }
    
          return originalGetQuery.call(context);
        },
      });
    
      Object.defineProperty(qbPrototype, 'addCommonTableExpression', {
        value: function addCommonTableExpression<T>(expression: CommonTableExpression<T>, name: string) {
          const context = this as SelectQueryBuilder<unknown>; // For typization only
          if (!context.expressionMap.commonTableExpressions) context.expressionMap.commonTableExpressions = { };
    
          if (typeof expression === 'function') {
            const subQueryBuilder = expression(context.subQuery());
            context.setParameters(subQueryBuilder.getParameters());
            context.expressionMap.commonTableExpressions[name] = subQueryBuilder.getQuery();
          } else {
            context.expressionMap.commonTableExpressions[name] = expression;
          }
    
          return context;
        },
      });
    }
    
    type OptionableQueryBuilder = typeof SelectQueryBuilder
    | typeof UpdateQueryBuilder
    | typeof DeleteQueryBuilder
    | typeof InsertQueryBuilder
    | typeof RelationQueryBuilder;
    
    type CommonTableExpression<T> = string | ((qb: SelectQueryBuilder<T>) => SelectQueryBuilder<T>);
    
    export interface IQueryBuilderWithCommonTableExpressions<Entity> {
      addCommonTableExpression<T = Entity>(expression: CommonTableExpression<T>, name: string): this;
      getQuery(): string;
    }
    
    declare module 'typeorm' {
      export interface SelectQueryBuilder<Entity> extends IQueryBuilderWithCommonTableExpressions<Entity> { }
    }
    
    declare module 'typeorm/query-builder/QueryExpressionMap' {
      export interface QueryExpressionMap {
        commonTableExpressions: Record<string, string>;
      }
    }

    Usage

    call patchTypeOrmQueryBuilderAddCommonTableExpressionsSupport some where at application startUp, to fully patch TypeORM's SelectQueryBuilder.

    patchTypeOrmQueryBuilderAddCommonTableExpressionsSupport(); // This will patch SelectQueryBuilder as default

    Use someWhere/at/some/repository.ts

    qb
      .addCommonTableExpression(`SELECT * FROM some_table`, 'some_cte')
      .addSelect(/* ... */)
  14. mopcweb commented on Apr 21, 2022

    @mopcweb

    @timfrans u could try to use solution above

  15. 67726e commented on Nov 11, 2022

    @67726e

    @mopcweb What version is this meant to work against? I've tried on 0.2.x and 0.3.x and in all cases it results in an error.

  16. ab958 commented on Sep 19, 2023

    @ab958

    any update on that?

  17. revskill10 commented on Feb 21, 2024

    @revskill10

    @Ginden How do we know if it supports a driver ?
    I'm using SQL server.

  18. Ginden commented on Feb 21, 2024

    @Ginden
    Contributor

    @revskill10 SQL Server is partially supported - https://github.com/typeorm/typeorm/blob/master/src/driver/sqlserver/SqlServerDriver.ts#L239

    I wasn't able to implement writable CTEs, because of issues with non-standard OUTPUT clause.

  19. added
    good first issue
    and removed on Apr 13, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions