Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions scripts/sync-content.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -190,17 +190,27 @@ async function parseMarkdown(fullPath) {
}
}

function getTableBuilder(table) {
if (!table.includes('.')) {
return supabase.from(table);
}

const [schema, ...rest] = table.split('.');
const tableName = rest.join('.');
return supabase.schema(schema).from(tableName);
}

async function upsert(table, rows, options = {}) {
if (!rows.length) return;
const { error } = await supabase.from(table).upsert(rows, options);
const { error } = await getTableBuilder(table).upsert(rows, options);
if (error) {
throw new Error(`Erro ao sincronizar ${table}: ${error.message}`);
}
}

async function insert(table, rows) {
if (!rows.length) return;
const { error } = await supabase.from(table).insert(rows);
const { error } = await getTableBuilder(table).insert(rows);
if (error) {
throw new Error(`Erro ao inserir em ${table}: ${error.message}`);
}
Expand All @@ -209,7 +219,7 @@ async function insert(table, rows) {
async function purge(table, column, values) {
if (!values.length) return;
for (const value of values) {
const { error } = await supabase.from(table).delete().eq(column, value);
const { error } = await getTableBuilder(table).delete().eq(column, value);
if (error) {
throw new Error(`Erro ao limpar ${table} (${column}=${value}): ${error.message}`);
}
Expand Down