Skip to content

Commit 8c48d76

Browse files
committed
use nitro server assets to load database in server plugin
1 parent 0bcfd12 commit 8c48d76

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

src/module.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,7 @@ export default defineNuxtModule({
5858
const dbHash = isDev ? 'content' : await database.toHash()
5959
const dbName = `db-${dbHash}.json`
6060
const dbUrl = joinURL(baseURL, dbFolder, dbName)
61-
const dbPath = resolve(baseDir, dbFolder, dbName)
62-
const config = { dbPath, dbUrl }
61+
const config = { dbName, dbUrl }
6362

6463
addPlugin(resolve(runtimeDir, 'plugins', 'db.server'))
6564
addPlugin(resolve(runtimeDir, 'plugins', 'db.client'))
@@ -96,6 +95,12 @@ export default defineNuxtModule({
9695
nuxt.hook('nitro:config', async nitroConfig => {
9796
nitroConfig.publicAssets ||= []
9897
nitroConfig.publicAssets.push({ dir: baseDir })
98+
99+
nitroConfig.serverAssets ||= []
100+
nitroConfig.serverAssets.push({
101+
baseName: 'nuxt-db',
102+
dir: buildDir
103+
})
99104
})
100105

101106
nuxt.hook('close', async () => {

src/runtime/plugins/db.server.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { readFile } from 'fs/promises'
1+
import { useStorage } from 'nitropack/runtime/storage'
22
import { defineNuxtPlugin } from '#app'
3-
import { dbPath, dbDirs } from '#build/nuxtdb-options'
3+
import { dbName, dbDirs } from '#build/nuxtdb-options'
44
import { useDB } from '#imports'
55

66
export default defineNuxtPlugin(async nuxtApp => {
@@ -10,7 +10,8 @@ export default defineNuxtPlugin(async nuxtApp => {
1010
async fetch() {
1111
this.fetchAt = Date.now()
1212
try {
13-
return JSON.parse(await readFile(dbPath, 'utf8'))
13+
const storage = useStorage('assets:nuxt-db')
14+
return await storage.getItem(dbName)
1415
} catch (e) {
1516
console.error(e)
1617
return []

0 commit comments

Comments
 (0)