Skip to content

Commit

Permalink
fix (sql): use the right params placeholder for typeorm (#22)
Browse files Browse the repository at this point in the history
Fix #21

Co-authored-by: Claudio Catterina <[email protected]>
Co-authored-by: Sergii <[email protected]>
  • Loading branch information
3 people authored Jun 17, 2021
1 parent 80efa04 commit 3f1d337
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
14 changes: 11 additions & 3 deletions packages/sql/spec/typeorm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ 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" = ?'
'WHERE "name" = :0'
].join(' '))
expect(query.getParameters()).to.eql({ 0: 'test' })
})

it('properly binds parameters for "IN" operator', () => {
Expand All @@ -43,8 +44,14 @@ 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(?, ?, ?)'
'WHERE "age" in(:0, :1, :2)'
].join(' '))

expect(query.getParameters()).to.eql({
0: 1,
1: 2,
2: 3
})
})

it('automatically inner joins relation when condition is set on relation field', () => {
Expand All @@ -55,8 +62,9 @@ describe('Condition interpreter for TypeORM', () => {
'SELECT "u"."id" AS "u_id", "u"."name" AS "u_name"',
'FROM "user" "u"',
'INNER JOIN "project" "projects" ON "projects"."userId"="u"."id"',
'WHERE "projects"."name" = ?'
'WHERE "projects"."name" = :0'
].join(' '))
expect(query.getParameters()).to.eql({ 0: 'test' })
})
})

Expand Down
7 changes: 4 additions & 3 deletions packages/sql/src/lib/typeorm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import {
createSqlInterpreter,
allInterpreters,
SqlOperator,
createDialects,
mysql
createDialects
} from '../index';

function joinRelation<Entity>(relationName: string, query: SelectQueryBuilder<Entity>) {
Expand All @@ -20,9 +19,11 @@ function joinRelation<Entity>(relationName: string, query: SelectQueryBuilder<En
return false;
}

const typeormPlaceholder = (index: number) => `:${index - 1}`;

const dialects = createDialects({
joinRelation,
paramPlaceholder: mysql.paramPlaceholder,
paramPlaceholder: typeormPlaceholder,
});

// eslint-disable-next-line no-multi-assign
Expand Down

0 comments on commit 3f1d337

Please sign in to comment.