sqlite3 vs lowdb vs mongodb-memory-server vs levelup vs nedb vs pouchdb
データベースとストレージ
sqlite3lowdbmongodb-memory-serverlevelupnedbpouchdb類似パッケージ:

データベースとストレージ

データベースとストレージライブラリは、Node.jsアプリケーション内でデータを保存、取得、管理するためのツールです。これらのライブラリは、ファイルベースのストレージから、NoSQLデータベース、リレーショナルデータベースまで、さまざまなデータストレージソリューションを提供します。選択するライブラリは、アプリケーションの要件、データの構造、スケーラビリティのニーズ、トランザクションの必要性などに依存します。

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
sqlite32,277,0586,4243.35 MB1742年前BSD-3-Clause
lowdb1,984,51022,47222.9 kB152年前MIT
mongodb-memory-server1,088,7742,8074.54 kB152ヶ月前MIT
levelup737,5444,079-04年前MIT
nedb118,10313,565-20910年前SEE LICENSE IN LICENSE
pouchdb54,18917,5475.53 MB1642年前Apache-2.0

機能比較: sqlite3 vs lowdb vs mongodb-memory-server vs levelup vs nedb vs pouchdb

ストレージタイプ

  • sqlite3:

    sqlite3は、リレーショナルデータベースで、データはテーブル形式で格納されます。SQLクエリを使用してデータを操作します。

  • lowdb:

    lowdbは、JSONファイルを使用したスキーマレスなストレージです。データはオブジェクトとして格納され、簡単に読み書きできます。

  • mongodb-memory-server:

    mongodb-memory-serverは、メモリ内でMongoDBのデータを管理します。データは一時的で、アプリケーションが終了すると消えます。

  • levelup:

    levelupは、キー・バリュー型のストレージを提供し、LevelDBをベースにしています。データはキーに基づいて格納され、順序付けられた状態で保存されます。

  • nedb:

    nedbは、ファイルベースのNoSQLデータベースで、データはスキーマレスの形式で保存されます。データはJSON形式でファイルに保存されます。

  • pouchdb:

    pouchdbは、ドキュメント指向のNoSQLデータベースで、データはJSON形式のドキュメントとして格納されます。

データ永続性

  • sqlite3:

    sqlite3は、データをファイルに保存するため、永続性があります。データはSQLデータベースファイルに格納されます。

  • lowdb:

    lowdbは、データをJSONファイルに永続化します。ファイルに直接読み書きするため、データの永続性が保証されます。

  • mongodb-memory-server:

    mongodb-memory-serverは、メモリ内でデータを管理するため、永続性はありません。アプリケーションが終了するとデータは失われます。

  • levelup:

    levelupは、データをディスクに永続化しますが、データベースの構造はLevelDBに依存します。

  • nedb:

    nedbは、データをファイルに保存するため、永続性があります。データはJSON形式でファイルに保存されます。

  • pouchdb:

    pouchdbは、データをローカルデバイスに保存し、必要に応じてサーバーと同期できます。

トランザクションサポート

  • sqlite3:

    sqlite3は、完全なトランザクションサポートを提供します。

  • lowdb:

    lowdbは、トランザクションをサポートしていませんが、JSONファイルへの書き込みは原子的です。

  • mongodb-memory-server:

    mongodb-memory-serverは、MongoDBのトランザクション機能をサポートしています。

  • levelup:

    levelupは、トランザクションをサポートしていませんが、LevelDB自体は原子的な書き込み操作を提供します。

  • nedb:

    nedbは、トランザクションをサポートしていませんが、原子的な操作が可能です。

  • pouchdb:

    pouchdbは、トランザクションをサポートしていませんが、複数のドキュメントを原子的に更新できます。

オフラインサポート

  • sqlite3:

    sqlite3は、オフライン環境での使用に適しています。

  • lowdb:

    lowdbは、オフライン環境での使用に適しています。

  • mongodb-memory-server:

    mongodb-memory-serverは、オフライン環境での使用には適していません。

  • levelup:

    levelupはオフラインサポートを提供しませんが、データはローカルに保存されます。

  • nedb:

    nedbは、オフライン環境での使用に適しています。

  • pouchdb:

    pouchdbは、オフラインファーストのアプローチを採用しており、オフラインでのデータ操作が可能です。

