typeorm vs sequelize vs bookshelf vs knex
Node.js ORM Libraries
typeormsequelizebookshelfknex类似的npm包:

Node.js ORM Libraries

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

npm下载趋势

3 年

GitHub Stars 排名

统计详情

npm包名称
下载量
Stars
大小
Issues
发布时间
License
typeorm4,048,72136,41120.8 MB5214 个月前MIT
sequelize2,633,43830,3552.91 MB1,02019 天前MIT
bookshelf06,365-2386 年前MIT
knex020,245953 kB7092 天前MIT

功能对比: typeorm vs sequelize vs bookshelf vs knex

设计原则

  • typeorm:

    TypeORM的设计原则是类型安全和灵活性,支持Active Record和Data Mapper两种模式,适合TypeScript开发者,强调使用装饰器和类来定义模型和关系。

  • sequelize:

    Sequelize的设计原则是功能丰富和易用性,提供了大量的内置功能,如模型验证、钩子、事务等,旨在简化复杂应用的开发。

  • bookshelf:

    Bookshelf遵循简单和易用的设计原则,旨在快速上手并提供基本的ORM功能。它依赖于Knex.js作为查询构建器,强调模型和关系的简单定义。

  • knex:

    Knex.js的设计原则是灵活性和可扩展性,提供了一个强大的查询构建器,允许开发者以链式调用的方式构建SQL查询,适合需要高度自定义的场景。

学习曲线

  • typeorm:

    TypeORM的学习曲线相对较高,尤其是对于不熟悉TypeScript的开发者,但其类型安全和强大的功能使得投资学习是值得的。

  • sequelize:

    Sequelize的学习曲线适中,虽然功能丰富,但文档详细,提供了大量示例,适合中级开发者学习和使用。

  • bookshelf:

    Bookshelf的学习曲线相对平缓,适合初学者和快速开发,文档清晰,易于理解基本概念和使用方法。

  • knex:

    Knex.js的学习曲线较陡,因为它不提供ORM功能,开发者需要对SQL有一定了解,才能充分利用其查询构建能力。

性能

  • typeorm:

    TypeORM在处理复杂关系时性能良好,支持懒加载和查询优化,适合需要高性能和类型安全的应用。

  • sequelize:

    Sequelize的性能在处理复杂关系和大量数据时可能会受到影响,尤其是在使用事务时,但其优化选项可以帮助提高性能。

  • bookshelf:

    Bookshelf的性能依赖于Knex.js的查询构建能力,适合小型到中型项目,但在处理复杂查询时可能会受到限制。

  • knex:

    Knex.js的性能非常高效,能够生成优化的SQL查询,适合需要高性能和复杂查询的应用。

扩展性

  • typeorm:

    TypeORM的扩展性强,支持多种数据库和复杂关系,适合需要灵活性和可扩展性的应用。

  • sequelize:

    Sequelize提供了丰富的扩展功能,如模型钩子、验证和自定义查询,适合需要复杂业务逻辑的项目。

  • bookshelf:

    Bookshelf的扩展性较好,支持插件和自定义模型方法,适合需要快速迭代和扩展的项目。

  • knex:

    Knex.js的扩展性非常强,允许开发者自定义查询构建器和插件,适合需要高度定制的应用。

社区支持

  • typeorm:

    TypeORM的社区正在快速增长,提供了良好的文档和支持,尤其是在TypeScript开发者中。

  • sequelize:

    Sequelize拥有一个活跃的社区和大量的文档、教程和示例,适合需要社区支持的开发者。

  • bookshelf:

    Bookshelf拥有一个小而活跃的社区,提供了基本的支持和文档,但相较于其他库,资源较少。

  • knex:

    Knex.js拥有一个庞大的社区和丰富的文档,开发者可以轻松找到支持和示例。

如何选择: typeorm vs sequelize vs bookshelf vs knex

  • typeorm:

    选择TypeORM如果你在使用TypeScript并需要一个支持Active Record和Data Mapper模式的ORM。TypeORM提供了强大的装饰器支持和灵活的查询构建,适合需要TypeScript类型安全和复杂关系的项目。

  • sequelize:

    选择Sequelize如果你需要一个功能丰富的ORM,支持多种数据库(如PostgreSQL、MySQL、SQLite等),并且需要复杂的模型关系和事务管理。Sequelize提供了丰富的功能和良好的文档,适合中到大型项目。

  • bookshelf:

    选择Bookshelf如果你需要一个简单的ORM,易于使用且与Knex.js紧密集成,适合小型到中型项目。它支持模型关系和基本的CRUD操作,适合快速开发原型。

  • knex:

    选择Knex.js如果你需要一个灵活的SQL查询构建器,能够直接与SQL数据库交互,适合需要自定义复杂查询的项目。Knex不提供ORM功能,但它的灵活性和强大的查询构建能力使其适合高级用户。

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