Skip to content

Commit 58bfd66

Browse files
committed
revert(db-vercel-postgres): roll back @neondatabase/serverless migration
1 parent 4ca6d6e commit 58bfd66

4 files changed

Lines changed: 44 additions & 51 deletions

File tree

packages/db-vercel-postgres/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@
7474
"renamePredefinedMigrations": "node --no-deprecation --import @swc-node/register/esm-register ./scripts/renamePredefinedMigrations.ts"
7575
},
7676
"dependencies": {
77-
"@neondatabase/serverless": "^1.0.2",
7877
"@payloadcms/drizzle": "workspace:*",
78+
"@vercel/postgres": "^0.10.0",
7979
"console-table-printer": "2.12.1",
8080
"drizzle-kit": "0.31.7",
8181
"drizzle-orm": "0.45.2",

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

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

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

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

23+
let client: pg.Pool | VercelPool
24+
2425
const connectionString = this.poolOptions?.connectionString ?? process.env.POSTGRES_URL
2526

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

42+
// Passed the poolOptions if provided,
43+
// else have vercel/postgres detect the connection string from the environment
44+
this.drizzle = drizzle({
45+
client: client as pg.Pool,
46+
logger,
47+
schema: this.schema,
48+
})
49+
4450
if (this.readReplicaOptions) {
4551
this.primaryDrizzle = this.drizzle as any
4652
const readReplicas = this.readReplicaOptions.map((connectionString) => {
4753
const options = {
4854
...this.poolOptions,
4955
connectionString,
5056
}
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 })
57+
const pool = new VercelPool(options)
58+
return drizzle({ client: pool as unknown as pg.Pool, logger, schema: this.schema })
5759
})
5860
const myReplicas = withReplicas(this.drizzle, readReplicas as any)
5961
this.drizzle = myReplicas

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import type { Pool, PoolConfig } from '@neondatabase/serverless'
21
import type {
32
BasePostgresAdapter,
43
GenericEnum,
54
MigrateDownArgs,
65
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'
1011
import type { DrizzleConfig } from 'drizzle-orm'
11-
import type { NeonDatabase } from 'drizzle-orm/neon-serverless'
12+
import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
1213
import type { PgSchema, PgTableFn, PgTransactionConfig } from 'drizzle-orm/pg-core'
1314

1415
export type Args = {
@@ -44,8 +45,10 @@ export type Args = {
4445
disableCreateDatabase?: boolean
4546
extensions?: string[]
4647
/**
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.
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
4952
*/
5053
forceUseVercelPostgres?: boolean
5154
/** Generated schema from payload generate:db-schema file path */
@@ -55,10 +58,10 @@ export type Args = {
5558
logger?: DrizzleConfig['logger']
5659
migrationDir?: string
5760
/**
58-
* Optional pool configuration
59-
* If not provided, will attempt to use the Vercel/Neon environment variables
61+
* Optional pool configuration for Vercel Postgres
62+
* If not provided, vercel/postgres will attempt to use the Vercel environment variables
6063
*/
61-
pool?: PoolConfig
64+
pool?: VercelPostgresPoolConfig
6265
prodMigrations?: {
6366
down: (args: MigrateDownArgs) => Promise<void>
6467
name: string
@@ -93,12 +96,12 @@ type ResolveSchemaType<T> = 'schema' extends keyof T
9396
? T['schema']
9497
: GeneratedDatabaseSchema['schemaUntyped']
9598

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

98101
export type VercelPostgresAdapter = {
99102
drizzle: Drizzle
100103
forceUseVercelPostgres?: boolean
101-
pool?: Pool
104+
pool?: VercelPool
102105
poolOptions?: Args['pool']
103106
} & BasePostgresAdapter
104107

@@ -123,7 +126,7 @@ declare module 'payload' {
123126
localesSuffix?: string
124127
logger: DrizzleConfig['logger']
125128
pgSchema?: { table: PgTableFn } | PgSchema
126-
pool: Pool
129+
pool: VercelPool
127130
poolOptions: Args['pool']
128131
prodMigrations?: {
129132
down: (args: MigrateDownArgs) => Promise<void>

pnpm-lock.yaml

Lines changed: 11 additions & 23 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)