knex vs sequelize vs bookshelf vs waterline
Node.js ORMライブラリ
knexsequelizebookshelfwaterline類似パッケージ:

Node.js ORMライブラリ

ORM(Object-Relational Mapping)ライブラリは、データベースとのやり取りを簡素化するためのツールです。これらのライブラリは、データベースのテーブルをオブジェクトとして扱い、SQLクエリを直接書くことなくデータの取得や保存を可能にします。これにより、開発者はデータベース操作をより直感的に行うことができ、コードの可読性と保守性が向上します。

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

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
knex3,112,80920,244874 kB7112年前MIT
sequelize2,437,97430,3512.91 MB1,01511日前MIT
bookshelf43,5746,364-2386年前MIT
waterline22,2915,4071.3 MB33-MIT

機能比較: knex vs sequelize vs bookshelf vs waterline

データベースサポート

  • knex:

    Knexは、PostgreSQL、MySQL、SQLite、Oracleなど、さまざまなデータベースをサポートしています。特に、複雑なSQLクエリを構築する際に強力です。

  • sequelize:

    Sequelizeは、PostgreSQL、MySQL、MariaDB、SQLite、Microsoft SQL Serverなど、広範なデータベースをサポートしています。多様なデータベースに対して一貫したAPIを提供します。

  • bookshelf:

    Bookshelfは、PostgreSQL、MySQL、SQLiteなどのリレーショナルデータベースをサポートしています。Knex.jsを使用しているため、これらのデータベースに対して簡単にクエリを実行できます。

  • waterline:

    Waterlineは、MongoDB、PostgreSQL、MySQL、Redisなど、複数のデータベースをサポートしています。異なるデータストアを統一的に扱うことができるのが特徴です。

リレーションシップ管理

  • knex:

    Knexは、リレーションシップ管理の機能を提供していませんが、SQLクエリを直接書くことで、複雑なリレーションを構築できます。

  • sequelize:

    Sequelizeは、リレーションシップの定義が非常に強力で、関連モデルの取得や操作が簡単です。多対多や1対多のリレーションを簡潔に定義できます。

  • bookshelf:

    Bookshelfは、モデル間のリレーションシップを簡単に定義できます。1対多、多対多のリレーションをサポートし、関連データの取得が容易です。

  • waterline:

    Waterlineは、リレーションシップをモデルに簡単に追加でき、関連データの取得もシンプルです。異なるデータストア間でのリレーションシップを管理するのに適しています。

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

  • knex:

    Knexは、トランザクションをサポートしており、複数のクエリを一つのトランザクションとして実行することができます。これにより、データの整合性を保つことができます。

  • sequelize:

    Sequelizeは、トランザクション管理が強力で、複雑なデータ操作を安全に実行できます。トランザクションの開始、コミット、ロールバックが簡単に行えます。

  • bookshelf:

    Bookshelfは、Knexを使用しているため、トランザクションをサポートしています。複数のデータベース操作を一つのトランザクションとして扱うことができます。

  • waterline:

    Waterlineは、トランザクションのサポートが限られていますが、アダプタに依存します。特定のデータストアでトランザクションがサポートされている場合に利用可能です。

学習曲線

  • knex:

    Knexは、SQLに近い構文を使用しているため、SQLに慣れている開発者には学習しやすいですが、ORMの機能は持っていません。

  • sequelize:

    Sequelizeは、多機能であるため、学習曲線がやや急ですが、豊富な機能を活用することで、複雑なアプリケーションを効率的に構築できます。

  • bookshelf:

    Bookshelfは、シンプルなAPIを提供しており、比較的学習しやすいですが、Knexの知識が必要です。

  • waterline:

    Waterlineは、シンプルなインターフェースを提供しており、学習しやすいですが、アダプタによって異なる挙動を理解する必要があります。

拡張性

  • knex:

    Knexは、SQLクエリビルダーとして非常に柔軟で、必要に応じて機能を追加できます。

  • sequelize:

    Sequelizeは、プラグインやカスタムバリデーションを追加することで、拡張性が高く、特定のビジネスロジックに合わせたカスタマイズが可能です。

  • bookshelf:

    Bookshelfは、カスタムモデルやリレーションを追加することで、拡張性がありますが、Knexの知識が必要です。

  • waterline:

    Waterlineは、アダプタを通じて拡張性がありますが、特定のデータストアに依存するため、注意が必要です。

