# @fmal/cerebral-storage 中文文档

规范地址：https://npmcn.com/package/%40fmal/cerebral-storage/

版本：1\.1\.0
npm 元数据更新时间：2022-06-07T13:41:38.967Z
中文译文更新时间：2026-11-15T03:11:29.798Z
软件包许可：UNLICENSED

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%40fmal/cerebral-storage
原文对照：https://npmcn.com/package/%40fmal/cerebral-storage/?lang=original
项目主页：https://github.com/fmal/monorepo#readme
源码仓库：https://github.com/fmal/monorepo

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：26 kB，35 个文件
- 模块格式：ESM + CJS
- TypeScript 类型：包内自带
- 运行依赖：1 个
- peer 依赖：cerebral ^5\.0\.0 \|\| ^5\.2\.2-0
- 首次发布：2019-10-14T10:55:16.824Z
- 最新版本发布：2022-06-07T13:41:38.890Z
- 近 30 天下载量：9235（2026-10-15 至 2026-11-13，npm 官方下载统计）
- 已发布版本数：9（近 12 个月 0 个）

## 中文简介

大脑存储提供者

## 安装

```sh
npm install @fmal/cerebral-storage
```

也可以使用：`pnpm add @fmal/cerebral-storage`、`yarn add @fmal/cerebral-storage`、`bun add @fmal/cerebral-storage`

## README

# @fmal/大脑存储器

[![npm](https://img.shields.io/npm/v/@fmal/cerebral-storage.svg?style=flat-square)](https://www.npmjs.com/package/@fmal/cerebral-storage)

原为 `@cerebral/storage`

## 安装

`npm install @fmal/cerebral-storage`

## 说明

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

## 证据

```js
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);
```

## 错误

### 存储器错误

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

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

## 起来

从存储中获取数据.

_动作_

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

_运算符_

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

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

## 删除

从存储中删除数据 .

_动作_

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

_运算符_

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

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

## 设定

将数据写入存储 .

_动作_

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

_运算符_

```javascript
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 是什么？

大脑存储提供者

### 如何安装 @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 和提交记录判断。

