Database Operations
Database ကို n8n တွင် ချိတ်ဆက်ခြင်း
Section titled “Database ကို n8n တွင် ချိတ်ဆက်ခြင်း”n8n သည် MySQL, PostgreSQL, SQLite, MariaDB, MS SQL Server တို့ကို Built-in Node ဖြင့် တိုက်ရိုက် ချိတ်ဆက်နိုင်သည်။ Google Sheets ကို Database ပုံ သုံးနေသည့် Workflow တွေကို Real Database ဖြင့် ပြောင်းရာတွင် ပိုမိုမြန်ဆန်ပြီး Scalable ဖြစ်သည်။
MySQL / MariaDB Credential ဖန်တီးနည်း
Section titled “MySQL / MariaDB Credential ဖန်တီးနည်း”n8n Credentials → “MySQL” ကို ရွေးပြီး:
MySQL Credential: Host: localhost (or your DB server IP) Port: 3306 Database: my_database User: n8n_user Password: your_password
SSL: Enable (Production အတွက်)MySQL Node Operations
Section titled “MySQL Node Operations”| Operation | Query |
|---|---|
| Select | Rows ဖတ်ရန် |
| Insert | Row အသစ် ထည့်ရန် |
| Update | Row ပြင်ဆင်ရန် |
| Delete | Row ဖျက်ရန် |
| Execute Query | Custom SQL ရေးရန် |
CRUD Operations ဥပမာများ
Section titled “CRUD Operations ဥပမာများ”Create — Row အသစ် ထည့်ခြင်း
Section titled “Create — Row အသစ် ထည့်ခြင်း”Form Submit → Database Save → Welcome Email
-- MySQL Insert Node (Execute Query mode)INSERT INTO users (name, email, created_at, status)VALUES ( '{{ $json.name }}', '{{ $json.email }}', NOW(), 'active');MySQL Node (Insert mode) ကို သုံးလျှင် ပိုလွယ်ကူ:
MySQL Node: Operation: Insert Table: users Columns: name, email, status, created_at Values: name: {{ $json.name }} email: {{ $json.email }} status: active created_at: {{ $now.toISO() }}Read — Data ဖတ်ခြင်း
Section titled “Read — Data ဖတ်ခြင်း”-- Pending Orders အားလုံး ဆွဲရန်SELECT o.id, o.total_amount, u.name AS customer_name, u.emailFROM orders oJOIN users u ON o.user_id = u.idWHERE o.status = 'pending'ORDER BY o.created_at DESCLIMIT 100;Update — Row ပြင်ဆင်ခြင်း
Section titled “Update — Row ပြင်ဆင်ခြင်း”Payment ဝင်လာသောအခါ Order Status Update ပြီး Receipt ပို့သည်
UPDATE ordersSET status = 'paid', paid_at = NOW(), transaction_id = '{{ $json.transactionId }}'WHERE id = '{{ $json.orderId }}' AND status = 'pending';Delete — Data ဖျက်ခြင်း
Section titled “Delete — Data ဖျက်ခြင်း”-- 90 ရက်ထက် ကြာသော Inactive Sessions ဖျက်ပါ (Cleanup Schedule)DELETE FROM sessionsWHERE last_active < DATE_SUB(NOW(), INTERVAL 90 DAY) AND is_permanent = 0;PostgreSQL Integration
Section titled “PostgreSQL Integration”MySQL နှင့် Node ပုံစံ တူသော်လည်း Connection String ကွဲပြားသည်:
PostgreSQL Credential: Host: db.example.com Port: 5432 Database: production_db User: n8n_user Password: your_password SSL: Required (Supabase / Neon ဆိုရင်)Supabase Integration
Section titled “Supabase Integration”Supabase ဟာ PostgreSQL ပေါ်တည်ဆောက်ထားသော Backend-as-a-Service ဖြစ်ပြီး Free Tier ရှိသည်:
n8n → Supabase API → PostgreSQL Database
Supabase Credential
Section titled “Supabase Credential”Supabase Node (HTTP Request ဖြင့် ချိတ်): URL: https://your-project.supabase.co/rest/v1/ Headers: apikey: your-anon-key Authorization: Bearer your-anon-key Content-Type: application/json Prefer: return=representation ← Insert ပြီးနောက် Row ပြန်ပေးသည်Supabase CRUD via HTTP Request
Section titled “Supabase CRUD via HTTP Request”GET /orders?status=eq.pending&order=created_at.desc&limit=50POST /orders Body: { "user_id": 1, "total": 50000 }PATCH /orders?id=eq.123 Body: { "status": "paid" }DELETE /orders?id=eq.123Transaction — Multiple Queries တစ်ကြိမ်တည်း
Section titled “Transaction — Multiple Queries တစ်ကြိမ်တည်း”Order တင်ရာတွင် Stock ကို တစ်ကြိမ်တည်း Update လုပ်ပြီး Rollback တတ်ရမည်:
-- MySQL Execute Query NodeSTART TRANSACTION;
-- 1. Order ထည့်INSERT INTO orders (user_id, total) VALUES ({{ $json.userId }}, {{ $json.total }});SET @order_id = LAST_INSERT_ID();
-- 2. Stock ပြင်UPDATE productsSET stock = stock - {{ $json.quantity }}WHERE id = {{ $json.productId }} AND stock >= {{ $json.quantity }};
-- Stock မလုံလျှင် RollbackIF ROW_COUNT() = 0 THEN ROLLBACK; SELECT 'INSUFFICIENT_STOCK' AS result;ELSE COMMIT; SELECT @order_id AS result;END IF;Scheduled Database Cleanup
Section titled “Scheduled Database Cleanup”Weekly Database Maintenance — Auto Cleanup နှင့် Archive
-- Cleanup Query 1: Old LogsDELETE FROM activity_logsWHERE created_at < DATE_SUB(NOW(), INTERVAL 30 DAY);
-- Cleanup Query 2: Archive OrdersINSERT INTO orders_archive SELECT * FROM ordersWHERE created_at < DATE_SUB(NOW(), INTERVAL 365 DAY) AND status IN ('delivered', 'cancelled');
DELETE FROM ordersWHERE created_at < DATE_SUB(NOW(), INTERVAL 365 DAY) AND status IN ('delivered', 'cancelled');နောက်သင်ခန်းစာမှာ Production Deployment — n8n ကို VPS ပေါ် Deploy လုပ်ပြီး Production ပတ်ဝန်းကျင်မှာ Run ဖို့ လေ့လာမည်!