• Stars
    star
    324
  • Rank 129,708 (Top 3 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 2 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Connect to Neon PostgreSQL from serverless/worker/edge functions

@neondatabase/serverless [BETA]

@neondatabase/serverless is Neon's PostgreSQL driver for JavaScript and TypeScript. It's:

  • Low-latency, thanks to message pipelining and other optimizations
  • Ideal for serverless/edge deployment, using https and WebSockets in place of TCP
  • A drop-in replacement for node-postgres, aka pg (on which it's based)

Get started

Install it

Install it with your preferred JavaScript package manager. For example:

npm install @neondatabase/serverless

Using TypeScript? No worries: types are included.

Configure it

Get your connection string from the Neon console and set it as an environment variable. Something like:

DATABASE_URL=postgres://username:[email protected]/neondb

Use it

For one-shot queries, use the neon function. For instance:

import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL);

const [post] = await sql`SELECT * FROM posts WHERE id = ${postId}`;
// `post` is now { id: 12, title: 'My post', ... } (or undefined)

Note: interpolating ${postId} here is safe from SQL injection.

Deploy it

Turn this example into a complete API endpoint deployed on Vercel Edge Functions at https://myapp.vercel.dev/api/post?postId=123 by following two simple steps:

  1. Create a new file api/post.ts:
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL);

export default async (req: Request, ctx: any) => {
  // get and validate the `postId` query parameter
  const postId = parseInt(new URL(req.url).searchParams.get('postId'), 10);
  if (isNaN(postId)) return new Response('Bad request', { status: 400 });

  // query and validate the post
  const [post] = await sql`SELECT * FROM posts WHERE id = ${postId}`;
  if (!post) return new Response('Not found', { status: 404 });

  // return the post as JSON
  return new Response(JSON.stringify(post), { 
    headers: { 'content-type': 'application/json' }
  });
}

export const config = {
  runtime: 'edge',
  regions: ['iad1'],  // specify the region nearest your Neon DB
};
  1. Test and deploy
npm install -g vercel  # install vercel CLI
npx vercel env add DATABASE_URL  # paste Neon connection string, select all environments
npx vercel dev  # check working locally, then ...
npx vercel deploy

The neon query function has a few additional options.

Sessions, transactions, and node-postgres compatibility

A query using the neon function, as shown above, is carried by an https fetch request.

This should work — and work fast — from any modern JavaScript environment. But you can only send one query at a time this way: sessions and transactions are not supported.

Pool and Client

Use the Pool or Client constructors instead when you need:

  • session or transaction support, or

  • node-postgres compatibility, to enable query libraries like Kysely or Zapatos.

Using Pool and Client, queries are carried by WebSockets. There are two key things you need to know:

  1. In Node.js and some other environments, there's no built-in WebSocket support. In these cases, supply a WebSocket constructor function.

  2. In serverless environments such as Vercel Edge Functions or Cloudflare Workers, WebSocket connections can't outlive a single request.

    That means Pool or Client objects must be connected, used and closed within a single request handler. Don't create them outside a request handler; don't create them in one handler and try to reuse them in another; and to avoid exhausting available connections, don't forget to close them.

These points are demonstrated in the examples below.

API

Example: Node.js with Pool.connect()

In Node.js, it takes two lines to configure WebSocket support. For example:

import { Pool, neonConfig } from '@neondatabase/serverless';

import ws from 'ws';
neonConfig.webSocketConstructor = ws;  // <-- this is the key bit

const pool = new Pool({ connectionString: process.env.DATABASE_URL });
pool.on('error', err => console.error(err));
// ...

const client = await pool.connect();

try {
  await client.query('BEGIN');
  const { rows: [{ id: postId }] } = await client.query('INSERT INTO posts (title) VALUES ($1) RETURNING id', ['Welcome']);
  await client.query('INSERT INTO photos (post_id, url) VALUES ($1, $2)', [postId, 's3.bucket/photo/url']);
  await client.query('COMMIT');

} catch (err) {
  await client.query('ROLLBACK');
  throw err;

} finally {
  client.release();
}

// ...
await pool.end();

Other WebSocket libraries are available. For example, you could replace ws in the above example with undici:

import { WebSocket } from 'undici';
neonConfig.webSocketConstructor = WebSocket; 

Example: Vercel Edge Function with Pool.query()

We can rewrite the Vercel Edge Function above to use Pool, as follows:

import { Pool } from '@neondatabase/serverless';

// *don't* create a `Pool` or `Client` here, outside the request handler

