@fmal/cerebral-storage

中文文档
1.1.0发布于 2022/06/07UNLICENSEDTypeScriptESM + CJS近 30 天下载 9235

大脑存储提供者

中文使用文档
文档目录8 节
  1. 安装
  2. 说明
  3. 证据
  4. 错误
  5. 存储器错误
  6. 起来
  7. 删除
  8. 设定

@fmal/大脑存储器

npm

原为 @cerebral/storage

安装

npm install @fmal/cerebral-storage

说明

本模块显示本地存储或会话存储为提供者, 默认地将它解析成JSON.

证据

import App from 'cerebral';
import StorageModule from '@fmal/cerebral-storage';

const storage = StorageModule({
  // instance of storage, can be window.localStorage / localStorage,
  // window.sessionStorage / sessionStorage, or asyncStorage on
  // react native. Async storage API is automatically managed
  target: localStorage,
  // Serializes and parses to JSON by default
  json: true,
  // Synchronize state when it changes
  sync: {
    someStorageKey: 'some.state.path'
  },
  // Set prefix for storagekey "somePrefix.someStorageKey"
  prefix: 'somePrefix'
});

const main = {
  modules: { storage }
});

const app = App(main);

错误

存储器错误

import { StorageProviderError } from '@fmal/cerebral-storage'

// Error structure
{
  name: 'StorageProviderError',
  message: 'Some storage error'
  stack: '...'
}

起来

从存储中获取数据.

动作

function someAction({ storage }) {
  // sync
  const value = storage.get('someKey');
  // async
  return storage.get('someKey').then(value => ({ value }));
}

运算符

import { getStorage } from '@fmal/cerebral-storage/factories';

export default [
  // sync and async
  getStorage('someKey'),
  function someAction({ props }) {
    props.value; // Whatever was on "someKey"
  }
];

删除

从存储中删除数据 .

动作

function someAction({ storage }) {
  // sync
  storage.remove('someKey');
  // async
  return storage.remove('someKey');
}

运算符

import { state } from 'cerebral';
import { removeStorage } from '@fmal/cerebral-storage/factories';

export default [
  // sync and async
  removeStorage(state`currentStorageKey`)
];

设定

将数据写入存储 .

动作

function someAction({ storage }) {
  // sync
  storage.set('someKey', { foo: 'bar' });
  // async
  return storage.set('someKey', { foo: 'bar' });
}

运算符

import { state, props } from 'cerebral';
import { setStorage } from '@fmal/cerebral-storage/factories';

export default [
  // sync and async
  setStorage(state`currentStorageKey`, props`someData`)
];

版本与发布记录

@fmal/cerebral-storage 共发布 9 个版本,首次发布于 2019/10/14;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.0latest2022/06/074 年前
3.1.02022/06/074 年前
1.0.3-1654608821202next2022/06/074 年前
1.0.22020/10/176 年前
1.0.2-16029488495712020/10/176 年前
1.0.12020/01/036 年前

关于 @fmal/cerebral-storage 的常见问题

@fmal/cerebral-storage 是什么?

大脑存储提供者

如何安装 @fmal/cerebral-storage?

在项目目录运行 npm install @fmal/cerebral-storage,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:cerebral(^5.0.0 || ^5.2.2-0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@fmal/cerebral-storage 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@fmal/cerebral-storage 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@fmal/cerebral-storage 可以商用吗?

@fmal/cerebral-storage 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@fmal/cerebral-storage 的下载量有多少?

根据 npm 官方下载统计,@fmal/cerebral-storage 在 2026/10/15 – 2026/11/13 这 30 天内被下载 9,235 次,日均约 308 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@fmal/cerebral-storage 还在维护吗?

最新版本 1.1.0 发布于 2022/06/07(4 年前);至今共发布 9 个版本,其中过去 12 个月发布了 0 个;首次发布于 2019/10/14。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。