Skip to content

Commit

Permalink
* Add db.clone API that allows the db object to be cloned with a clon…
Browse files Browse the repository at this point in the history
…ed list of models (for further extension).

* Add db.extend that allows models in a db object to be further extended with functionalities.
* Add test case in test.js to for the new functionalities.
  • Loading branch information
redblaze committed Feb 19, 2014
1 parent cbdf393 commit 5b3ff46
Show file tree
Hide file tree
Showing 3 changed files with 147 additions and 23 deletions.
113 changes: 94 additions & 19 deletions lib/node-mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,106 @@ var DB = Class(_DB, {
},

add: function(cfg) {
var me = this;

var model = {};

var Row = Class($M.Row, $U.extend({
_init: function(data) {
this.parent._init.call(this, data, {
table: Table
});
}
}, cfg.Row || {}));

var TableClass = Class($M.Table, cfg.Table || {});

var Table = new TableClass({
db: this,
name: cfg.name,
rowClass: Row,
idFieldName: cfg.idFieldName || 'id',
versionFieldName: cfg.versionFieldName || 'version',
createdFieldName: cfg.createdFieldName || 'date_created',
updatedFieldName: cfg.updatedFieldName || 'last_updated'
var RowTemplate = function(model) {
var closedRowClass = Class($M.Row, {
_init: function(data) {
this.parent._init.call(this, data, {
table: model.Table
});
}
});
return Class(closedRowClass, cfg.Row || {});
};

var Row = RowTemplate(model);

$U.extend(model, {
Row: Row,
_RowTemplate: RowTemplate
});


var TableTemplate = function(model) {
var closedTableClass = Class($M.Table, {
_init: function() {
this.parent._init.call(this, {
db: me,
name: cfg.name,
rowClass: model.Row,
idFieldName: cfg.idFieldName || 'id',
versionFieldName: cfg.versionFieldName || 'version',
createdFieldName: cfg.createdFieldName || 'date_created',
updatedFieldName: cfg.updatedFieldName || 'last_updated'
});
}
});

return Class(closedTableClass, cfg.Table || {});
};

var TableClass = TableTemplate(model);
var Table = new TableClass();

$U.extend(model, {
Table: Table,
_TableTemplate: TableTemplate
});

this._models[cfg.name] = model;

return Table;
},

clone: function() {
var tempClass = function() {
this._models = $U.clone(this._models);
};

tempClass.prototype = this;
return new tempClass();
},

extend: function(cfg) {
var _model = this.get(cfg.name);

if (!_model) {
throw new Error('node-mysql-runtime-error: db.extend can not be used on empty model.');
}

var model = {};

var _RowTemplate = _model._RowTemplate;

var RowTemplate = function(model) {
var superClass = _RowTemplate(model);
return Class(superClass, cfg.Row || {});
};

var Row = RowTemplate(model);

$U.extend(model, {
Row: Row,
Table: Table
_RowTemplate: RowTemplate
});

var _TableTemplate = _model._TableTemplate;

var TableTemplate = function(model) {
var superClass = _TableTemplate(model);
return Class(superClass, cfg.Table || {});
};

var TableClass = TableTemplate(model);

var Table = new TableClass();

$U.extend(model, {
Table: Table,
_TableTemplate: TableTemplate
});

this._models[cfg.name] = model;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "A wrapper of node.js mysql package to make it a bit easier to use.",
"main": "./lib/node-mysql",
"author": "Chiyan Chen",
"version": "0.3.6",
"version": "0.3.7",
"repository" : {
"type" : "git",
"url" : "https://github.com/redblaze/node-mysql.git"
Expand Down
55 changes: 52 additions & 3 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
Error.stackTraceLimit = Infinity;

var cps = require('cps');

var db = require('../lib/node-mysql.js');
var DB = db.DB;
var Row = db.Row;
var Table = db.Table;

var cb = function() {
var handleError = function(e) {
Expand Down Expand Up @@ -73,5 +73,54 @@ var cursorTest = function(cb) {
}, cb);
};

var modelTest = function(cb) {
box.add({
name: 'coupons',
idFieldName: 'coupon_id'
})
;

box.add({
name: 'products',
idFieldName: 'product_id'
})
;

var oldBox = box;

box = box.clone();

box.extend({
name: 'coupons',
Row: {
getDiscountType: function() {
return this._data['discount_type'];
}
}
})
.linksTo({
name: 'product',
table: 'products',
key: 'product_id'
})
;

var Coupon = box.get('coupons').Table;

box.connect(function(conn, cb) {
cps.seq([
function(_, cb) {
var q = Coupon.baseQuery('where product_id is not null limit 1');
Coupon.find(conn, q, cb);
},
function(coupons, cb) {
cps.pmap(coupons, function(coupon, cb) {
// coupon.linksTo(conn, 'product', cb);
cb(null, coupon.getDiscountType());
}, cb);
}
], cb);
}, cb);
};

cursorTest(cb);
modelTest(cb);

0 comments on commit 5b3ff46

Please sign in to comment.