export default async (req: Request, ctx: any) => {
  // create a `Pool` inside the request handler
  const pool = new Pool({ connectionString: process.env.DATABASE_URL });

  // get and validate the `postId` query parameter
  const postId = parseInt(new URL(req.url).searchParams.get('postId'), 10);
  if (isNaN(postId)) return new Response('Bad request', { status: 400 });

  // query and validate the post
  const [post] = await pool.query('SELECT * FROM posts WHERE id = $1', [postId]);
  if (!post) return new Response('Not found', { status: 404 });

  // end the `Pool` inside the same request handler 
  // (unlike `await`, `ctx.waitUntil` won't hold up the response)
  ctx.waitUntil(pool.end());

  // return the post as JSON
  return new Response(JSON.stringify(post), { 
    headers: { 'content-type': 'application/json' }
  });
}

export const config = {
  runtime: 'edge',
  regions: ['iad1'],  // specify the region nearest your Neon DB
};

Note: we don't actually use the pooling capabilities of Pool in this example. But it's slightly briefer than using Client and, because Pool.query is designed for one-shot queries, we may in future automatically route these queries over https for lower latency.

Example: Vercel Edge Function with Client

Using Client instead, the example looks like this:

import { Client } from '@neondatabase/serverless';

// don't create a `Pool` or `Client` here, outside the request handler

export default async (req: Request, ctx: any) => {
  // create a `Client` inside the request handler
  const client = new Client(process.env.DATABASE_URL);
  await client.connect();

  // get and validate the `postId` query parameter
  const postId = parseInt(new URL(req.url).searchParams.get('postId'), 10);
  if (isNaN(postId)) return new Response('Bad request', { status: 400 });

  // query and validate the post
  const [post] = await client.query('SELECT * FROM posts WHERE id = $1', [postId]);
  if (!post) return new Response('Not found', { status: 404 });

  // end the `Client` inside the same request handler 
  // (unlike `await`, `ctx.waitUntil` won't hold up the response)
  ctx.waitUntil(client.end());

  // return the post as JSON
  return new Response(JSON.stringify(post), { 
    headers: { 'content-type': 'application/json' }
  });
}

export const config = {
  runtime: 'edge',
  regions: ['iad1'],  // specify the region nearest your Neon DB
};

More examples

These repos show how to use @neondatabase/serverless with a variety of environments and tools:

Bring your own Postgres database

This package comes configured to connect to a Neon database over a secure (wss:) WebSocket. But you can also use it to connect to your own Postgres instances if you run your own WebSocket proxy.

Open-source

This code is released under the MIT license.

Feedback and support

Please visit Neon Community or Support.

More Repositories

1

neon

Neon: Serverless Postgres. We separated storage and compute to offer autoscaling, code-like database branching, and scale to zero.
Rust
13,985
star
2

pg_embedding

Hierarchical Navigable Small World (HNSW) algorithm for vector similarity search in PostgreSQL
C
557
star
3

website

Official docs and website for Neon.
JavaScript
191
star
4

autoscaling

Postgres vertical autoscaling in k8s
Go
149
star
5

yc-idea-matcher

Submit your idea and get a list of similar ideas that YCombinator has invested in in the past.
TypeScript
145
star
6

wsproxy

Go
115
star
7

ask-neon

Chatbot: Search your own knowledge base by semantic similarity
TypeScript
54
star
8

neonctl

Neon CLI tool. The Neon CLI is a command-line interface that lets you manage Neon Serverless Postgres directly from the terminal.
TypeScript
49
star
9

drizzle-overview

Demo Drizzle ORM, Hono & Neon API
TypeScript
49
star
10

postgres-ai-playground

TypeScript
33
star
11

cloudflare-drizzle-neon

Example API using Cloudflare Workers, Drizzle ORM and Neon
TypeScript
33
star
12

helm-charts

neondatabase helm charts
Smarty
32
star
13

psql-describe

psql's \d (describe) family of commands ported to JavaScript
JavaScript
31
star
14

create-branch-action

GitHub Action to create a new Neon branch
30
star
15

preview-branches-with-vercel

Example project that shows how you can create a branch for every preview deployment on Vercel using GitHub actions
TypeScript
27
star
16

serverless-cfworker-demo

Demo app for @neondatabase/serverless — details at https://blog.cloudflare.com/neon-postgres-database-from-workers/
HTML
23
star
17

postgres

PostgreSQL in Neon
C
21
star
18

examples

Examples and code snippets demonstrating common ways of integrating Neon with various frameworks and languages.
TypeScript
19
star
19

postgres-sample-dbs

A collection of sample Postgres databases for learning, testing, and development.
PLpgSQL
18
star
20

naturesnap

TypeScript
18
star
21

neonvm

NeonVM: QEMU-based virtualization API and controller for Kubernetes
Go
17
star
22

preview-branches-with-fly

A Neon branch for every Fly Preview app
TypeScript
16
star
23

ping-thing

Ping a Neon Serverless Postgres database using a Vercel Edge Function to see the journey your request makes.
JavaScript
15
star
24

neon-vercel-kysely

Example use of Neon serverless driver on Vercel Edge Functions with Kysely and kysely-codegen
TypeScript
12
star
25

