From 30759e155231f16444877a1c0bc892d8d5a619fc Mon Sep 17 00:00:00 2001 From: Claudio Catterina Date: Fri, 19 Nov 2021 14:54:09 +0100 Subject: [PATCH] TypeORM adapter: pass rootAlias option to the interpreter Fix issue #26 for TypeORM adapter --- packages/sql/spec/typeorm.spec.ts | 4 ++-- packages/sql/src/lib/typeorm.ts | 12 ++++++++---- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/packages/sql/spec/typeorm.spec.ts b/packages/sql/spec/typeorm.spec.ts index f28a231..da70b87 100644 --- a/packages/sql/spec/typeorm.spec.ts +++ b/packages/sql/spec/typeorm.spec.ts @@ -32,7 +32,7 @@ describe('Condition interpreter for TypeORM', () => { expect(query.getQuery()).to.equal([ 'SELECT "u"."id" AS "u_id", "u"."name" AS "u_name"', 'FROM "user" "u"', - 'WHERE "name" = :0' + 'WHERE "u"."name" = :0' ].join(' ')) expect(query.getParameters()).to.eql({ 0: 'test' }) }) @@ -44,7 +44,7 @@ describe('Condition interpreter for TypeORM', () => { expect(query.getQuery()).to.equal([ 'SELECT "u"."id" AS "u_id", "u"."name" AS "u_name"', 'FROM "user" "u"', - 'WHERE "age" in(:0, :1, :2)' + 'WHERE "u"."age" in(:0, :1, :2)' ].join(' ')) expect(query.getParameters()).to.eql({ diff --git a/packages/sql/src/lib/typeorm.ts b/packages/sql/src/lib/typeorm.ts index b6d19b6..709dde0 100644 --- a/packages/sql/src/lib/typeorm.ts +++ b/packages/sql/src/lib/typeorm.ts @@ -4,7 +4,8 @@ import { createSqlInterpreter, allInterpreters, SqlOperator, - createDialects + createDialects, + SqlQueryOptions } from '../index'; function joinRelation(relation: string, query: SelectQueryBuilder) { @@ -48,12 +49,15 @@ export function createInterpreter(interpreters: Record> return (condition: Condition, query: SelectQueryBuilder) => { const dialect = query.connection.options.type as keyof typeof dialects; - const options = dialects[dialect]; - - if (!options) { + if (!dialects[dialect]) { throw new Error(`Unsupported database dialect: ${dialect}`); } + const options: SqlQueryOptions = { + rootAlias: query.alias, + ...dialects[dialect], + }; + const [sql, params] = interpretSQL(condition, options, query); return query.where(sql, params); };