knex vs sequelize vs bookshelf vs waterline
Node.js ORM Libraries
knexsequelizebookshelfwaterline类似的npm包:
Node.js ORM Libraries

Node.js ORM(对象关系映射)库提供了一种简化与数据库交互的方法,使开发者能够通过JavaScript对象而非SQL语句来操作数据库。这些库通常提供模型定义、数据验证、关系管理等功能,使得数据库操作更加直观和高效。选择合适的ORM库可以显著提高开发效率,减少错误,并增强代码的可维护性。

npm下载趋势
3 年
GitHub Stars 排名
统计详情
npm包名称
下载量
Stars
大小
Issues
发布时间
License
knex2,806,40020,103874 kB7132 年前MIT
sequelize2,145,95530,2352.91 MB9998 个月前MIT
bookshelf83,4926,367-2375 年前MIT
waterline28,9505,4111.3 MB34-MIT
功能对比: knex vs sequelize vs bookshelf vs waterline

模型定义

  • knex:

    Knex本身并不提供ORM功能,但它允许开发者通过查询构建器定义复杂的SQL查询。开发者可以使用Knex来创建和管理数据库表,但需要手动管理模型和关系。

  • sequelize:

    Sequelize提供了强大的模型定义功能,支持数据验证、钩子(hooks)和虚拟字段等特性。它允许开发者定义模型之间的关系,并提供了丰富的API来操作数据。

  • bookshelf:

    Bookshelf允许开发者通过简单的JavaScript对象定义模型,并支持继承和扩展。它提供了丰富的API来定义模型的属性和关系,使得模型的创建和管理变得简单。

  • waterline:

    Waterline提供了一种统一的模型定义方式,支持多种数据存储。开发者可以通过简单的JSON格式定义模型,并使用Waterline的API进行数据操作。

关系管理

  • knex:

    Knex不直接支持ORM关系管理,但开发者可以通过手动编写SQL查询来实现复杂的关系查询。它提供了灵活性,但需要更多的手动工作。

  • sequelize:

    Sequelize提供了强大的关系管理功能,支持多种关系类型,并允许开发者在模型之间定义关联。它自动处理关联查询,使得数据操作更加简便。

  • bookshelf:

    Bookshelf支持一对一、一对多和多对多关系的定义和管理。开发者可以轻松地在模型之间建立关系,并通过链式调用来查询相关数据。

  • waterline:

    Waterline支持多种关系类型的定义,允许开发者在模型之间建立关联。它提供了简单的API来查询和操作相关数据。

查询构建

  • knex:

    Knex是一个强大的SQL查询构建器,支持链式调用和多种数据库。它允许开发者以直观的方式构建复杂的SQL查询,适合需要精细控制的项目。

  • sequelize:

    Sequelize提供了丰富的查询API,允许开发者使用简单的JavaScript语法构建复杂的查询。它支持条件查询、分页和排序等功能,使得数据操作更加灵活。

  • bookshelf:

    Bookshelf基于Knex构建,允许开发者使用Knex的查询构建器进行复杂查询。它结合了ORM的便利性和SQL的灵活性,使得查询构建变得高效。

  • waterline:

    Waterline提供了一种简单的查询API,允许开发者通过模型方法进行数据查询。虽然功能不如Knex强大,但对于常见的查询需求已足够使用。

事务支持

  • knex:

    Knex提供了强大的事务支持,允许开发者在多个查询之间创建事务,确保操作的原子性。它适合需要复杂数据库操作的场景。

  • sequelize:

    Sequelize内置了事务管理功能,允许开发者在模型操作之间创建事务,确保数据的一致性。它支持嵌套事务和并发控制。

  • bookshelf:

    Bookshelf支持事务管理,允许开发者在多个数据库操作之间创建事务,以确保数据的一致性和完整性。

  • waterline:

    Waterline对事务的支持有限,主要依赖于底层数据库的事务功能。对于简单的应用场景,通常足够使用,但在复杂场景下可能需要额外处理。

学习曲线

  • knex:

    Knex的学习曲线较低,特别是对于熟悉SQL的开发者。它的查询构建器直观易用,但对于不熟悉SQL的开发者可能需要一些时间适应。

  • sequelize:

    Sequelize的学习曲线相对较陡,尤其是对于初学者。它的功能丰富,但需要理解模型、关系和查询的复杂性。

  • bookshelf:

    Bookshelf的学习曲线相对较平缓,特别是对于已经熟悉Knex的开发者。它的API设计简单易懂,适合中小型项目。

  • waterline:

    Waterline的学习曲线较平缓,特别是对于Sails.js用户。它的API设计简单,适合快速开发和原型设计。

如何选择: knex vs sequelize vs bookshelf vs waterline
  • knex:

    选择Knex如果你需要一个强大的SQL查询构建器,能够支持多种数据库,并且希望在SQL查询和ORM之间有更大的控制权。Knex适合需要复杂查询的项目。

  • sequelize:

    选择Sequelize如果你需要一个功能全面的ORM,支持多种数据库,并且希望使用丰富的模型定义和验证功能。Sequelize适合大型应用,提供了强大的事务管理和关系管理能力。

  • bookshelf:

    选择Bookshelf如果你需要一个简单易用的ORM,支持关系映射,并且希望与Knex.js紧密集成。Bookshelf适合中小型项目,提供了丰富的功能和灵活性。

  • waterline:

    选择Waterline如果你需要一个适用于多种数据源的ORM,特别是在使用Sails.js框架时。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();