bookshelf、objection、sequelize、waterline は、Node.js 应用程序でデータベース操作を簡素化するための ORM(Object-Relational Mapping)ライブラリです。これらは SQL データベースのテーブルを JavaScript のオブジェクトとして扱えるようにし、クエリ作成やリレーション管理を抽象化します。sequelize は機能豊富で多くのデータベースをサポートし、objection と bookshelf は Knex クエリビルダーを基盤にしています。waterline は Sails.js フレームワークの標準 ORM として知られていますが、近年は保守状況に注意が必要です。
bookshelf、objection、sequelize、waterline は、Node.js でデータベースを扱うための代表的な ORM ライブラリですが、内部の仕組みや保守状況に大きな違いがあります。フルスタック開発や BFF(Backend for Frontend)層を構築する際、これらの選択はプロジェクトの長期維持性に直結します。主要な違いを技術的に比較します。
sequelize は定義関数またはクラスを使用し、メタデータが豊富です。
// sequelize: Model definition
const User = sequelize.define('User', {
firstName: { type: DataTypes.STRING, allowNull: false },
lastName: { type: DataTypes.STRING }
});
objection は ES6 クラスを拡張してモデルを定義します。
tableName や relationMappings をプロパティとして定義します。// objection: Model class
class User extends Model {
static get tableName() {
return 'users';
}
}
bookshelf もクラス拡張ですが、より簡素な構造です。
bookshelf.Model.extend を使用して定義します。// bookshelf: Model extend
const User = bookshelf.Model.extend({
tableName: 'users'
});
waterline は設定オブジェクト中心の定義です。
attributes オブジェクトで宣言します。// waterline: Definition object
module.exports = {
attributes: {
firstName: { type: 'string', required: true },
lastName: { type: 'string' }
}
};
sequelize は独自のクエリインターフェースを持ちます。
findAll や findOne などのメソッドでデータを取得します。// sequelize: Fetching data
const users = await User.findAll({
where: { lastName: 'Smith' }
});
objection は Knex クエリビルダーをそのまま使用します。
query() メソッドから開始し、Knex のメソッドを連鎖させます。// objection: Fetching data
const users = await User.query().where('lastName', 'Smith');
bookshelf も Knex をベースにしていますが、ラッパーがあります。
fetchAll や where を使用してクエリを構築します。objection に比べると機能追加は緩やかです。// bookshelf: Fetching data
const users = await User.where('lastName', 'Smith').fetchAll();
waterline は独自のクエリ構文を持ちます。
find メソッドを使用し、オブジェクトで条件を指定します。// waterline: Fetching data
const users = await User.find({
where: { lastName: 'Smith' }
});
sequelize はモデル間で関連を定義し、include で読み込みます。
hasMany や belongsTo で関係を宣言します。// sequelize: Relations
User.hasMany(Post);
const user = await User.findOne({ include: [Post] });
objection は relationMappings で関係を定義します。
withGraphFetched で実行します。// objection: Relations
class User extends Model {
static get relationMappings() {
return { posts: { relation: Model.HasManyRelation, modelClass: Post } };
}
}
const user = await User.query().withGraphFetched('posts');
bookshelf は hasMany などで関係を定義します。
fetch 時に { withRelated: [...] } オプションで関連データを読み込みます。// bookshelf: Relations
const User = bookshelf.Model.extend({
posts: function() { return this.hasMany(Post); }
});
const user = await User.fetch({ withRelated: ['posts'] });
waterline は attributes 内で関連を定義します。
collection や model キーで関連付けを行います。// waterline: Relations
module.exports = {
attributes: {
posts: { collection: 'post', via: 'userId' }
}
};
// Usage: User.find().populate('posts')
sequelize は活発に保守されており、バージョン 7 へ移行中です。
objection も継続的に更新されており、コミュニティ支持が厚いです。
bookshelf は更新頻度が低下しており、注意が必要です。
objection への移行を検討すべきです。waterline は Sails.js のメンテナンスモードに伴い、リスクが高いです。
| 機能 | sequelize | objection | bookshelf | waterline |
|---|---|---|---|---|
| 基盤 | 独自 | Knex | Knex | 独自 |
| 定義スタイル | 関数/クラス | クラス | クラス | オブジェクト |
| クエリ制御 | 抽象化重視 | 柔軟性重視 | 標準的 | 制限あり |
| TypeScript | 対応 | 良好 | 限定的 | 限定的 |
| 保守状況 | 活発 | 活発 | 低下 | リスク大 |
sequelize は機能完备な工具箱 🧰 のように、大規模で標準化されたプロジェクトに適しています。複数のデータベース対応や、内蔵マイグレーションが必要な場合に最適です。
objection は精密なエンジニアリングキット 🔧 のように、クエリ制御とオブジェクトマッピングのバランスを求める場合に適しています。現代の Node.js 開発において、最もバランスの取れた選択肢の一つです。
bookshelf と waterline は、既存システムの維持以外では使用を避けるべきです — 保守リスクがメリットを上回ります。
結論:新規プロジェクトでは objection または sequelize を選定し、プロジェクトの規模と制御性の要件に合わせて決定してください。
過去の資産がある場合を除き、新規プロジェクトでは慎重な検討が必要です。保守頻度が低下しており、コミュニティの支持も objection へ移行しつつあります。既存のレガシーシステムを維持する場合以外は、代替案を検討すべきです。
Knex クエリビルダーの灵活性を活かしたい開発者に向いています。モデル定義がクラスベースで、TypeScript との相性も良好です。データベーススキーマへの制御を保ちつつ、オブジェクトマッピングの利便性も得たいバランス重視のプロジェクトに最適です。
大規模なエンタープライズプロジェクトや、複数のデータベースエンジンを使い分ける必要がある場合に適しています。機能セットが豊富で、マイグレーションツールも内蔵されているため、包括的な解决方案を求めるチームに向いています。ただし、ライブラリが重くなる傾向があるため、シンプルな用途ではオーバーキルになる可能性があります。
Sails.js フレームワークを使用しない限り、新規プロジェクトでの採用は避けるべきです。保守体制が弱まっており、将来のリスクがあります。スタンドアロンの ORM として使用するよりも、フレームワークに縛られない選択肢を検討することをお勧めします。
Bookshelf is a JavaScript ORM for Node.js, built on the Knex SQL query builder. It features both Promise-based and traditional callback interfaces, transaction support, eager/nested-eager relation loading, polymorphic associations, and support for one-to-one, one-to-many, and many-to-many relations.
It is designed to work with PostgreSQL, MySQL, and SQLite3.
Website and documentation. The project is hosted on GitHub, and has a comprehensive test suite.
Bookshelf aims to provide a simple library for common tasks when querying databases in JavaScript, and forming relations between these objects, taking a lot of ideas from the Data Mapper Pattern.
With a concise, literate codebase, Bookshelf is simple to read, understand, and extend. It doesn't force you to use any specific validation scheme, and provides flexible, efficient relation/nested-relation loading and first-class transaction support.
It's a lean object-relational mapper, allowing you to drop down to the raw Knex interface whenever you need a custom query that doesn't quite fit with the stock conventions.
You'll need to install a copy of Knex, and either mysql, pg, or sqlite3 from npm.
$ npm install knex
$ npm install bookshelf
# Then add one of the following:
$ npm install pg
$ npm install mysql
$ npm install sqlite3
The Bookshelf library is initialized by passing an initialized Knex client instance. The Knex documentation provides a number of examples for different databases.
// Setting up the database connection
const knex = require('knex')({
client: 'mysql',
connection: {
host : '127.0.0.1',
user : 'your_database_user',
password : 'your_database_password',
database : 'myapp_test',
charset : 'utf8'
}
})
const bookshelf = require('bookshelf')(knex)
// Defining models
const User = bookshelf.model('User', {
tableName: 'users'
})
This initialization should likely only ever happen once in your application. As it creates a connection pool for the current database, you should use the bookshelf instance returned throughout your library. You'll need to store this instance created by the initialize somewhere in the application so you can reference it. A common pattern to follow is to initialize the client in a module so you can easily reference it later:
// In a file named, e.g. bookshelf.js
const knex = require('knex')(dbConfig)
module.exports = require('bookshelf')(knex)
// elsewhere, to use the bookshelf client:
const bookshelf = require('./bookshelf')
const Post = bookshelf.model('Post', {
// ...
})
Here is an example to get you started:
const knex = require('knex')({
client: 'mysql',
connection: process.env.MYSQL_DATABASE_CONNECTION
})
const bookshelf = require('bookshelf')(knex)
const User = bookshelf.model('User', {
tableName: 'users',
posts() {
return this.hasMany(Posts)
}
})
const Post = bookshelf.model('Post', {
tableName: 'posts',
tags() {
return this.belongsToMany(Tag)
}
})
const Tag = bookshelf.model('Tag', {
tableName: 'tags'
})
new User({id: 1}).fetch({withRelated: ['posts.tags']}).then((user) => {
console.log(user.related('posts').toJSON())
}).catch((error) => {
console.error(error)
})
.set() on a model.Model, adding timestamps, attribute validation and some native CRUD methods.Have questions about the library? Come join us in the #bookshelf freenode IRC channel for support on knex.js and bookshelf.js, or post an issue on Stack Overflow.
If you want to contribute to Bookshelf you'll usually want to report an issue or submit a pull-request. For this purpose the online repository is available on GitHub.
For further help setting up your local development environment or learning how you can contribute to Bookshelf you should read the Contributing document available on GitHub.
Yes, you can call .asCallback(function(err, resp) { on any database operation method and use the standard (err, result) style callback interface if you prefer.
Make sure to check that the type is correct for the initial parameters passed to the initial model being fetched. For example new Model({id: '1'}).load([relations...]) will not return the same as new Model({id: 1}).load([relations...]) - notice that the id is a string in one case and a number in the other. This can be a common mistake if retrieving the id from a url parameter.
This is only an issue if you're eager loading data with load without first fetching the original model. new Model({id: '1'}).fetch({withRelated: [relations...]}) should work just fine.
The issue here is that Knex, the database abstraction layer used by Bookshelf, uses connection pooling and thus keeps the database connection open. If you want your process to exit after your script has finished, you will have to call .destroy(cb) on the knex property of your Bookshelf instance or on the Knex instance passed during initialization. More information about connection pooling can be found over at the Knex docs.
If you pass debug: true in the options object to your knex initialize call, you can see all of the query calls being made. You can also pass that same option to all methods that access the database, like model.fetch() or model.destroy(). Examples:
// Turning on debug mode for all queries
const knex = require('knex')({
debug: true,
client: 'mysql',
connection: process.env.MYSQL_DATABASE_CONNECTION
})
const bookshelf = require('bookshelf')(knex)
// Debugging a single query
new User({id: 1}).fetch({debug: true, withRelated: ['posts.tags']}).then(user => {
// ...
})
Sometimes you need to dive a bit further into the various calls and see what all is going on behind the scenes. You can use node-inspector, which allows you to debug code with debugger statements like you would in the browser.
Bookshelf uses its own copy of the bluebird Promise library. You can read up here for more on debugging Promises.
Adding the following block at the start of your application code will catch any errors not otherwise caught in the normal Promise chain handlers, which is very helpful in debugging:
process.stderr.on('data', (data) => {
console.log(data)
})
See the CONTRIBUTING document on GitHub.
While it primarily targets Node.js, all dependencies are browser compatible, and it could be adapted to work with other javascript environments supporting a sqlite3 database, by providing a custom Knex adapter. No such adapter exists though.
We found the following projects using Bookshelf, but there can be more: