docs: add runtime storage configuration examples (#22189)

This commit is contained in:
Qin Guan 2023-07-18 18:25:47 +08:00 committed by GitHub
parent b8a282e115
commit a6b4c3c9d8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -340,10 +340,12 @@ Never combine `next()` callback with a legacy middleware that is `async` or retu
### Server Storage
Nitro provides a cross-platform [storage layer](https://nitro.unjs.io/guide/storage). In order to configure additional storage mount points, you can use `nitro.storage`.
Nitro provides a cross-platform [storage layer](https://nitro.unjs.io/guide/storage). In order to configure additional storage mount points, you can use `nitro.storage`, or [server plugins](#server-plugins).
#### Example: Using Redis
Using `nitro.storage`:
```ts [nuxt.config.ts]
export default defineNuxtConfig({
nitro: {
@ -363,6 +365,43 @@ export default defineNuxtConfig({
})
```
Alternatively, using server plugins:
::code-group
```ts [server/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 [nuxt.config.ts]
export default defineNuxtConfig({
runtimeConfig: {
redis: { // Default values
host: '',
port: 0,
/* other redis connector options */
}
}
})
```
::
Create a new file in `server/api/test.post.ts`:
```ts [server/api/test.post.ts]