Thuta Learning
ရှာဖွေရန်
IntermediateDevOps & Toolsintermediate

Data ဖျက်ခြင်း (Delete)

စိတ်လျှော့ပါ။ ဒီခန်းကို စာအုပ်လိုမဟုတ်ဘဲ စကားပြောသလိုပဲ၊ နားလည်လွယ်အောင် ရှင်းပါမယ်။

Data ဖျက်ခြင်း (Delete)

deleteDoc က document တစ်ခုကိုဖျက်ပေးပါတယ်။ Delete action သည် irreversible ဖြစ်နိုင်လို့ user confirmation ထည့်တာကောင်းပါတယ်။ “Delete” ခလုတ်ကို နှိပ်တာနဲ့ချက်ချင်းဖျက်တာထက် confirm dialog ထည့်ပါ။ လက်မှားရင် database က မျက်ရည်မကျပေမဲ့ user က ကျနိုင်ပါတယ်။

Code Example

javascript
import { getFirestore, doc, deleteDoc } from 'firebase/firestore';

const db = getFirestore();

async function deleteNote(noteId) {
  const isConfirmed = window.confirm('ဒီ note ကို ဖျက်မှာ သေချာလား?');
  if (!isConfirmed) return;

  await deleteDoc(doc(db, 'notes', noteId));
  console.log('Note deleted');
}

Subcollection သတိပြုရန်

Document တစ်ခုကိုဖျက်တာက အောက်က subcollection တွေကို အမြဲတမ်း auto ဖျက်ပေးတာမဟုတ်ပါ။ Nested data တွေရှိရင် delete strategy ကို သေချာရေးဆွဲပါ။

Best practice

လုပ်ငန်းသုံး app တွေမှာ hard delete အစား deletedAt field ထည့်ပြီး soft delete လုပ်တာလည်း အသုံးဝင်ပါတယ်။ နောက်မှ restore လုပ်နိုင်ပါတယ်။

Data ဖျက်ခြင်း (Delete) | Thuta Learning