0
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?

【TypeORM】論理削除の実行と論理削除した値の取得

0
Posted at

TypeORMで論理削除を実行する方法と、論理削除されたデータを取得する方法についてまとめました。

論理削除の実行

論理削除を行うには .softDelete() を使用します。

// idを指定して論理削除を実行
await userRepository.softDelete(1);

// 条件を指定して論理削除を実行
await userRepository.softDelete({ name: 'John' });


論理削除した値を取得する

デフォルトの挙動

find や findOne を使用してデータを取得する場合、特に何も指定しなければデフォルトで論理削除済みの値(データ)は自動的に除外されます。

// 論理削除されていないデータのみが取得される
const users = await userRepository.find();

論理削除済みのデータも含めて取得する

削除済みのデータも取得したい場合は、オプションに withDeleted: true を明示的に指定します。

// 論理削除済みのデータも含めて取得する
const allUsers = await userRepository.find({
  withDeleted: true,
});

// findOne の場合
const user = await userRepository.findOne({
  where: { id: 1 },
  withDeleted: true,
});

0
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
0
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?