sqlite-orm
Advanced tools
+2
-0
@@ -185,4 +185,6 @@ | ||
| Mapper.BOOL = 'BOOL'; | ||
| return Mapper; | ||
| })(); |
+60
-10
@@ -14,3 +14,3 @@ var Mixin, ModelAssociation, ModelBaseMixin, Q, _, | ||
| module.exports = ModelBaseMixin = (function(superClass) { | ||
| var getHook, getIdFromWhere; | ||
| var boolOperator, dateOperator, getHook, getIdFromWhere, getOperator; | ||
@@ -48,2 +48,55 @@ extend(ModelBaseMixin, superClass); | ||
| dateOperator = { | ||
| capture: function(opts) { | ||
| if (opts.type === 'DATETIME') { | ||
| opts.type = 'INTEGER'; | ||
| return true; | ||
| } | ||
| }, | ||
| set: function(val) { | ||
| if (val instanceof Date) { | ||
| return val.getTime(); | ||
| } else { | ||
| return val; | ||
| } | ||
| }, | ||
| get: function(val) { | ||
| return new Date(val); | ||
| } | ||
| }; | ||
| boolOperator = { | ||
| capture: function(opts) { | ||
| if (opts.type === 'BOOL') { | ||
| opts.type = 'INTEGER'; | ||
| return true; | ||
| } | ||
| }, | ||
| set: function(val) { | ||
| if (val) { | ||
| return 1; | ||
| } else { | ||
| return 0; | ||
| } | ||
| }, | ||
| get: function(val) { | ||
| if (val === 1) { | ||
| return true; | ||
| } else { | ||
| return false; | ||
| } | ||
| } | ||
| }; | ||
| getOperator = function(opts) { | ||
| var i, len, operator, ref; | ||
| ref = [boolOperator, dateOperator]; | ||
| for (i = 0, len = ref.length; i < len; i++) { | ||
| operator = ref[i]; | ||
| if (operator.capture(opts)) { | ||
| return operator; | ||
| } | ||
| } | ||
| }; | ||
| getHook = function(name) { | ||
@@ -62,9 +115,6 @@ var hookName, hookObj; | ||
| ModelBaseMixin.defineAttr = function(name, opts) { | ||
| var defaultVal, hookFunc, isDate, key, ref; | ||
| var defaultVal, hookFunc, key, operator, ref; | ||
| key = '_' + name; | ||
| defaultVal = (ref = opts["default"]) != null ? ref : null; | ||
| isDate = opts.type === 'DATETIME'; | ||
| if (isDate) { | ||
| opts.type = 'INTEGER'; | ||
| } | ||
| operator = getOperator(opts); | ||
| hookFunc = getHook.call(this, name); | ||
@@ -75,4 +125,4 @@ return Object.defineProperty(this.prototype, name, { | ||
| val = (ref1 = this[key]) != null ? ref1 : defaultVal; | ||
| if (isDate) { | ||
| val = new Date(val); | ||
| if (operator) { | ||
| val = operator.get(val); | ||
| } | ||
@@ -85,4 +135,4 @@ if ((hookFunc != null) && (hookFunc.get != null)) { | ||
| set: function(val) { | ||
| if (isDate && val instanceof Date) { | ||
| val = val.getTime(); | ||
| if (operator) { | ||
| val = operator.set(val); | ||
| } | ||
@@ -89,0 +139,0 @@ if ((hookFunc != null) && (hookFunc.set != null)) { |
+1
-1
| { | ||
| "name": "sqlite-orm", | ||
| "description": "the ORM framework for sqlite", | ||
| "version": "0.1.22", | ||
| "version": "0.1.23", | ||
| "homepage": "https://github.com/liuxiong332/node-sqlite-orm", | ||
@@ -6,0 +6,0 @@ "bugs": "https://github.com/liuxiong332/node-sqlite-orm/issues", |
+2
-0
@@ -121,2 +121,4 @@ # sqlite-orm | ||
| * **BOOL** `String` the Bool date type. | ||
| ### Migration | ||
@@ -123,0 +125,0 @@ |
60659
1.75%1695
2.91%242
0.83%