Hi,
I'm trying to execute a SQL migration script when my nuxt server starts but I'm not sure where the best place to do this is?
I've tried as a plugin but it seems to execute at every request so whilst it works, its not practical for obvious reasons:
plugins/migrate.server.ts:
import { migrateDb } from './database'
export default defineNuxtPlugin(async () => {
const runtimeConfig = useRuntimeConfig()
await migrateDb(runtimeConfig.databaseUrl)
})
Next up I've tried to execute it in a hook, but this doesnt seem to work as my database url is set as an env variable NUXT_DATABASE_URL in my .env file but it says its an empty string:
runtimeConfig: {
databaseUrl: ''
},
hooks: {
async ready(nuxt) {
console.log(nuxt.options.runtimeConfig) //runtimeConfig.databaseUrl === ''
const runtimeConfig = nuxt.options.runtimeConfig
await migrateDb(runtimeConfig.databaseUrl)
},
},
Any idea how I can resolve this? Thanks.