Netlify-optimized wrapper for @neondatabase/serverless
with automatic environment configuration.
@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)
Automatically uses your Neon database connection via Netlify environment variables:
# Install package
npm install @netlify/neon
# Configure database (via Netlify CLI)
netlify db init
import { neon } from '@netlify/neon'
const sql = neon() // automatically uses env NETLIFY_DATABASE_URL
const [post] = await sql`SELECT * FROM posts WHERE id = ${postId}`
// `post` is now { id: 12, title: 'My post', ... } (or undefined)
// ./src/db/index.ts
import { neon } from '@netlify/neon'
import { drizzle } from 'drizzle-orm/neon-http'
import { posts } from "./schema"
export const db = drizzle({
schema,
client: neon() // Uses NETLIFY_DATABASE_URL automatically
})
// ./src/db/schema.ts
import { integer, pgTable, varchar, text } from 'drizzle-orm/pg-core'
export const postsTable = pgTable('posts', {
id: integer().primaryKey().generatedAlwaysAsIdentity(),
title: varchar({ length: 255 }).notNull(),
content: text().notNull().default('')
})
import { db } from "./db"
import { postsTable } from "./db/schema"
const posts = await db.select().from(postsTable)
Read more about using Drizzle-ORM with Neon: Get started
The neon
function imported from @netlify/neon
also supports all of the same HTTP options as @neondatabase/serverless
.
import { neon } from '@netlify/neon'
// automatically using connection string from env NETLIFY_DATABASE_URL
const sql = neon({
arrayMode: true,
fetchOptions: { priority: 'high' }
})
// or when explicitly passing in a connection string override
const sql = neon("postgres://user:pass@host/db",{
arrayMode: true,
fetchOptions: { priority: 'high' }
})
Supports all Neon transaction options:
import { neon } from '@netlify/neon'
const sql = neon() // automatically uses env NETLIFY_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`],
{
isolationLevel: 'RepeatableRead',
readOnly: true,
}
)