-
Notifications
You must be signed in to change notification settings - Fork 95
Open
Labels
bugSomething isn't workingSomething isn't working
Description
Describe the bug
After migrating my own Drizzle setup to Nuxt Hub, when using a function from server/utils inside of server/db/schema.ts, useEvent is not defined. This had previously worked with my own Drizzle setup.
Steps to reproduce
Steps to reproduce the behavior:
- Create an empty Nuxt project
- Enable Nuxt Hub and Nuxt Hub Database with Postgresql following the Nuxt Hub docs
- Add a util for audit fields:
// server/utils/audit.ts
export function getRequestingUserId(): string {
const event = useEvent();
return event.context.user.id ?? 'anonymous';
}- Ensure
asyncContextis enabled:
export default defineNuxtConfig({
compatibilityDate: '2025-07-15',
devtools: { enabled: true },
modules: ['@nuxthub/core'],
hub: {
db: 'postgresql'
},
experimental: {
asyncContext: true
},
nitro: {
experimental: {
asyncContext: true
}
}
})- Add a schema using the audit fields:
// server/db/schema.ts
import { pgTable, text, serial, timestamp } from 'drizzle-orm/pg-core'
import { getRequestingUserId } from '../utils/audit'
const audit = {
createdAt: timestamp().defaultNow(),
updatedAt: timestamp().$onUpdate(() => new Date()),
createdBy: text().notNull().$defaultFn(getRequestingUserId),
updatedBy: text().$onUpdateFn(getRequestingUserId),
};
export const users = pgTable('users', {
id: serial().primaryKey(),
name: text().notNull(),
email: text().notNull().unique(),
password: text().notNull(),
avatar: text().notNull(),
...audit,
})- Try to write to the db:
// server/api/users.post.ts
export default defineEventHandler(async (event) => {
await db.insert(schema.users).values({
name: "John Doe",
email: "john@example.com",
password: "securepassword",
avatar: "https://example.com/avatar.jpg"
})
})Expected behavior
The audit fields should be populated and written to the database. When running with my own Drizzle setup, this works. After migrating to Nuxt Hub I get the following:
ReferenceError {
stack: 'useEvent is not defined\n' +
'at PgText.getRequestingUserId [as defaultFn] (./.nuxt/hub/db/schema.mjs:5:2)\n' +
'at PgDialect.buildInsertQuery (./node_modules/.pnpm/drizzle-orm@0.45.1_@cloudflare+workers-types@4.20251228.0_@electric-sql+pglite@0.3.14_postgres@3.4.7/node_modules/drizzle-orm/pg-core/dialect.js:379:43)\n' +
'at QueryPromise.getSQL (./node_modules/.pnpm/drizzle-orm@0.45.1_@cloudflare+workers-types@4.20251228.0_@electric-sql+pglite@0.3.14_postgres@3.4.7/node_modules/drizzle-orm/pg-core/query-builders/insert.js:157:25)\n' +
'at ./node_modules/.pnpm/drizzle-orm@0.45.1_@cloudflare+workers-types@4.20251228.0_@electric-sql+pglite@0.3.14_postgres@3.4.7/node_modules/drizzle-orm/pg-core/query-builders/insert.js:166:69)\n' +
'at Object.startActiveSpan (./node_modules/.pnpm/drizzle-orm@0.45.1_@cloudflare+workers-types@4.20251228.0_@electric-sql+pglite@0.3.14_postgre'... 1201 more characters,
message: 'useEvent is not defined',
}
My custom Drizzle setup was not that different to Nuxt Hub. I had to make very minimal changes to migrate to Nuxt Hub.
Metadata
Metadata
Assignees
Labels
bugSomething isn't workingSomething isn't working