mirror of
https://github.com/brianc/node-postgres.git
synced 2026-01-25 16:03:13 +00:00
29 lines
779 B
TypeScript
29 lines
779 B
TypeScript
import { Client } from 'pg'
|
|
|
|
export interface Env {
|
|
USER: string
|
|
PGUSER: string
|
|
PGPASSWORD: string
|
|
}
|
|
|
|
export default {
|
|
async fetch(request: Request, env: Env, ctx: ExecutionContext): Promise<Response> {
|
|
const url = new URL(request.url)
|
|
if (url.pathname === '/favicon.ico') return new Response(null, { status: 404 })
|
|
|
|
const params = url.searchParams
|
|
const ssl = params.has('ssl')
|
|
|
|
var client = new Client({
|
|
user: env.PGUSER || env.USER,
|
|
password: env.PGPASSWORD,
|
|
ssl,
|
|
})
|
|
await client.connect()
|
|
const resp = Response.json(await client.query('SELECT $1::text', ['Hello, World!']))
|
|
// Clean up the client, ensuring we don't kill the worker before that is completed.
|
|
ctx.waitUntil(client.end())
|
|
return resp
|
|
},
|
|
}
|