typeorm vs sequelize vs objection vs bookshelf
Node.js ORM Libraries
typeormsequelizeobjectionbookshelf类似的npm包:
Node.js ORM Libraries

Node.js ORM(对象关系映射)库提供了一种抽象方式来与数据库交互,使开发者能够使用JavaScript对象而不是SQL查询来操作数据。这些库简化了数据库操作,提供了模型定义、关系管理和查询构建等功能,从而提高了开发效率和代码可维护性。选择合适的ORM库可以影响应用程序的性能、可扩展性和开发速度。

npm下载趋势
3 年
GitHub Stars 排名
统计详情
npm包名称
下载量
Stars
大小
Issues
发布时间
License
typeorm3,067,62236,12820.8 MB54012 天前MIT
sequelize2,463,07130,2742.91 MB1,0009 个月前MIT
objection210,7077,352645 kB1271 年前MIT
bookshelf85,5626,365-2376 年前MIT
功能对比: typeorm vs sequelize vs objection vs bookshelf

数据库支持

  • typeorm:

    TypeORM支持PostgreSQL、MySQL、MariaDB、SQLite、Oracle和Microsoft SQL Server等多种数据库。它的设计使得在不同数据库之间切换变得简单。

  • sequelize:

    Sequelize支持多种数据库,包括PostgreSQL、MySQL、MariaDB、SQLite和Microsoft SQL Server。它提供了丰富的功能来处理不同数据库的特性。

  • objection:

    Objection.js同样支持PostgreSQL、MySQL和SQLite,利用Knex.js的强大功能,能够处理复杂的查询和关系。

  • bookshelf:

    Bookshelf主要支持PostgreSQL、MySQL和SQLite。它依赖于Knex.js作为查询构建器,因此可以利用Knex.js的多数据库支持。

关系管理

  • typeorm:

    TypeORM支持多种关系类型,允许开发者使用装饰器来定义关系,提供了直观的方式来管理复杂的关系。

  • sequelize:

    Sequelize提供了强大的关系管理功能,支持多种关系类型,并提供了便捷的方法来定义和操作这些关系。

  • objection:

    Objection.js在关系管理方面非常灵活,支持复杂的关系和自定义查询。它允许开发者使用Knex.js的查询构建能力来处理关系。

  • bookshelf:

    Bookshelf提供了简单的关系管理功能,支持一对一、一对多和多对多关系。它允许开发者通过模型定义轻松管理这些关系。

查询构建

  • typeorm:

    TypeORM提供了灵活的查询构建方式,支持使用QueryBuilder进行复杂查询,适合需要高度自定义的查询需求。

  • sequelize:

    Sequelize的查询构建功能非常丰富,支持复杂的查询条件、排序和分页,适合大型应用程序。

  • objection:

    Objection.js提供了强大的查询构建能力,允许开发者使用Knex.js的所有功能,适合需要复杂查询的场景。

  • bookshelf:

    Bookshelf使用Knex.js作为查询构建器,提供了简单的链式查询语法,适合基本的查询需求。

学习曲线

  • typeorm:

    TypeORM的学习曲线较陡,尤其是对于TypeScript新手,但其类型安全性和灵活性使其在大型项目中非常有价值。

  • sequelize:

    Sequelize的学习曲线适中,功能丰富但也相对复杂,适合需要全面功能的开发者。

  • objection:

    Objection.js的学习曲线稍陡,尤其是对于复杂的查询和关系管理,但提供了强大的灵活性。

  • bookshelf:

    Bookshelf的学习曲线相对较平缓,适合初学者快速上手,尤其是对于小型项目。

扩展性

  • typeorm:

    TypeORM支持多种设计模式,适合需要高度扩展和自定义的项目,特别是在TypeScript环境中。

  • sequelize:

    Sequelize功能丰富,适合大型应用程序,支持多种扩展和自定义功能。

  • objection:

    Objection.js非常灵活,适合需要高度自定义和扩展的项目,能够处理复杂的业务逻辑。

  • bookshelf:

    Bookshelf相对简单,适合小型项目,但在扩展性方面可能不如其他库。

如何选择: typeorm vs sequelize vs objection vs bookshelf
  • typeorm:

    选择TypeORM如果你在使用TypeScript并希望利用其类型系统。TypeORM支持Active Record和Data Mapper模式,适合需要高度类型安全和灵活性的项目。

  • sequelize:

    选择Sequelize如果你需要一个功能丰富且支持多种数据库的ORM。Sequelize提供了强大的模型定义、验证和钩子功能,适合大型应用程序和复杂的数据库结构。

  • objection:

    选择Objection.js如果你需要一个灵活且功能强大的ORM,支持复杂的查询和关系。它基于Knex.js,提供了更强的查询构建能力,适合需要高度自定义的应用程序。

  • bookshelf:

    选择Bookshelf如果你需要一个轻量级的ORM,且希望使用Knex.js作为查询构建器。Bookshelf提供了简单的模型定义和关系管理,非常适合小型到中型项目。

