feat: make database idempotent by adding unique index and using UPSERT for order insertions
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 1m2s

This commit is contained in:
Cauê Faleiros
2026-05-08 15:48:37 -03:00
parent d4fad9f6c1
commit 8be7dbbe06

View File

@@ -41,6 +41,10 @@ const initDB = async () => {
await pool.query(`ALTER TABLE orders ADD COLUMN IF NOT EXISTS pedido_id VARCHAR(100);`).catch(() => {}); await pool.query(`ALTER TABLE orders ADD COLUMN IF NOT EXISTS pedido_id VARCHAR(100);`).catch(() => {});
await pool.query(`CREATE UNIQUE INDEX IF NOT EXISTS unique_order_product ON orders (pedido_id, produto_id);`).catch(err => {
console.error("Notice: Could not create unique index (might already exist or there are duplicates):", err.message);
});
console.log("Database initialized successfully."); console.log("Database initialized successfully.");
} catch (err) { } catch (err) {
console.error("Failed to initialize database:", err); console.error("Failed to initialize database:", err);
@@ -159,6 +163,11 @@ app.post('/api/data', authenticateAPIKey, async (req, res) => {
})(); })();
}); });
app.listen(PORT, '0.0.0.0', () => {
console.log(`Nexstar Backend running at http://localhost:${PORT}`);
console.log(`Endpoint for n8n: POST http://localhost:${PORT}/api/data`);
});;
app.listen(PORT, '0.0.0.0', () => { app.listen(PORT, '0.0.0.0', () => {
console.log(`Nexstar Backend running at http://localhost:${PORT}`); console.log(`Nexstar Backend running at http://localhost:${PORT}`);
console.log(`Endpoint for n8n: POST http://localhost:${PORT}/api/data`); console.log(`Endpoint for n8n: POST http://localhost:${PORT}/api/data`);