Node.js 的 ORM 框架:rdb
rdb 是 Node.js 的一個 ORM 框架,支持 Postgres 和 MySQL 數據庫。
特點:
-
簡單靈活的映射器
-
支持事務處理
-
可定制的持久化策略
-
支持延遲加載
-
基于 promises.
示例代碼:
var rdb = require('rdb');
var Customer = rdb.table('_customer');
Customer.primaryColumn('cId').guid().as('id');
Customer.column('cName').string().as('name');
Customer.column('cBalance').numeric().as('balance');
Customer.column('cRegdate').date().as('registeredDate');
Customer.column('cIsActive').boolean().as('isActive');
Customer.column('cPicture').binary().as('picture');
var db = rdb('postgres://postgres:postgres@localhost/test');
db.transaction()
.then(getById)
.then(printCustomer)
.then(rdb.commit)
.then(null, rdb.rollback)
.then(onOk, onFailed);
function getById() {
return Customer.getById('a0000000-0000-0000-0000-000000000000');
}
function printCustomer(customer) {
var format = 'Customer Id: %s, name: %s, Balance: %s, Registered Date: %s, Is Active: %s, Picture: %s';
var args = [format, customer.id, customer.name, customer.balance, customer.registeredDate, customer.isActive, customer.picture];
console.log.apply(null,args);
}
function onOk() {
console.log('Success');
console.log('Waiting for connection pool to teardown....');
}
function onFailed(err) {
console.log('Rollback');
console.log(err);
} 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!