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

ORM(Object-Relational Mapping)ライブラリは、データベースとアプリケーション間のデータのやり取りを簡素化するためのツールです。これにより、開発者はSQLクエリを直接書くことなく、オブジェクト指向の方法でデータベース操作を行うことができます。これらのライブラリは、データベースのスキーマをモデルとして定義し、データの取得、挿入、更新、削除を容易にします。

npmのダウンロードトレンド
3 年
GitHub Starsランキング
統計詳細
パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
knex3,531,72220,219874 kB7062年前MIT
typeorm3,472,86636,30920.8 MB4982ヶ月前MIT
sequelize2,396,37130,3352.91 MB1,0061年前MIT
bookshelf59,1576,367-2376年前MIT
機能比較: knex vs typeorm vs sequelize vs bookshelf

データベースサポート

  • knex:

    Knexは、PostgreSQL、MySQL、SQLite、Oracleなど、さまざまなデータベースをサポートしており、クエリビルダーとしても機能します。

  • typeorm:

    TypeORMは、PostgreSQL、MySQL、MariaDB、SQLite、Microsoft SQL Server、Oracleなど、広範なデータベースをサポートしています。

  • sequelize:

    Sequelizeは、PostgreSQL、MySQL、MariaDB、SQLite、Microsoft SQL Serverなど、複数のデータベースをサポートしています。

  • bookshelf:

    Bookshelfは、PostgreSQL、MySQL、SQLiteなどの一般的なリレーショナルデータベースをサポートしています。

APIのシンプルさ

  • knex:

    Knexは、クエリビルダーとしての柔軟性を持ちながら、APIは比較的シンプルで、SQLに近い形でクエリを構築できます。

  • typeorm:

    TypeORMは、TypeScriptの型安全性を活かしたAPIを提供しており、オブジェクト指向プログラミングに慣れた開発者には使いやすいですが、初心者には少し難しいかもしれません。

  • sequelize:

    Sequelizeは、強力な機能を持ちながらも、APIはやや複雑で、学習曲線が急になる可能性があります。

  • bookshelf:

    Bookshelfは、シンプルで直感的なAPIを提供しており、リレーショナルデータベースの操作が容易です。

トランザクション管理

  • knex:

    Knexは、トランザクションを簡単に管理できる機能を提供しており、複雑なデータベース操作に対応できます。

  • typeorm:

    TypeORMは、トランザクション管理をサポートしており、エンティティの操作をトランザクションとしてまとめることができます。

  • sequelize:

    Sequelizeは、トランザクション管理が組み込まれており、複数の操作を一つのトランザクションとして処理することができます。

  • bookshelf:

    Bookshelfは、トランザクション管理をサポートしていますが、Knexを使用してトランザクションを明示的に管理する必要があります。

学習曲線

  • knex:

    Knexは、SQLに近い構文を持つため、SQLに慣れている開発者には比較的学びやすいです。

  • typeorm:

    TypeORMは、TypeScriptを使用するため、TypeScriptに慣れている開発者には学びやすいですが、初めての方には少し難しいかもしれません。

  • sequelize:

    Sequelizeは、機能が豊富なため、学習曲線が急で、特に初心者には難しいかもしれません。

  • bookshelf:

    Bookshelfは、シンプルなAPIを持っているため、学習曲線は緩やかです。

エコシステムとコミュニティ

  • knex:

    Knexは、広範なエコシステムを持ち、多くのプラグインや拡張機能が利用可能です。

  • typeorm:

    TypeORMは、TypeScriptを使用する開発者に人気があり、活発なコミュニティと多くのリソースがあります。

  • sequelize:

    Sequelizeは、非常に活発なコミュニティを持ち、多くのリソースやサポートが利用できます。

  • bookshelf:

    Bookshelfは、Knexに依存しているため、Knexのエコシステムを活用できますが、他のORMに比べるとコミュニティは小さいです。

選び方: knex vs typeorm vs sequelize vs bookshelf
  • knex:

    Knexは、クエリビルダーとして使用されるため、SQLクエリを細かく制御したい場合に選択すべきです。データベースのスキーマを直接操作したい場合や、複雑なクエリを構築する必要がある場合に最適です。

  • typeorm:

    TypeORMは、TypeScriptとの統合が優れており、型安全性を重視するプロジェクトに最適です。エンティティベースのアプローチを採用しているため、オブジェクト指向プログラミングに慣れている開発者に適しています。

  • sequelize:

    Sequelizeは、強力な機能を持つフル機能のORMであり、複数のデータベースをサポートしています。トランザクションやバリデーションなどの高度な機能が必要な場合に選ぶべきです。

  • bookshelf:

    Bookshelfは、Knex.jsを基盤として構築されているため、Knexを使用したい場合に最適です。また、シンプルなAPIを持ち、リレーショナルデータベースの操作を簡単に行いたい場合に適しています。

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();