typeorm的README

TypeORM is an ORM that can run in Node.js, Browser, Cordova, Ionic, React Native, NativeScript, Expo, and Electron platforms and can be used with TypeScript and JavaScript (ES2021). Its goal is to always support the latest JavaScript features and provide additional features that help you to develop any kind of application that uses databases - from small applications with a few tables to large-scale enterprise applications with multiple databases.

TypeORM supports more databases than any other JS/TS ORM: Google Spanner, Microsoft SqlServer, MySQL/MariaDB, MongoDB, Oracle, Postgres, SAP HANA and SQLite, as well we derived databases and different drivers.

TypeORM supports both Active Record and Data Mapper patterns, unlike all other JavaScript ORMs currently in existence, which means you can write high-quality, loosely coupled, scalable, maintainable applications in the most productive way.

TypeORM is highly influenced by other ORMs, such as Hibernate, Doctrine and Entity Framework.

Features

  • Supports both DataMapper and ActiveRecord (your choice).
  • Entities and columns.
  • Database-specific column types.
  • Entity manager.
  • Repositories and custom repositories.
  • Clean object-relational model.
  • Associations (relations).
  • Eager and lazy relations.
  • Unidirectional, bidirectional, and self-referenced relations.
  • Supports multiple inheritance patterns.
  • Cascades.
  • Indices.
  • Transactions.
  • Migrations and automatic migrations generation.
  • Connection pooling.
  • Replication.
  • Using multiple database instances.
  • Working with multiple database types.
  • Cross-database and cross-schema queries.
  • Elegant-syntax, flexible and powerful QueryBuilder.
  • Left and inner joins.
  • Proper pagination for queries using joins.
  • Query caching.
  • Streaming raw results.
  • Logging.
  • Listeners and subscribers (hooks).
  • Supports closure table pattern.
  • Schema declaration in models or separate configuration files.
  • Supports MySQL / MariaDB / Postgres / CockroachDB / SQLite / Microsoft SQL Server / Oracle / SAP Hana / sql.js.
  • Supports MongoDB NoSQL database.
  • Works in Node.js / Browser / Ionic / Cordova / React Native / NativeScript / Expo / Electron platforms.
  • TypeScript and JavaScript support.
  • ESM and CommonJS support.
  • Produced code is performant, flexible, clean, and maintainable.
  • Follows all possible best practices.
  • CLI.

And more...

With TypeORM, your models look like this:

import { Entity, PrimaryGeneratedColumn, Column } from "typeorm"

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    firstName: string

    @Column()
    lastName: string

    @Column()
    age: number
}

And your domain logic looks like this:

const userRepository = MyDataSource.getRepository(User)

const user = new User()
user.firstName = "Timber"
user.lastName = "Saw"
user.age = 25
await userRepository.save(user)

const allUsers = await userRepository.find()
const firstUser = await userRepository.findOneBy({
    id: 1,
}) // find by id
const timber = await userRepository.findOneBy({
    firstName: "Timber",
    lastName: "Saw",
}) // find by firstName and lastName

await userRepository.remove(timber)

Alternatively, if you prefer to use the ActiveRecord implementation, you can use it as well:

import { Entity, PrimaryGeneratedColumn, Column, BaseEntity } from "typeorm"

@Entity()
export class User extends BaseEntity {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    firstName: string

    @Column()
    lastName: string

    @Column()
    age: number
}

And your domain logic will look this way:

const user = new User()
user.firstName = "Timber"
user.lastName = "Saw"
user.age = 25
await user.save()

const allUsers = await User.find()
const firstUser = await User.findOneBy({
    id: 1,
})
const timber = await User.findOneBy({
    firstName: "Timber",
    lastName: "Saw",
})

await timber.remove()

Samples

Take a look at the samples in sample for examples of usage.

There are a few repositories that you can clone and start with:

Extensions

There are several extensions that simplify working with TypeORM and integrating it with other modules:

Contributing

Learn about contribution here and how to set up your development environment here.

This project exists thanks to all the people who contribute:

Sponsors

Open source is hard and time-consuming. If you want to invest in TypeORM's future, you can become a sponsor and allow our core team to spend more time on TypeORM's improvements and new features.

Champion

Become a champion sponsor and get premium technical support from our core contributors. Become a champion

Supporter

Support TypeORM's development with a monthly contribution. Become a supporter

Community

Join our community of supporters and help sustain TypeORM. Become a community supporter

Sponsor

Make a one-time or recurring contribution of your choice. Become a sponsor