Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/bubblydoo/cloudflare-workers-postgres-client

Connect to a Postgres database from a Cloudflare Worker, using Cloudflare Tunnel
https://github.com/bubblydoo/cloudflare-workers-postgres-client

cloudflare-workers postgresql

Last synced: 8 days ago
JSON representation

Connect to a Postgres database from a Cloudflare Worker, using Cloudflare Tunnel

Awesome Lists containing this project

README

        

![npm](https://img.shields.io/npm/v/@bubblydoo/cloudflare-workers-postgres-client)

## ⚠️ This module is unmaintained, we recommend using [Cloudflare Hyperdrive](https://developers.cloudflare.com/hyperdrive/) instead.

# Cloudflare Workers Postgres Client

This is an experimental module.

Heavily based on [cloudflare/worker-template-postgres](https://github.com/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](https://github.com/bubblydoo/cloudflare-tunnel-postgres-docker-compose/blob/main/docker-compose.yml).

```bash
npm i @bubblydoo/cloudflare-workers-postgres-client
# or
yarn add @bubblydoo/cloudflare-workers-postgres-client
```

```ts
import { Client } from '@bubblydoo/cloudflare-workers-postgres-client';

const createClient = () => {
return new Client({
user: 'postgres',
database: 'postgres',
hostname: 'https://',
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](https://deno.land/x/[email protected]) Deno module, bundles it, and adds some code to make it work with Cloudflare Workers.