Skip to content
Merged
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
14 changes: 7 additions & 7 deletions docs/content/docs/2.features/database.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ Install Drizzle ORM, Drizzle Kit, and the appropriate driver(s) for the database

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

export default eventHandler(async (event) => {
const { id } = getRouterParams(event)

const deletedUser = await db
.delete(schema.users)
.where(eq(schema.users.id, Number(id)))
Expand Down Expand Up @@ -449,7 +449,7 @@ export default defineTask({
},
async run() {
console.log('Seeding database...')

const users = [
{
name: 'John Doe',
Expand All @@ -466,9 +466,9 @@ export default defineTask({
createdAt: new Date()
}
]

await useDrizzle().insert(tables.users).values(users)

return { result: 'Database seeded successfully' }
}
})
Expand Down Expand Up @@ -499,7 +499,7 @@ export default defineNuxtConfig({
hub: {
database: {
dialect: 'postgresql',
driver: 'node-postgres', // Optional: explicitly choose driver
driver: 'postgres-js', // Optional: explicitly choose driver
connection: {
connectionString: process.env.DATABASE_URL
}
Expand Down
8 changes: 4 additions & 4 deletions src/features/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function resolveDatabaseConfig(nuxt: Nuxt, hub: HubConfig): Promise
case 'postgresql': {
config.connection = defu(config.connection, { url: process.env.DATABASE_URL || process.env.POSTGRES_URL || process.env.POSTGRESQL_URL || '' })
if (config.connection.url) {
config.driver ||= 'node-postgres'
config.driver ||= 'postgres-js'
break
}
// Local PGLite
Expand Down Expand Up @@ -93,7 +93,7 @@ export async function setupDatabase(nuxt: Nuxt, hub: HubConfig, deps: Record<str
if (!deps['drizzle-orm'] || !deps['drizzle-kit']) {
logWhenReady(nuxt, 'Please run `npx nypm i drizzle-orm drizzle-kit` to properly setup Drizzle ORM with NuxtHub.', 'error')
}
if (driver === 'node-postgres' && !deps.pg) {
if (driver === 'postgres-js' && !deps.pg) {
logWhenReady(nuxt, 'Please run `npx nypm i pg` to use PostgreSQL as database.', 'error')
} else if (driver === 'pglite' && !deps['@electric-sql/pglite']) {
logWhenReady(nuxt, 'Please run `npx nypm i @electric-sql/pglite` to use PGlite as database.', 'error')
Expand Down Expand Up @@ -244,8 +244,8 @@ const db = drizzle(binding, { schema })
export { db, schema }
`
}
if (['node-postgres', 'mysql2'].includes(driver) && hub.hosting.includes('cloudflare')) {
const bindingName = driver === 'node-postgres' ? 'POSTGRES' : 'MYSQL'
if (['postgres-js', 'mysql2'].includes(driver) && hub.hosting.includes('cloudflare')) {
const bindingName = driver === 'postgres-js' ? 'POSTGRES' : 'MYSQL'
drizzleOrmContent = `import { drizzle } from 'drizzle-orm/${driver}'
import * as schema from './database/schema.mjs'

Expand Down
2 changes: 1 addition & 1 deletion src/types/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export interface DatabaseConfig {
* Database driver (optional, auto-detected if not provided)
*
* SQLite drivers: 'better-sqlite3', 'libsql', 'bun-sqlite', 'd1'
* PostgreSQL drivers: 'node-postgres', 'pglite'
* PostgreSQL drivers: 'postgres-js', 'pglite'
* MySQL drivers: 'mysql2'
*/
driver?: string
Expand Down
4 changes: 2 additions & 2 deletions src/utils/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ export async function createDrizzleClient(config: ResolvedDatabaseConfig) {
let pkg = ''
if (driver === 'libsql') {
pkg = 'drizzle-orm/libsql'
} else if (driver === 'node-postgres') {
pkg = 'drizzle-orm/node-postgres'
} else if (driver === 'postgres-js') {
pkg = 'drizzle-orm/postgres-js'
} else if (driver === 'mysql2') {
pkg = 'drizzle-orm/mysql2'
} else if (driver === 'pglite') {
Expand Down
2 changes: 1 addition & 1 deletion src/utils/devtools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function launchDrizzleStudio(nuxt: Nuxt, hub: HubConfig) {
})
} else {
const { startStudioPostgresServer } = await import('drizzle-kit/api')
// For node-postgres and other PostgreSQL drivers
// For postgres-js and other PostgreSQL drivers
log.info(`Launching Drizzle Studio with PostgreSQL...`)
await startStudioPostgresServer(schema, connection, { port })
}
Expand Down
12 changes: 6 additions & 6 deletions test/database.config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe('resolveDatabaseConfig', () => {
})

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

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

expect(result).toMatchObject({
dialect: 'postgresql',
driver: 'node-postgres',
driver: 'postgres-js',
connection: {
url: 'postgresql://user:pass@localhost:5432/db'
}
})
})

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

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

expect(result).toMatchObject({
dialect: 'postgresql',
driver: 'node-postgres',
driver: 'postgres-js',
connection: {
url: 'postgresql://user:pass@localhost:5432/db'
}
})
})

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

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

expect(result).toMatchObject({
dialect: 'postgresql',
driver: 'node-postgres',
driver: 'postgres-js',
connection: {
url: 'postgresql://user:pass@localhost:5432/db'
}
Expand Down