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
20 changes: 16 additions & 4 deletions src/module/templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,24 @@ export const db = undefined`
}

export function buildSchemaExportCode(hasHubDb: boolean, hubDialect: DbDialect): string {
if (!hasHubDb)
return 'export const schema = undefined\n'
if (!hasHubDb) {
return `export const user = undefined
export const session = undefined
export const account = undefined
export const verification = undefined
export const schema = undefined
`
}

return `export * from './schema.${hubDialect}.mjs'
import * as schema from './schema.${hubDialect}.mjs'
export { schema }
import * as generatedSchema from './schema.${hubDialect}.mjs'

const getGeneratedTable = name => generatedSchema[name] ?? generatedSchema[\`\${name}s\`]
export const user = getGeneratedTable('user')
export const session = getGeneratedTable('session')
export const account = getGeneratedTable('account')
export const verification = getGeneratedTable('verification')
export const schema = { ...generatedSchema, user, session, account, verification }
`
}

Expand Down
24 changes: 22 additions & 2 deletions test/auth-schema-export.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,29 @@ describe('#auth/schema export', async () => {
rootDir: fileURLToPath(new URL('./cases/auth-schema-export', import.meta.url)),
})

it('exports stable auth tables (user + verification)', async () => {
const res = await $fetch('/api/test/schema') as { hasUser: boolean, hasVerification: boolean }
it('exports stable auth tables with plural generation enabled', async () => {
const res = await $fetch('/api/test/schema') as {
hasUser: boolean
hasNamedUser: boolean
hasUsers: boolean
hasSession: boolean
hasNamedSession: boolean
hasSessions: boolean
hasAccount: boolean
hasNamedAccount: boolean
hasAccounts: boolean
hasVerification: boolean
}

expect(res.hasUser).toBe(true)
expect(res.hasNamedUser).toBe(true)
expect(res.hasUsers).toBe(true)
expect(res.hasSession).toBe(true)
expect(res.hasNamedSession).toBe(true)
expect(res.hasSessions).toBe(true)
expect(res.hasAccount).toBe(true)
expect(res.hasNamedAccount).toBe(true)
expect(res.hasAccounts).toBe(true)
expect(res.hasVerification).toBe(true)
})
})
4 changes: 4 additions & 0 deletions test/cases/auth-schema-export/nuxt.config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export default defineNuxtConfig({
modules: ['@nuxthub/core', '../../../src/module'],

auth: {
schema: { usePlural: true },
},

hub: { db: 'sqlite' },

runtimeConfig: {
Expand Down
10 changes: 9 additions & 1 deletion test/cases/auth-schema-export/server/api/test/schema.get.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
import { schema } from '#auth/schema'
import { account, schema, session, user } from '#auth/schema'

export default defineEventHandler(() => {
return {
hasUser: Boolean(schema?.user),
hasNamedUser: Boolean(user),
hasUsers: Boolean(schema?.users),
hasSession: Boolean(schema?.session),
hasNamedSession: Boolean(session),
hasSessions: Boolean(schema?.sessions),
hasAccount: Boolean(schema?.account),
hasNamedAccount: Boolean(account),
hasAccounts: Boolean(schema?.accounts),
hasVerification: Boolean(schema?.verification),
}
})
12 changes: 12 additions & 0 deletions test/schema-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
import { join } from 'node:path'
import { getAuthTables } from 'better-auth/db'
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
import { buildSchemaExportCode } from '../src/module/templates'
import { defineClientAuth, defineServerAuth } from '../src/runtime/config'
import { generateDrizzleSchema, loadUserAuthConfig } from '../src/schema-generator'

Expand Down Expand Up @@ -76,6 +77,17 @@ describe('generateDrizzleSchema', () => {
})
})

describe('buildSchemaExportCode', () => {
it('exports stable undefined auth table aliases without hub db', () => {
const code = buildSchemaExportCode(false, 'sqlite')
expect(code).toContain('export const user = undefined')
expect(code).toContain('export const session = undefined')
expect(code).toContain('export const account = undefined')
expect(code).toContain('export const verification = undefined')
expect(code).toContain('export const schema = undefined')
})
})

describe('getAuthTables with secondaryStorage', () => {
it('excludes session table when secondaryStorage is provided', () => {
const mockStorage = { get: async () => null, set: async () => {}, delete: async () => {} }
Expand Down
Loading