Loop နှင့် Batch Processing
Loop ဘာကြောင့် လိုအပ်သလဲ?
Section titled “Loop ဘာကြောင့် လိုအပ်သလဲ?”n8n မှာ Node တစ်ခုဟာ Items အများကြီးကို တစ်ခါတည်း Process လုပ်တတ်ပါတယ်။ ဒါပေမဲ့ တချို့ API တွေက တစ်ကြိမ်ကို Item တစ်ခုစီသာ ခေါ်လို့ရတဲ့ Endpoint မျိုး ရှိပါတယ်။ ဒါမှမဟုတ် Item တစ်ခုချင်းစီ Process ပြီး Result ကို ဆက်ပြေးဖို့ Loop လိုတဲ့ အချိန်မျိုးမှာ Loop Pattern ကို သုံးရပါတယ်။
Loop မလိုသောအချိန် (n8n Default): [Get 100 Users] ──▶ [Send Email] ← Email Node က Users ၁၀၀ ကို တစ်ခါတည်း Process
Loop လိုသောအချိန်: [Get 100 Users] │ [Loop: User တစ်ဦးချင်းစီ] │ ├──▶ [Get User Details from Slow API] ← တစ်ကြိမ်ကို ၁ ယောက် └──▶ [Update Database Row]Loop Over Items (Basic)
Section titled “Loop Over Items (Basic)”n8n ၏ Loop Over Items Node (ကောင်းသောနာမည်: splitInBatches node ၏ loop mode) ကို သုံးပြီး Item တစ်ခုချင်းစီကို Process နိုင်သည်:
500 Users ကို တစ်ဦးချင်းစီ API ခေါ်ပြီး Merge ပြုလုပ်သည်
SplitInBatches Node
Section titled “SplitInBatches Node”Large Dataset ကို Group ဖြင့် ခွဲပြီး Process လုပ်သည်။ API Rate Limit ကို ကျော်ဖြတ်ရာတွင် အသုံးဝင်ဆုံး Pattern ဖြစ်သည်:
1000 emails ကို 50 ချပ် ၂၀ ကြိမ် ခွဲပို့သည်
SplitInBatches Configuration
Section titled “SplitInBatches Configuration”SplitInBatches Node: Batch Size: 50 ← တစ်ကြိမ်ကို ဘယ်နှ Item Options: Reset: false ← Loop ပြန်မစဘဲ ဆက်ပြေးသည်Loop ဖြင့် Process လုပ်သော Code Pattern
Section titled “Loop ဖြင့် Process လုပ်သော Code Pattern”// SplitInBatches ပြီးနောက် Code Nodeconst batchItems = $input.all();
console.log(`Processing batch of ${batchItems.length} items...`);
// Batch ထဲ Item တစ်ခုချင်းစီ Processconst results = batchItems.map(item => ({ json: { ...item.json, processed: true, processedAt: new Date().toISOString() }}));
return results;Rate Limiting ကိုင်တွယ်နည်း
Section titled “Rate Limiting ကိုင်တွယ်နည်း”API Rate Limit ဆိုတာ API Server မှ တစ်မိနစ်ကို Request X ခုသာ ခွင့်ပြုတဲ့ ကန့်သတ်ချက်ပါ:
Wait Node ထည့်ပြီး Rate Limit ကာကွယ်ပါ
Section titled “Wait Node ထည့်ပြီး Rate Limit ကာကွယ်ပါ”Workflow Pattern: [SplitInBatches] ──▶ [API Call] ──▶ [Wait: 1s] ──▶ (loop back)
Wait Node: Wait Amount: 1 Wait Unit: Seconds
← API ကို Batch တစ်ကြိမ်ပြီးတိုင်း 1 စက္ကန့် ခဏနားပါConditional Loop — Loop ဘယ်အချိန် ရပ်မလဲ?
Section titled “Conditional Loop — Loop ဘယ်အချိန် ရပ်မလဲ?”Loop ကို Condition ဖြင့် ရပ်တန့်ရာတွင် IF Node ကို Loop ထဲ ထည့်သည်:
Pagination ဖြင့် API Data အကုန်ဆွဲနည်း
Section titled “Pagination ဖြင့် API Data အကုန်ဆွဲနည်း”API မှာ Page တစ်ခုကို Items ၁၀ ခုသာ Return ပြန်ပေးပြီး နောက် Page ကို ထပ်ခေါ်ရမည့် Pattern:
// Code Node — Pagination State ထိန်းသည်const currentPage = $execution.resumeData?.page ?? 1;const totalPages = $input.first().json.total_pages;
// State ကို Updateconst output = { items: $input.first().json.data, nextPage: currentPage + 1, hasMore: currentPage < totalPages, currentPage};
return [{ json: output }];လက်တွေ့ Project: Bulk Email Sender
Section titled “လက်တွေ့ Project: Bulk Email Sender”Google Sheet ထဲ Email List ကို ဖတ်ပြီး Personalized Email ပို့သောကြောင့် SplitInBatches ကို သုံးသည်:
နေ့တိုင်း Email List မှ Bulk Personalized Email ပို့ပြီး Rate Limit ကာကွယ်သည်
// Code Node — Email Personalizeconst item = $input.first().json;
return [{ json: { to: item.email, subject: `မင်္ဂလာပါ ${item.name}! ဒီနေ့ Special Offer`, html: ` <h2>မင်္ဂလာပါ ${item.name}!</h2> <p>သင်၏ Membership Level: <strong>${item.tier}</strong></p> <p>ဒီနေ့ Discount Code: <strong>${item.code}</strong></p> ` }}];နောက်သင်ခန်းစာမှာ AI Workflow — n8n တွင် AI ကို ထည့်သွင်းပြီး Smart Automation ဆောက်နည်း လေ့လာမည်!