Storage Layer
Nitro provides a built-in storage layer that can abstract filesystem or database or any other data source.
useStorage()
is an instance of createStorage using the memory driver.
Example: Simple (in memory) operations
await useStorage().setItem('test:foo', { hello: 'world' })await useStorage().getItem('test:foo')// You can also specify the base in useStorage(base)await useStorage('test').setItem('foo', { hello: 'world' })await useStorage('test').getItem('foo')
See Unstorage for detailed usage.
Mountpoints
You can mount storage drivers using the storage
option:
nitro.config.ts
import { defineNitroConfig } from 'nitropack'export default defineNitroConfig({ storage: { 'redis': { driver: 'redis', /* redis connector options */ }, 'db': { driver: 'fs', base: './data/db' } }})
Usage:
await useStorage('redis').setItem('foo', { hello: 'world' })await useStorage('redis').getItem('foo')// orawait useStorage().setItem('redis:foo', { hello: 'world' })await useStorage().getItem('redis:foo')
You can find the list of drivers on unstorage documentation.
In development, Nitro adds the cache
mountpoint using the FS driver writting to .nitro/cache
or .nuxt/cache
if using Nuxt.
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.
nitro.config.ts
export default defineNitroConfig({ // Production storage: { db: { driver: 'redis', /* redis connector options */ } } // Development devStorage: { db: { driver: 'fs', base: './data/db' } }})
Table of Contents