neon-api-python

a Python client for the Neon API
Python
12
star
26

tokio-epoll-uring

Use io_uring from vanilla tokio.
Rust
9
star
27

semicolons

Take a string with multiple Postgres SQL statements, separated by semicolons, and split it into its constituent statements
TypeScript
8
star
28

delete-branch-action

7
star
29

instant-postgres

TypeScript
7
star
30

neon-postgresql-expert

Input for an OpenAI GPT that can answer questions about Neon database and Postgres
Python
5
star
31

github-automations

Scripts that we use to track issues in github's (beta) projects
TypeScript
5
star
32

pg-import

A CLI tool for importing data from one PostgreSQL database to another.
JavaScript
5
star
33

fastapi-apprunner-neon

Create a serverless API using FastAPI, deployed on AWS App Runner and powered by Neon Postgres
Python
5
star
34

delete-branch-by-name-action

Delete Neon database branch by name
4
star
35

rfcs

4
star
36

neon-branches-visualizer

Visualize your Neon Postgres branches
TypeScript
4
star
37

zenith.tech

JavaScript
3
star
38

neon-vercel-rawsql

Example use of Neon serverless driver on Vercel Edge Functions with raw SQL
TypeScript
3
star
39

aws-cost-reporter

Create and share AWS Cost and Usage reports in Slack.
Go
3
star
40

pg_session_jwt

Postgres Extension for JWT Sessions
Rust
3
star
41

neon-vector-search-openai-notebooks

Jupyter Notebook for Vector Search with Neon and OpenAI
Jupyter Notebook
3
star
42

kube-previews-application

Example project that shows how to create a Neon branch for preview environments deployed on Kubernetes using Argo CD
TypeScript
3
star
43

postgresql_anonymizer

Neon fork of https://gitlab.com/dalibo/postgresql_anonymizer
PLpgSQL
3
star
44

neon_twitter

TypeScript
2
star
45

aversion

Rust
2
star
46

keycloak-example

TypeScript
2
star
47

neon-hyperdrive

Example use of Neon with Hyperdrive on Cloudflare Workers
TypeScript
2
star
48

meeting-notes

2
star
49

mastodon-fly

Dockerfile
2
star
50

regional-latency

A tool that tracks latencies between popular application hosting platforms and nearby Neon Postgres regions.
TypeScript
2
star
51

prisma-vercel-load-test

An app that tests prisma on vercel with vercel postgres
CSS
2
star
52

lambda-cdk-neon

This is an example API built using AWS Lambda, API Gateway, Secrets Manager and Neon
TypeScript
2
star
53

neon-vercel-http

Example use of Neon serverless driver's experimental HTTP feature on Vercel Edge Functions
TypeScript
1
star
54

neon-vercel-knex

Example use of Neon serverless driver on Vercel Edge Functions with Knex.js
JavaScript
1
star
55

dev-actions

JavaScript
1
star
56

vm-monitor

Rust
1
star
57

restore-neon-branch

Script to restore a Neon branch to a previous state while preserving the same endpoint
TypeScript
1
star
58

neon-google-colab-notebooks

Neon Google Colab Notebooks
Jupyter Notebook
1
star
59

guide-neon-prisma

Example application for Neon Prisma Guide
JavaScript
1
star
60

guide-neon-drizzle

Example application for Neon with Drizzle
TypeScript
1
star
61

s3-scrubber

Rust
1
star
62

zenith-coverage-data

1
star
63

.github

Public organization profile
1
star
64

latency-dashboard

TypeScript
1
star
65

neon-vercel-zapatos

Example use of Neon serverless driver on Vercel Edge Functions with Zapatos
TypeScript
1
star
66

rust_wal.experimental

Wrap a database frontend in rust based consensus
Rust
1
star
67

neon-ecto-getting-started-app

Neon Ecto Getting Started
Elixir
1
star
68

kube-previews-manifests

Example manifests used to create preview environments deployed on Kubernetes using Argo CD
Shell
1
star
69

rustls-split

Rust
1
star
70

pgvector

C
1
star
71

devdays2

Neon Developer Days Side Project
JavaScript
1
star
72

docker-images

Docker images that helps build and test Neon product
Dockerfile
1
star
73

branching-demo

Copy your database in milliseconds with Neon
TypeScript
1
star
74

zenith-perf-data

Simple collection of zenith performance test runs
HTML
1
star
75

reset-branch-action

1
star
76

proxy-bench

Benchmarking tools for Neon's Postgres Proxy
Rust
1
star
77

qovery-lifecycle-job

Shell
1
star
78

pg_neon_ai

Rust
1
star
79

guide-neon-next-clerk

How to use Clerk with Neon
TypeScript
1
star
80

guide-neon-next-auth0

How to use Auth0 with Noeon
TypeScript
1
star