1 var Type = require("./index").Type, 2 comb = require("comb"); 3 4 //Boolean Types 5 var setBoolType = function() { 6 return function(val) { 7 if (!comb.isBoolean(val)) { 8 if (val == "true") { 9 val = true; 10 } else if (val == "false") { 11 val = false; 12 } else { 13 throw new Error("Invalid Boolean value"); 14 } 15 } 16 return val; 17 }; 18 }; 19 20 var checkBooleanType = function(type) { 21 return function(val) { 22 if (!comb.isBoolean(val)) { 23 throw new Error(type + " requires a boolean type"); 24 } 25 }; 26 }; 27 28 29 var boolDefaults = { 30 allowNull : true, 31 primaryKey : false, 32 foreignKey : false, 33 "default" : null, 34 description : "" 35 }; 36 37 /** 38 * Mysql BOOL datatype 39 * 40 * @function 41 * @param {Object} options options for the BOOL data type. 42 * @param {Boolean} [options.allowNull=true] should the field allow null 43 * @param {Boolean} [options.default = null] default value of the field 44 * @param {Boolean} [options.unsigned = false] unsigned number 45 * @param {Boolean} [options.description = ""] description fo the field. 46 * 47 * @return {Type} A Type representing a BOOL column. 48 * 49 * @name BOOL 50 * @memberOf moose.adapters.mysql.types 51 * 52 */ 53 exports.BOOL = function(options) { 54 var ops = comb.merge({}, boolDefaults, {type : "BOOL"}, options || {}); 55 ops.setSql = setBoolType(); 56 ops.checkType = checkBooleanType(ops.type); 57 return new Type(ops); 58 }; 59 60 /** 61 * Mysql BOOLEAN datatype 62 * 63 * @function 64 * @param {Object} options options for the BOOLEAN data type. 65 * @param {Boolean} [options.allowNull=true] should the field allow null 66 * @param {Boolean} [options.default = null] default value of the field 67 * @param {Boolean} [options.unsigned = false] unsigned number 68 * @param {Boolean} [options.description = ""] description fo the field. 69 * 70 * @return {Type} A Type representing a BOOLEAN column. 71 * 72 * @name BOOLEAN 73 * @memberOf moose.adapters.mysql.types 74 * 75 */ 76 exports.BOOLEAN = function(options) { 77 var ops = comb.merge({}, boolDefaults, {type : "BOOLEAN"}, options || {}); 78 ops.setSql = setBoolType(); 79 ops.checkType = checkBooleanType(ops.type); 80 return new Type(ops); 81 }; 82