neondatabase/serverless
neondatabase/serverless has 556 GitHub stars and 91 forks. GitHot measured 0 stars in 24 hours. Last checked 2026-11-10 04:24 UTC. Last push 2026-11-05.
Comment ces chiffres sont mesurés
Connect to Neon PostgreSQL from serverless/worker/edge functions
Progression mesurée des étoiles
- in 24h
- 0
- in 7 days
- Mise à jour retardée
- in 15 days
- Mise à jour retardée
- in 1 month
- Mise à jour retardée
Variations nettes sur des périodes qui se chevauchent, pas des totaux quotidiens. Les mesures absentes restent vides. Les étoiles indiquent l’intérêt, pas la qualité.
README.md
Original@neondatabase/serverless
@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. It's published as @neondatabase/serverless on npm. So, for example:
npm install @neondatabase/serverless
Using TypeScript? No worries: types are included.
Note: to install with npm for use by another package that declares a dependency on pg (node-postgres), use an alias plus an override, which will look something like this in your package.json:
"dependencies": {
"pg": "npm:@neondatabase/serverless@^1.2.0"
},
"overrides": {
"pg": "npm:@neondatabase/serverless@^1.2.0"
}
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.
There are more details and options for neon() function.
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:
- 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
};
- 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
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.
transaction()
Multiple queries can be issued via fetch request within a single, non-interactive transaction by using the transaction() function. This is exposed as a property on the query function.
For example:
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL);
const showLatestN = 10;
const [posts, tags] = await sql.transaction([
sql`SELECT * FROM posts ORDER BY posted_at DESC LIMIT ${showLatestN}`,
sql`SELECT * FROM tags`,
]);
There are some additional options when using transaction().
Pool and Client
Use the Pool or Client constructors, instead of the functions described above, when you need:
session or interactive transaction support, and/or
compatibility with node-postgres, which supports query libraries like Kysely or Zapatos.
Queries using Pool and Client are carried by WebSockets. There are two key things to know about this:
In Node.js v21 and earlier and some other environments, there's no built-in WebSocket support. In these cases, supply a WebSocket constructor function.
In serverless environments such as Vercel Edge Functions or Cloudflare Workers, WebSocket connections can't outlive a single request.
That means
PoolorClientobjects 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.Note: on Cloudflare Workers, consider using Cloudflare Hyperdrive instead of this driver.
These points are demonstrated in the examples below.
API
The full API guide to
PoolandClientcan be found in the node-postgres docs.There are a few additional configuration options that apply to
PoolandClienthere.
Using a function as the password connection parameter
As in node-postgres, Pool and Client accept a function (sync or async) for the password connection parameter. This can be useful when connecting to services using short-lived auth tokens.
A password function can be combined with a connection string by making use of the parseIntoClientConfig function (a top-level export here, based on the implementation in pg-connection-string):
import { Pool, parseIntoClientConfig } from '@neondatabase/serverless';
const pool = new Pool({
...parseIntoClientConfig(process.env.DATABASE_URL),
password: () => getDatabaseAuthToken('argument'),
});
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';
// only do this in Node v21 and below
import ws from 'ws';
neonConfig.webSocketConstructor = ws;
const pool = new Pool({ connectionString: process.env.DATABASE_URL });
pool.on('error', (err) => console.error(err)); // deal with e.g. re-connect
// ...
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 shown above (under the heading 'Deploy it') 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 });
try {
// 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 { rows: [post] } = await pool.query('SELECT * FROM posts WHERE id = $1', [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' }
});
} finally {
// end the `Pool` inside the same request handler
// (unlike `await`, `ctx.waitUntil` won't hold up the response)
ctx.waitUntil(pool.end());
}
}
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();
try {
// 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 { rows: [post] } = await client.query('SELECT * FROM posts WHERE id = $1', [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' }
});
} finally {
// end the `Client` inside the same request handler
// (unlike `await`, `ctx.waitUntil` won't hold up the response)
ctx.waitUntil(client.end());
}
}
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:
- Raw SQL + Vercel Edge Functions
- Raw SQL via https + Vercel Edge Functions
- Raw SQL + Cloudflare Workers
- Kysely + Vercel Edge Functions
- Zapatos + Vercel Edge Functions
Bring your own Postgres database
This package comes configured to connect to a Neon database. 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.
Explorer des projets similaires
Asabeneh/30-Days-Of-Python
Le défi de programmation 30 Days of Python est un guide pas à pas pour apprendre le langage de programmation Python en 30 jours. Ce défi peut prendre plus de 100 jours. Avancez à votre rythme. Ces vidéos peuvent aussi vous aider : https://www.youtube.com/channel/UC7PNRuno1rzYPb1xLa4yktw
78,560 étoilesgrafana/grafana
Plateforme ouverte et composable d’observabilité et de visualisation de données. Visualise les métriques, les journaux et les traces issus de plusieurs sources, comme Prometheus, Loki, Elasticsearch, InfluxDB, Postgres et bien d’autres.
77,830 étoilesredis/redis
Pour les développeurs qui créent des applications en temps réel pilotées par les données, Redis est le cache, le serveur de structures de données et le moteur de requêtes documentaires et vectorielles préféré, le plus rapide et le plus riche en fonctionnalités.
77,395 étoilesapache/superset
Apache Superset est une plateforme de visualisation et d'exploration de données.
75,473 étoiles