Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/db-vercel-postgres/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@
"renamePredefinedMigrations": "node --no-deprecation --import @swc-node/register/esm-register ./scripts/renamePredefinedMigrations.ts"
},
"dependencies": {
"@neondatabase/serverless": "^1.0.2",
"@payloadcms/drizzle": "workspace:*",
"@vercel/postgres": "^0.10.0",
"console-table-printer": "2.12.1",
"drizzle-kit": "0.31.7",
"drizzle-orm": "0.45.2",
Expand Down
36 changes: 17 additions & 19 deletions packages/db-vercel-postgres/src/connect.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
import type { DrizzleAdapter } from '@payloadcms/drizzle'
import type { Connect, Migration } from 'payload'

import { Pool as NeonPool } from '@neondatabase/serverless'
import { pushDevSchema } from '@payloadcms/drizzle'
import { sql, VercelPool } from '@vercel/postgres'
import { drizzle } from 'drizzle-orm/node-postgres'
import { drizzle as drizzleNeon } from 'drizzle-orm/neon-serverless'
import { drizzle as drizzlePg } from 'drizzle-orm/node-postgres'
import { withReplicas } from 'drizzle-orm/pg-core'
import pg from 'pg'

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

let client: pg.Pool | VercelPool

const connectionString = this.poolOptions?.connectionString ?? process.env.POSTGRES_URL

// Use non-vercel postgres for local database
if (
const useLocalPg =
!this.forceUseVercelPostgres &&
connectionString &&
['127.0.0.1', 'localhost'].includes(new URL(connectionString).hostname)
) {
client = new pg.Pool(

if (useLocalPg) {
const client = new pg.Pool(
this.poolOptions ?? {
connectionString,
},
)
this.drizzle = drizzlePg({ client, logger, schema: this.schema })
} else {
client = this.poolOptions ? new VercelPool(this.poolOptions) : sql
const client = new NeonPool(this.poolOptions ?? { connectionString })
this.drizzle = drizzleNeon({ client, logger, schema: this.schema })
}

// Passed the poolOptions if provided,
// else have vercel/postgres detect the connection string from the environment
this.drizzle = drizzle({
client: client as pg.Pool,
logger,
schema: this.schema,
})

if (this.readReplicaOptions) {
this.primaryDrizzle = this.drizzle as any
const readReplicas = this.readReplicaOptions.map((connectionString) => {
const options = {
...this.poolOptions,
connectionString,
}
const pool = new VercelPool(options)
return drizzle({ client: pool as unknown as pg.Pool, logger, schema: this.schema })
if (useLocalPg) {
const pool = new pg.Pool(options)
return drizzlePg({ client: pool, logger, schema: this.schema })
}
const pool = new NeonPool(options)
return drizzleNeon({ client: pool, logger, schema: this.schema })
})
const myReplicas = withReplicas(this.drizzle, readReplicas as any)
this.drizzle = myReplicas
Expand Down
23 changes: 10 additions & 13 deletions packages/db-vercel-postgres/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { Pool, PoolConfig } from '@neondatabase/serverless'
import type {
BasePostgresAdapter,
GenericEnum,
MigrateDownArgs,
MigrateUpArgs,
PostgresDB,
PostgresSchemaHook,
} from '@payloadcms/drizzle/postgres'
import type { DrizzleAdapter } from '@payloadcms/drizzle/types'
import type { VercelPool, VercelPostgresPoolConfig } from '@vercel/postgres'
import type { DrizzleConfig } from 'drizzle-orm'
import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
import type { NeonDatabase } from 'drizzle-orm/neon-serverless'
import type { PgSchema, PgTableFn, PgTransactionConfig } from 'drizzle-orm/pg-core'

export type Args = {
Expand Down Expand Up @@ -45,10 +44,8 @@ export type Args = {
disableCreateDatabase?: boolean
extensions?: string[]
/**
* By default, we connect to a local database using the `pg` module instead of `@vercel/postgres`.
* This is because `@vercel/postgres` doesn't work with local databases.
* If you still want to use `@vercel/postgres` even locally you can pass `true` here
* 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
* By default, we connect to a local database using the `pg` module instead of `@neondatabase/serverless`.
* If you still want to use `@neondatabase/serverless` even locally you can pass `true` here.
*/
forceUseVercelPostgres?: boolean
/** Generated schema from payload generate:db-schema file path */
Expand All @@ -58,10 +55,10 @@ export type Args = {
logger?: DrizzleConfig['logger']
migrationDir?: string
/**
* Optional pool configuration for Vercel Postgres
* If not provided, vercel/postgres will attempt to use the Vercel environment variables
* Optional pool configuration
* If not provided, will attempt to use the Vercel/Neon environment variables
*/
pool?: VercelPostgresPoolConfig
pool?: PoolConfig
prodMigrations?: {
down: (args: MigrateDownArgs) => Promise<void>
name: string
Expand Down Expand Up @@ -96,12 +93,12 @@ type ResolveSchemaType<T> = 'schema' extends keyof T
? T['schema']
: GeneratedDatabaseSchema['schemaUntyped']

type Drizzle = NodePgDatabase<ResolveSchemaType<GeneratedDatabaseSchema>>
type Drizzle = NeonDatabase<ResolveSchemaType<GeneratedDatabaseSchema>>

export type VercelPostgresAdapter = {
drizzle: Drizzle
forceUseVercelPostgres?: boolean
pool?: VercelPool
pool?: Pool
poolOptions?: Args['pool']
} & BasePostgresAdapter

Expand All @@ -126,7 +123,7 @@ declare module 'payload' {
localesSuffix?: string
logger: DrizzleConfig['logger']
pgSchema?: { table: PgTableFn } | PgSchema
pool: VercelPool
pool: Pool
poolOptions: Args['pool']
prodMigrations?: {
down: (args: MigrateDownArgs) => Promise<void>
Expand Down
62 changes: 43 additions & 19 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading