Nitro provides a built-in storage layer that can abstract filesystem or database or any other data source.
See unjs/unstorage for more usage information.
Example: Simple (in memory) operations
await useStorage().setItem('test:foo', { hello: 'world' })await useStorage().getItem('test:foo')
By default storage is in-memory with mounted cache:
prefix only for development.
await useStorage().setItem('cache:foo', { hello: 'world' })await useStorage().getItem('cache:foo')
You can mount other storage drivers through the Nitro config using the storage
option:
// nitro.config.tsimport { defineNitroConfig } from 'nitropack'export default defineNitroConfig({ storage: { 'redis': { driver: 'redis', /* redis connector options */ }, 'db': { driver: 'fs', base: './data/db' } }})
Usage:
await useStorage().setItem('redis:foo', { hello: 'world' })await useStorage().getItem('redis:foo')
You can find the list of drivers on the unstorage repository.
You can use the devStorage
key to overwrite the storage configuration during development.
export default defineNitroConfig({ // Production storage: { 'db': { driver: 'redis', /* redis connector options */ } } // Development devStorage: { 'db': { driver: 'fs', base: './data/db' } }})