Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 254fd24

Browse files
committed
fix(preset): allow presets to provide default options
Presets can provide default fallback for database
1 parent 0866008 commit 254fd24

File tree

4 files changed

+34
-39
lines changed

4 files changed

+34
-39
lines changed

src/module.ts

+18-32
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
import type { Nuxt } from '@nuxt/schema'
1414
import type { ModuleOptions as MDCModuleOptions } from '@nuxtjs/mdc'
1515
import { hash } from 'ohash'
16-
import { join, isAbsolute } from 'pathe'
16+
import { join } from 'pathe'
1717
import htmlTags from '@nuxtjs/mdc/runtime/parser/utils/html-tags-list'
1818
import { kebabCase, pascalCase } from 'scule'
1919
import defu from 'defu'
@@ -75,14 +75,6 @@ export default defineNuxtModule<ModuleOptions>({
7575
},
7676
},
7777
async setup(options, nuxt) {
78-
// Provide default database configuration here since nuxt is merging defaults and user options
79-
if (!options.database) {
80-
options.database = {
81-
type: 'sqlite',
82-
filename: './contents.sqlite',
83-
}
84-
}
85-
8678
const resolver = createResolver(import.meta.url)
8779
const manifest: Manifest = {
8880
checksum: {},
@@ -91,31 +83,9 @@ export default defineNuxtModule<ModuleOptions>({
9183
collections: [],
9284
}
9385

94-
// Create local database
95-
await refineDatabaseConfig(options._localDatabase, nuxt)
96-
if (options._localDatabase?.type === 'sqlite') {
97-
options._localDatabase!.filename = isAbsolute(options._localDatabase!.filename)
98-
? options._localDatabase!.filename
99-
: join(nuxt.options.rootDir, options._localDatabase!.filename)
100-
}
101-
102-
// Create sql database
103-
await refineDatabaseConfig(options.database, nuxt)
104-
10586
const { collections } = await loadContentConfig(nuxt)
10687
manifest.collections = collections
10788

108-
// Module Options
109-
nuxt.options.runtimeConfig.public.content = {
110-
wsUrl: '',
111-
}
112-
nuxt.options.runtimeConfig.content = {
113-
version,
114-
database: options.database,
115-
localDatabase: options._localDatabase!,
116-
integrityCheck: true,
117-
} as never
118-
11989
nuxt.options.vite.optimizeDeps ||= {}
12090
nuxt.options.vite.optimizeDeps.exclude ||= []
12191
nuxt.options.vite.optimizeDeps.exclude.push('@sqlite.org/sqlite-wasm')
@@ -159,9 +129,25 @@ export default defineNuxtModule<ModuleOptions>({
159129
}
160130
}
161131

162-
// Load nitro preset and set db adapter
163132
const preset = findPreset(nuxt)
164133
await preset?.setup?.(options, nuxt)
134+
135+
// Provide default database configuration here since nuxt is merging defaults and user options
136+
options.database ||= { type: 'sqlite', filename: './contents.sqlite' }
137+
await refineDatabaseConfig(options._localDatabase, { rootDir: nuxt.options.rootDir, updateSqliteFileName: true })
138+
await refineDatabaseConfig(options.database, { rootDir: nuxt.options.rootDir })
139+
140+
// Module Options
141+
nuxt.options.runtimeConfig.public.content = {
142+
wsUrl: '',
143+
}
144+
nuxt.options.runtimeConfig.content = {
145+
version,
146+
database: options.database,
147+
localDatabase: options._localDatabase!,
148+
integrityCheck: true,
149+
} as never
150+
165151
nuxt.hook('nitro:config', async (config) => {
166152
const preset = findPreset(nuxt)
167153
await preset.setupNitro(config, { manifest, resolver, moduleOptions: options })

src/presets/cloudflare-pages.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ export default definePreset({
99
async setupNitro(nitroConfig, { manifest, resolver }) {
1010
if (nitroConfig.runtimeConfig?.content?.database?.type === 'sqlite') {
1111
logger.warn('Deploying to Cloudflare Pages requires using D1 database, switching to D1 database with binding `DB`.')
12-
nitroConfig.runtimeConfig!.content!.database = {
13-
type: 'd1',
14-
binding: 'DB',
15-
}
12+
nitroConfig.runtimeConfig!.content!.database = { type: 'd1', bindingName: 'DB' }
1613
}
1714

1815
nitroConfig.publicAssets ||= []

src/presets/nuxthub.ts

+9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { mkdir, writeFile } from 'node:fs/promises'
22
import { resolve } from 'pathe'
33
import { definePreset } from '../utils/preset'
4+
import { logger } from '../utils/dev'
45
import cfPreset from './cloudflare-pages'
56

67
export default definePreset({
@@ -9,8 +10,16 @@ export default definePreset({
910
// Make sure database is enabled
1011
const nuxthubOptions: { database?: boolean } = (nuxt.options as unknown as { hub: unknown }).hub ||= {}
1112
nuxthubOptions.database = true
13+
14+
// Set up database
15+
options.database ||= { type: 'd1', bindingName: 'DB' }
1216
},
1317
async setupNitro(nitroConfig, options) {
18+
if (nitroConfig.runtimeConfig?.content?.database?.type === 'sqlite') {
19+
logger.warn('Deploying to NuxtHub requires using D1 database, switching to D1 database with binding `DB`.')
20+
nitroConfig.runtimeConfig!.content!.database = { type: 'd1', bindingName: 'DB' }
21+
}
22+
1423
await cfPreset.setupNitro(nitroConfig, options)
1524

1625
if (nitroConfig.dev === false) {

src/utils/database.ts

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { mkdir } from 'node:fs/promises'
22
import type { Connector } from 'db0'
33
import type { Resolver } from '@nuxt/kit'
4-
import type { Nuxt } from '@nuxt/schema'
54
import cloudflareD1Connector from 'db0/connectors/cloudflare-d1'
65
import { isAbsolute, join, dirname } from 'pathe'
76
import { isWebContainer } from '@webcontainer/env'
@@ -25,7 +24,7 @@ function isSqlite3Available() {
2524
}
2625
}
2726

28-
export async function refineDatabaseConfig(database: ModuleOptions['database'], nuxt: Nuxt) {
27+
export async function refineDatabaseConfig(database: ModuleOptions['database'], opts: { rootDir: string, updateSqliteFileName?: boolean }) {
2928
if (database.type === 'd1') {
3029
if (!('bindingName' in database)) {
3130
// @ts-expect-error bindingName
@@ -36,8 +35,12 @@ export async function refineDatabaseConfig(database: ModuleOptions['database'],
3635
if (database.type === 'sqlite') {
3736
const path = isAbsolute(database.filename)
3837
? database.filename
39-
: join(nuxt.options.rootDir, database.filename)
38+
: join(opts.rootDir, database.filename)
4039
await mkdir(dirname(path), { recursive: true }).catch(() => {})
40+
41+
if (opts.updateSqliteFileName) {
42+
database.filename = path
43+
}
4144
}
4245
}
4346

0 commit comments

Comments
 (0)