1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Prisma where句 条件分岐

Last updated at Posted at 2023-03-26

TypeORMからPrismaに移行する際のメモ

TypeORMだと特定の条件の時だけwhereを使う場合はifを使っていた。

const qb = this.createQueryBuilder('user');
if (userType) qb.where('user.userType IN (:...userType)', { userType });

Prismaだとこれができないため3項演算子とスプレッド構文を使う。

prisma.user.findMany({
  where: {
    ...(userType ? { userType: { in: userType } } : {}),
  },
});
1
0
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?