Skip to content

Commit 73fd300

Browse files
authored
fix: support plural auth schema exports (#378)
1 parent 0fbb648 commit 73fd300

5 files changed

Lines changed: 63 additions & 7 deletions

File tree

src/module/templates.ts

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,12 +146,24 @@ export const db = undefined`
146146
}
147147

148148
export function buildSchemaExportCode(hasHubDb: boolean, hubDialect: DbDialect): string {
149-
if (!hasHubDb)
150-
return 'export const schema = undefined\n'
149+
if (!hasHubDb) {
150+
return `export const user = undefined
151+
export const session = undefined
152+
export const account = undefined
153+
export const verification = undefined
154+
export const schema = undefined
155+
`
156+
}
151157

152158
return `export * from './schema.${hubDialect}.mjs'
153-
import * as schema from './schema.${hubDialect}.mjs'
154-
export { schema }
159+
import * as generatedSchema from './schema.${hubDialect}.mjs'
160+
161+
const getGeneratedTable = name => generatedSchema[name] ?? generatedSchema[\`\${name}s\`]
162+
export const user = getGeneratedTable('user')
163+
export const session = getGeneratedTable('session')
164+
export const account = getGeneratedTable('account')
165+
export const verification = getGeneratedTable('verification')
166+
export const schema = { ...generatedSchema, user, session, account, verification }
155167
`
156168
}
157169

test/auth-schema-export.test.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,29 @@ describe('#auth/schema export', async () => {
77
rootDir: fileURLToPath(new URL('./cases/auth-schema-export', import.meta.url)),
88
})
99

10-
it('exports stable auth tables (user + verification)', async () => {
11-
const res = await $fetch('/api/test/schema') as { hasUser: boolean, hasVerification: boolean }
10+
it('exports stable auth tables with plural generation enabled', async () => {
11+
const res = await $fetch('/api/test/schema') as {
12+
hasUser: boolean
13+
hasNamedUser: boolean
14+
hasUsers: boolean
15+
hasSession: boolean
16+
hasNamedSession: boolean
17+
hasSessions: boolean
18+
hasAccount: boolean
19+
hasNamedAccount: boolean
20+
hasAccounts: boolean
21+
hasVerification: boolean
22+
}
23+
1224
expect(res.hasUser).toBe(true)
25+
expect(res.hasNamedUser).toBe(true)
26+
expect(res.hasUsers).toBe(true)
27+
expect(res.hasSession).toBe(true)
28+
expect(res.hasNamedSession).toBe(true)
29+
expect(res.hasSessions).toBe(true)
30+
expect(res.hasAccount).toBe(true)
31+
expect(res.hasNamedAccount).toBe(true)
32+
expect(res.hasAccounts).toBe(true)
1333
expect(res.hasVerification).toBe(true)
1434
})
1535
})

test/cases/auth-schema-export/nuxt.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
export default defineNuxtConfig({
22
modules: ['@nuxthub/core', '../../../src/module'],
33

4+
auth: {
5+
schema: { usePlural: true },
6+
},
7+
48
hub: { db: 'sqlite' },
59

610
runtimeConfig: {
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
1-
import { schema } from '#auth/schema'
1+
import { account, schema, session, user } from '#auth/schema'
22

33
export default defineEventHandler(() => {
44
return {
55
hasUser: Boolean(schema?.user),
6+
hasNamedUser: Boolean(user),
7+
hasUsers: Boolean(schema?.users),
8+
hasSession: Boolean(schema?.session),
9+
hasNamedSession: Boolean(session),
10+
hasSessions: Boolean(schema?.sessions),
11+
hasAccount: Boolean(schema?.account),
12+
hasNamedAccount: Boolean(account),
13+
hasAccounts: Boolean(schema?.accounts),
614
hasVerification: Boolean(schema?.verification),
715
}
816
})

test/schema-generator.test.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { existsSync, mkdirSync, rmSync, writeFileSync } from 'node:fs'
22
import { join } from 'node:path'
33
import { getAuthTables } from 'better-auth/db'
44
import { afterAll, beforeAll, describe, expect, it } from 'vitest'
5+
import { buildSchemaExportCode } from '../src/module/templates'
56
import { defineClientAuth, defineServerAuth } from '../src/runtime/config'
67
import { generateDrizzleSchema, loadUserAuthConfig } from '../src/schema-generator'
78

@@ -76,6 +77,17 @@ describe('generateDrizzleSchema', () => {
7677
})
7778
})
7879

80+
describe('buildSchemaExportCode', () => {
81+
it('exports stable undefined auth table aliases without hub db', () => {
82+
const code = buildSchemaExportCode(false, 'sqlite')
83+
expect(code).toContain('export const user = undefined')
84+
expect(code).toContain('export const session = undefined')
85+
expect(code).toContain('export const account = undefined')
86+
expect(code).toContain('export const verification = undefined')
87+
expect(code).toContain('export const schema = undefined')
88+
})
89+
})
90+
7991
describe('getAuthTables with secondaryStorage', () => {
8092
it('excludes session table when secondaryStorage is provided', () => {
8193
const mockStorage = { get: async () => null, set: async () => {}, delete: async () => {} }

0 commit comments

Comments
 (0)