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

Node.js ORMライブラリ

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

npmのダウンロードトレンド

3 年

GitHub Starsランキング

統計詳細

パッケージ
ダウンロード数
Stars
サイズ
Issues
公開日時
ライセンス
typeorm3,738,57636,41920.8 MB5344ヶ月前MIT
bookshelf65,9536,364-2386年前MIT
mongoose027,4702.07 MB1881日前MIT
sequelize030,3532.91 MB1,0211ヶ月前MIT

機能比較: typeorm vs bookshelf vs mongoose vs sequelize

データベースサポート

  • typeorm:

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

  • bookshelf:

    Bookshelfは、Knex.jsを使用して、MySQL、PostgreSQL、SQLiteなどのリレーショナルデータベースをサポートしています。

  • mongoose:

    MongooseはMongoDB専用であり、MongoDBの機能をフルに活用できます。

  • sequelize:

    Sequelizeは、MySQL、PostgreSQL、SQLite、MariaDBなど、複数のリレーショナルデータベースをサポートしています。

スキーマ定義

  • typeorm:

    TypeORMは、エンティティを使用してスキーマを定義し、TypeScriptの型安全性を活かしたバリデーションを行うことができます。

  • bookshelf:

    Bookshelfでは、モデルを定義する際に、スキーマを明示的に定義する必要はありませんが、必要に応じてバリデーションを追加できます。

  • mongoose:

    Mongooseは、スキーマを使用してデータの構造を厳密に定義し、バリデーションを行うことができます。

  • sequelize:

    Sequelizeは、モデルを定義する際にスキーマを使用し、データの整合性を保つためのバリデーション機能を提供します。

クエリビルダー

  • typeorm:

    TypeORMは、TypeScriptの機能を活かしたクエリビルダーを提供し、エンティティに基づいたクエリを構築できます。

  • bookshelf:

    Bookshelfは、Knex.jsのクエリビルダーを利用して、柔軟なクエリを構築できます。

  • mongoose:

    Mongooseは、MongoDBのクエリ言語を使用して、直感的にクエリを作成できます。

  • sequelize:

    Sequelizeは、Promiseベースのクエリビルダーを提供し、複雑なクエリを簡単に作成できます。

トランザクション管理

  • typeorm:

    TypeORMは、トランザクション管理をサポートしており、エンティティに基づいたトランザクション操作が可能です。

  • bookshelf:

    Bookshelfは、Knex.jsのトランザクション機能を利用して、トランザクションを管理できます。

  • mongoose:

    Mongooseは、MongoDBのトランザクション機能をサポートしていますが、複雑なトランザクション管理には向いていません。

  • sequelize:

    Sequelizeは、トランザクション管理を強力にサポートしており、複雑なデータ操作を安全に行えます。

学習曲線

  • typeorm:

    TypeORMは、TypeScriptの知識が必要ですが、型安全性を活かした開発ができるため、学習曲線はやや急ですが、長期的には有利です。

  • bookshelf:

    Bookshelfはシンプルで直感的なAPIを持っており、学習曲線は比較的緩やかです。

  • mongoose:

    Mongooseは、MongoDBの特性を理解する必要があるため、初学者には少し学習曲線が急ですが、ドキュメントが豊富です。

  • sequelize:

    Sequelizeは、豊富な機能を持っていますが、学習曲線はやや急です。特に、複雑なクエリやトランザクション管理を理解する必要があります。

選び方: typeorm vs bookshelf vs mongoose vs sequelize

  • typeorm:

    TypeORMはTypeScriptで書かれたORMで、エンティティベースのアプローチを採用しています。TypeScriptの型安全性を活かしたい場合や、複雑なリレーションを持つデータベースを扱う場合に最適です。また、マイグレーションやカスタムリポジトリのサポートも充実しています。

  • bookshelf:

    Bookshelfは、Knex.jsをベースにしたシンプルで柔軟なORMです。軽量で、リレーショナルデータベースを使用したいが、複雑さを避けたい場合に最適です。特に、シンプルなプロジェクトや小規模なアプリケーションに向いています。

  • mongoose:

    MongooseはMongoDB専用のORMで、スキーマベースのデータモデリングを提供します。MongoDBを使用する場合、データの整合性を強制し、バリデーションを行いたい場合に最適です。特に、ドキュメント指向のデータベースを利用する場合に便利です。

  • sequelize:

    Sequelizeは、PromiseベースのORMで、複数のSQLデータベース(MySQL、PostgreSQL、SQLiteなど)をサポートしています。トランザクション管理やマイグレーション機能が必要な場合に適しています。大規模なアプリケーションや複雑なデータベース構造を持つプロジェクトに向いています。

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