KV 存储
¥KV Storage
Nitro 提供了一个内置的存储层,可以抽象文件系统、数据库或任何其他数据源。
Nitro 内置了与 unjs/unstorage 的集成,以提供与运行时无关的持久层。
¥Nitro has built-in integration with unjs/unstorage to provide a runtime agnostic persistent layer.
用法
¥Usage
要使用存储层,你可以使用 useStorage()
并调用 getItem(key)
来检索项目,并使用 setItem(key, value)
来设置项目。
¥To use the storage layer, you can use the useStorage()
and call getItem(key)
to retrieve an item and setItem(key, value)
to set an item.
// Default storage is in memory
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' })
// You can use data storage to write data to default .data/kv directory
const dataStorage = useStorage('data')
await dataStorage.setItem('test', 'works')
await dataStorage.getItem('data:test') // Value persists
// You can use generics to define types
await useStorage<{ hello: string }>('test').getItem('foo')
await useStorage('test').getItem<{ hello: string }>('foo')
配置
¥Configuration
你可以使用 storage
配置挂载一个或多个自定义存储驱动程序。键是挂载点名称,值是驱动程序名称和配置。
¥You can mount one or multiple custom storage drivers using the storage
config.
The key is the mount point name, and the value is the driver name and configuration.
export default defineNitroConfig({
storage: {
redis: {
driver: 'redis',
/* redis connector options */
},
db: {
driver: 'fs',
base: './data/db'
}
}
})
运行时配置
¥Runtime configuration
在运行时才知道挂载点配置的情况下,Nitro 可以在启动时使用 plugins 动态添加挂载点。
¥In scenarios where the mount point configuration is not known until runtime, Nitro can dynamically add mount points during startup using plugins.
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)
})
::warning 这是一个临时解决方法,未来会有更好的解决方案!请关注 GitHub 问题 此处。::
¥::warning This is a temporary workaround, with a better solution coming in the future! Keep a lookout on the GitHub issue here. ::
仅用于开发的挂载点
¥Development-only mount points
默认情况下,Nitro 会在开发时使用文件系统驱动程序挂载项目目录和其他一些目录。
¥By default, Nitro will mount the project directory and some other dirs using the filesystem driver in development time.
// Access to project root dir
const rootStorage = useStorage('root')
// Access to project src dir (same as root by default)
const srcStorage = useStorage('src')
// Access to server cache dir
const cacheStorage = useStorage('cache')
// Access to the temp build dir
const buildStorage = useStorage('build')
devStorage
键覆盖存储配置。当你在生产环境中使用数据库,并在开发环境中使用文件系统时,这非常有用。为了使用 devStorage
键,你需要使用 nitro dev
命令,并且 storage
选项中的键必须与生产环境中的键相同。
¥In order to use the devStorage
key, you need to use the nitro dev
command and the key in the storage
option must be the same as the production one.
export default defineNitroConfig({
// Production
storage: {
db: {
driver: 'redis',
/* redis connector options */
}
},
// Development
devStorage: {
db: {
driver: 'fs',
base: './data/db'
}
}
})
你还只能在开发期间访问存储层中的 build
命名空间。它包含 Nitro 生成的文件。
¥You will also be able to access to a build
namespace in the storage layer only during development. It contains file generated by Nitro.