Skip to content

Commit

Permalink
docs(storage): add runtime storage configuration examples (#1456)
Browse files Browse the repository at this point in the history
  • Loading branch information
qin-guan authored Aug 4, 2023
1 parent 982f5e3 commit 3a4fdee
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions docs/content/1.guide/4.storage.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,59 @@ export default defineNuxtConfig({
```
::


### Runtime configuration

In scenarios where the mount point configuration is not known until runtime, Nitro can dynamically add mount points during startup using [plugins](/guide/plugins):

::alert{type="info"}
This is a temporary workaround, with a better solution coming in the future! Keep a lookout on the GitHub issue [here](https://github.com/unjs/nitro/issues/1161#issuecomment-1511444675).
::

::code-group
```ts [plugins/storage.ts]
import redisDriver from 'unstorage/drivers/redis'

export default defineNitroPlugin(() => {
const storage = useStorage()

// Dynamically pass in credentials from runtime configuration, or other sources
const driver = redisDriver({
base: 'redis',
host: useRuntimeConfig().redis.host,
port: useRuntimeConfig().redis.port,
/* other redis connector options */
})

// Mount driver
storage.mount('redis', driver)
})
```
``` ts [nitro.config.ts]
export default defineNitroConfig({
runtimeConfig: {
redis: { // Default values
host: '',
port: 0,
/* other redis connector options */
}
}
})
```
``` ts [nuxt.config.ts]
export default defineNuxtConfig({
runtimeConfig: {
redis: { // Default values
host: '',
port: 0,
/* other redis connector options */
}
}
})
```
::


Usage:

```js
Expand Down

0 comments on commit 3a4fdee

Please sign in to comment.