localforage vs levelup vs lowdb vs nedb vs node-persist vs pouchdb
JavaScript 資料儲存庫
localforageleveluplowdbnedbnode-persistpouchdb類似套件:

JavaScript 資料儲存庫

這些 NPM 套件提供了不同的資料儲存解決方案,適用於各種應用場景。它們各自具有獨特的功能和設計理念,幫助開發者在客戶端或伺服器端有效地管理資料。無論是需要持久化資料、簡單的 JSON 儲存,還是需要更複雜的資料庫功能,這些套件都能提供合適的解決方案。

npm下載趨勢

3 年

GitHub Stars 排名

統計詳情

套件
下載數
Stars
大小
Issues
發布時間
許可
localforage11,392,87625,806-2515 年前Apache-2.0
levelup04,066-05 年前MIT
lowdb022,58222.9 kB173 年前MIT
nedb013,524-20811 年前SEE LICENSE IN LICENSE
node-persist072640.1 kB172 年前MIT
pouchdb017,6255.53 MB1962 年前Apache-2.0

功能比較: localforage vs levelup vs lowdb vs nedb vs node-persist vs pouchdb

資料儲存方式

  • localforage:

    LocalForage 支援多種儲存方式,包括 IndexedDB、WebSQL 和 localStorage,並自動選擇最佳的儲存方式。

  • levelup:

    LevelUP 是基於 LevelDB 的,提供高效能的鍵值儲存,適合需要快速讀取和寫入的應用。

  • lowdb:

    LowDB 使用 JSON 檔案作為儲存,適合小型應用,資料結構簡單易用。

  • nedb:

    NeDB 以文件為基礎,提供類似 MongoDB 的 API,適合需要簡單查詢的應用。

  • node-persist:

    Node-Persist 使用文件系統進行資料儲存,簡單易用,適合 Node.js 環境。

  • pouchdb:

    PouchDB 是一個可以在客戶端和伺服器端使用的資料庫,支援離線儲存和資料同步。

API 設計

  • localforage:

    LocalForage 提供簡單的 Promise 基礎 API,易於使用,適合前端開發者。

  • levelup:

    LevelUP 提供流式 API,讓資料操作更加靈活,適合需要高效能的應用。

  • lowdb:

    LowDB 提供簡單的 CRUD 操作,使用 JavaScript 物件進行資料管理,易於上手。

  • nedb:

    NeDB 提供與 MongoDB 類似的 API,方便開發者快速上手,適合小型專案。

  • node-persist:

    Node-Persist 提供簡單的鍵值對 API,易於使用,適合快速開發。

  • pouchdb:

    PouchDB 提供強大的資料同步和查詢功能,適合需要離線支援的應用。

性能

  • localforage:

    LocalForage 的性能依賴於底層儲存方式,通常在 IndexedDB 上表現最佳。

  • levelup:

    LevelUP 的性能非常高,適合需要快速讀取和寫入的應用,特別是在處理大量資料時。

  • lowdb:

    LowDB 在小型應用中表現良好,但在處理大量資料時性能可能下降。

  • nedb:

    NeDB 在小型資料集上表現良好,但在大型資料集上性能可能受到影響。

  • node-persist:

    Node-Persist 的性能取決於文件系統的速度,通常適合小型應用。

  • pouchdb:

    PouchDB 在客戶端和伺服器端都能提供良好的性能,特別是在資料同步方面。

使用場景

  • localforage:

    適合需要在瀏覽器中儲存資料的前端應用,特別是需要離線功能的應用。

  • levelup:

    適合需要高效能資料儲存的應用,如即時數據處理或大型資料集。

  • lowdb:

    適合小型專案或原型設計,快速開發和測試。

  • nedb:

    適合桌面應用或小型伺服器應用,簡單的資料管理需求。

  • node-persist:

    適合需要簡單資料儲存的 Node.js 應用,快速開發。

  • pouchdb:

    適合需要離線支援和資料同步的應用,特別是移動應用。

擴展性

  • localforage:

    LocalForage 可以與其他前端儲存庫一起使用,提供更靈活的資料管理。

  • levelup:

    LevelUP 可以與其他 LevelDB 的擴展庫一起使用,提供更多功能。

  • lowdb:

    LowDB 的擴展性有限,但對於小型應用來說已經足夠。

  • nedb:

    NeDB 的擴展性有限,適合小型應用,但不適合大型資料庫需求。

  • node-persist:

    Node-Persist 的擴展性有限,主要用於簡單的資料儲存。

  • pouchdb:

    PouchDB 支援多種擴展功能,特別是在資料同步和查詢方面。

如何選擇: localforage vs levelup vs lowdb vs nedb vs node-persist vs pouchdb

  • localforage:

    選擇 LocalForage 如果你需要一個簡單的 API 來在瀏覽器中儲存資料,並且希望它能自動選擇最佳的儲存方式(如 IndexedDB 或 WebSQL)。

  • levelup:

    選擇 LevelUP 當你需要一個基於 LevelDB 的高效能資料儲存解決方案,並且希望利用其流式 API 來進行資料操作。

  • lowdb:

    選擇 LowDB 當你需要一個輕量級的 JSON 檔案資料庫,適合小型應用或原型設計,並且希望使用簡單的 JavaScript 物件來進行資料操作。

  • nedb:

    選擇 NeDB 如果你需要一個輕量級的嵌入式資料庫,並且希望使用 MongoDB 的 API 來進行資料操作,適合小型應用或桌面應用。

  • node-persist:

    選擇 Node-Persist 當你需要一個簡單的文件系統資料儲存解決方案,並且希望能夠輕鬆地在 Node.js 環境中使用。

  • pouchdb:

    選擇 PouchDB 如果你需要一個可以在客戶端和伺服器端都運行的資料庫,並且希望能夠與 CouchDB 同步資料。

