Skip to content

bubblydoo/cloudflare-workers-postgres-client

Repository files navigation

npm

⚠️ This module is unmaintained, we recommend using Cloudflare Hyperdrive instead.

Cloudflare Workers Postgres Client

This is an experimental module.

Heavily based on cloudflare/worker-template-postgres, but cleaned up and bundled into a single module.

This needs a Cloudflare Tunnel to your database running. To setup a Cloudflare Tunnel, you can use this docker-compose.yml.

npm i @bubblydoo/cloudflare-workers-postgres-client
# or
yarn add @bubblydoo/cloudflare-workers-postgres-client
import { Client } from '@bubblydoo/cloudflare-workers-postgres-client';

const createClient = () => {
  return new Client({
    user: 'postgres',
    database: 'postgres',
    hostname: 'https://<YOUR CLOUDFLARE TUNNEL>',
    password: 'keyboardcat',
    port: 5432,
  });
}

const worker = {
  async fetch(request, env, ctx) {
    const client = createClient();

    await client.connect()

    const userIds = await client.queryArray('select id from "Users" limit 10');

    ctx.waitUntil(client.end());

    return new Response(JSON.stringify(userIds));
  }
}

export default worker;

How it works

It uses the postgres Deno module, bundles it, and adds some code to make it work with Cloudflare Workers.