Skip to content

Commit dc6d7d2

Browse files
committed
fix(db): handle build cache buildDir for schema types
During build, Nuxt uses node_modules/.cache/nuxt/.nuxt/ as buildDir where tsdown doesn't generate .d.mts. Fall back to .nuxt/ for types. Also: - Merge build+copy into single hook (prevent race condition) - Only write stubs if files don't exist (prevent overwriting)
1 parent 55a3f13 commit dc6d7d2

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/db/setup.ts

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mkdir, copyFile, writeFile, readFile } from 'node:fs/promises'
1+
import { mkdir, copyFile, writeFile, readFile, stat } from 'node:fs/promises'
22
import chokidar from 'chokidar'
33
import { glob } from 'tinyglobby'
44
import { join, resolve as resolveFs, relative } from 'pathe'
@@ -274,28 +274,39 @@ async function generateDatabaseSchema(nuxt: Nuxt, hub: ResolvedHubConfig) {
274274
write: true
275275
})
276276

277-
// Build schema types during prepare/dev/build
277+
// Build schema types during prepare/dev/build, then copy to node_modules
278278
nuxt.hooks.hookOnce('app:templatesGenerated', async () => {
279+
// Build first
279280
await buildDatabaseSchema(nuxt.options.buildDir, { relativeDir: nuxt.options.rootDir })
280-
})
281281

282-
// Copy schema to node_modules/@nuxthub/db/ for workflow compatibility
283-
nuxt.hooks.hookOnce('app:templatesGenerated', async () => {
282+
// Then copy to node_modules/@nuxthub/db/ for workflow compatibility
284283
const physicalDbDir = join(nuxt.options.rootDir, 'node_modules', '@nuxthub', 'db')
285284
await mkdir(physicalDbDir, { recursive: true })
286285

287286
try {
288287
await copyFile(join(nuxt.options.buildDir, 'hub/db/schema.mjs'), join(physicalDbDir, 'schema.mjs'))
289288

290-
// Copy the generated .d.mts file for TypeScript support (overwrites stub from setupDatabaseClient)
291-
const schemaDtsSource = join(nuxt.options.buildDir, 'hub/db/schema.d.mts')
289+
// Copy the generated .d.mts file for TypeScript support
290+
// Try buildDir first, then fall back to .nuxt (for when buildDir is in .cache during build)
291+
const buildDirSource = join(nuxt.options.buildDir, 'hub/db/schema.d.mts')
292+
const nuxtDirSource = join(nuxt.options.rootDir, '.nuxt/hub/db/schema.d.mts')
293+
294+
let schemaTypes: string | undefined
292295
try {
293-
const schemaTypes = await readFile(schemaDtsSource, 'utf-8')
294-
await writeFile(join(physicalDbDir, 'schema.d.mts'), schemaTypes)
296+
schemaTypes = await readFile(buildDirSource, 'utf-8')
295297
} catch {
296-
// During tests, don't overwrite existing types with stub (preserves dev-generated types)
297-
if (nuxt.options.test) return
298-
// Fallback: create a simple re-export if .d.mts doesn't exist yet
298+
// Fallback to .nuxt directory (types generated during prepare)
299+
try {
300+
schemaTypes = await readFile(nuxtDirSource, 'utf-8')
301+
} catch {
302+
// Types not found in either location
303+
}
304+
}
305+
306+
if (schemaTypes && schemaTypes.length > 50) {
307+
await writeFile(join(physicalDbDir, 'schema.d.mts'), schemaTypes)
308+
} else if (!nuxt.options.test) {
309+
// Fallback: create a simple re-export if types not available
299310
await writeFile(join(physicalDbDir, 'schema.d.mts'), `export * from './schema.mjs'`)
300311
}
301312
} catch (error) {
@@ -531,9 +542,13 @@ export const db: ReturnType<typeof drizzleCore<typeof schema>>
531542
}
532543
try {
533544
await writeFile(join(physicalDbDir, 'package.json'), JSON.stringify(packageJson, null, 2))
534-
// Stub schema files (overwritten with actual schema during build)
535-
await writeFile(join(physicalDbDir, 'schema.mjs'), 'export {}')
536-
await writeFile(join(physicalDbDir, 'schema.d.mts'), 'export {}')
545+
// Stub schema files only if they don't exist (real types written by app:templatesGenerated hook)
546+
const schemaPath = join(physicalDbDir, 'schema.mjs')
547+
const schemaDtsPath = join(physicalDbDir, 'schema.d.mts')
548+
const schemaExists = await stat(schemaPath).then(s => s.size > 20).catch(() => false)
549+
const schemaDtsExists = await stat(schemaDtsPath).then(s => s.size > 20).catch(() => false)
550+
if (!schemaExists) await writeFile(schemaPath, 'export {}')
551+
if (!schemaDtsExists) await writeFile(schemaDtsPath, 'export {}')
537552
} catch (error) {
538553
throw new Error(`Failed to create @nuxthub/db package files: ${(error as Error).message}`)
539554
}

0 commit comments

Comments
 (0)