Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
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
- Host: GitHub
- URL: https://github.com/bubblydoo/cloudflare-workers-postgres-client
- Owner: bubblydoo
- Created: 2022-06-14T16:29:56.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-05T08:00:22.000Z (10 months ago)
- Last Synced: 2024-12-24T21:33:04.585Z (15 days ago)
- Topics: cloudflare-workers, postgresql
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@bubblydoo/cloudflare-workers-postgres-client
- Size: 114 KB
- Stars: 22
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
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.