Skip to content

Commit b6f6f07

Browse files
committed
feat(db/postgres): add connection string option
1 parent b41b4b2 commit b6f6f07

File tree

1 file changed

+27
-21
lines changed

1 file changed

+27
-21
lines changed

lib/db/drivers/postgres.ts

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,34 @@ import { z } from 'zod'
55
import { defineDatabaseDriver } from '~/lib/db/defineDatabaseDriver'
66

77
export const postgresDriver = defineDatabaseDriver({
8-
envSchema: z.object({
9-
DB_POSTGRES_DATABASE: z.string(),
10-
DB_POSTGRES_HOST: z.string(),
11-
DB_POSTGRES_USER: z.string(),
12-
DB_POSTGRES_PASSWORD: z.string(),
13-
DB_POSTGRES_PORT: z.coerce.number().int(),
14-
}),
15-
async setup({
16-
DB_POSTGRES_DATABASE,
17-
DB_POSTGRES_HOST,
18-
DB_POSTGRES_PASSWORD,
19-
DB_POSTGRES_PORT,
20-
DB_POSTGRES_USER,
21-
}) {
22-
const pool = new pg.Pool({
23-
database: DB_POSTGRES_DATABASE,
24-
host: DB_POSTGRES_HOST,
25-
password: DB_POSTGRES_PASSWORD,
26-
port: DB_POSTGRES_PORT,
27-
user: DB_POSTGRES_USER,
28-
max: 10,
8+
envSchema: z
9+
.object({
10+
DB_POSTGRES_DATABASE: z.string(),
11+
DB_POSTGRES_HOST: z.string(),
12+
DB_POSTGRES_USER: z.string(),
13+
DB_POSTGRES_PASSWORD: z.string(),
14+
DB_POSTGRES_PORT: z.coerce.number().int(),
2915
})
16+
.or(
17+
z.object({
18+
DB_POSTGRES_URL: z.string(),
19+
}),
20+
),
21+
async setup(options) {
22+
const pool = new pg.Pool(
23+
'DB_POSTGRES_URL' in options
24+
? {
25+
connectionString: options.DB_POSTGRES_URL,
26+
}
27+
: {
28+
database: options.DB_POSTGRES_DATABASE,
29+
host: options.DB_POSTGRES_HOST,
30+
password: options.DB_POSTGRES_PASSWORD,
31+
port: options.DB_POSTGRES_PORT,
32+
user: options.DB_POSTGRES_USER,
33+
max: 10,
34+
},
35+
)
3036
await pool.connect()
3137
return new PostgresDialect({
3238
pool,

0 commit comments

Comments
 (0)