LoginSignup
6
4

More than 3 years have passed since last update.

TypeORMでIN句のWHERE検索がしたい

Last updated at Posted at 2019-06-30

表題通りで、TypeORMにて以下のようなIN句をどう指定するかという話です。

select * from posts where id in(1, 3, 5);

早速結論

getRepository(Post)
        .createQueryBuilder("posts")
        .where("posts.id IN (:...ids)", { ids: [1, 3, 5] })
        .getMany()
        .catch(e => console.error(e));

上記の通り、parse側でスプレッドさせるように書けば where メソッドの第2引数に直接配列渡せます。
展開された結果、 WHERE id IN($1, $2, $3) のように個別にプレースホルダを作ってくれる感じですね。

環境

  "typescript": "^3.4.3"
  "pg": "^7.11.0",
  "typeorm": "^0.2.17"
6
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
6
4