sequelize vs objection vs bookshelf vs waterline
Node.js ORM 库
sequelizeobjectionbookshelfwaterline类似的npm包:
Node.js ORM 库

Node.js ORM(对象关系映射)库是用于在 Node.js 应用程序中简化与关系数据库交互的工具。它们通过将数据库表映射到 JavaScript 对象,使开发人员能够使用面向对象的方式进行数据库操作,从而减少编写原始 SQL 查询的需要。这些库通常提供模型定义、查询构建、数据验证和关系管理等功能,帮助开发人员更高效地处理数据库操作。sequelize 是一个功能丰富且广泛使用的 ORM,支持多种数据库,提供强大的查询构建器和事务管理。bookshelf 是一个基于 knex 的 ORM,支持模型关系和插件扩展,适合需要灵活性的项目。objection 是一个轻量级的 ORM,强调可组合性和类型安全,适合现代 JavaScript 和 TypeScript 项目。waterline 是一个数据访问层(DAL)库,支持多种数据库和适配器,提供统一的 API 进行数据操作,适合需要跨数据库兼容性的应用。

npm下载趋势
3 年
GitHub Stars 排名
统计详情
npm包名称
下载量
Stars
大小
Issues
发布时间
License
sequelize2,474,54530,2752.91 MB9999 个月前MIT
objection210,4067,352645 kB1271 年前MIT
bookshelf84,8676,365-2376 年前MIT
waterline26,7005,4081.3 MB34-MIT
功能对比: sequelize vs objection vs bookshelf vs waterline

数据库支持

  • sequelize:

    sequelize 支持多种关系数据库,包括 MySQL、PostgreSQL、SQLite 和 MSSQL,提供广泛的兼容性和灵活性。

  • objection:

    objection 也基于 knex,支持所有 knex 支持的数据库,包括 MySQL、PostgreSQL 和 SQLite,提供良好的兼容性。

  • bookshelf:

    bookshelf 依赖于 knex,支持多种数据库(如 MySQL、PostgreSQL 和 SQLite),但不支持 MSSQL。

  • waterline:

    waterline 支持多种数据库和适配器,包括 MySQL、MongoDB 和 SQLite,提供跨数据库操作的能力。

模型关系

  • sequelize:

    sequelize 提供强大的模型关系支持,包括一对一、一对多和多对多关系,支持延迟加载和预加载。

  • objection:

    objection 强调模型关系的可组合性,支持一对一、一对多和多对多关系,允许使用 knex 进行自定义查询。

  • bookshelf:

    bookshelf 也支持复杂的模型关系,允许自定义关系和延迟加载,具有更高的灵活性。

  • waterline:

    waterline 提供基本的模型关系支持,包括一对一、一对多和多对多关系,但相对较简单。

查询构建

  • sequelize:

    sequelize 提供强大的查询构建器,支持链式调用、条件查询、聚合函数和事务管理,功能全面。

  • objection:

    objection 继承了 knex 的查询构建能力,支持复杂查询、嵌套查询和事务,强调类型安全。

  • bookshelf:

    bookshelf 基于 knex 的查询构建能力,支持链式调用和自定义查询,灵活性高。

  • waterline:

    waterline 提供简单的查询 API,支持链式调用和条件查询,但功能相对较弱。

迁移和种子

  • sequelize:

    sequelize 提供内置的迁移和种子功能,支持命令行工具和 API,易于管理数据库结构和初始数据。

  • objection:

    objection 也依赖于 knex 的迁移功能,支持自定义迁移和种子脚本。

  • bookshelf:

    bookshelf 不提供内置的迁移功能,但可以与 knex 的迁移工具结合使用。

  • waterline:

    waterline 提供基本的迁移和种子支持,但功能较为简单。