Ease of Use: Code Examples

  • sqlite3:

    sqlite3の基本的な使用例

    const sqlite3 = require('sqlite3').verbose();
    const db = new sqlite3.Database(':memory:');
    
    db.serialize(() => {
      db.run('CREATE TABLE user (id INT, name TEXT)');
      const stmt = db.prepare('INSERT INTO user VALUES (?, ?)');
      stmt.run(1, 'Alice');
      stmt.finalize();
      db.each('SELECT * FROM user', (err, row) => {
        console.log(row);
      });
    });
    
    db.close();
    
  • lowdb:

    lowdbの基本的な使用例

    const { Low, JSONFile } = require('lowdb');
    
    const db = new Low(new JSONFile('db.json'));
    
    async function main() {
      await db.read();
      db.data ||= { posts: [] };
      db.data.posts.push({ id: 1, title: '低DBを使う' });
      await db.write();
      console.log(db.data);
    }
    main();
    
  • mongodb-memory-server:

    mongodb-memory-serverの基本的な使用例

    const { MongoMemoryServer } = require('mongodb-memory-server');
    const mongoose = require('mongoose');
    
    async function run() {
      const mongoServer = await MongoMemoryServer.create();
      await mongoose.connect(mongoServer.getUri());
      // データベース操作
      await mongoose.disconnect();
      await mongoServer.stop();
    }
    run();
    
  • levelup:

    levelupの基本的な使用例

    const levelup = require('levelup');
    const leveldown = require('leveldown');
    
    const db = levelup(leveldown('./mydb'));
    
    db.put('key', 'value', (err) => {
      if (err) return console.error('Error putting data:', err);
      db.get('key', (err, value) => {
        if (err) return console.error('Error getting data:', err);
        console.log('Value:', value);
      });
    });
    
  • nedb:

    nedbの基本的な使用例

    const Datastore = require('nedb');
    const db = new Datastore({ filename: 'data.db', autoload: true });
    
    db.insert({ name: 'Alice' }, (err, newDoc) => {
      db.find({}, (err, docs) => {
        console.log(docs);
      });
    });
    
  • pouchdb:

    pouchdbの基本的な使用例

    const PouchDB = require('pouchdb');
    const db = new PouchDB('mydb');
    
    db.put({ _id: 'doc1', title: 'PouchDBを使う' }).then(() => {
      return db.get('doc1');
    }).then((doc) => {
      console.log(doc);
    });
    

選び方: sqlite3 vs lowdb vs mongodb-memory-server vs levelup vs nedb vs pouchdb

  • sqlite3:

    sqlite3は、軽量で自己完結型のリレーショナルデータベースで、ファイルベースのデータストレージを提供します。小規模から中規模のアプリケーションに適しており、トランザクションサポートやSQLクエリが利用できるため、データの整合性が重要なプロジェクトに向いています。

  • lowdb:

    lowdbは、JSONファイルを使用したシンプルで軽量なデータベースです。小規模なプロジェクトやプロトタイプ、設定データの保存に適しています。スキーマが不要で、簡単にデータを読み書きできるため、迅速な開発が可能です。

  • mongodb-memory-server:

    mongodb-memory-serverは、メモリ内でMongoDBインスタンスを提供するライブラリで、テスト環境でのMongoDBの使用に最適です。実際のデータベースを使用せずにテストを行いたい場合に便利で、クリーンな状態でテストを実行できます。

  • levelup:

    levelupは、LevelDBをラップしたストレージライブラリで、高速でスケーラブルなキー・バリュー型ストレージが必要な場合に最適です。特に、データの順序が重要で、低レベルのストレージ操作を行いたい開発者に向いています。

  • nedb:

    nedbは、Node.jsおよびブラウザ向けの軽量なファイルベースのNoSQLデータベースです。小規模なアプリケーションやデスクトップアプリケーションに適しており、簡単にセットアップでき、スキーマレスなデータストレージを提供します。

  • pouchdb:

    pouchdbは、クライアントサイドとサーバーサイドの両方で動作するNoSQLデータベースで、オフラインファーストのアプリケーションに最適です。CouchDBとの同期機能があり、オフライン環境でもデータを操作できるため、モバイルアプリや不安定なネットワーク環境に適しています。

sqlite3 のREADME

⚙️ node-sqlite3

Asynchronous, non-blocking SQLite3 bindings for Node.js.

Latest release Build Status FOSSA Status N-API v3 Badge N-API v6 Badge

Features

Installing

You can use npm or yarn to install sqlite3:

  • (recommended) Latest published package:
npm install sqlite3
# or
yarn add sqlite3
  • GitHub's master branch: npm install https://github.com/tryghost/node-sqlite3/tarball/master

Prebuilt binaries

sqlite3 v5+ was rewritten to use Node-API so prebuilt binaries do not need to be built for specific Node versions. sqlite3 currently builds for both Node-API v3 and v6. Check the Node-API version matrix to ensure your Node version supports one of these. The prebuilt binaries should be supported on Node v10+.

The module uses prebuild-install to download the prebuilt binary for your platform, if it exists. These binaries are hosted on GitHub Releases for sqlite3 versions above 5.0.2, and they are hosted on S3 otherwise. The following targets are currently provided:

  • darwin-arm64
  • darwin-x64
  • linux-arm64
  • linux-x64
  • linuxmusl-arm64
  • linuxmusl-x64
  • win32-ia32
  • win32-x64

Unfortunately, prebuild cannot differentiate between armv6 and armv7, and instead uses arm as the {arch}. Until that is fixed, you will still need to install sqlite3 from source.

Support for other platforms and architectures may be added in the future if CI supports building on them.

If your environment isn't supported, it'll use node-gyp to build SQLite, but you will need to install a C++ compiler and linker.

Other ways to install

It is also possible to make your own build of sqlite3 from its source instead of its npm package (See below.).

The sqlite3 module also works with node-webkit if node-webkit contains a supported version of Node.js engine. (See below.)

