Node.js ORM框架:bookshelf
一個基于Knex.js的Node.js ORM框架,支持PostgreSQL,MySQL和SQLite3
簡單來說,Bookself是一個優秀的代碼庫,它易于閱讀、理解、可擴展。它不強制你使用任何特定的校驗scheme,而是提供靈活有效的關系或嵌套關系加載策略,一級類支持事務。它是一個精益的對象關系映射器(lean Object Relation Mapper),允許你使用原始的knex接口,當你需要自定義查詢時,因為它有時并不能完全滿足老一套的慣例。
Bookshelf遵從和Backbone.js一樣棒的Models和Collections思想,使用相同的模式、命名慣例和哲學構建輕量、易于操控的ORM。如果你已經了解Backbone,你就知道如何使用Bookshelf.
插件
- Registry: Register models in a central location so that you can refer to them using a string in relations instead of having to require it every time. Helps deal with the challenges of circular module dependencies in Node.
- Virtuals: Define virtual properties on your model to compute new values.
- Visibility: Specify a whitelist/blacklist of model attributes when serialized toJSON.
支持
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 or in the GitHub issue tracker.
示例:
var knex = require('knex')({client: 'mysql', connection: process.env.MYSQL_DATABASE_CONNECTION }); var bookshelf = require('bookshelf')(knex); var User = bookshelf.Model.extend({ tableName: 'users', messages: function() { return this.hasMany(Posts); } }); var Posts = bookshelf.Model.extend({ tableName: 'messages', tags: function() { return this.belongsToMany(Tag); } }); var Tag = bookshelf.Model.extend({ tableName: 'tags' }) User.where('id', 1).fetch({withRelated: ['posts.tags']}).then(function(user) { console.log(user.related('posts').toJSON()); }).catch(function(err) { console.error(err); });
本文由用戶 cw63 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!