代码示例

  • sequelize:

    使用 sequelize 创建模型和查询

    const { Sequelize, DataTypes } = require('sequelize');
    const sequelize = new Sequelize('database', 'username', 'password', { dialect: 'mysql' });
    
    const User = sequelize.define('User', { name: DataTypes.STRING, email: DataTypes.STRING });
    const Post = sequelize.define('Post', { title: DataTypes.STRING, content: DataTypes.TEXT });
    
    User.hasMany(Post);
    Post.belongsTo(User);
    
    (async () => {
      await sequelize.sync();
      const user = await User.create({ name: 'Alice', email: 'alice@example.com' });
      const post = await Post.create({ title: 'Hello World', content: 'This is my first post.', UserId: user.id });
      const posts = await User.findByPk(user.id, { include: Post });
      console.log(posts);
    })();
    
  • objection:

    使用 objection 创建模型和查询

    const { Model, knex } = require('objection');
    const Knex = require('knex');
    const knexConfig = { client: 'mysql', connection: { host: 'localhost', user: 'root', password: '', database: 'test' }};
    const knex = Knex(knexConfig);
    Model.knex(knex);
    
    class User extends Model { static get tableName() { return 'users'; }}
    class Post extends Model { static get tableName() { return 'posts'; }}
    
    User.hasMany(Post);
    Post.belongsTo(User);
    
    (async () => {
      await knex.migrate.latest();
      const user = await User.query().insert({ name: 'Alice', email: 'alice@example.com' });
      const post = await Post.query().insert({ title: 'Hello World', content: 'This is my first post.', userId: user.id });
      const posts = await User.query().findById(user.id).withGraphFetched('posts');
      console.log(posts);
    })();
    
  • bookshelf:

    使用 bookshelf 创建模型和查询

    const knex = require('knex')({ client: 'mysql', connection: { host: 'localhost', user: 'root', password: '', database: 'test' }});
    const bookshelf = require('bookshelf')(knex);
    
    const User = bookshelf.model('User', { tableName: 'users' });
    const Post = bookshelf.model('Post', { tableName: 'posts' });
    
    User.hasMany(Post);
    Post.belongsTo(User);
    
    (async () => {
      await knex.migrate.latest();
      const user = await new User({ name: 'Alice', email: 'alice@example.com' }).save();
      const post = await new Post({ title: 'Hello World', content: 'This is my first post.', user_id: user.id }).save();
      const posts = await User.where({ id: user.id }).fetch({ withRelated: ['posts'] });
      console.log(posts);
    })();
    
  • waterline:

    使用 waterline 创建模型和查询

    const Waterline = require('waterline');
    const mysqlAdapter = require('sails-mysql');
    const orm = new Waterline();
    
    const User = Waterline.Collection.extend({
      identity: 'user',
      datastore: 'mysql',
      attributes: { name: { type: 'string' }, email: { type: 'string' }, posts: { collection: 'post', via: 'user' }}
    });
    
    const Post = Waterline.Collection.extend({
      identity: 'post',
      datastore: 'mysql',
      attributes: { title: { type: 'string' }, content: { type: 'string' }, user: { model: 'user' }}
    });
    
    orm.registerModel(User);
    orm.registerModel(Post);
    
    const config = { adapters: { mysql: mysqlAdapter }, datastores: { mysql: { adapter: 'mysql', host: 'localhost', user: 'root', password: '', database: 'test' }} };
    
    orm.initialize(config, (err, models) => {
      if (err) throw err;
      models.user.create({ name: 'Alice', email: 'alice@example.com' }).then(user => {
        return models.post.create({ title: 'Hello World', content: 'This is my first post.', user: user.id });
      }).then(() => {
        return models.user.findOne({ name: 'Alice' }).populate('posts');
      }).then(user => {
        console.log(user);
      });
    });
    
如何选择: sequelize vs objection vs bookshelf vs waterline
  • sequelize:

    选择 sequelize 如果您需要一个功能全面且成熟的 ORM,支持多种数据库(如 MySQL、PostgreSQL、SQLite 和 MSSQL),并提供强大的事务、验证和迁移功能。它适合大型应用程序和需要复杂查询的项目。

  • objection:

    选择 objection 如果您需要一个轻量级且高度可定制的 ORM,支持复杂的查询构建和模型关系。它与 knex 紧密集成,适合需要类型安全和可组合性的现代 JavaScript 和 TypeScript 项目。

  • bookshelf:

    选择 bookshelf 如果您希望在 knex 的基础上构建一个灵活的 ORM,支持模型关系(如一对一、一对多和多对多)和插件扩展。它适合中型项目,特别是那些需要自定义模型行为的项目。

  • waterline:

    选择 waterline 如果您需要一个支持多种数据库和适配器的 ORM,提供统一的 API 进行数据操作。它适合需要跨数据库兼容性的应用,特别是在使用 Sails.js 框架时。

sequelize的README

Sequelize logo

Sequelize

npm version Build Status npm downloads contributors Open Collective sponsor Merged PRs semantic-release License: MIT

Sequelize is an easy-to-use and promise-based Node.js ORM tool for Postgres, MySQL, MariaDB, SQLite, DB2, Microsoft SQL Server, and Snowflake. It features solid transaction support, relations, eager and lazy loading, read replication and more.

Would you like to contribute? Read our contribution guidelines to know more. There are many ways to help! 😃

🚀 Seeking New Maintainers for Sequelize! 🚀

We're looking for new maintainers to help finalize and release the next major version of Sequelize! If you're passionate about open-source and database ORMs, we'd love to have you onboard.

💰 Funding Available

We distribute $2,500 per quarter among maintainers and have additional funds for full-time contributions.

🛠️ What You’ll Work On

  • Finalizing and releasing Sequelize’s next major version
  • Improving TypeScript support and database integrations
  • Fixing critical issues and shaping the ORM’s future

🤝 How to Get Involved

Interested? Join our Slack and reach out to @WikiRik or @sdepold:
➡️ sequelize.org/slack

We’d love to have you on board! 🚀

:computer: Getting Started

Ready to start using Sequelize? Head to sequelize.org to begin!

:money_with_wings: Supporting the project

Do you like Sequelize and would like to give back to the engineering team behind it?

We have recently created an OpenCollective based money pool which is shared amongst all core maintainers based on their contributions. Every support is wholeheartedly welcome. ❤️

:pencil: Major version changelog

Please find upgrade information to major versions here:

:book: Resources

:wrench: Tools

:speech_balloon: Translations

:warning: Responsible disclosure

If you have security issues to report, please refer to our Responsible Disclosure Policy for more details.