ရိုးရိုးလေး ရှင်းပြမယ်
Delete ကို ရိုးရိုးလေး ပြောရရင်
Delete က ရှေ့သင်ခန်းစာ သုံးခုမှာ ဆောက်ခဲ့တဲ့ ဘဝလည်ပတ်မှုရဲ့ နောက်ဆုံးအဆင့်ပါ: create, read, update, delete။ DELETE /v1/users/u_3 ဆိုတာ "ဒီ user ကို ဖယ်ရှားပါ" လို့ ဆိုလိုတာပါ။ အောင်မြင်ရင် server က 204 No Content ပြန်ပါတယ်: အောင်မြင်တယ်၊ ပြန်ပို့စရာ ဘာမှ မရှိဘူး၊ ဘာလို့လဲဆိုတော့ ဖော်ပြစရာ resource က မရှိတော့လို့ပါ။ စိတ်ဝင်စားစရာ မေးခွန်းတွေက ဝန်းကျင်မှာ ရှိပါတယ်: user တစ်ယောက်တည်းကို နှစ်ကြိမ်ဖျက်ရင် ဘာဖြစ်မလဲ၊ ဖယ်ရှားလို့မရတဲ့ user ဆိုရင် ဘာဖြစ်မလဲ၊ "ဖယ်ရှားပြီး" ဆိုတာ data တကယ်ပျောက်သွားတာလား။
Problem → Why → Solution
ပြဿနာ: စည်းမျဉ်းမစစ်တဲ့ တစ်ကြောင်းတည်း delete
ပထမ delete endpoint က များသောအားဖြင့် တစ်ကြောင်းတည်းပါ: row ကို ဖျက်ပြီး { "success": true } ပြန်တာပါ။ အပေါက်သုံးခု ရှိပါတယ်။ မရှိတော့တဲ့ resource အတွက် body ပါနေလို့ client တွေ မှီခိုစရာ တသမတ်တည်း ပုံစံ မရှိပါဘူး။ Admin တစ်ယောက်က နောက်ဆုံး administrator ကို ဖျက်တာကို ဘာမှ မတားလို့ user စီမံနိုင်သူ တစ်ယောက်မှ မကျန်တော့တဲ့ system ဖြစ်သွားနိုင်ပါတယ်။ ပြီးတော့ client က timeout ပြီး retry လုပ်တဲ့အခါ မခန့်မှန်းနိုင်ပါဘူး: ဒုတိယ DELETE က မျှော်ထားတဲ့ row ကို ရှာမတွေ့တော့လို့ 500 နဲ့ crash ဖြစ်ပါတယ်။ နောက်ပိုင်း ဥပဒေဘက်က လူတစ်ယောက်ရဲ့ data ကို ဖျက်ခိုင်းတဲ့အခါ endpoint က flag ပဲ ပြောင်းထားတာကို team က တွေ့ရပါတယ်။
app.delete("/v1/users/:id", async (req, res) => {
await db.users.delete(req.params.id); // throws if the row is already gone
res.json({ success: true }); // 200 with a body, and no rules checked
});Delete က ပြန်ရဖို့ ခက်တဲ့ state ပြောင်းလဲမှုမို့ contract တိကျရမယ်
Delete က လူတွေ လွယ်လွယ် ပြန်မရနိုင်တဲ့ state ပြောင်းလဲမှုမို့ contract က တိကျရပါမယ်: အောင်မြင်ရင် status ဘာလဲ၊ ထပ်လုပ်ရင် ဘာအဓိပ္ပာယ်လဲ၊ ဘယ်စည်းမျဉ်းတွေ ပိတ်ဆို့နိုင်လဲ၊ သိမ်းထားတဲ့ data အတွက် "deleted" ဆိုတာ ဘာလဲ။ Timeout ပြီး client က retry လုပ်တာက ပုံမှန်ပါ၊ ဒုတိယအကြိမ်ကို ကျရှုံးတဲ့ endpoint က အန္တရာယ်မရှိတဲ့ retry ကို အဖြစ်အပျက်ကြီး ဖြစ်စေပါတယ်။
Core concept: 204, idempotency နဲ့ ဖယ်ရှားတာကို ပိတ်တဲ့ စည်းမျဉ်းများ
DELETE /v1/users/{id} က resource ကို ဖယ်ရှားဖို့ server ကို တောင်းတာပါ။ အောင်မြင်ရင် 204 No Content ပါ: body မပါဘူး၊ client ကလည်း body ကို parse လုပ်ဖို့ မကြိုးစားရပါဘူး (204 မှာ res.json() ခေါ်ရင် SyntaxError: Unexpected end of JSON input ရပါတယ်)။ API တချို့က 200 နဲ့ body တိုတို သို့ ဖျက်လိုက်တဲ့ resource ကို ပြန်ပါတယ်၊ နှစ်မျိုးလုံး ခွင့်ပြုပေမဲ့ 204 က အသုံးများပြီး ဒီအခန်းမှာ သုံးတာပါ။
DELETE က idempotent ပါ: HTTP က ဒါကို "တူညီတဲ့ request အကြိမ်ကြိမ်ရဲ့ ရည်ရွယ်ထားတဲ့ ထိရောက်မှုက တစ်ကြိမ်နဲ့ တူတယ်" လို့ သတ်မှတ်ပြီး ဒီမှာ ရည်ရွယ်ချက်က user မရှိတော့တာပါ။ Response တူရမယ်လို့ မပြောပါဘူး။ ပထမ call က 204 ပြန်ပြီးနောက် ဒုတိယ call က ဘာမှ မတွေ့ပါဘူး။ API တွေ ကွာပါတယ်: တချို့က 404 ပြန်ပြီး တချို့က အကြိမ်တိုင်း 204 ပြန်ပါတယ်။ ဒီ API က 404 user_not_found ပြန်ပါတယ်၊ ဘာလို့လဲဆိုတော့ အမှန်တရားကို client ကို ပြောပြတာမို့ပါ: ဒီတစ်ကြိမ် ဖျက်စရာ မရှိခဲ့ဘူး။ ပေးရတဲ့ တန်ဖိုးက timeout ပြီး retry လုပ်တဲ့ client က ပထမ ကြိုးစားမှု အောင်မြင်ခဲ့လား၊ user မရှိခဲ့တာလား ခွဲမသိနိုင်တာပါ၊ cleanup အတွက် စည်းမျဉ်းက ရိုးရှင်းပါတယ်: 404 ကို "ဖယ်ရှားပြီးသား" လို့ သဘောထားပြီး ဆက်သွားပါ။ စည်းမျဉ်းတစ်ခု ရွေးပြီး documentation ရေးကာ resource အားလုံးမှာ တူအောင်ထားပါ။ တခြား API တွေလည်း ဒီလိုပဲလို့ မယူဆပါနဲ့။
Business rule တွေက delete ကို ပိတ်ဆို့နိုင်ပါတယ်။ User က နောက်ဆုံး administrator ဖြစ်နေရင်၊ ဒါမှမဟုတ် ဖွင့်ထားတဲ့ order တွေ ပိုင်ရှိနေရင် server က ပုံမှန် error body (code က last_admin) နဲ့ 409 Conflict ပြန်ပြီး ဘာမှ မဖျက်ပါဘူး။ Authorization က read မှာလိုပဲ အရင်လာပါတယ်: ဒီ user ကို ဖျက်ခွင့်မရှိတဲ့ caller က policy အလိုက် 404 (ရှိမရှိ ဖုံးကွယ်) ဒါမှမဟုတ် 403 ရပါမယ်၊ ဒီစစ်ဆေးမှုကို id တိုင်းအတွက် လုပ်ပါ။
| ရွေးချယ်မှု | Server လုပ်တာ | အကျိုးအပြစ် |
|---|---|---|
| Hard delete | Row ကို ဖယ်ရှား | ရိုးရှင်း၊ email ပြန်သုံးလို့ရ၊ ပြန်မရ |
| Soft delete | Flag ထိုးပြီး ဖုံးထား | ပြန်ယူလို့ရ၊ data ကျန်နေသေး |
| Anonymise / purge | ကိုယ်ရေးကိုယ်တာ field တွေ ဖျက် သို့ ပြင် | Erasure ကို ဖြည့်ဆည်း၊ အသေးစိတ် ဆုံးရှုံး |
| Async (202) | အလုပ်ကို တန်းစီပြီး status URL ပေး | ကြာတဲ့ cascade အတွက်၊ client poll လုပ်ရ |
Hard delete က row ကို ဖယ်ပါတယ်: ရိုးရှင်းပြီး email လိပ်စာကို ပြန်သုံးလို့ရပေမဲ့ အပြီးအပိုင် ပျောက်သွားပါတယ်၊ ဆက်စပ် record တွေ (order, comment) ကိုလည်း ဖျက်၊ အမည်ဝှက် သို့ ပိတ်ဆို့ရပါမယ်။ Soft delete က record ကို မှတ်သားပြီး read တွေကနေ ဖုံးထားပါတယ် (ဥပမာ deleted flag): GET က 404 ပြန်၊ list က ကျော်၊ admin က ပြန်ယူလို့ရပါတယ်။ အမှားတွေနဲ့ audit trail အတွက် အကူအညီဖြစ်ပေမဲ့ erasure မဟုတ်ပါဘူး: ကိုယ်ရေးကိုယ်တာ data က ကျန်ရှိနေသေးလို့ ဥပဒေ ဒါမှမဟုတ် user က erasure တောင်းရင် တကယ် purge သို့ anonymise လုပ်ရပါမယ်၊ backup တွေကိုလည်း အချိန်ဇယားနဲ့ပေါ့။ အလုပ်က အချိန်ကြာရင် (cascade, search index, backup) server က 204 အစား status URL ပါတဲ့ 202 Accepted ပြန်နိုင်ပြီး client က poll လုပ်ပါတယ်။
- User ကို ရှာ (မရှိရင် 404)
- စည်းမျဉ်းစစ်: နောက်ဆုံး admin က 409
- Record ကို ဖယ် သို့ flag ထိုး
- Body မပါတဲ့ 204 ပြန်
User မရှိတော့တာနဲ့ delete အောင်မြင်တာမို့ တူညီတဲ့ request ထပ်လာရင် ဘာမှမတွေ့ပါဘူး၊ ဒါကြောင့် 404 က ရိုးသားတဲ့ အဖြေပါ။
အထွေထွေ ပုံစံ
DELETE /v1/<collection>/<id>
204 No Content (deleted; there is no body)
404 Not Found { "error": { ... } } (nothing to delete)
409 Conflict { "error": { ... } } (a rule blocks the delete)
202 Accepted Location: <status url> (deleted later; poll the URL)Request မှာ body မလိုပါဘူး။ ရလဒ်ကို status ကပဲ ပြောပါတယ်: 204 ဖယ်ပြီး၊ 404 ဖယ်စရာမရှိ၊ 409 စည်းမျဉ်းက ပိတ်ထား၊ 202 နောက်မှ ဖယ်မယ်။
Worked example: 204၊ ထပ်ဖျက်တာနဲ့ နောက်ဆုံး admin
ဒီ sample က random local port တစ်ခုမှာ users API ကို run ပါတယ်၊ Alice (admin), Bob နဲ့ Carol ပါပါတယ်။ ပထမတစ်ခုက Carol ကို DELETE လုပ်ပြီး 204 နဲ့ body အလွတ်ကို မြင်ရတာပါ၊ ပြီးတော့ GET က 404 ပေးပြီး ဒုတိယ DELETE က 404 user_not_found ပေးပါတယ်။ ပြီးရင် deleteUser() helper က 204 ကို "deleted"၊ 404 ကို "already gone" လို့ ပြောင်းပါတယ်၊ နောက်ဆုံး admin ဖြစ်တဲ့ Alice ကိုတော့ 409 last_admin နဲ့ ငြင်းပါတယ်။
import http from "node:http";
// The users API (stands in for https://api.example.com/v1/users)
const row = (n, name, role) => ({
id: "u_" + n,
name,
email: name.toLowerCase() + "@example.com",
role,
bio: null,
version: 1,
});
const users = new Map([
["u_1", row(1, "Alice", "admin")],
["u_2", row(2, "Bob", "member")],
["u_3", row(3, "Carol", "member")],
]);
const publicView = ({ id, name, email, role, bio }) => ({ id, name, email, role, bio });
const send = (res, status, body, headers = {}) => {
if (body === undefined) return res.writeHead(status, headers).end(); // 204: no body
res.writeHead(status, { "Content-Type": "application/json", ...headers });
res.end(JSON.stringify(body));
};
const fail = (res, status, code, message, headers) =>
send(res, status, { error: { code, message } }, headers);
const server = http.createServer((req, res) => {
const [, , resource, id] = new URL(req.url, "http://localhost").pathname.split("/");
if (resource !== "users" || !id) return fail(res, 404, "not_found", "No such route");
const user = users.get(id);
if (req.method === "GET") {
if (!user) return fail(res, 404, "user_not_found", "No user " + id);
return send(res, 200, publicView(user));
}
if (req.method !== "DELETE") {
const allow = { Allow: "GET, DELETE" };
return fail(res, 405, "method_not_allowed", "Use GET or DELETE", allow);
}
if (!user) return fail(res, 404, "user_not_found", "No user " + id);
const admins = [...users.values()].filter((u) => u.role === "admin");
if (user.role === "admin" && admins.length === 1) {
return fail(res, 409, "last_admin", "The last admin cannot be deleted");
}
users.delete(id);
send(res, 204); // success, and nothing to send back
});
await new Promise((resolve) => server.listen(0, "127.0.0.1", resolve));
const base = "http://127.0.0.1:" + server.address().port;
// The client
async function deleteUser(id) {
const res = await fetch(base + "/v1/users/" + id, { method: "DELETE" });
if (res.status === 204) return "deleted";
if (res.status === 404) return "already gone"; // the goal (user absent) is met
const { error } = await res.json();
throw new Error(res.status + " " + error.code);
}
const first = await fetch(base + "/v1/users/u_3", { method: "DELETE" });
const emptyBody = JSON.stringify(await first.text());
console.log("DELETE u_3:", first.status, "ok:", first.ok, "body:", emptyBody);
const check = await fetch(base + "/v1/users/u_3");
console.log("GET u_3:", check.status);
const second = await fetch(base + "/v1/users/u_3", { method: "DELETE" });
console.log("DELETE u_3 again:", second.status, (await second.json()).error.code);
console.log("deleteUser(u_3):", await deleteUser("u_3"));
console.log("deleteUser(u_2):", await deleteUser("u_2"));
try {
await deleteUser("u_1");
} catch (err) {
console.log("deleteUser(u_1) failed:", err.message);
}
server.close();Code ကို တစ်ပိုင်းချင်း ရှင်းပြမယ်
- 204 has no body
if (body === undefined) return res.writeHead(status, headers).end(); // 204: no bodyBody မပါရင် Content-Type မပါဘဲ response ကို ချက်ချင်းပိတ်ပါတယ်။ 204 မှာ body ပို့ခွင့်မရှိလို့ client ဘက် res.text() က "" ပဲ ပြန်ပါတယ်။
- 404 for what is not there
if (!user) return fail(res, 404, "user_not_found", "No user " + id);ဖျက်ပြီးသား user ကို ထပ်ဖျက်ရင် ဒီစာကြောင်း အလုပ်လုပ်ပါတယ်။ GET မှာလည်း ဒီ error ပဲ ပြန်ပါတယ်၊ ဒါကြောင့် ဖျက်ပြီးနောက် GET က 404 ဖြစ်တာနဲ့ ကိုက်ညီပါတယ်။
- A rule blocks the delete
return fail(res, 409, "last_admin", "The last admin cannot be deleted");User က admin ဖြစ်ပြီး admin က တစ်ယောက်ပဲ ရှိနေတဲ့အခါ 409 ပါ။ တန်ဖိုးတွေ မှန်ပေမဲ့ လက်ရှိ state နဲ့ ရိုက်ခတ်နေလို့ create မှာလိုပဲ 409 သုံးတာပါ။
- Remove, then answer 204
users.delete(id);စည်းမျဉ်းအားလုံးကို ကျော်မှသာ Map ထဲက ဖယ်ပါတယ်။ ဒါကြောင့် 404 နဲ့ 409 လမ်းကြောင်းတွေက ဘာမှ မပြောင်းလဲစေပါဘူး။
- Do not parse a 204
if (res.status === 204) return "deleted";Client က status ကို အရင်စစ်ပြီး 204 မှာ res.json() ကို မခေါ်ပါဘူး၊ ခေါ်ရင် SyntaxError ပါ။ error body ကို ဖတ်တာ 204 နဲ့ 404 မဟုတ်တဲ့ အခြေအနေမှာပဲ ဖြစ်ပါတယ်။
- 404 as already gone
if (res.status === 404) return "already gone"; // the goal (user absent) is metCleanup ရဲ့ ရည်ရွယ်ချက်က user မရှိတော့တာမို့ 404 က ရည်ရွယ်ချက်ပြည့်မီပြီးသားပါ။ Retry လုပ်တဲ့ client က ဒီနည်းနဲ့ ပထမ ကြိုးစားမှု အောင်မြင်ခဲ့ရင်တောင် မှားမသွားပါဘူး။
DELETE u_3: 204 ok: true body: ""
GET u_3: 404
DELETE u_3 again: 404 user_not_found
deleteUser(u_3): already gone
deleteUser(u_2): deleted
deleteUser(u_1) failed: 409 last_admin- id နဲ့ user ကို ရှာပါ
- ဖျက်ခွင့်ရှိမရှိ စစ်ပါ
- စည်းမျဉ်းတွေ (နောက်ဆုံး admin) ကို စစ်ပါ
- ဖယ်ပြီး 204 ပြန်ပါ
User မရှိ
404 user_not_found and nothing changes
စည်းမျဉ်းက ပိတ်ထား
409 last_admin and nothing is deleted
အားလုံး ကျော်
204 No Content with an empty body
Soft delete, restore နဲ့ purge
အောက်က ပုံစံက soft delete ရဲ့ အနှစ်ကို ပြပါတယ်။ softDelete က record ကို ဖျက်မပစ်ဘဲ flag ထိုးပါတယ်၊ ဒါကြောင့် read တွေက မမြင်ရပေမဲ့ array ထဲမှာ ကျန်နေပြီး restore နဲ့ ပြန်ယူလို့ရပါတယ်။ purge ကသာ တကယ်ဖယ်တာပါ။ သတိထားစရာ တစ်ခုက email unique စည်းမျဉ်းပါ: soft-delete လုပ်ထားတဲ့ user ရဲ့ email က ကျန်နေသေးလို့ လူတစ်ယောက်က အဲဒီ email နဲ့ ပြန်စာရင်းသွင်းရင် 409 ဖြစ်နိုင်ပါတယ်၊ ဒါကြောင့် deleted ပြီးသားကို ဘယ်လိုလုပ်မလဲ (email ကို ပြန်လွှတ်၊ ဒါမှမဟုတ် restore ကို အသိပေး) ဆုံးဖြတ်ထားရပါမယ်။
const users = [
{ id: "u_1", name: "Alice", email: "alice@example.com", deleted: false },
{ id: "u_2", name: "Bob", email: "bob@example.com", deleted: false },
];
// Reads only see users that are not flagged
const find = (id) => users.find((u) => u.id === id && !u.deleted);
function softDelete(id) {
const user = find(id);
if (!user) return 404;
user.deleted = true; // still stored, just hidden
return 204;
}
function restore(id) {
const user = users.find((u) => u.id === id && u.deleted);
if (!user) return 404;
user.deleted = false;
return 200;
}
function purge(id) {
const index = users.findIndex((u) => u.id === id);
if (index === -1) return 404;
users.splice(index, 1); // really gone
return 204;
}
console.log(softDelete("u_2")); // 204
console.log(find("u_2") ?? "hidden"); // hidden: GET would answer 404
console.log(users.length); // 2: the row is still stored
console.log(restore("u_2")); // 200
console.log(find("u_2").name); // Bob
console.log(purge("u_2"), users.length); // 204 1ကြာတဲ့ delete နဲ့ 202 pattern
User တစ်ယောက်ကို ဖျက်ရာမှာ order တွေ၊ search index နဲ့ backup တွေကိုပါ လိုက်ရှင်းရတဲ့အခါ request တစ်ခုအတွင်း မပြီးနိုင်ပါဘူး။ ဒီအခါ server က 202 Accepted နဲ့ အလုပ်ရဲ့ status ကို ကြည့်လို့ရတဲ့ URL ကို Location မှာ ပေးပါတယ်၊ client က အဲဒီ URL ကို poll လုပ်ပါတယ်။ 204 နဲ့ မတူတာက 202 က "လက်ခံထားပြီ၊ မပြီးသေးဘူး" ဆိုတဲ့ အဓိပ္ပာယ်ပါ၊ ဒါကြောင့် client က user မရှိတော့ပြီလို့ ချက်ချင်း မယူဆရပါဘူး။ ဥပမာ operations URL က illustrative ပါ၊ တကယ့် API ရဲ့ docs ကို လိုက်ပါ။
DELETE /v1/users/u_3 HTTP/1.1
Host: api.example.com
HTTP/1.1 202 Accepted
Location: /v1/operations/op_17
Content-Type: application/json
{ "status": "pending" }curl -i -X DELETE https://api.example.com/v1/users/u_3
# HTTP/1.1 204 No Contentလက်တွေ့ project တွေမှာ
လက်တွေ့ project တွေမှာ delete endpoint
Provider တွေက ဒီအချက်တွေကို မတူကြပါဘူး။ GitHub REST API က DELETE အများစုကို 204 နဲ့ ဖြေပြီး Stripe က deleted: true ပါတဲ့ object သေးသေးလေးနဲ့ 200 ပြန်ပါတယ်။ နှစ်မျိုးလုံး မှန်ပါတယ်၊ docs ကို လိုက်နာပါ။ SaaS app အများစုက account ဖျက်တာကို soft delete လုပ်ပြီး ရက်သတ်မှတ်ချိန် (ဥပမာ ရက် ၃၀) ပြီးမှ purge လုပ်ပါတယ်၊ အဲဒီကြားမှာ user က စိတ်ပြောင်းရင် restore လုပ်လို့ရပါတယ်။ GDPR လို ကိုယ်ရေးကိုယ်တာ ဥပဒေတွေက erasure ကို အခွင့်အရေးအဖြစ် ပေးထားလို့ flag တစ်ခုထိုးရုံနဲ့ မလုံလောက်ပါဘူး၊ ဥပဒေအကြံပေးနဲ့ တိုင်ပင်ပါ။
- Admin dashboard က delete မလုပ်ခင် "သေချာလား" ဆိုတဲ့ confirmation ပြပြီး 409 ကိုရရင် ဘာကြောင့် မဖျက်နိုင်လဲ message ကို ပြပါတယ်။
- Cleanup script တွေက DELETE ကို ထပ်ခါထပ်ခါ ပို့လည်း အဆင်ပြေရမယ်၊ ဒါကြောင့် 404 ကို အောင်မြင်တာလို သဘောထားပါတယ်။
- Test တွေက ဖျက်ပြီးနောက် GET က 404၊ ထပ်ဖျက်ရင် 404၊ နောက်ဆုံး admin က 409၊ restore နဲ့ purge ကို စစ်ပါတယ်။
အမှားများ
Delete မှာ ဖြစ်လေ့ရှိတဲ့ အမှားများ
204 မှာ res.json() ခေါ်ခြင်း
204 မှာ body မရှိလို့ await res.json() က SyntaxError: Unexpected end of JSON input ပစ်ပါတယ်၊ ပြီးတော့ res.ok က true ဖြစ်နေလို့ အောင်မြင်တဲ့ delete ကို error လို့ ထင်မိပါတယ်။ Status ကို အရင်စစ်ပါ။
ဒုတိယ delete ရဲ့ 404 ကို ကျရှုံးမှုလို့ ယူခြင်း
Retry လုပ်တဲ့ cleanup က 404 မှာ error ပစ်ရင် ပထမကြိုးစားမှု အောင်မြင်ခဲ့ပေမဲ့ ဒုတိယမှာ လုံးလုံး ပျက်သွားပါတယ်။ ရည်ရွယ်ချက်က user မရှိတော့တာမို့ 404 ကို "ဖယ်ပြီးသား" လို့ ယူပါ၊ ဒါပေမဲ့ ဒါက DELETE အတွက်ပဲပါ၊ GET မှာ 404 က တကယ့် error ပါ။
စည်းမျဉ်းမစစ်ဘဲ ဖျက်ခြင်း
နောက်ဆုံး admin ကို ဖျက်တာ၊ အလုပ်ကျန်နေတဲ့ owner ကို ဖျက်တာကို မတားရင် system က စီမံနိုင်သူမရှိတော့ပါဘူး။ စည်းမျဉ်းတွေကို database မပြောင်းခင် စစ်ပြီး 409 နဲ့ ငြင်းပါ။
Soft delete က erasure ပြီလို့ ထင်ခြင်း
deleted: true flag က data ကို ဖုံးထားတာပဲ ရှိပြီး ကိုယ်ရေးကိုယ်တာ data က database ထဲ ကျန်နေပါတယ်။ Erasure တောင်းတဲ့အခါ purge သို့ anonymise လုပ်တဲ့ လုပ်ငန်းစဉ်တစ်ခု သီးခြား လိုပါတယ်။
Best Practices
Best practices
- ထပ်ဖျက်ရင် 404 ပြန်မလား 204 ပြန်မလား တစ်ခုရွေးပြီး API docs မှာ ရေးပါ၊ resource အားလုံးမှာ တူအောင်ထားပါ။
- ဖျက်ဖို့ ခွင့်ပြုချက်နဲ့ business rule တွေကို data မပြောင်းခင် စစ်ပါ၊ ဖျက်ခွင့်မရှိရင် 404 သို့ 403 ကို policy အတိုင်း တသမတ်တည်း သုံးပါ။
- အရေးကြီးတဲ့ data အတွက် soft delete နဲ့ ရက်သတ်မှတ်ထားတဲ့ purge ကို ပေါင်းသုံးပါ၊ erasure လိုအပ်ချက်ကို ကြိုတွေးပါ။
Mini challenge
deleteUser နဲ့ cleanup ကို ကိုယ်တိုင် ရေးကြည့်ပါ
HTTP မလိုဘဲ deleteUser(users, id) ကို ရေးပါ: id မရှိရင် { status: 404, error: "user_not_found" }၊ နောက်ဆုံး admin ဆိုရင် { status: 409, error: "last_admin" }၊ မဟုတ်ရင် array ထဲက ဖယ်ပြီး { status: 204 }။ ပြီးတော့ cleanup(users, ids) ကို ရေးပါ: id တစ်ခုချင်းကို ဖျက်ပြီး 204 ကို deleted၊ 404 ကို alreadyGone အဖြစ် ရေတွက်ကာ တခြား ရလဒ်တွေကို "id:error" စာကြောင်းအဖြစ် blocked ထဲ ထည့်ပါ။
- နောက်ဆုံး admin ကို စစ်ရာမှာ users ထဲက admin အရေအတွက်ကို သုံးပါ
- ဖျက်မယ့် user ကို array ထဲကနေ တကယ် ဖယ်ရမယ် (splice)
- cleanup က ထပ်နေတဲ့ id နဲ့ မရှိတဲ့ id မှာ throw မလုပ်ရ
- ရလဒ်ကို { deleted, alreadyGone, blocked } object အဖြစ် ပြန်ပါ
const users = [
{ id: "u_1", name: "Alice", role: "admin" },
{ id: "u_2", name: "Bob", role: "member" },
{ id: "u_3", name: "Carol", role: "member" },
{ id: "u_4", name: "Dan", role: "admin" },
];
function deleteUser(users, id) {
// TODO: 404 { error: "user_not_found" } when the id does not exist
// TODO: 409 { error: "last_admin" } when it is the only admin
// TODO: otherwise remove it from the array and return { status: 204 }
}
function cleanup(users, ids) {
// TODO: delete every id and count 204 as "deleted" and 404 as "alreadyGone"
// TODO: put any other outcome into "blocked" as "id:error"
// TODO: return { deleted, alreadyGone, blocked }
}ရလဒ်: နှစ်ကြောင်း ထွက်ပါမယ်: {"deleted":2,"alreadyGone":2,"blocked":["u_4:last_admin"]} နဲ့ ကျန်တဲ့ user id တွေ u_2 u_4။
အရိပ်အမြွက် ကြည့်ရန်
findIndex နဲ့ index ကို ရှာပြီး -1 ဆိုရင် 404 ပါ။ Admin အရေအတွက်ကို filter နဲ့ ရေပြီး users[index].role === "admin" && admins.length === 1 ကို စစ်ပါ။ cleanup မှာ status သုံးမျိုးအတွက် if / else if / else တစ်ခုပဲ လိုပါတယ်။
အဖြေ ကြည့်ရန်
const users = [
{ id: "u_1", name: "Alice", role: "admin" },
{ id: "u_2", name: "Bob", role: "member" },
{ id: "u_3", name: "Carol", role: "member" },
{ id: "u_4", name: "Dan", role: "admin" },
];
function deleteUser(users, id) {
const index = users.findIndex((u) => u.id === id);
if (index === -1) return { status: 404, error: "user_not_found" };
const admins = users.filter((u) => u.role === "admin");
if (users[index].role === "admin" && admins.length === 1) {
return { status: 409, error: "last_admin" };
}
users.splice(index, 1);
return { status: 204 };
}
function cleanup(users, ids) {
const summary = { deleted: 0, alreadyGone: 0, blocked: [] };
for (const id of ids) {
const result = deleteUser(users, id);
if (result.status === 204) summary.deleted++;
else if (result.status === 404) summary.alreadyGone++;
else summary.blocked.push(id + ":" + result.error);
}
return summary;
}
console.log(JSON.stringify(cleanup(users, ["u_3", "u_3", "u_99", "u_1", "u_4"])));
console.log(users.map((u) => u.id).join(" "));Quick Quiz 1 / 3
Quick Quiz 2 / 3
Quick Quiz 3 / 3
မှတ်ထားရမယ့် အချက်များ
- DELETE အောင်မြင်ရင် body မပါတဲ့ 204 ပါ၊ client က status ကို အရင်စစ်ပြီး 204 မှာ res.json() မခေါ်ရပါဘူး။
- DELETE ရဲ့ နောက်ဆုံး state က idempotent ပါ (user မရှိတော့)၊ response ကတော့ API အလိုက် ကွာနိုင်ပါတယ်: ဒီ API က ထပ်ဖျက်ရင် 404၊ cleanup က 404 ကို ဖယ်ပြီးသားလို့ ယူပါတယ်။
- စည်းမျဉ်းတစ်ခု ချိုးမယ့် delete (နောက်ဆုံး admin) ကို တူညီတဲ့ error body နဲ့ 409 ပြန်ပြီး ဘာမှ မဖျက်ပါနဲ့။
- Soft delete က ဖုံးတာ၊ hard delete က ဖယ်တာ၊ erasure လိုရင် purge/anonymise လုပ်ပါ၊ ကြာတဲ့ delete ကို 202 နဲ့ ပြန်လို့ရပါတယ်။
RFC 9110 — HTTP Semantics — IETF