[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"project-10432":3},{"id":4,"name":5,"fullName":6,"owner":5,"repo":5,"description":7,"homepage":8,"htmlUrl":9,"language":10,"languages":9,"totalLinesOfCode":9,"stars":11,"forks":12,"watchers":13,"openIssues":14,"contributorsCount":15,"subscribersCount":15,"size":15,"stars1d":15,"stars7d":16,"stars30d":17,"stars90d":15,"forks30d":15,"starsTrendScore":15,"compositeScore":18,"rankGlobal":9,"rankLanguage":9,"license":19,"archived":20,"fork":20,"defaultBranch":21,"hasWiki":22,"hasPages":22,"topics":23,"createdAt":9,"pushedAt":9,"updatedAt":31,"readmeContent":32,"aiSummary":33,"trendingCount":15,"starSnapshotCount":15,"syncStatus":17,"lastSyncTime":34,"discoverSource":35},10432,"bookshelf","bookshelf\u002Fbookshelf","A simple Node.js ORM for PostgreSQL, MySQL and SQLite3 built on top of Knex.js","http:\u002F\u002Fbookshelfjs.org",null,"JavaScript",6352,573,90,224,0,1,2,39.28,"MIT License",false,"master",true,[5,24,25,26,27,28,29,30],"database","javascript","mysql","nodejs","orm","postgresql","sqlite","2026-06-12 02:02:21","# bookshelf.js\n\n[![NPM Version](https:\u002F\u002Fimg.shields.io\u002Fnpm\u002Fv\u002Fbookshelf.svg?style=flat)](https:\u002F\u002Fwww.npmjs.com\u002Fpackage\u002Fbookshelf)\n[![Build Status](https:\u002F\u002Fapi.travis-ci.org\u002Fbookshelf\u002Fbookshelf.svg?branch=master)](https:\u002F\u002Ftravis-ci.org\u002Fbookshelf\u002Fbookshelf)\n[![Dependency Status](https:\u002F\u002Fdavid-dm.org\u002Fbookshelf\u002Fbookshelf\u002Fstatus.svg)](https:\u002F\u002Fdavid-dm.org\u002Fbookshelf\u002Fbookshelf)\n[![devDependency Status](https:\u002F\u002Fdavid-dm.org\u002Fbookshelf\u002Fbookshelf\u002Fdev-status.svg)](https:\u002F\u002Fdavid-dm.org\u002Fbookshelf\u002Fbookshelf?type=dev)\n\nBookshelf is a JavaScript ORM for Node.js, built on the [Knex](http:\u002F\u002Fknexjs.org) SQL query builder. It features both Promise-based and traditional callback interfaces, transaction support, eager\u002Fnested-eager relation loading, polymorphic associations, and support for one-to-one, one-to-many, and many-to-many relations.\n\nIt is designed to work with PostgreSQL, MySQL, and SQLite3.\n\n[Website and documentation](http:\u002F\u002Fbookshelfjs.org). The project is [hosted on GitHub](http:\u002F\u002Fgithub.com\u002Fbookshelf\u002Fbookshelf\u002F), and has a comprehensive [test suite](https:\u002F\u002Ftravis-ci.org\u002Fbookshelf\u002Fbookshelf).\n\n## Introduction\n\nBookshelf 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](http:\u002F\u002Fen.wikipedia.org\u002Fwiki\u002FData_mapper_pattern).\n\nWith 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\u002Fnested-relation loading and first-class transaction support.\n\nIt'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.\n\n## Installation\n\nYou'll need to install a copy of [Knex](http:\u002F\u002Fknexjs.org\u002F), and either `mysql`, `pg`, or `sqlite3` from npm.\n\n```js\n$ npm install knex\n$ npm install bookshelf\n\n# Then add one of the following:\n$ npm install pg\n$ npm install mysql\n$ npm install sqlite3\n```\n\nThe Bookshelf library is initialized by passing an initialized [Knex](http:\u002F\u002Fknexjs.org\u002F) client instance. The [Knex documentation](http:\u002F\u002Fknexjs.org\u002F) provides a number of examples for different databases.\n\n```js\n\u002F\u002F Setting up the database connection\nconst knex = require('knex')({\n  client: 'mysql',\n  connection: {\n    host     : '127.0.0.1',\n    user     : 'your_database_user',\n    password : 'your_database_password',\n    database : 'myapp_test',\n    charset  : 'utf8'\n  }\n})\nconst bookshelf = require('bookshelf')(knex)\n\n\u002F\u002F Defining models\nconst User = bookshelf.model('User', {\n  tableName: 'users'\n})\n```\n\nThis 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:\n\n```js\n\u002F\u002F In a file named, e.g. bookshelf.js\nconst knex = require('knex')(dbConfig)\nmodule.exports = require('bookshelf')(knex)\n\n\u002F\u002F elsewhere, to use the bookshelf client:\nconst bookshelf = require('.\u002Fbookshelf')\n\nconst Post = bookshelf.model('Post', {\n  \u002F\u002F ...\n})\n```\n\n## Examples\n\nHere is an example to get you started:\n\n```js\nconst knex = require('knex')({\n  client: 'mysql',\n  connection: process.env.MYSQL_DATABASE_CONNECTION\n})\nconst bookshelf = require('bookshelf')(knex)\n\nconst User = bookshelf.model('User', {\n  tableName: 'users',\n  posts() {\n    return this.hasMany(Posts)\n  }\n})\n\nconst Post = bookshelf.model('Post', {\n  tableName: 'posts',\n  tags() {\n    return this.belongsToMany(Tag)\n  }\n})\n\nconst Tag = bookshelf.model('Tag', {\n  tableName: 'tags'\n})\n\nnew User({id: 1}).fetch({withRelated: ['posts.tags']}).then((user) => {\n  console.log(user.related('posts').toJSON())\n}).catch((error) => {\n  console.error(error)\n})\n```\n\n## Official Plugins\n\n* [Virtuals](https:\u002F\u002Fgithub.com\u002Fbookshelf\u002Fvirtuals-plugin): Define virtual properties on your model to compute new values.\n* [Case Converter](https:\u002F\u002Fgithub.com\u002Fbookshelf\u002Fcase-converter-plugin): Handles the conversion between the database's snake_cased and a model's camelCased properties automatically.\n* [Processor](https:\u002F\u002Fgithub.com\u002Fbookshelf\u002Fprocessor-plugin): Allows defining custom processor functions that handle transformation of values whenever they are `.set()` on a model.\n\n## Community plugins\n\n* [bookshelf-cascade-delete](https:\u002F\u002Fgithub.com\u002Fseegno\u002Fbookshelf-cascade-delete) - Cascade delete related models on destroy.\n* [bookshelf-json-columns](https:\u002F\u002Fgithub.com\u002Fseegno\u002Fbookshelf-json-columns) - Parse and stringify JSON columns on save and fetch instead of manually define hooks for each model (PostgreSQL and SQLite).\n* [bookshelf-mask](https:\u002F\u002Fgithub.com\u002Fseegno\u002Fbookshelf-mask) - Similar to the functionality of the {@link Model#visible} attribute but supporting multiple scopes, masking models and collections using the [json-mask](https:\u002F\u002Fgithub.com\u002Fnemtsov\u002Fjson-mask) API.\n* [bookshelf-schema](https:\u002F\u002Fgithub.com\u002Fbogus34\u002Fbookshelf-schema) - A plugin for handling fields, relations, scopes and more.\n* [bookshelf-signals](https:\u002F\u002Fgithub.com\u002Fbogus34\u002Fbookshelf-signals) - A plugin that translates Bookshelf events to a central hub.\n* [bookshelf-paranoia](https:\u002F\u002Fgithub.com\u002Festate\u002Fbookshelf-paranoia) - Protect your database from data loss by soft deleting your rows.\n* [bookshelf-uuid](https:\u002F\u002Fgithub.com\u002Festate\u002Fbookshelf-uuid) - Automatically generates UUIDs for your models.\n* [bookshelf-modelbase](https:\u002F\u002Fgithub.com\u002Fbsiddiqui\u002Fbookshelf-modelbase) - An alternative to extend `Model`, adding timestamps, attribute validation and some native CRUD methods.\n* [bookshelf-advanced-serialization](https:\u002F\u002Fgithub.com\u002Fsequiturs\u002Fbookshelf-advanced-serialization) - A more powerful visibility plugin, supporting serializing models and collections according to access permissions, application context, and after ensuring relations have been loaded.\n* [bookshelf-plugin-mode](https:\u002F\u002Fgithub.com\u002Fpopodidi\u002Fbookshelf-plugin-mode) - Plugin inspired by the functionality of the {@link Model#visible} attribute, allowing to specify different modes with corresponding visible\u002Fhidden fields of model.\n* [bookshelf-secure-password](https:\u002F\u002Fgithub.com\u002Fvenables\u002Fbookshelf-secure-password) - A plugin for easily securing passwords using bcrypt.\n* [bookshelf-bcrypt](https:\u002F\u002Fgithub.com\u002Fbsiddiqui\u002Fbookshelf-bcrypt) - Another plugin for automatic password hashing for your bookshelf models using bcrypt.\n* [bookshelf-bcrypt.js](https:\u002F\u002Fgithub.com\u002F7kasper\u002Fbookshelf-bcrypt.js) - Fork of bookshelf-bcrypt using bcryptjs, using less dependencies.\n* [bookshelf-default-select](https:\u002F\u002Fgithub.com\u002FDJAndries\u002Fbookshelf-default-select) - Enables default column selection for models. Inspired by the functionality of the {@link Model#visible} attribute, but operates on the database level.\n* [bookshelf-ez-fetch](https:\u002F\u002Fgithub.com\u002FDJAndries\u002Fbookshelf-ez-fetch) - Convenient fetching methods which allow for compact filtering, relation selection and error handling.\n* [bookshelf-manager](https:\u002F\u002Fgithub.com\u002Fericclemmons\u002Fbookshelf-manager) - Model & Collection manager to make it easy to create & save deep, nested JSON structures from API requests.\n* [bookshelf-spotparse](https:\u002F\u002Fgithub.com\u002F7kasper\u002Fbookshelf-spotparse) - A plugin that makes formatting, parsing and finding models easier.\n* [bookshelf-update](https:\u002F\u002Fgithub.com\u002F7kasper\u002Fbookshelf-update) - Simple Bookshelf plugin that allows simple patching of models and skips updating if no values have changed.\n\n## Support\n\nHave questions about the library? Come join us in the [#bookshelf freenode IRC channel](http:\u002F\u002Fwebchat.freenode.net\u002F?channels=bookshelf) for support on [knex.js](http:\u002F\u002Fknexjs.org\u002F) and bookshelf.js, or post an issue on [Stack Overflow](http:\u002F\u002Fstackoverflow.com\u002Fquestions\u002Ftagged\u002Fbookshelf.js).\n\n## Contributing\n\nIf you want to contribute to Bookshelf you'll usually want to report an issue or submit a\npull-request. For this purpose the [online repository](https:\u002F\u002Fgithub.com\u002Fbookshelf\u002Fbookshelf\u002F) is\navailable on GitHub.\n\nFor further help setting up your local development environment or learning how you can contribute to\nBookshelf you should read the [Contributing document](https:\u002F\u002Fgithub.com\u002Fbookshelf\u002Fbookshelf\u002Fblob\u002Fmaster\u002F.github\u002FCONTRIBUTING.md)\navailable on GitHub.\n\n## F.A.Q.\n\n### Can I use standard node.js style callbacks?\n\nYes, you can call `.asCallback(function(err, resp) {` on any database operation method and use the standard `(err, result)` style callback interface if you prefer.\n\n### My relations don't seem to be loading, what's up?\n\nMake 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.\n\nThis 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.\n\n### My process won't exit after my script is finished, why?\n\nThe 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](http:\u002F\u002Fknexjs.org\u002F#Installation-pooling).\n\n### How do I debug?\n\nIf 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:\n\n```js\n\u002F\u002F Turning on debug mode for all queries\nconst knex = require('knex')({\n  debug: true,\n  client: 'mysql',\n  connection: process.env.MYSQL_DATABASE_CONNECTION\n})\nconst bookshelf = require('bookshelf')(knex)\n\n\u002F\u002F Debugging a single query\nnew User({id: 1}).fetch({debug: true, withRelated: ['posts.tags']}).then(user => {\n  \u002F\u002F ...\n})\n```\n\nSometimes 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](https:\u002F\u002Fgithub.com\u002Fdannycoates\u002Fnode-inspector), which allows you to debug code with `debugger` statements like you would in the browser.\n\nBookshelf uses its own copy of the `bluebird` Promise library. You can read up [here](http:\u002F\u002Fbluebirdjs.com\u002Fdocs\u002Fapi\u002Fpromise.config.html) for more on debugging Promises.\n\nAdding 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:\n\n```js\nprocess.stderr.on('data', (data) => {\n  console.log(data)\n})\n```\n\n### How do I run the test suite?\n\nSee the [CONTRIBUTING](https:\u002F\u002Fgithub.com\u002Fbookshelf\u002Fbookshelf\u002Fblob\u002Fmaster\u002F.github\u002FCONTRIBUTING.md#running-the-tests)\ndocument on GitHub.\n\n### Can I use Bookshelf outside of Node.js?\n\nWhile 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](http:\u002F\u002Fknexjs.org\u002F#Adapters). No such adapter exists though.\n\n### Which open-source projects are using Bookshelf?\n\nWe found the following projects using Bookshelf, but there can be more:\n\n* [Ghost](https:\u002F\u002Fghost.org\u002F) (A blogging platform) uses bookshelf. [[Link](https:\u002F\u002Fgithub.com\u002FTryGhost\u002FGhost\u002Ftree\u002Fmaster\u002Fcore\u002Fserver\u002Fmodels)]\n* [Soapee](http:\u002F\u002Fsoapee.com\u002F) (Soap Making Community and Resources) uses bookshelf. [[Link](https:\u002F\u002Fgithub.com\u002Fnazar\u002Fsoapee-api\u002Ftree\u002Fmaster\u002Fsrc\u002Fmodels)]\n* [NodeZA](http:\u002F\u002Fnodeza.co.za\u002F) (Node.js social platform for developers in South Africa) uses bookshelf. [[Link](https:\u002F\u002Fgithub.com\u002Fqawemlilo\u002Fnodeza\u002Ftree\u002Fmaster\u002Fmodels)]\n* [Sunday Cook](https:\u002F\u002Fgithub.com\u002Fsunday-cooks\u002Fsunday-cook) (A social cooking event platform) uses bookshelf. [[Link](https:\u002F\u002Fgithub.com\u002Fsunday-cooks\u002Fsunday-cook\u002Ftree\u002Fmaster\u002Fserver\u002Fbookshelf)]\n* [FlyptoX](http:\u002F\u002Fwww.flyptox.com\u002F) (Open-source Node.js cryptocurrency exchange) uses bookshelf. [[Link](https:\u002F\u002Fgithub.com\u002FFlipSideHR\u002FFlyptoX\u002Ftree\u002Fmaster\u002Fserver\u002Fmodels)]\n* And of course, everything on [here](https:\u002F\u002Fwww.npmjs.com\u002Fbrowse\u002Fdepended\u002Fbookshelf) use bookshelf too.\n","Bookshelf.js 是一个基于 Knex.js 的 Node.js ORM 库，支持 PostgreSQL、MySQL 和 SQLite3 数据库。它提供了基于 Promise 和传统回调的接口，支持事务处理、关联加载（包括热切和嵌套热切加载）、多态关联以及一对一、一对多和多对多关系。Bookshelf.js 设计简洁，易于理解和扩展，不强制使用特定的验证方案，并且允许用户在需要时直接使用底层的 Knex 接口进行自定义查询。适用于需要高效地管理数据库操作及对象关系映射的应用场景，如后端服务开发等。","2026-06-11 03:28:22","top_topic"]