-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn8n-script-insert.js
More file actions
101 lines (90 loc) · 3.23 KB
/
Copy pathn8n-script-insert.js
File metadata and controls
101 lines (90 loc) · 3.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/**
* n8n Code Node: Insert Script into Supabase
*
* Copy this code into an n8n Code node (JavaScript mode)
* Make sure to set your Supabase credentials below or as environment variables
*/
// ===== CONFIGURATION =====
// Replace these with your actual Supabase credentials
// Or set them as n8n environment variables: $env.SUPABASE_URL and $env.SUPABASE_ANON_KEY
const SUPABASE_URL = $env.SUPABASE_URL || 'https://yunrhoxuxrzbcbkrvgjw.supabase.co';
const SUPABASE_ANON_KEY = $env.SUPABASE_ANON_KEY || 'YOUR_SUPABASE_ANON_KEY_HERE';
// ===== MAIN CODE =====
const inputData = $input.all();
const results = [];
for (const item of inputData) {
const data = item.json;
// Map input data to script schema
// Adjust these mappings based on your n8n workflow data structure
const scriptData = {
title: data.title || data.name || data.heading || 'Untitled Script',
raw_text: data.raw_text || data.content || data.text || data.body || '',
content_cn_draft: data.content_cn_draft || data.draft || data.chinese_content || data.content || '',
source_url: data.source_url || data.url || data.link || null,
content_cn_final: data.content_cn_final || data.final_content || null,
content_en: data.content_en || data.english_content || null,
audio_url: data.audio_url || data.audio || null,
status: ['new', 'editing', 'done'].includes(data.status) ? data.status : 'new',
tags: Array.isArray(data.tags) ? data.tags : (data.tags ? data.tags.split(',').map(t => t.trim()) : [])
};
// Validate required fields
if (!scriptData.title || !scriptData.raw_text || !scriptData.content_cn_draft) {
results.push({
json: {
success: false,
error: 'Missing required fields',
message: 'title, raw_text, and content_cn_draft are required',
received: {
title: scriptData.title,
raw_text: scriptData.raw_text ? 'present' : 'missing',
content_cn_draft: scriptData.content_cn_draft ? 'present' : 'missing'
}
}
});
continue;
}
try {
// Insert into Supabase using REST API
const response = await $http.request({
method: 'POST',
url: `${SUPABASE_URL}/rest/v1/scripts`,
headers: {
'apikey': SUPABASE_ANON_KEY,
'Authorization': `Bearer ${SUPABASE_ANON_KEY}`,
'Content-Type': 'application/json',
'Prefer': 'return=representation' // Return the inserted row
},
body: scriptData
});
results.push({
json: {
success: true,
script: response,
id: response.id,
title: response.title,
status: response.status,
createdAt: response.created_at
}
});
} catch (error) {
// Handle errors
let errorMessage = 'Unknown error';
let errorDetails = null;
if (error.response) {
// HTTP error response
errorMessage = error.response.body?.message || error.response.body?.error_description || 'HTTP error';
errorDetails = error.response.body;
} else if (error.message) {
errorMessage = error.message;
}
results.push({
json: {
success: false,
error: errorMessage,
errorDetails: errorDetails,
attemptedData: scriptData
}
});
}
}
return results;