Skip to content

bubblydoo/cloudflare-workers-postgres-client

main
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Code

Latest commit

 

Git stats

Files

Permalink
Failed to load latest commit information.
Type
Name
Latest commit message
Commit time
 
 
 
 
 
 
 
 
 
 
 
 
 
 

npm

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.