localforage 的 README

localForage

Build Status NPM version Dependency Status npm jsDelivr Hits minzipped size

localForage is a fast and simple storage library for JavaScript. localForage improves the offline experience of your web app by using asynchronous storage (IndexedDB or WebSQL) with a simple, localStorage-like API.

localForage uses localStorage in browsers with no IndexedDB or WebSQL support. See the wiki for detailed compatibility info.

To use localForage, just drop a single JavaScript file into your page:

<script src="localforage/dist/localforage.js"></script>
<script>localforage.getItem('something', myCallback);</script>

Try the live example.

Download the latest localForage from GitHub, or install with npm:

npm install localforage

Support

Lost? Need help? Try the localForage API documentation. localForage API文档也有中文版。

If you're having trouble using the library, running the tests, or want to contribute to localForage, please look through the existing issues for your problem first before creating a new one. If you still need help, feel free to file an issue.

How to use localForage

Callbacks vs Promises

Because localForage uses async storage, it has an async API. It's otherwise exactly the same as the localStorage API.

localForage has a dual API that allows you to either use Node-style callbacks or Promises. If you are unsure which one is right for you, it's recommended to use Promises.

Here's an example of the Node-style callback form:

localforage.setItem('key', 'value', function (err) {
  // if err is non-null, we got an error
  localforage.getItem('key', function (err, value) {
    // if err is non-null, we got an error. otherwise, value is the value
  });
});

And the Promise form:

localforage.setItem('key', 'value').then(function () {
  return localforage.getItem('key');
}).then(function (value) {
  // we got our value
}).catch(function (err) {
  // we got an error
});

Or, use async/await:

try {
    const value = await localforage.getItem('somekey');
    // This code runs once the value has been loaded
    // from the offline store.
    console.log(value);
} catch (err) {
    // This code runs if there were any errors.
    console.log(err);
}

For more examples, please visit the API docs.

Storing Blobs, TypedArrays, and other JS objects

You can store any type in localForage; you aren't limited to strings like in localStorage. Even if localStorage is your storage backend, localForage automatically does JSON.parse() and JSON.stringify() when getting/setting values.

localForage supports storing all native JS objects that can be serialized to JSON, as well as ArrayBuffers, Blobs, and TypedArrays. Check the API docs for a full list of types supported by localForage.

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

Configuration

You can set database information with the config() method. Available options are driver, name, storeName, version, size, and description.

Example:

localforage.config({
    driver      : localforage.WEBSQL, // Force WebSQL; same as using setDriver()
    name        : 'myApp',
    version     : 1.0,
    size        : 4980736, // Size of database, in bytes. WebSQL-only for now.
    storeName   : 'keyvaluepairs', // Should be alphanumeric, with underscores.
    description : 'some description'
});

Note: you must call config() before you interact with your data. This means calling config() before using getItem(), setItem(), removeItem(), clear(), key(), keys() or length().

Multiple instances

You can create multiple instances of localForage that point to different stores using createInstance. All the configuration options used by config are supported.

var store = localforage.createInstance({
  name: "nameHere"
});

var otherStore = localforage.createInstance({
  name: "otherName"
});

// Setting the key on one of these doesn't affect the other.
store.setItem("key", "value");
otherStore.setItem("key", "value2");

RequireJS

You can use localForage with RequireJS:

define(['localforage'], function(localforage) {
    // As a callback:
    localforage.setItem('mykey', 'myvalue', console.log);

    // With a Promise:
    localforage.setItem('mykey', 'myvalue').then(console.log);
});

TypeScript

If you have the allowSyntheticDefaultImports compiler option set to true in your tsconfig.json (supported in TypeScript v1.8+), you should use:

import localForage from "localforage";

Otherwise you should use one of the following:

import * as localForage from "localforage";
// or, in case that the typescript version that you are using
// doesn't support ES6 style imports for UMD modules like localForage
import localForage = require("localforage");

Framework Support

If you use a framework listed, there's a localForage storage driver for the models in your framework so you can store data offline with localForage. We have drivers for the following frameworks:

If you have a driver you'd like listed, please open an issue to have it added to this list.

Custom Drivers

You can create your own driver if you want; see the defineDriver API docs.

There is a list of custom drivers on the wiki.

Working on localForage

You'll need node/npm and bower.

To work on localForage, you should start by forking it and installing its dependencies. Replace USERNAME with your GitHub username and run the following:

# Install bower globally if you don't have it:
npm install -g bower

# Replace USERNAME with your GitHub username:
git clone [email protected]:USERNAME/localForage.git
cd localForage
npm install
bower install

Omitting the bower dependencies will cause the tests to fail!

Running Tests

You need PhantomJS installed to run local tests. Run npm test (or, directly: grunt test). Your code must also pass the linter.

localForage is designed to run in the browser, so the tests explicitly require a browser environment. Local tests are run on a headless WebKit (using PhantomJS).

When you submit a pull request, tests will be run against all browsers that localForage supports on Travis CI using Sauce Labs.

Library Size

As of version 1.7.3 the payload added to your app is rather small. Served using gzip compression, localForage will add less than 10k to your total bundle size:

minified
`~29kB`
gzipped
`~8.8kB`
brotli'd
`~7.8kB`

License

This program is free software; it is distributed under an Apache License.


Copyright (c) 2013-2016 Mozilla (Contributors).