Skip to content

Commit

Permalink
TypeORM adapter: pass rootAlias option to the interpreter
Browse files Browse the repository at this point in the history
Fix issue stalniy#26 for TypeORM adapter
  • Loading branch information
Claudio Catterina authored and ccatterina committed Aug 26, 2022
1 parent b635779 commit 30759e1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
4 changes: 2 additions & 2 deletions packages/sql/spec/typeorm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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' })
})
Expand All @@ -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({
Expand Down
12 changes: 8 additions & 4 deletions packages/sql/src/lib/typeorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ import {
createSqlInterpreter,
allInterpreters,
SqlOperator,
createDialects
createDialects,
SqlQueryOptions
} from '../index';

function joinRelation<Entity>(relation: string, query: SelectQueryBuilder<Entity>) {
Expand Down Expand Up @@ -48,12 +49,15 @@ export function createInterpreter(interpreters: Record<string, SqlOperator<any>>

return <Entity>(condition: Condition, query: SelectQueryBuilder<Entity>) => {
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);
};
Expand Down

0 comments on commit 30759e1

Please sign in to comment.