diff --git a/docs/content/1.guide/1.introduction/4.storage.md b/docs/content/1.guide/1.introduction/4.storage.md index 571ec87014..8a6dd05c4e 100644 --- a/docs/content/1.guide/1.introduction/4.storage.md +++ b/docs/content/1.guide/1.introduction/4.storage.md @@ -2,28 +2,27 @@ Nitro provides a built-in storage layer that can abstract filesystem or database or any other data source. -See [unjs/unstorage](https://github.com/unjs/unstorage) for more usage information. +`useStorage()` is an instance of [createStorage](https://unstorage.unjs.io/usage) using the [memory driver](https://unstorage.unjs.io/drivers/memory). **Example:** Simple (in memory) operations ```js await useStorage().setItem('test:foo', { hello: 'world' }) await useStorage().getItem('test:foo') -``` -## Defining Mountpoints +// Could can also specific the base in useStorage(base) +await useStorage('test').setItem('foo', { hello: 'world' }) +await useStorage('test').getItem('foo') +``` -By default storage is in-memory with mounted `cache:` prefix only for development. +See [Unstorage](https://unstorage.unjs.io/usage) for detailed usage. -```js -await useStorage().setItem('cache:foo', { hello: 'world' }) -await useStorage().getItem('cache:foo') -``` +## Mountpoints -You can mount other storage drivers through the Nitro config using the `storage` option: +You can mount storage drivers using the `storage` option: -```js -// nitro.config.ts +::code-group +```ts [nitro.config.ts] import { defineNitroConfig } from 'nitropack' export default defineNitroConfig({ @@ -39,35 +38,85 @@ export default defineNitroConfig({ } }) ``` +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + nitro: { + storage: { + redis: { + driver: 'redis', + /* redis connector options */ + }, + db: { + driver: 'fs', + base: './data/db' + } + } + } +}) +``` +:: Usage: ```js +await useStorage('redis').setItem('foo', { hello: 'world' }) +await useStorage('redis').getItem('foo') +// or await useStorage().setItem('redis:foo', { hello: 'world' }) await useStorage().getItem('redis:foo') ``` -You can find the list of drivers [on the unstorage repository](https://github.com/unjs/unstorage#drivers). +You can find the list of drivers on [unstorage documentation](https://unstorage.unjs.io/). -## DevStorage - -You can use the `devStorage` key to overwrite the storage configuration during development. +In development, Nitro adds the `cache` mountpoint using the [FS driver](https://unstorage.unjs.io/drivers/fs) writting to `.nitro/cache` or `.nuxt/cache` if using [Nuxt](https://nuxt.com). ```js +await useStorage('cache').setItem('foo', { hello: 'world' }) +await useStorage('cache').getItem('foo') +``` + + +## Development storage + +You can use the `devStorage` key to overwrite the storage configuration during development, very useful when you use a database in production and want to use the filesystem in development. + +::code-group +```ts [nitro.config.ts] export default defineNitroConfig({ // Production storage: { - 'db': { + db: { driver: 'redis', /* redis connector options */ } } // Development devStorage: { - 'db': { + db: { driver: 'fs', base: './data/db' } } }) ``` +```ts [nuxt.config.ts] +export default defineNuxtConfig({ + nitro: { + // Production + storage: { + db: { + driver: 'redis', + /* redis connector options */ + } + } + // Development + devStorage: { + db: { + driver: 'fs', + base: './data/db' + } + } + } +}) +``` +:: diff --git a/src/rollup/plugins/storage.ts b/src/rollup/plugins/storage.ts index f686590bc2..09e468bd5d 100644 --- a/src/rollup/plugins/storage.ts +++ b/src/rollup/plugins/storage.ts @@ -43,14 +43,20 @@ for (const base of bundledStorage) { return virtual( { "#internal/nitro/virtual/storage": ` -import { createStorage } from 'unstorage' +import { createStorage, prefixStorage } from 'unstorage' import { assets } from '#internal/nitro/virtual/server-assets' ${driverImports.map((i) => genImport(i, genSafeVariableName(i))).join("\n")} const storage = createStorage({}) -export const useStorage = () => storage +export const useStorage = (base = '') => { + if (base) { + return prefixStorage(storage, base) + } + + return storage +} storage.mount('/assets', assets) diff --git a/src/runtime/virtual/storage.d.ts b/src/runtime/virtual/storage.d.ts index cabbb09d8b..2d85b38cbf 100644 --- a/src/runtime/virtual/storage.d.ts +++ b/src/runtime/virtual/storage.d.ts @@ -1,3 +1,3 @@ import type { Storage } from "unstorage"; -export declare const useStorage: () => Storage; +export declare const useStorage: (base?: string) => Storage;