neon-serverless

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Neon Serverless Skill

Neon Serverless Skill

Configures the Neon Serverless Driver for optimal performance in serverless and edge computing environments.
为无服务器和边缘计算环境配置Neon Serverless Driver以实现最佳性能。

When to Use

适用场景

  • Setting up connections for edge functions (Vercel Edge, Cloudflare Workers)
  • Configuring serverless APIs (AWS Lambda, Google Cloud Functions)
  • Optimizing for low-latency database access
  • Implementing connection pooling for high-throughput apps
Not recommended for: Complex multi-statement transactions (use WebSocket Pool), persistent servers (use native PostgreSQL drivers), or offline-first applications.
  • 为边缘函数(Vercel Edge、Cloudflare Workers)设置连接
  • 配置无服务器API(AWS Lambda、Google Cloud Functions)
  • 优化低延迟数据库访问
  • 为高吞吐量应用实现连接池
不推荐用于: 复杂多语句事务(使用WebSocket Pool)、持久化服务器(使用原生PostgreSQL驱动)或离线优先应用。

Code Generation Rules

代码生成规则

When generating TypeScript/JavaScript code:
  • BEFORE generating import statements, check tsconfig.json for path aliases (compilerOptions.paths)
  • If path aliases exist (e.g., "@/": ["./src/"]), use them (e.g., import { x } from '@/lib/utils')
  • If NO path aliases exist or unsure, ALWAYS use relative imports (e.g., import { x } from '../../../lib/utils')
  • Verify imports match the project's configuration
  • Default to relative imports - they always work regardless of configuration
生成TypeScript/JavaScript代码时:
  • 在生成导入语句之前,检查tsconfig.json中的路径别名(compilerOptions.paths)
  • 如果存在路径别名(例如"@/": ["./src/"]),则使用它们(例如import { x } from '@/lib/utils')
  • 如果不存在路径别名或不确定,始终使用相对导入(例如import { x } from '../../../lib/utils')
  • 验证导入是否与项目配置匹配
  • 默认使用相对导入——无论配置如何,它们始终有效

Reference Documentation

参考文档

Primary Resource: See
[neon-serverless.mdc](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/neon-serverless.mdc)
in project root for comprehensive guidelines including:
  • Installation and compatibility requirements
  • HTTP vs WebSocket adapter selection
  • Connection pooling strategies
  • Query optimization patterns
  • Error handling and troubleshooting
主要资源: 请查看项目根目录中的
[neon-serverless.mdc](https://raw.githubusercontent.com/neondatabase-labs/ai-rules/main/neon-serverless.mdc)
,获取包含以下内容的全面指南:
  • 安装和兼容性要求
  • HTTP与WebSocket适配器选择
  • 连接池策略
  • 查询优化模式
  • 错误处理与故障排除

Quick Setup

快速设置

Installation

安装

bash
npm install @neondatabase/serverless
bash
npm install @neondatabase/serverless

Connection Patterns

连接模式

HTTP Client (recommended for edge/serverless):
typescript
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL!);
const rows = await sql`SELECT * FROM users WHERE id = ${userId}`;
WebSocket Pool (for Node.js long-lived connections):
typescript
import { Pool } from '@neondatabase/serverless';
const pool = new Pool({ connectionString: process.env.DATABASE_URL! });
const result = await pool.query('SELECT * FROM users WHERE id = $1', [userId]);
See
templates/
for complete examples:
  • templates/http-connection.ts
    - HTTP client setup
  • templates/websocket-pool.ts
    - WebSocket pool configuration
HTTP客户端(推荐用于边缘/无服务器环境):
typescript
import { neon } from '@neondatabase/serverless';
const sql = neon(process.env.DATABASE_URL!);
const rows = await sql`SELECT * FROM users WHERE id = ${userId}`;
WebSocket连接池(适用于Node.js长连接):
typescript
import { Pool } from '@neondatabase/serverless';
const pool = new Pool({ connectionString: process.env.DATABASE_URL! });
const result = await pool.query('SELECT * FROM users WHERE id = $1', [userId]);
查看
templates/
获取完整示例:
  • templates/http-connection.ts
    - HTTP客户端设置
  • templates/websocket-pool.ts
    - WebSocket连接池配置

Validation

验证

Use
scripts/validate-connection.ts
to test your database connection before deployment.
部署前,使用
scripts/validate-connection.ts
测试数据库连接。

Related Skills

相关技能

  • neon-auth - Add authentication
  • neon-js - Full SDK with auth + data API
  • neon-drizzle - For ORM with serverless connections
  • neon-toolkit - For ephemeral database testing

Want best practices in your project? Run
neon-plugin:add-neon-docs
with parameter
SKILL_NAME="neon-serverless"
to add reference links.
  • neon-auth - 添加认证功能
  • neon-js - 包含认证+数据API的完整SDK
  • neon-drizzle - 用于无服务器连接的ORM
  • neon-toolkit - 用于临时数据库测试

希望在项目中加入最佳实践? 运行
neon-plugin:add-neon-docs
并传入参数
SKILL_NAME="neon-serverless"
以添加参考链接。