Last active
February 8, 2025 19:48
-
-
Save mferoc/df2dfbcc1b9f24ffd49b5a7e294e2d48 to your computer and use it in GitHub Desktop.
supabase-subscription-insert-log-table
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- consultando tabela de logs | |
SELECT | |
(log_payload_json->>'eventType') AS event_type, | |
(log_payload_json->'new'->>'name') AS name, | |
(log_payload_json->'new'->>'last_name') AS last_name, | |
id, | |
created_at | |
FROM | |
clientes_logs | |
WHERE | |
log_payload_json->>'eventType' = 'INSERT'; | |
-- consultando logs api | |
select | |
cast(timestamp as datetime) as timestamp, | |
path, | |
r.method, | |
rp.status_code, | |
event_message | |
from edge_logs | |
cross join unnest(metadata) as m | |
cross join unnest(m.request) as r | |
cross join unnest(m.response) as rp | |
where | |
path like '%rest/v1%' | |
order by | |
timestamp desc | |
limit 100 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
<meta charset="UTF-8" /> | |
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | |
<title>Document</title> | |
</head> | |
<body> | |
<form id="clienteForm"> | |
<label for="name">Name:</label> | |
<input type="text" id="name" name="name" required /> | |
<br /> | |
<label for="last_name">Last Name:</label> | |
<input type="text" id="last_name" name="last_name" required /> | |
<br /> | |
<button type="button" id="sendButton">Send</button> | |
</form> | |
<script type="module" src="script.js"></script> | |
</body> | |
</html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// filepath: /home/matheus/github-projetos/univesp-ache-membros-pi/script2.js | |
import { createClient } from "https://cdn.jsdelivr.net/npm/@supabase/supabase-js/+esm"; | |
const supabaseUrl = "https://ewewqgsvcmcrymmhddbc.supabase.co"; | |
const supabaseKey = | |
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6ImV3ZXdxZ3N2Y21jcnltbWhkZGJjIiwicm9sZSI6ImFub24iLCJpYXQiOjE3Mzg2MzM5NTUsImV4cCI6MjA1NDIwOTk1NX0.bFFd9nTayXNb2T1Rq1p1II5hjt-lUBZ-OCleh0vt8sM"; | |
const supabase = createClient(supabaseUrl, supabaseKey); | |
// Configure a assinatura para a tabela "clientes" | |
const channel = supabase | |
.channel("supabase_realtime:public:clientes") | |
.on( | |
"postgres_changes", | |
{ | |
event: "*", | |
schema: "public", | |
}, | |
async (payload) => { | |
await supabase.from("clientes_logs").insert([{ log_payload_json: payload }]); | |
console.log("Change received!", payload); | |
} | |
) | |
.subscribe(); | |
document.getElementById("sendButton").addEventListener("click", async () => { | |
const name = document.getElementById("name").value.trim(); | |
const lastName = document.getElementById("last_name").value.trim(); | |
if (!name || !lastName) { | |
alert("Please fill in both fields."); | |
return; | |
} | |
const { data, error } = await supabase | |
.from("clientes") | |
.insert([{ name, last_name: lastName }]); | |
if (error) { | |
console.error("Error inserting data:", error); | |
alert("Error inserting data."); | |
} else { | |
alert("Data inserted successfully."); | |
console.log("Inserted data:", data); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment