Waddler has native support for Neon connections with the neon-http and neon-websockets drivers. These use the neon-serverless driver under the hood.
With the neon-http and neon-websockets drivers, you can access a Neon database from serverless environments over HTTP or WebSockets instead of TCP.
Querying over HTTP is faster for single, non-interactive transactions.
If you need session or interactive transaction support, or a fully compatible drop-in replacement for the pg driver, you can use the WebSocket-based neon-serverless driver.
You can connect to a Neon database directly using Postgres
For an example of using Waddler with the Neon Serverless driver in a Cloudflare Worker, see here.
To use Neon from a serverful environment, you can use the PostgresJS driver, as described in Neon’s official Node.js docs — see docs.
Step 1 - Install packages
npm
yarn
pnpm
bun
npm i waddler @neondatabase/serverless
yarn add waddler @neondatabase/serverless
pnpm add waddler @neondatabase/serverless
bun add waddler @neondatabase/serverless
Step 2 - Initialize the driver and make a query
Neon HTTP
Neon Websockets
node-postgres
postgres.js
import { waddler } from 'waddler/neon-http';const sql = waddler(process.env.DATABASE_URL);const result = await sql`select 1;`;
import { waddler } from 'waddler/neon-serverless';const sql = waddler(process.env.DATABASE_URL);const result = await sql`select 1;`;
// For Node.js - make sure to install the 'ws' and 'bufferutil' packagesimport { waddler } from 'waddler/neon-serverless';import ws from 'ws';const sql = waddler({ connection: process.env.DATABASE_URL, ws: ws,});const result = await sql`select 1;`;
IMPORTANT
Additional configuration is required to use WebSockets in environments where the WebSocket global is not defined, such as Node.js.
Add the ws and bufferutil packages to your project’s dependencies, and set ws in the Waddler config.
// Make sure to install the 'pg' package import { waddler } from 'waddler/node-postgres';const sql = waddler(process.env.DATABASE_URL);const result = await sql`select 1;`;
// Make sure to install the 'postgres' packageimport { waddler } from 'waddler/postgres-js';const sql = waddler(process.env.DATABASE_URL);const result = await sql`select 1;`;
If you need to provide your existing drivers:
Neon HTTP
Neon Websockets
node-postgres
postgres.js
import { neon } from '@neondatabase/serverless';import { waddler } from 'waddler/neon-http';const sqlClient = neon(process.env.DATABASE_URL!);const sql = waddler({ client: sqlClient });const result = await sql`select 1;`;
import { Pool } from '@neondatabase/serverless';import { waddler } from 'waddler/neon-serverless';const pool = new Pool({ connectionString: process.env.DATABASE_URL });const sql = waddler({ client: pool })const result = await sql`select 1;`;
// For Node.js - make sure to install the 'ws' and 'bufferutil' packagesimport { Pool, neonConfig } from '@neondatabase/serverless';import { waddler } from 'waddler/neon-serverless';import ws from 'ws';neonConfig.webSocketConstructor = ws;const pool = new Pool({ connectionString: process.env.DATABASE_URL });const sql = waddler({ client: pool })const result = await sql`select 1;`;
IMPORTANT
Additional configuration is required to use WebSockets in environments where the WebSocket global is not defined, such as Node.js.
Add the ws and bufferutil packages to your project’s dependencies, and set ws in the Waddler config.
// Make sure to install the 'pg' package import { waddler } from "waddler/node-postgres";import { Pool } from "pg";const pool = new Pool({ connectionString: process.env.DATABASE_URL,});const sql = waddler({ client: pool });const result = await sql`select 1;`;
// Make sure to install the 'postgres' packageimport { waddler } from 'waddler/postgres-js';import postgres from 'postgres';const queryClient = postgres(process.env.DATABASE_URL);const sql = waddler({ client: queryClient });const result = await sql`select 1;`;