From a6b4c3c9d8ae678116abbcd67d0ea810414b6222 Mon Sep 17 00:00:00 2001 From: Qin Guan Date: Tue, 18 Jul 2023 18:25:47 +0800 Subject: [PATCH] docs: add runtime storage configuration examples (#22189) --- .../2.guide/2.directory-structure/1.server.md | 41 ++++++++++++++++++- 1 file changed, 40 insertions(+), 1 deletion(-) diff --git a/docs/2.guide/2.directory-structure/1.server.md b/docs/2.guide/2.directory-structure/1.server.md index edbde12713..02b72bcd9e 100644 --- a/docs/2.guide/2.directory-structure/1.server.md +++ b/docs/2.guide/2.directory-structure/1.server.md @@ -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]