@feathersjs-offline/localforage

中文文档
1.1.1发布于 2021/11/19MITTypeScriptCommonJS近 30 天下载 3407

A FeathersJS client side service based on localforage that persists to IndexedDB, WebSQL, or LocalStorage

原始使用文档
文档目录6 节
  1. API
  2. service(options)
  3. Storing Blobs, TypedArrays, and other JS objects
  4. Example
  5. Browser
  6. License

@feathersjs-offline/localforage

npm version Build Status Known Vulnerabilities Maintainability Test Coverage Download Status

@featherjs-offline/localforage is a database service adapter wrapping localForage that persists to either IndexedDB, WebSQL, or LocalStorage making it very useful for mobile and offline-first applications with the additional ability to seamlessly handle Blobs, TypedArrays, and other JS objects.

$ npm install --save @feathersjs-offline/localforage

Important: @feathersjs-offline/localforage implements the Feathers Common database adapter API and querying syntax.

API

service(options)

Returns a new service instance initialized with the given options.

const service = require('@feathersjs-offline/localforage');

app.use('/messages', service({
  storage: ['IndexedDB', 'localStorage']
}));
app.use('/messages', service({ storage, id, startId, name, store, paginate }));

Options:

  • storage (optional, default: 'INDEXEDDB') - The storage backend. Must be one or more of 'INDEXEDDB', 'WEBSQL', or 'LOCALSTORAGE'. The adapter will use the same sequence as fall-back if the desired storage type is not supported on the actual device. Alternatively, you can supply an array of storage backends determining the priority of your choice.
  • version (optional, default: 1.0) - localforage driver version to use. Currently only 1.0 exists.
  • size (optional, default 4980736) - The maximum database size required. Default DB size is JUST UNDER 5MB, as it's the highest size we can use without a prompt in any browser.
  • id (optional, default: 'id') - The name of the id field property.
  • name (optional, default: 'feathers') - The key to store data under in local or async storage.
  • store (optional) - An object with id to item assignments to pre-initialize the data store.
  • dates (optional, default false) - Convert ISO-formatted date strings to Date objects in result sets.
  • events (optional) - A list of custom service events sent by this service.
  • paginate (optional) - A pagination object containing a default and max page size.
  • whitelist (optional) - A list of additional query parameters to allow.
  • multi (optional) - Allow create with arrays and update and remove with id null to change multiple items. Can be true for all methods or an array of allowed methods (e.g. [ 'remove', 'create' ]).
  • reuseKeys (optional, default: false) Allow duplicate keys (see name) i.e. last definition wins. Mostly useful for demonstration and testing purposes.

Storing Blobs, TypedArrays, and other JS objects

As this is an implementation on top of localForage you can store any type in @feathersjs-offline/localforage; you aren't limited to strings like in localStorage. Even if localStorage is your storage backend, @feathersjs-offline/localforage automatically does JSON.parse() and JSON.stringify() when getting/setting values.

feathers-localforage supports storing all native JS objects that can be serialized to JSON, as well as ArrayBuffers, Blobs, and TypedArrays. Check the localForage API docs for a full list of types supported. In addition, setting the option dates to true will make sure any ISO-formatted dates in your results will in fact be date objects and not text strings.

All types are supported in every storage backend, though storage limits in localStorage make storing many large Blobs impossible.

We default to indexedDB if available and fall-back to localStorage as a last resort.

Example

See the clients chapter for more information about using Feathers in the browser and React Native.

Browser

<script type="text/javascript" src="//unpkg.com/@feathersjs/client@^4.5.11/dist/feathers.js"></script>
<script type="text/javascript" src="//unpkg.com/@feathersjs-offline/localforage@^1.0.0/dist/localforage.js"></script>
<script type="text/javascript">
  var service = feathersjsOfflineLocalforage.init({
    storage: ['indexeddb', 'websql', 'localStorage']
  });
  var app = feathers().use('/messages', service);

  var messages = app.service('messages');

  messages.on('created', function(message) {
    console.log('Someone created a message', message);
  });

  messages.create({
    text: 'Message created in browser'
  });
</script>

License

Copyright (c) 2021 by Feathers

Licensed under the MIT license.

版本与发布记录

@feathersjs-offline/localforage 共发布 6 个版本,首次发布于 2021/10/23;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.1.1latest2021/11/194 年前
1.0.42021/10/285 年前
1.0.32021/10/265 年前
1.0.22021/10/265 年前
1.0.12021/10/255 年前
1.0.02021/10/235 年前

关于 @feathersjs-offline/localforage 的常见问题

@feathersjs-offline/localforage 是什么?

A FeathersJS client side service based on localforage that persists to IndexedDB, WebSQL, or LocalStorage

如何安装 @feathersjs-offline/localforage?

在项目目录运行 npm install @feathersjs-offline/localforage,或使用 pnpm add / yarn add / bun add。

@feathersjs-offline/localforage 支持 TypeScript 吗?

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

@feathersjs-offline/localforage 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@feathersjs-offline/localforage 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >= 12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@feathersjs-offline/localforage 可以商用吗?

@feathersjs-offline/localforage 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@feathersjs-offline/localforage 的下载量有多少?

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

@feathersjs-offline/localforage 还在维护吗?

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