Skip to content

Commit 4ca6d6e

Browse files
committed
fix(db-vercel-postgres): migrate from deprecated @vercel/postgres to @neondatabase/serverless
- `@vercel/postgres` has been deprecated. All Vercel Postgres stores have been migrated to Neon's native integration. The package is no longer maintained and its `VercelPool` (which extends `@neondatabase/serverless`'s `Pool`, not `pg`'s `Pool`) is incompatible with `drizzle-orm@0.45.2`'s stricter `NodePgClient` type.
1 parent 623e63f commit 4ca6d6e

2 files changed

Lines changed: 27 additions & 32 deletions

File tree

packages/db-vercel-postgres/src/connect.ts

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
1+
import type { DrizzleAdapter } from '@payloadcms/drizzle'
22
import type { Connect, Migration } from 'payload'
33

4+
import { Pool as NeonPool } from '@neondatabase/serverless'
45
import { pushDevSchema } from '@payloadcms/drizzle'
5-
import { sql, VercelPool } from '@vercel/postgres'
6-
import { drizzle } from 'drizzle-orm/node-postgres'
6+
import { drizzle as drizzleNeon } from 'drizzle-orm/neon-serverless'
7+
import { drizzle as drizzlePg } from 'drizzle-orm/node-postgres'
78
import { withReplicas } from 'drizzle-orm/pg-core'
89
import pg from 'pg'
910

@@ -20,42 +21,39 @@ export const connect: Connect = async function connect(
2021
try {
2122
const logger = this.logger || false
2223

23-
let client: pg.Pool | VercelPool
24-
2524
const connectionString = this.poolOptions?.connectionString ?? process.env.POSTGRES_URL
2625

2726
// Use non-vercel postgres for local database
28-
if (
27+
const useLocalPg =
2928
!this.forceUseVercelPostgres &&
3029
connectionString &&
3130
['127.0.0.1', 'localhost'].includes(new URL(connectionString).hostname)
32-
) {
33-
client = new pg.Pool(
31+
32+
if (useLocalPg) {
33+
const client = new pg.Pool(
3434
this.poolOptions ?? {
3535
connectionString,
3636
},
3737
)
38+
this.drizzle = drizzlePg({ client, logger, schema: this.schema })
3839
} else {
39-
client = this.poolOptions ? new VercelPool(this.poolOptions) : sql
40+
const client = new NeonPool(this.poolOptions ?? { connectionString })
41+
this.drizzle = drizzleNeon({ client, logger, schema: this.schema })
4042
}
4143

42-
// Passed the poolOptions if provided,
43-
// else have vercel/postgres detect the connection string from the environment
44-
this.drizzle = drizzle({
45-
client,
46-
logger,
47-
schema: this.schema,
48-
})
49-
5044
if (this.readReplicaOptions) {
5145
this.primaryDrizzle = this.drizzle as any
5246
const readReplicas = this.readReplicaOptions.map((connectionString) => {
5347
const options = {
5448
...this.poolOptions,
5549
connectionString,
5650
}
57-
const pool = new VercelPool(options)
58-
return drizzle({ client: pool, logger, schema: this.schema })
51+
if (useLocalPg) {
52+
const pool = new pg.Pool(options)
53+
return drizzlePg({ client: pool, logger, schema: this.schema })
54+
}
55+
const pool = new NeonPool(options)
56+
return drizzleNeon({ client: pool, logger, schema: this.schema })
5957
})
6058
const myReplicas = withReplicas(this.drizzle, readReplicas as any)
6159
this.drizzle = myReplicas

packages/db-vercel-postgres/src/types.ts

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1+
import type { Pool, PoolConfig } from '@neondatabase/serverless'
12
import type {
23
BasePostgresAdapter,
34
GenericEnum,
45
MigrateDownArgs,
56
MigrateUpArgs,
6-
PostgresDB,
77
PostgresSchemaHook,
88
} from '@payloadcms/drizzle/postgres'
99
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
10-
import type { VercelPool, VercelPostgresPoolConfig } from '@vercel/postgres'
1110
import type { DrizzleConfig } from 'drizzle-orm'
12-
import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
11+
import type { NeonDatabase } from 'drizzle-orm/neon-serverless'
1312
import type { PgSchema, PgTableFn, PgTransactionConfig } from 'drizzle-orm/pg-core'
1413

1514
export type Args = {
@@ -45,10 +44,8 @@ export type Args = {
4544
disableCreateDatabase?: boolean
4645
extensions?: string[]
4746
/**
48-
* By default, we connect to a local database using the `pg` module instead of `@vercel/postgres`.
49-
* This is because `@vercel/postgres` doesn't work with local databases.
50-
* If you still want to use `@vercel/postgres` even locally you can pass `true` here
51-
* and you'd to spin up the database with a special Neon's Docker Compose setup - https://vercel.com/docs/storage/vercel-postgres/local-development#option-2:-local-postgres-instance-with-docker
47+
* By default, we connect to a local database using the `pg` module instead of `@neondatabase/serverless`.
48+
* If you still want to use `@neondatabase/serverless` even locally you can pass `true` here.
5249
*/
5350
forceUseVercelPostgres?: boolean
5451
/** Generated schema from payload generate:db-schema file path */
@@ -58,10 +55,10 @@ export type Args = {
5855
logger?: DrizzleConfig['logger']
5956
migrationDir?: string
6057
/**
61-
* Optional pool configuration for Vercel Postgres
62-
* If not provided, vercel/postgres will attempt to use the Vercel environment variables
58+
* Optional pool configuration
59+
* If not provided, will attempt to use the Vercel/Neon environment variables
6360
*/
64-
pool?: VercelPostgresPoolConfig
61+
pool?: PoolConfig
6562
prodMigrations?: {
6663
down: (args: MigrateDownArgs) => Promise<void>
6764
name: string
@@ -96,12 +93,12 @@ type ResolveSchemaType<T> = 'schema' extends keyof T
9693
? T['schema']
9794
: GeneratedDatabaseSchema['schemaUntyped']
9895

99-
type Drizzle = NodePgDatabase<ResolveSchemaType<GeneratedDatabaseSchema>>
96+
type Drizzle = NeonDatabase<ResolveSchemaType<GeneratedDatabaseSchema>>
10097

10198
export type VercelPostgresAdapter = {
10299
drizzle: Drizzle
103100
forceUseVercelPostgres?: boolean
104-
pool?: VercelPool
101+
pool?: Pool
105102
poolOptions?: Args['pool']
106103
} & BasePostgresAdapter
107104

@@ -126,7 +123,7 @@ declare module 'payload' {
126123
localesSuffix?: string
127124
logger: DrizzleConfig['logger']
128125
pgSchema?: { table: PgTableFn } | PgSchema
129-
pool: VercelPool
126+
pool: Pool
130127
poolOptions: Args['pool']
131128
prodMigrations?: {
132129
down: (args: MigrateDownArgs) => Promise<void>

0 commit comments

Comments
 (0)