Ashutosh
Ashutosh

Reputation: 4675

Typeorm: Execute raw query with parameters

I'm trying to execute raw query in typeorm with parameters I tried following queries:

insert into data(id, name, gender) values(?, ?,?)
insert into data(id, name, gender) values($1, $2, $3)
insert into data(id, name, gender) values(:id, :name, :gender)

The typeorm code is:

import { getManager } from 'typeorm';
await getManager().query(query, [1, 'test', 'male']);

What is wrong? Is there any other way?

Upvotes: 11

Views: 60972

Answers (3)

Jesus Monzon Legido
Jesus Monzon Legido

Reputation: 1313

as @ashutosh said, it depends on the driver of your database

For mysql/mysql2 you should use ? as placehoder. For example

manager.query('SELECT id FROM foos WHERE createdAt > ? AND id > ?', [new Date(), 3])

Upvotes: 3

hamza lakhal
hamza lakhal

Reputation: 51

For oracle, you can use something like this for me like this :

const querySingleValue = SELECT * FROM TABLE1 WHERE name in (:param) ;

string value :

getManager().query(querySingleValue,[param]) ;

Upvotes: 1

Ashutosh
Ashutosh

Reputation: 4675

Issue solved with this link. It depends on the underlying database which syntax to use.

https://github.com/typeorm/typeorm/issues/881

Upvotes: 9

Related Questions