SQLite's SQLCipher extension is also supported. (See below.)

API

See the API documentation in the wiki.

Usage

Note: the module must be installed before use.

const sqlite3 = require('sqlite3').verbose();
const db = new sqlite3.Database(':memory:');

db.serialize(() => {
    db.run("CREATE TABLE lorem (info TEXT)");

    const stmt = db.prepare("INSERT INTO lorem VALUES (?)");
    for (let i = 0; i < 10; i++) {
        stmt.run("Ipsum " + i);
    }
    stmt.finalize();

    db.each("SELECT rowid AS id, info FROM lorem", (err, row) => {
        console.log(row.id + ": " + row.info);
    });
});

db.close();

Source install

To skip searching for pre-compiled binaries, and force a build from source, use

npm install --build-from-source

The sqlite3 module depends only on libsqlite3. However, by default, an internal/bundled copy of sqlite will be built and statically linked, so an externally installed sqlite3 is not required.

If you wish to install against an external sqlite then you need to pass the --sqlite argument to npm wrapper:

npm install --build-from-source --sqlite=/usr/local

If building against an external sqlite3 make sure to have the development headers available. Mac OS X ships with these by default. If you don't have them installed, install the -dev package with your package manager, e.g. apt-get install libsqlite3-dev for Debian/Ubuntu. Make sure that you have at least libsqlite3 >= 3.6.

Note, if building against homebrew-installed sqlite on OS X you can do:

npm install --build-from-source --sqlite=/usr/local/opt/sqlite/

Custom file header (magic)

The default sqlite file header is "SQLite format 3". You can specify a different magic, though this will make standard tools and libraries unable to work with your files.

npm install --build-from-source --sqlite_magic="MyCustomMagic15"

Note that the magic must be exactly 15 characters long (16 bytes including null terminator).

Building for node-webkit

Because of ABI differences, sqlite3 must be built in a custom to be used with node-webkit.

To build sqlite3 for node-webkit:

  1. Install nw-gyp globally: npm install nw-gyp -g (unless already installed)

  2. Build the module with the custom flags of --runtime, --target_arch, and --target:

NODE_WEBKIT_VERSION="0.8.6" # see latest version at https://github.com/rogerwang/node-webkit#downloads
npm install sqlite3 --build-from-source --runtime=node-webkit --target_arch=ia32 --target=$(NODE_WEBKIT_VERSION)

You can also run this command from within a sqlite3 checkout:

npm install --build-from-source --runtime=node-webkit --target_arch=ia32 --target=$(NODE_WEBKIT_VERSION)

Remember the following:

  • You must provide the right --target_arch flag. ia32 is needed to target 32bit node-webkit builds, while x64 will target 64bit node-webkit builds (if available for your platform).

  • After the sqlite3 package is built for node-webkit it cannot run in the vanilla Node.js (and vice versa).

    • For example, npm test of the node-webkit's package would fail.

Visit the “Using Node modules” article in the node-webkit's wiki for more details.

Building for SQLCipher

For instructions on building SQLCipher, see Building SQLCipher for Node.js. Alternatively, you can install it with your local package manager.

To run against SQLCipher, you need to compile sqlite3 from source by passing build options like:

npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=/usr/

node -e 'require("sqlite3")'

If your SQLCipher is installed in a custom location (if you compiled and installed it yourself), you'll need to set some environment variables:

On OS X with Homebrew

Set the location where brew installed it:

export LDFLAGS="-L`brew --prefix`/opt/sqlcipher/lib"
export CPPFLAGS="-I`brew --prefix`/opt/sqlcipher/include/sqlcipher"
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=`brew --prefix`

node -e 'require("sqlite3")'

On most Linuxes (including Raspberry Pi)

Set the location where make installed it:

export LDFLAGS="-L/usr/local/lib"
export CPPFLAGS="-I/usr/local/include -I/usr/local/include/sqlcipher"
export CXXFLAGS="$CPPFLAGS"
npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=/usr/local --verbose

node -e 'require("sqlite3")'

Custom builds and Electron

Running sqlite3 through electron-rebuild does not preserve the SQLCipher extension, so some additional flags are needed to make this build Electron compatible. Your npm install sqlite3 --build-from-source command needs these additional flags (be sure to replace the target version with the current Electron version you are working with):

--runtime=electron --target=18.2.1 --dist-url=https://electronjs.org/headers

In the case of MacOS with Homebrew, the command should look like the following:

npm install sqlite3 --build-from-source --sqlite_libname=sqlcipher --sqlite=`brew --prefix` --runtime=electron --target=18.2.1 --dist-url=https://electronjs.org/headers

Testing

npm test

Contributors

Acknowledgments

Thanks to Orlando Vazquez, Eric Fredricksen and Ryan Dahl for their SQLite bindings for node, and to mraleph on Freenode's #v8 for answering questions.

This module was originally created by Mapbox & is now maintained by Ghost.

Changelog

We use GitHub releases for notes on the latest versions. See CHANGELOG.md in git history for details on older versions.

License

node-sqlite3 is BSD licensed.

FOSSA Status