Skip to content

Commit

Permalink
feat(sql.raw): Allow parameters in raw sql (kysely-org#512)
Browse files Browse the repository at this point in the history
* feat(sql.raw): Allow parameters in raw sql

* use pre-compiled query

* Apply suggestions from code review

Co-authored-by: Igal Klebanov <[email protected]>

---------

Co-authored-by: Igal Klebanov <[email protected]>
  • Loading branch information
2 people authored and Gaspero committed Oct 2, 2023
1 parent 1abac75 commit c19206d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/query-compiler/compiled-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ export interface CompiledQuery<O = unknown> {
}

export const CompiledQuery = freeze({
raw(sql: string): CompiledQuery {
raw(sql: string, parameters: unknown[] = []): CompiledQuery {
return freeze({
sql,
query: RawNode.createWithSql(sql),
parameters: freeze([]),
parameters: freeze(parameters),
})
},
})
14 changes: 13 additions & 1 deletion test/node/src/raw-sql.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { sql } from '../../../'
import { sql, CompiledQuery } from '../../../'

import {
DIALECTS,
Expand Down Expand Up @@ -268,6 +268,18 @@ for (const dialect of DIALECTS) {
})
}

if (dialect === 'postgres') {
it('CompiledQuery should support raw query with parameters', async () => {
const query = CompiledQuery.raw(
'select * from "person" where "public"."person"."first_name" between $1 and $2',
['A', 'B']
)
expect(query.sql).to.equal('select * from "person" where "public"."person"."first_name" between $1 and $2');
expect(query.parameters).to.deep.equal(['A', 'B']);
await ctx.db.executeQuery(query)
})
}

it('raw sql kitchen sink', async () => {
const result = await sql`insert into ${sql.table('toy')} (${sql.join([
sql.ref('name'),
Expand Down

0 comments on commit c19206d

Please sign in to comment.