@netlify/neon
TypeScript icon, indicating that this package has built-in type declarations

0.1.0 • Public • Published

@netlify/neon

npm version License

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)

Netlify Integration

Automatically uses your Neon database connection via Netlify environment variables:

# Install package
npm install @netlify/neon

# Configure database (via Netlify CLI)
netlify db init

Basic Usage

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)

Usage with Drizzle-ORM

// ./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('')
})

Querying with Drizzle

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

With additional options

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' }
})

Transactions

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,
  }
)

Documentation

📚 Neon Serverless Driver Docs
📚 Drizzle with Neon Postgres

Readme

Keywords

none

Package Sidebar

Install

npm i @netlify/neon

Weekly Downloads

396

Version

0.1.0

License

ISC

Unpacked Size

8.91 kB

Total Files

5

Last publish

Collaborators

  • seanroberts
  • biilmann
  • eduardoboucas
  • netlify-bot
  • sarahetter
  • mikewen
  • kathmbeck
  • hrishikeshk
  • vitaliyr
  • denar90
  • smnh
  • berdav
  • youvalv
  • serhalp-netlify
  • rmulligan-netlify
  • akardet