npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@drizzle-adapter/neon-http

v1.0.5

Published

Neon serverless PostgreSQL adapter implementation for the Drizzle Adapter ecosystem.

Downloads

386

Readme

@drizzle-adapter/neon-http

Neon serverless PostgreSQL adapter implementation for the Drizzle Adapter ecosystem.

Overview

The @drizzle-adapter/neon-http package provides the Neon implementation for the Drizzle Adapter interface. While you don't interact with this package directly (you use @drizzle-adapter/core instead), it enables support for Neon's serverless PostgreSQL in the Drizzle Adapter ecosystem.

Why Neon?

Neon offers unique advantages for modern applications:

  • HTTP-based PostgreSQL: Direct database access over HTTP, perfect for serverless and edge environments
  • Serverless PostgreSQL: True serverless with instant scaling to zero
  • Database Branching: Create instant database branches for development and testing
  • Auto-scaling: Scales compute and storage independently
  • Bottomless Storage: Separates storage from compute for unlimited capacity
  • Edge Computing: Low-latency access through compute placement
  • Cost Efficiency: Pay only for actual compute and storage usage
  • PostgreSQL Compatibility: 100% PostgreSQL compatible with latest features
  • Point-in-Time Recovery: Built-in backup and restore capabilities

Perfect For:

  1. Serverless Applications: Zero cold starts with instant scaling
  2. Development Workflows: Instant database branching for testing and staging
  3. Edge Applications: Low-latency data access worldwide
  4. Cost-Optimized Workloads: Pay only for what you use
  5. Modern Development: Perfect for JAMstack and serverless architectures

Installation

# Install both the core package and the Neon adapter
pnpm install @drizzle-adapter/core @drizzle-adapter/neon-http

Important: Adapter Registration

For the adapter to work correctly with the DrizzleAdapterFactory, you must import it for its self-registration side effects:

// Import for side effects - adapter will self-register
import '@drizzle-adapter/neon-http';

// Now you can use the factory
import { DrizzleAdapterFactory } from '@drizzle-adapter/core';

Usage

Configuration

import { DrizzleAdapterFactory, TypeDrizzleDatabaseConfig } from '@drizzle-adapter/core';

const config: TypeDrizzleDatabaseConfig = {
  DATABASE_DRIVER: 'neon-http',
  DATABASE_URL: 'postgres://user:[email protected]/neondb'
};

const factory = new DrizzleAdapterFactory();
const adapter = factory.create(config);

Important Note About Connections

While Neon operates over HTTP and doesn't require traditional database connections, this adapter implements the standard connection interface for compatibility. This means your code remains portable across different database adapters:

// These connection operations are no-ops for Neon HTTP but ensure
// your code remains portable if you switch to another database
const connection = await adapter.getConnection();

const client = connection.getClient();

try {
  // Your database operations
} finally {
  await connection.disconnect(); // No-op for Neon HTTP
}

This design allows you to switch between Neon and other databases without changing your application code.

Schema Definition

const dataTypes = adapter.getDataTypes();

const users = dataTypes.dbTable('users', {
  id: dataTypes.dbUuid('id').primaryKey().defaultRandom(),
  name: dataTypes.dbText('name').notNull(),
  email: dataTypes.dbText('email').notNull().unique(),
  profile: dataTypes.dbJsonb('profile'),
  searchVector: dataTypes.dbTsVector('search_vector'),
  createdAt: dataTypes.dbTimestampTz('created_at').defaultNow()
});

const posts = dataTypes.dbTable('posts', {
  id: dataTypes.dbUuid('id').primaryKey().defaultRandom(),
  userId: dataTypes.dbUuid('user_id')
    .references(() => users.id),
  title: dataTypes.dbText('title').notNull(),
  content: dataTypes.dbText('content').notNull(),
  tags: dataTypes.dbArray('tags', { type: 'text' }),
  metadata: dataTypes.dbJsonb('metadata'),
  published: dataTypes.dbBoolean('published').default(false),
  createdAt: dataTypes.dbTimestampTz('created_at').defaultNow()
});

Basic CRUD Operations

import { eq, and, or, desc, sql } from 'drizzle-orm';

const client = await adapter.getConnection().getClient();

// INSERT
// Single insert with returning
const [newUser] = await client
  .insert(users)
  .values({
    name: 'John Doe',
    email: '[email protected]',
    profile: { bio: 'Hello!' }
  })
  .returning();

// Bulk insert
await client
  .insert(posts)
  .values([
    {
      userId: newUser.id,
      title: 'First Post',
      content: 'Hello, world!',
      tags: ['hello', 'first']
    },
    {
      userId: newUser.id,
      title: 'Second Post',
      content: 'Another post',
      tags: ['second']
    }
  ]);

// SELECT
// Select all
const allUsers = await client
  .select()
  .from(users);

// Select with conditions
const user = await client
  .select()
  .from(users)
  .where(eq(users.email, '[email protected]'));

// Select with join and array operations
const postsWithTags = await client
  .select({
    userName: users.name,
    postTitle: posts.title,
    tags: posts.tags
  })
  .from(posts)
  .leftJoin(users, eq(posts.userId, users.id))
  .where(sql`${posts.tags} && ARRAY['hello']::text[]`)
  .orderBy(desc(posts.createdAt));

// UPDATE
// Update with JSONB operations
await client
  .update(users)
  .set({
    profile: sql`${users.profile} || '{"title": "New Title"}'::jsonb`
  })
  .where(eq(users.id, newUser.id));

// DELETE
await client
  .delete(posts)
  .where(
    and(
      eq(posts.userId, newUser.id),
      eq(posts.published, false)
    )
  );

Neon-Specific Features

Database Branching

Perfect for development and testing workflows:

// Development branch
const devAdapter = factory.create({
  DATABASE_DRIVER: 'neon-http',
  DATABASE_URL: 'postgres://user:[email protected]/neondb'
});

// Staging branch
const stagingAdapter = factory.create({
  DATABASE_DRIVER: 'neon-http',
  DATABASE_URL: 'postgres://user:[email protected]/neondb'
});

// Production branch
const prodAdapter = factory.create({
  DATABASE_DRIVER: 'neon-http',
  DATABASE_URL: 'postgres://user:[email protected]/neondb'
});

HTTP Optimization

const client = await adapter.getConnection().getClient();

// Queries are automatically optimized for HTTP
const result = await client
  .select()
  .from(users)
  .where(eq(users.id, userId));

Best Practices

  1. Query Optimization: Minimize the number of queries to reduce HTTP requests
  2. Database Branching: Use branches for development, testing, and staging environments
  3. Edge Computing: Deploy close to your users for lower latency
  4. Error Handling: Implement proper retry logic for network issues
  5. Portability: Use the connection interface for database portability

Contributing

We welcome contributions! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.

Related Packages