@fachkraftfreund/n8n-nodes-supabase 1.4.1 → 1.4.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -881,24 +881,45 @@ async function handleBatchCount(supabase, itemCount) {
|
|
|
881
881
|
if (!j.table)
|
|
882
882
|
continue;
|
|
883
883
|
(0, supabaseClient_1.validateTableName)(j.table);
|
|
884
|
+
const fkQuery = `
|
|
885
|
+
SELECT kcu.column_name AS fk_column, ccu.column_name AS parent_column
|
|
886
|
+
FROM information_schema.table_constraints tc
|
|
887
|
+
JOIN information_schema.key_column_usage kcu ON tc.constraint_name = kcu.constraint_name AND tc.table_schema = kcu.table_schema
|
|
888
|
+
JOIN information_schema.constraint_column_usage ccu ON tc.constraint_name = ccu.constraint_name AND tc.table_schema = ccu.table_schema
|
|
889
|
+
WHERE tc.constraint_type = 'FOREIGN KEY'
|
|
890
|
+
AND tc.table_name = '${j.table.replace(/'/g, "''")}'
|
|
891
|
+
AND ccu.table_name = '${table.replace(/'/g, "''")}'
|
|
892
|
+
LIMIT 1
|
|
893
|
+
`;
|
|
894
|
+
const { data: fkData, error: fkError } = await supabase.rpc('exec_sql_select', { sql: fkQuery });
|
|
895
|
+
if (fkError)
|
|
896
|
+
throw new Error(`Failed to discover FK for ${j.table}: ${(0, supabaseClient_1.formatSupabaseError)(fkError)}`);
|
|
897
|
+
if (!Array.isArray(fkData) || fkData.length === 0) {
|
|
898
|
+
throw new Error(`No foreign key found from "${j.table}" to "${table}". Check that a FK relationship exists.`);
|
|
899
|
+
}
|
|
900
|
+
const fkColumn = fkData[0].fk_column;
|
|
901
|
+
const parentColumn = fkData[0].parent_column;
|
|
884
902
|
const joinType = j.joinType === 'inner' ? 'INNER JOIN' : 'LEFT JOIN';
|
|
885
|
-
joinClauses.push(`${joinType} "${j.table}" ON "${j.table}"."${
|
|
903
|
+
joinClauses.push(`${joinType} "${j.table}" ON "${j.table}"."${fkColumn}" = "${table}"."${parentColumn}"`);
|
|
886
904
|
}
|
|
887
905
|
const whereStr = whereClauses.length > 0 ? ` WHERE ${whereClauses.join(' AND ')}` : '';
|
|
888
906
|
const joinStr = joinClauses.length > 0 ? ` ${joinClauses.join(' ')}` : '';
|
|
889
|
-
const sql = `SELECT "${groupByColumn}", COUNT(
|
|
907
|
+
const sql = `SELECT "${groupByColumn}", COUNT(DISTINCT "${table}"."id") as count FROM "${table}"${joinStr}${whereStr} GROUP BY "${groupByColumn}" ORDER BY count DESC`;
|
|
890
908
|
console.log(`[Supabase BATCH COUNT] sql: ${sql}`);
|
|
891
909
|
const { data, error } = await supabase.rpc('exec_sql_select', { sql });
|
|
892
910
|
if (error)
|
|
893
911
|
throw new Error((0, supabaseClient_1.formatSupabaseError)(error));
|
|
894
912
|
if (!Array.isArray(data) || data.length === 0) {
|
|
895
|
-
return [{ json: { table, groupByColumn,
|
|
896
|
-
}
|
|
897
|
-
return
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
913
|
+
return [{ json: { table, groupByColumn, data: [] } }];
|
|
914
|
+
}
|
|
915
|
+
return [{
|
|
916
|
+
json: {
|
|
917
|
+
table,
|
|
918
|
+
groupByColumn,
|
|
919
|
+
data: data.map((row) => ({
|
|
920
|
+
[groupByColumn]: row[groupByColumn],
|
|
921
|
+
count: Number(row.count),
|
|
922
|
+
})),
|
|
923
|
+
},
|
|
924
|
+
}];
|
|
904
925
|
}
|
package/package.json
CHANGED