Skip to content

Commit

Permalink
feat(db-vercel-postgres): allow to use a local database using pg
Browse files Browse the repository at this point in the history
  • Loading branch information
r1tsuu committed Dec 5, 2024
1 parent 1fc9c47 commit e189979
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
23 changes: 22 additions & 1 deletion packages/db-vercel-postgres/src/connect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Connect } from 'payload'
import { pushDevSchema } from '@payloadcms/drizzle'
import { sql, VercelPool } from '@vercel/postgres'
import { drizzle } from 'drizzle-orm/node-postgres'
import pg from 'pg'

import type { VercelPostgresAdapter } from './types.js'

Expand All @@ -24,10 +25,30 @@ 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 (
!this.disableUsePgForLocalDatabase &&
connectionString &&
['127.0.0.1', 'localhost'].includes(new URL(connectionString).hostname)
) {
client = new pg.Pool(
this.poolOptions ?? {
connectionString,
},
)
} else {
client = this.poolOptions ? new VercelPool(this.poolOptions) : sql
}

// Passed the poolOptions if provided,
// else have vercel/postgres detect the connection string from the environment
this.drizzle = drizzle({
client: this.poolOptions ? new VercelPool(this.poolOptions) : sql,
client,
logger,
schema: this.schema,
})
Expand Down
1 change: 1 addition & 0 deletions packages/db-vercel-postgres/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export function vercelPostgresAdapter(args: Args = {}): DatabaseAdapterObj<Verce
createExtensions,
defaultDrizzleSnapshot,
disableCreateDatabase: args.disableCreateDatabase ?? false,
disableUsePgForLocalDatabase: args.disableUsePgForLocalDatabase ?? false,
drizzle: undefined,
enums: {},
extensions,
Expand Down
8 changes: 8 additions & 0 deletions packages/db-vercel-postgres/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@ export type Args = {
* @default false
*/
disableCreateDatabase?: boolean
/**
* 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
*/
disableUsePgForLocalDatabase?: boolean
extensions?: string[]
idType?: 'serial' | 'uuid'
localesSuffix?: string
Expand Down Expand Up @@ -58,6 +65,7 @@ export type Args = {
}

export type VercelPostgresAdapter = {
disableUsePgForLocalDatabase: boolean
pool?: VercelPool
poolOptions?: Args['pool']
} & BasePostgresAdapter
Expand Down
8 changes: 8 additions & 0 deletions test/generateDatabaseAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ export const allDatabaseAdapters = {
connectionString: process.env.POSTGRES_URL || 'postgres://127.0.0.1:5432/payloadtests',
},
})`,
'postgres-vercel': `
import { vercelPostgresAdapter } from '@payloadcms/db-vercel-postgres'
export const databaseAdapter = vercelPostgresAdapter({
pool: {
connectionString: process.env.POSTGRES_URL || 'postgres://127.0.0.1:5432/payloadtests',
},
})`,
'postgres-custom-schema': `
import { postgresAdapter } from '@payloadcms/db-postgres'
Expand Down

0 comments on commit e189979

Please sign in to comment.