igorludi
igorludi

Reputation: 1581

How to run parametrized raw SQL in Kysely?

I know I can run a raw sql with:

import { sql } from 'kysely'

const id = 123
const snippet = sql<Person[]>`select * from person where id = ${id}`

but I want to make a function that will accept different WHERE clauses and params, something like:

function runQuery(sql: string, args: any): Person[] {
   ... // how to?
}

and then invoke it with different sql+args values:

runQuery('SELECT * from person WHERE id = ${id}', { id: 123})
runQuery('SELECT * from person WHERE name = ${id}', { name: 'John'})

... and be protected from the SQL injection etc.
Is this possible with Kysely?

Upvotes: 0

Views: 377

Answers (1)

Pierre-D Savard
Pierre-D Savard

Reputation: 551

finally I found the solution to call some stored proc from Kysely:

  let rep = await sql<SpValideLoginUsagerRep>`
        declare @retour int
        exec @retour = [dbo].[ValideLoginUsager] ${params.codeUsager}, ${params.passphrase}, ${params.password}
        select @retour as [valide]
    `.execute(ewGlobal);

Upvotes: 0

Related Questions