8000 Storage mounts with dynamic configuration · Issue #1161 · nitrojs/nitro · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Storage mounts with dynamic configuration #1161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 task done
maou-shonen opened this issue Apr 17, 2023 · 11 comments
Open
1 task done

Storage mounts with dynamic configuration #1161

maou-shonen opened this issue Apr 17, 2023 · 11 comments
Assignees

Comments

@maou-shonen
Copy link
Contributor
maou-shonen commented Apr 17, 2023

Describe the feature

relates nuxt/nuxt#20195

The examples in the above discussions are fairly common,
maybe provide a function to escape?

Additional information

  • Would you be willing to help implement this feature?
@pi0 pi0 changed the title Ability to preserve process.env in config.storage in some form Storage mounts with dynamic configuration Apr 17, 2023
@pi0 pi0 added enhancement New feature or request workaround available and removed pending triage labels Apr 17, 2023
@pi0
Copy link
Member
pi0 commented Apr 17, 2023

Configuration from nitro.config and app.config is serialized during building we cannot use dynamic configuration from there. I am thinking to support server/storage.ts to make it possible or nitro.storage in app.config (however nuxt needs to tree-shake it first)

As a workaround, you can use a runtime plugin to mount in app context using plugins/storage.ts (nuxt: server/plugins/storage.ts)

import redis from "unstorage/drivers/redis";

export default defineNitroPlugin(() => {
  const storage = useStorage();
  storage.mount(
    "/redis",
    redis({
      // ... options from runtime config, app config or process.env
    })
  );
});

@pi0 pi0 added the discussion label Apr 17, 2023
@maou-shonen
Copy link
Contributor Author
maou-shonen commented Apr 17, 2023

If possible, I would still prefer to have some way of use process.env in config.
like this nuxt/nuxt#18910
if I have many modules and want to use different default values on different environments, it would provide a better DX , without creating a bunch of plugins.

@qin-guan
Copy link
Contributor

