import * as postgres from "https://deno.land/x/postgres@v0.14.2/mod.ts";
import { serve } from "https://deno.land/std@0.114.0/http/server.ts";
const databaseUrl = Deno.env.get("DATABASE_URL")!;
const pool = new postgres.Pool(databaseUrl, 3, true);
serve(async (_req) => {
try {
const connection = await pool.connect();
try {
const result = await connection.queryObject`SELECT * FROM animals`;
const users = result.rows;
const body = JSON.stringify(users, null, 2);
return new Response(body, {
status: 200,
headers: {
"Content-Type": "application/json; charset=utf-8",
},
});
} finally {
connection.release();
}
} catch(err) {
console.error(err);
return new Response(String(err?.message ?? err), { status: 500 });
}
});