選び方: knex vs sequelize vs bookshelf vs waterline

  • knex:

    Knexは、SQLクエリビルダーであり、ORMではありません。柔軟性が高く、複雑なクエリを構築する必要がある場合や、特定のデータベース機能を直接利用したい場合に選択すべきです。

  • sequelize:

    Sequelizeは、強力な機能を持つORMで、多くのデータベースをサポートしています。トランザクションやバリデーション、フックなどの機能が豊富で、複雑なアプリケーションに最適です。

  • bookshelf:

    Bookshelfは、Knex.jsを基盤にしたORMで、リレーショナルデータベースを扱う際に、モデルとリレーションシップの定義が簡単です。シンプルなプロジェクトや、リレーションシップが複雑でない場合に適しています。

  • waterline:

    Waterlineは、アダプターパターンを使用しており、異なるデータストアを統一的に扱うことができます。複数のデータベースを使用するアプリケーションや、データストアの変更が予想される場合に適しています。

knex のREADME

knex.js

npm version npm downloads Coverage Status Dependencies Status Gitter chat

A SQL query builder that is flexible, portable, and fun to use!

A batteries-included, multi-dialect (PostgreSQL, MySQL, CockroachDB, MSSQL, SQLite3, Oracle (including Oracle Wallet Authentication)) query builder for Node.js, featuring:

Node.js versions 12+ are supported.

You can report bugs and discuss features on the GitHub issues page or send tweets to @kibertoad.

For support and questions, join our Gitter channel.

For knex-based Object Relational Mapper, see:

To see the SQL that Knex will generate for a given query, you can use Knex Query Lab

Examples

We have several examples on the website. Here is the first one to get you started:

const knex = require('knex')({
  client: 'sqlite3',
  connection: {
    filename: './data.db',
  },
});

try {
  // Create a table
  await knex.schema
    .createTable('users', (table) => {
      table.increments('id');
      table.string('user_name');
    })
    // ...and another
    .createTable('accounts', (table) => {
      table.increments('id');
      table.string('account_name');
      table.integer('user_id').unsigned().references('users.id');
    });

  // Then query the table...
  const insertedRows = await knex('users').insert({ user_name: 'Tim' });

  // ...and using the insert id, insert into the other table.
  await knex('accounts').insert({
    account_name: 'knex',
    user_id: insertedRows[0],
  });

  // Query both of the rows.
  const selectedRows = await knex('users')
    .join('accounts', 'users.id', 'accounts.user_id')
    .select('users.user_name as user', 'accounts.account_name as account');

  // map over the results
  const enrichedRows = selectedRows.map((row) => ({ ...row, active: true }));

  // Finally, add a catch statement
} catch (e) {
  console.error(e);
}

TypeScript example

import { Knex, knex } from 'knex';

interface User {
  id: number;
  age: number;
  name: string;
  active: boolean;
  departmentId: number;
}

const config: Knex.Config = {
  client: 'sqlite3',
  connection: {
    filename: './data.db',
  },
};

const knexInstance = knex(config);

try {
  const users = await knex<User>('users').select('id', 'age');
} catch (err) {
  // error handling
}

Usage as ESM module

If you are launching your Node application with --experimental-modules, knex.mjs should be picked up automatically and named ESM import should work out-of-the-box. Otherwise, if you want to use named imports, you'll have to import knex like this:

import { knex } from 'knex/knex.mjs';

You can also just do the default import:

import knex from 'knex';

If you are not using TypeScript and would like the IntelliSense of your IDE to work correctly, it is recommended to set the type explicitly:

/**
 * @type {Knex}
 */
const database = knex({
  client: 'mysql',
  connection: {
    host: '127.0.0.1',
    user: 'your_database_user',
    password: 'your_database_password',
    database: 'myapp_test',
  },
});
database.migrate.latest();