Would it be possible to document the temporary workaround as mentioned by @pi0 (#1161 (comment)) inside Nuxt documentation?

Would be happy to open a PR for this! Thanks (:

@pi0
Copy link
Member
pi0 commented Jul 17, 2023

PR also welcome for nitro docs to mention workaround in storage section 👍🏼

@daniandl
Copy link

Sorry for necro but is there a way to do this for the cache system too?
Currently, if I try to mount cache in a plugin using pi0's way, i get ERROR [worker init] already mounted at cache.

Is the only way here to override every single defineCachedEventHandler to use a custom mount point (base)?

@pi0
Copy link
Member
pi0 commented Mar 18, 2024

you might need to call .unmount() first.

@censujiang
Copy link

same question

@censujiang
Copy link

When I create a plugin using the following code

import redisDriver from 'unstorage/drivers/redis'
import azureAppConfigurationDriver from 'unstorage/drivers/azure-app-configuration'

export default defineNitroPlugin((nuxtApp) => {
  const storage = useStorage()
  if (useRuntimeConfig().systemRuntimeType == 'azure') {
    storage.unmount('cache')
    const azure = azureAppConfigurationDriver({
      prefix: "default",
      label: "cache",
    })
    storage.mount('cache', azure)
  } else {
    storage.unmount('cache')
    const redis = redisDriver({
      url: "redis://localhost:6379",
    })
    storage.mount('cache', redis)
  }
})

The following error occurred

 ERROR  [nuxt] [request error] [unhandled] [500] Cannot access 'nitroApp' before initialization                                                                                                                                          20:03:46  
  at Object.onRequest (/D:/my-nuxt-template/.nuxt/prerender/chunks/runtime.mjs:6801:7)
  at Object.handler (/D:/my-nuxt-template/node_modules/h3/dist/index.mjs:1959:21)
  at toNodeHandle (/D:/my-nuxt-template/node_modules/h3/dist/index.mjs:2266:17)
  at callHandle (/D:/my-nuxt-template/node_modules/unenv/runtime/fetch/call.mjs:25:12)
  at ufetch (/D:/my-nuxt-template/node_modules/unenv/runtime/fetch/index.mjs:9:23)
  at Object.localFetch (/D:/my-nuxt-template/.nuxt/prerender/chunks/runtime.mjs:6821:39)
  at /D:/my-nuxt-template/.nuxt/prerender/chunks/runtime.mjs:3162:36

How can I solve this problem?

@censujiang
Copy link

When I create a plugin using the following code

import redisDriver from 'unstorage/drivers/redis'
import azureAppConfigurationDriver from 'unstorage/drivers/azure-app-configuration'

export default defineNitroPlugin((nuxtApp) => {
  const storage = useStorage()
  if (useRuntimeConfig().systemRuntimeType == 'azure') {
    storage.unmount('cache')
    const azure = azureAppConfigurationDriver({
      prefix: "default",
      label: "cache",
    })
    storage.mount('cache', azure)
  } else {
    storage.unmount('cache')
    const redis = redisDriver({
      url: "redis://localhost:6379",
    })
    storage.mount('cache', redis)
  }
})

The following error occurred

 ERROR  [nuxt] [request error] [unhandled] [500] Cannot access 'nitroApp' before initialization                                                                                                                                          20:03:46  
  at Object.onRequest (/D:/my-nuxt-template/.nuxt/prerender/chunks/runtime.mjs:6801:7)
  at Object.handler (/D:/my-nuxt-template/node_modules/h3/dist/index.mjs:1959:21)
  at toNodeHandle (/D:/my-nuxt-template/node_modules/h3/dist/index.mjs:2266:17)
  at callHandle (/D:/my-nuxt-template/node_modules/unenv/runtime/fetch/call.mjs:25:12)
  at ufetch (/D:/my-nuxt-template/node_modules/unenv/runtime/fetch/index.mjs:9:23)
  at Object.localFetch (/D:/my-nuxt-template/.nuxt/prerender/chunks/runtime.mjs:6821:39)
  at /D:/my-nuxt-template/.nuxt/prerender/chunks/runtime.mjs:3162:36

How can I solve this problem?

After testing, this state will only appear when the database is cache

@censujiang
Copy link

When I create a plugin using the following code

import redisDriver from 'unstorage/drivers/redis'
import azureAppConfigurationDriver from 'unstorage/drivers/azure-app-configuration'

export default defineNitroPlugin((nuxtApp) => {
  const storage = useStorage()
  if (useRuntimeConfig().systemRuntimeType == 'azure') {
    storage.unmount('cache')
    const azure = azureAppConfigurationDriver({
      prefix: "default",
      label: "cache",
    })
    storage.mount('cache', azure)
  } else {
    storage.unmount('cache')
    const redis = redisDriver({
      url: "redis://localhost:6379",
    })
    storage.mount('cache', redis)
  }
})

The following error occurred

 ERROR  [nuxt] [request error] [unhandled] [500] Cannot access 'nitroApp' before initialization                                                                                                                                          20:03:46  
  at Object.onRequest (/D:/my-nuxt-template/.nuxt/prerender/chunks/runtime.mjs:6801:7)
  at Object.handler (/D:/my-nuxt-template/node_modules/h3/dist/index.mjs:1959:21)
  at toNodeHandle (/D:/my-nuxt-template/node_modules/h3/dist/index.mjs:2266:17)
  at callHandle (/D:/my-nuxt-template/node_modules/unenv/runtime/fetch/call.mjs:25:12)
  at ufetch (/D:/my-nuxt-template/node_modules/unenv/runtime/fetch/index.mjs:9:23)
  at Object.localFetch (/D:/my-nuxt-template/.nuxt/prerender/chunks/runtime.mjs:6821:39)
  at /D:/my-nuxt-template/.nuxt/prerender/chunks/runtime.mjs:3162:36

How can I solve this problem?

After testing, this state will only appear when the database is cache

@pi0 I think this is a problem that needs to be solved urgently. Even the most basic ones can't be used. . .

@pi0
Copy link
Member
pi0 commented Apr 25, 2025

I will pick on this issue later. There is a workaround to mount drivers using nitro plugin and feed any dynamic configuration to id from various sources.

Ideally more unstorage drivers should support environment variables or we support an interpolation support (self assigned issue to pick on this later)

If you encounter any issues with workaround, please consider reporting in new issue with minimal nitro reproduction 🙏🏼

@pi0 pi0 self-assigned this Apr 25, 2025
@pi0 pi0 marked this as a duplicate of #2991 Apr 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
0