Skip to content
GitHub

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 အတွက်)
OperationQuery
SelectRows ဖတ်ရန်
InsertRow အသစ် ထည့်ရန်
UpdateRow ပြင်ဆင်ရန်
DeleteRow ဖျက်ရန်
Execute QueryCustom SQL ရေးရန်

Create — Row အသစ် ထည့်ခြင်း

Section titled “Create — Row အသစ် ထည့်ခြင်း”
Webhook: Form Submit
MySQL: Insert Row users table
Gmail: Welcome Email

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() }}
-- Pending Orders အားလုံး ဆွဲရန်
SELECT
o.id,
o.total_amount,
u.name AS customer_name,
u.email
FROM orders o
JOIN users u ON o.user_id = u.id
WHERE o.status = 'pending'
ORDER BY o.created_at DESC
LIMIT 100;

Update — Row ပြင်ဆင်ခြင်း

Section titled “Update — Row ပြင်ဆင်ခြင်း”
Webhook: Payment Success
MySQL: Update Order Status SET status='paid'
Gmail: Receipt Email

Payment ဝင်လာသောအခါ Order Status Update ပြီး Receipt ပို့သည်

UPDATE orders
SET
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 sessions
WHERE last_active < DATE_SUB(NOW(), INTERVAL 90 DAY)
AND is_permanent = 0;

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 ဟာ PostgreSQL ပေါ်တည်ဆောက်ထားသော Backend-as-a-Service ဖြစ်ပြီး Free Tier ရှိသည်:

n8n Automation Layer
Supabase Node REST API
Supabase DB PostgreSQL

n8n → Supabase API → PostgreSQL Database

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 ပြန်ပေးသည်
GET /orders?status=eq.pending&order=created_at.desc&limit=50
POST /orders Body: { "user_id": 1, "total": 50000 }
PATCH /orders?id=eq.123 Body: { "status": "paid" }
DELETE /orders?id=eq.123

Transaction — Multiple Queries တစ်ကြိမ်တည်း

Section titled “Transaction — Multiple Queries တစ်ကြိမ်တည်း”

Order တင်ရာတွင် Stock ကို တစ်ကြိမ်တည်း Update လုပ်ပြီး Rollback တတ်ရမည်:

-- MySQL Execute Query Node
START TRANSACTION;
-- 1. Order ထည့်
INSERT INTO orders (user_id, total) VALUES ({{ $json.userId }}, {{ $json.total }});
SET @order_id = LAST_INSERT_ID();
-- 2. Stock ပြင်
UPDATE products
SET stock = stock - {{ $json.quantity }}
WHERE id = {{ $json.productId }} AND stock >= {{ $json.quantity }};
-- Stock မလုံလျှင် Rollback
IF ROW_COUNT() = 0 THEN
ROLLBACK;
SELECT 'INSUFFICIENT_STOCK' AS result;
ELSE
COMMIT;
SELECT @order_id AS result;
END IF;
Schedule Trigger Every Sunday 3am
MySQL: Delete Old Logs > 30 days
MySQL: Archive Orders > 1 year
Slack: Cleanup Report

Weekly Database Maintenance — Auto Cleanup နှင့် Archive

-- Cleanup Query 1: Old Logs
DELETE FROM activity_logs
WHERE created_at < DATE_SUB(NOW(), INTERVAL 30 DAY);
-- Cleanup Query 2: Archive Orders
INSERT INTO orders_archive SELECT * FROM orders
WHERE created_at < DATE_SUB(NOW(), INTERVAL 365 DAY)
AND status IN ('delivered', 'cancelled');
DELETE FROM orders
WHERE created_at < DATE_SUB(NOW(), INTERVAL 365 DAY)
AND status IN ('delivered', 'cancelled');

နောက်သင်ခန်းစာမှာ Production Deployment — n8n ကို VPS ပေါ် Deploy လုပ်ပြီး Production ပတ်ဝန်းကျင်မှာ Run ဖို့ လေ့လာမည်!