Skip to content

Commit 57f30fc

Browse files
RihanArfanatinux
andauthored
chore(database): switch to postgres-js (#675)
Co-authored-by: Sébastien Chopin <[email protected]>
1 parent 3f57348 commit 57f30fc

File tree

6 files changed

+21
-21
lines changed

6 files changed

+21
-21
lines changed

docs/content/docs/2.features/database.md

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ Install Drizzle ORM, Drizzle Kit, and the appropriate driver(s) for the database
1616

1717
::tabs{sync="database-dialect"}
1818
:::tabs-item{label="PostgreSQL" icon="i-simple-icons-postgresql"}
19-
:pm-install{name="drizzle-orm drizzle-kit pg @electric-sql/pglite"}
19+
:pm-install{name="drizzle-orm drizzle-kit postgres @electric-sql/pglite"}
2020
::callout
2121
NuxtHub automatically detects your database connection using environment variables:
2222
- Uses `PGlite` (embedded PostgreSQL) if no environment variables are set.
23-
- Uses `node-postgres` driver if you set `DATABASE_URL`, `POSTGRES_URL`, or `POSTGRESQL_URL` environment variable.
23+
- Uses `postgres-js` driver if you set `DATABASE_URL`, `POSTGRES_URL`, or `POSTGRESQL_URL` environment variable.
2424
::
2525
:::
2626
:::tabs-item{label="MySQL" icon="i-simple-icons-mysql"}
@@ -257,7 +257,7 @@ import { db, schema } from 'hub:database'
257257

258258
export default eventHandler(async (event) => {
259259
const { id } = getRouterParams(event)
260-
260+
261261
const deletedUser = await db
262262
.delete(schema.users)
263263
.where(eq(schema.users.id, Number(id)))
@@ -449,7 +449,7 @@ export default defineTask({
449449
},
450450
async run() {
451451
console.log('Seeding database...')
452-
452+
453453
const users = [
454454
{
455455
name: 'John Doe',
@@ -466,9 +466,9 @@ export default defineTask({
466466
createdAt: new Date()
467467
}
468468
]
469-
469+
470470
await useDrizzle().insert(tables.users).values(users)
471-
471+
472472
return { result: 'Database seeded successfully' }
473473
}
474474
})
@@ -499,7 +499,7 @@ export default defineNuxtConfig({
499499
hub: {
500500
database: {
501501
dialect: 'postgresql',
502-
driver: 'node-postgres', // Optional: explicitly choose driver
502+
driver: 'postgres-js', // Optional: explicitly choose driver
503503
connection: {
504504
connectionString: process.env.DATABASE_URL
505505
}

src/features/database.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export async function resolveDatabaseConfig(nuxt: Nuxt, hub: HubConfig): Promise
5353
case 'postgresql': {
5454
config.connection = defu(config.connection, { url: process.env.DATABASE_URL || process.env.POSTGRES_URL || process.env.POSTGRESQL_URL || '' })
5555
if (config.connection.url) {
56-
config.driver ||= 'node-postgres'
56+
config.driver ||= 'postgres-js'
5757
break
5858
}
5959
// Local PGLite
@@ -93,7 +93,7 @@ export async function setupDatabase(nuxt: Nuxt, hub: HubConfig, deps: Record<str
9393
if (!deps['drizzle-orm'] || !deps['drizzle-kit']) {
9494
logWhenReady(nuxt, 'Please run `npx nypm i drizzle-orm drizzle-kit` to properly setup Drizzle ORM with NuxtHub.', 'error')
9595
}
96-
if (driver === 'node-postgres' && !deps.pg) {
96+
if (driver === 'postgres-js' && !deps.pg) {
9797
logWhenReady(nuxt, 'Please run `npx nypm i pg` to use PostgreSQL as database.', 'error')
9898
} else if (driver === 'pglite' && !deps['@electric-sql/pglite']) {
9999
logWhenReady(nuxt, 'Please run `npx nypm i @electric-sql/pglite` to use PGlite as database.', 'error')
@@ -244,8 +244,8 @@ const db = drizzle(binding, { schema })
244244
export { db, schema }
245245
`
246246
}
247-
if (['node-postgres', 'mysql2'].includes(driver) && hub.hosting.includes('cloudflare')) {
248-
const bindingName = driver === 'node-postgres' ? 'POSTGRES' : 'MYSQL'
247+
if (['postgres-js', 'mysql2'].includes(driver) && hub.hosting.includes('cloudflare')) {
248+
const bindingName = driver === 'postgres-js' ? 'POSTGRES' : 'MYSQL'
249249
drizzleOrmContent = `import { drizzle } from 'drizzle-orm/${driver}'
250250
import * as schema from './database/schema.mjs'
251251

src/types/module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export interface DatabaseConfig {
4646
* Database driver (optional, auto-detected if not provided)
4747
*
4848
* SQLite drivers: 'better-sqlite3', 'libsql', 'bun-sqlite', 'd1'
49-
* PostgreSQL drivers: 'node-postgres', 'pglite'
49+
* PostgreSQL drivers: 'postgres-js', 'pglite'
5050
* MySQL drivers: 'mysql2'
5151
*/
5252
driver?: string

src/utils/database.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export async function createDrizzleClient(config: ResolvedDatabaseConfig) {
2929
let pkg = ''
3030
if (driver === 'libsql') {
3131
pkg = 'drizzle-orm/libsql'
32-
} else if (driver === 'node-postgres') {
33-
pkg = 'drizzle-orm/node-postgres'
32+
} else if (driver === 'postgres-js') {
33+
pkg = 'drizzle-orm/postgres-js'
3434
} else if (driver === 'mysql2') {
3535
pkg = 'drizzle-orm/mysql2'
3636
} else if (driver === 'pglite') {

src/utils/devtools.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ async function launchDrizzleStudio(nuxt: Nuxt, hub: HubConfig) {
3333
})
3434
} else {
3535
const { startStudioPostgresServer } = await import('drizzle-kit/api')
36-
// For node-postgres and other PostgreSQL drivers
36+
// For postgres-js and other PostgreSQL drivers
3737
log.info(`Launching Drizzle Studio with PostgreSQL...`)
3838
await startStudioPostgresServer(schema, connection, { port })
3939
}

test/database.config.test.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('resolveDatabaseConfig', () => {
154154
})
155155

156156
describe('PostgreSQL dialect', () => {
157-
it('should use node-postgres driver when DATABASE_URL is set', async () => {
157+
it('should use postgres-js driver when DATABASE_URL is set', async () => {
158158
process.env.DATABASE_URL = 'postgresql://user:pass@localhost:5432/db'
159159

160160
const nuxt = createMockNuxt()
@@ -164,14 +164,14 @@ describe('resolveDatabaseConfig', () => {
164164

165165
expect(result).toMatchObject({
166166
dialect: 'postgresql',
167-
driver: 'node-postgres',
167+
driver: 'postgres-js',
168168
connection: {
169169
url: 'postgresql://user:pass@localhost:5432/db'
170170
}
171171
})
172172
})
173173

174-
it('should use node-postgres driver when POSTGRES_URL is set', async () => {
174+
it('should use postgres-js driver when POSTGRES_URL is set', async () => {
175175
process.env.POSTGRES_URL = 'postgresql://user:pass@localhost:5432/db'
176176

177177
const nuxt = createMockNuxt()
@@ -181,14 +181,14 @@ describe('resolveDatabaseConfig', () => {
181181

182182
expect(result).toMatchObject({
183183
dialect: 'postgresql',
184-
driver: 'node-postgres',
184+
driver: 'postgres-js',
185185
connection: {
186186
url: 'postgresql://user:pass@localhost:5432/db'
187187
}
188188
})
189189
})
190190

191-
it('should use node-postgres driver when POSTGRESQL_URL is set', async () => {
191+
it('should use postgres-js driver when POSTGRESQL_URL is set', async () => {
192192
process.env.POSTGRESQL_URL = 'postgresql://user:pass@localhost:5432/db'
193193

194194
const nuxt = createMockNuxt()
@@ -198,7 +198,7 @@ describe('resolveDatabaseConfig', () => {
198198

199199
expect(result).toMatchObject({
200200
dialect: 'postgresql',
201-
driver: 'node-postgres',
201+
driver: 'postgres-js',
202202
connection: {
203203
url: 'postgresql://user:pass@localhost:5432/db'
204204
}

0 commit comments

Comments
 (0)