表題通りで、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"