/lib/adapters/types/mysql/string.js

https://github.com/Babay88/moose · JavaScript · 297 lines · 135 code · 16 blank · 146 comment · 48 complexity · 658ae18e9f05d03f0e17ad5b43388b6e MD5 · raw file

  1. var Type = require("./index").Type,
  2. comb = require("comb");
  3. var stringDefaults = {
  4. allowNull : true,
  5. primaryKey : false,
  6. foreignKey : false,
  7. "default" : null,
  8. unique : false,
  9. description : ""
  10. };
  11. var getStringType = function(cmpFun) {
  12. return function(val) {
  13. if (!val && val !== "") {
  14. val = null;
  15. } else if (typeof val != "string") {
  16. val = "" + val;
  17. }
  18. cmpFun && cmpFun(val);
  19. return val;
  20. };
  21. };
  22. var checkStringType = function(type, cmpFun) {
  23. return function(val) {
  24. if (typeof val != "string") throw new Error(type + " requires a string type.");
  25. cmpFun && cmpFun(val);
  26. };
  27. };
  28. //String Types
  29. /**
  30. *
  31. *
  32. * Mysql CHAR datatype
  33. *
  34. * @function
  35. * @param {Object} options options for the CHAR data type.
  36. * @param {Number} [options.length=255] the length of the field
  37. * @param {Boolean} [options.allowNull=true] should the field allow null
  38. * @param {Boolean} [options.default = null] default value of the field
  39. * @param {Boolean} [options.description = ""] description fo the field.
  40. *
  41. * @return {Type} A Type representing a CHAR column.
  42. *
  43. * @name CHAR
  44. * @memberOf moose.adapters.mysql.types
  45. *
  46. */
  47. exports.CHAR = function(options) {
  48. var ops = comb.merge({}, stringDefaults, {length : 255, type : "CHAR"}, options || {});
  49. if (ops.length > 4294967295) {
  50. throw new Error("Max char type length is 255");
  51. }
  52. var cmpFun = function(val) {
  53. if (ops.length == undefined && val.length > 255) throw new Error("value not of length " + (ops.length || 255));
  54. else if (val && val.length != ops.length) throw new Error("value not of length " + (ops.length || 255));
  55. };
  56. ops.setSql = getStringType(cmpFun);
  57. ops.checkType = checkStringType(ops.type, cmpFun);
  58. return new Type(ops);
  59. };
  60. /**
  61. *
  62. *
  63. * String alias for {@link varchar} datatype.
  64. *
  65. * @function
  66. * @param {Object} options options for the STRING data type.
  67. * @param {Number} [options.length=255] the length of the field
  68. * @param {Boolean} [options.allowNull=true] should the field allow null
  69. * @param {Boolean} [options.default = null] default value of the field
  70. * @param {Boolean} [options.description = ""] description fo the field.
  71. *
  72. * @return {Type} A Type representing a VARCHAR column.
  73. *
  74. * @name STRING
  75. * @memberOf moose.adapters.mysql.types
  76. *
  77. */
  78. exports.STRING = (exports.VARCHAR = function(options) {
  79. var ops = comb.merge({}, stringDefaults, {length : 255, type : "VARCHAR"}, options || {});
  80. if (ops.length > 4294967295) {
  81. throw new Error("Max char type length is 255 please use text");
  82. }
  83. var cmpFun = function(val) {
  84. if ((val.length > ops.length) || val.length > 255)
  85. throw new Error("value greater than valid varchar length of " + (ops.length || 255));
  86. };
  87. ops.setSql = getStringType(cmpFun);
  88. ops.checkType = checkStringType(ops.type, cmpFun);
  89. return new Type(ops);
  90. });
  91. /**
  92. *
  93. * Mysql TINYTEXT datatype
  94. *
  95. * @function
  96. * @param {Object} options options for the TINYTEXT data type.
  97. * @param {Number} [options.length] the length of the field
  98. * @param {Boolean} [options.allowNull=true] should the field allow null
  99. * @param {Boolean} [options.default = null] default value of the field
  100. * @param {Boolean} [options.description = ""] description fo the field.
  101. *
  102. * @return {Type} A Type representing a TINYTEXT column.
  103. *
  104. * @name TINYTEXT
  105. * @memberOf moose.adapters.mysql.types
  106. *
  107. */
  108. exports.TINYTEXT = function(options) {
  109. var ops = comb.merge({}, stringDefaults, {length : null, type : "TINYTEXT"}, options || {});
  110. var cmpFun = function(val) {
  111. if (val.length > 255) throw new Error("value greater than valid tinytext length of 255");
  112. };
  113. ops.setSql = getStringType(cmpFun);
  114. ops.checkType = checkStringType(ops.type, cmpFun);
  115. return new Type(ops);
  116. };
  117. /**
  118. *
  119. *
  120. * Mysql MEDIUMTEXT datatype
  121. *
  122. * @function
  123. * @param {Object} options options for the MEDIUMTEXT data type.
  124. * @param {Number} [options.length] the length of the field
  125. * @param {Boolean} [options.allowNull=true] should the field allow null
  126. * @param {Boolean} [options.default = null] default value of the field
  127. * @param {Boolean} [options.description = ""] description fo the field.
  128. *
  129. * @return {Type} A Type representing a MEDIUMTEXT column.
  130. *
  131. * @name MEDIUMTEXT
  132. * @memberOf moose.adapters.mysql.types
  133. *
  134. */
  135. exports.MEDIUMTEXT = function(options) {
  136. var ops = comb.merge({}, stringDefaults, {length : null, type : "MEDIUMTEXT"}, options || {});
  137. var cmpFun = function(val) {
  138. if (val.length > 16777215) throw new Error("value greater than valid tinytext length of 16777215");
  139. };
  140. ops.setSql = getStringType(cmpFun);
  141. ops.checkType = checkStringType(ops.type, cmpFun);
  142. return new Type(ops);
  143. };
  144. /**
  145. *
  146. *
  147. * Mysql LONGTEXT datatype
  148. *
  149. * @function
  150. * @param {Object} options options for the LONGTEXT data type.
  151. * @param {Number} [options.length] the length of the field
  152. * @param {Boolean} [options.allowNull=true] should the field allow null
  153. * @param {Boolean} [options.default = null] default value of the field
  154. * @param {Boolean} [options.description = ""] description fo the field.
  155. *
  156. * @return {Type} A Type representing a LONGTEXT column.
  157. *
  158. * @name LONGTEXT
  159. * @memberOf moose.adapters.mysql.types
  160. *
  161. */
  162. exports.LONGTEXT = function(options) {
  163. var ops = comb.merge({}, stringDefaults, {length : null, type : "LONGTEXT"}, options || {});
  164. var cmpFun = function(val) {
  165. if (val.length > 4294967295) throw new Error("value greater than valid tinytext length of 4294967295");
  166. };
  167. ops.setSql = getStringType(cmpFun);
  168. ops.checkType = checkStringType(ops.type, cmpFun);
  169. return new Type(ops);
  170. };
  171. /**
  172. *
  173. *
  174. * Mysql TEXT datatype
  175. *
  176. * @function
  177. * @param {Object} options options for the TEXT data type.
  178. * @param {Number} [options.length] the length of the field
  179. * @param {Boolean} [options.allowNull=true] should the field allow null
  180. * @param {Boolean} [options.default = null] default value of the field
  181. * @param {Boolean} [options.description = ""] description fo the field.
  182. *
  183. * @return {Type} A Type representing a TEXT column.
  184. *
  185. * @name TEXT
  186. * @memberOf moose.adapters.mysql.types
  187. *
  188. */
  189. exports.TEXT = function(options) {
  190. var ops = comb.merge({}, stringDefaults, {length : 4294967295, type : "TEXT"}, options || {});
  191. var cmpFun = function(val) {
  192. if (val.length > 65535) throw new Error("value greater than valid tinytext length of 65535");
  193. };
  194. ops.setSql = getStringType(cmpFun);
  195. ops.checkType = checkStringType(ops.type, cmpFun);
  196. return new Type(ops);
  197. };
  198. var checkEnumType = function(enumTypes, type) {
  199. var enums = comb.merge([], enumTypes);
  200. return function(val) {
  201. if (typeof val != "string" || enums.indexOf(val) == -1) {
  202. throw new Error(type + " value must be a string and contained in the enum set");
  203. }
  204. };
  205. };
  206. var checkSetType = function(enumTypes, type) {
  207. var check = checkEnumType(enumTypes, type);
  208. return function(val) {
  209. if (typeof val == "string") {
  210. return check(val);
  211. }
  212. val.forEach(function(v) {
  213. return check(v);
  214. });
  215. };
  216. };
  217. var getSetType = function(cmpFunc) {
  218. return function(val) {
  219. if (typeof val == "string") {
  220. val = val.split(",");
  221. }
  222. cmpFunc && cmpFunc(val);
  223. return val;
  224. };
  225. };
  226. /**
  227. *
  228. *
  229. * Mysql ENUM datatype
  230. *
  231. * @function
  232. * @param {Object} options options for the TINYTEXT data type.
  233. * @param {Array} options.enums the characters allowed for this ENUM
  234. * @param {Number} [options.length] the length of the field
  235. * @param {Boolean} [options.allowNull=true] should the field allow null
  236. * @param {Boolean} [options.default = null] default value of the field
  237. * @param {Boolean} [options.description = ""] description fo the field.
  238. *
  239. * @return {Type} A Type representing a ENUM column.
  240. *
  241. * @name ENUM
  242. * @memberOf moose.adapters.mysql.types
  243. *
  244. */
  245. exports.ENUM = function(options) {
  246. var ops = comb.merge({}, stringDefaults, {type : "ENUM", enums : []}, options || {});
  247. if (ops.enums && ops.enums.length > 65535) {
  248. throw new Error("Max number of enum values is 65535");
  249. }
  250. ops.setSql = getStringType(checkEnumType(ops.enums, ops.type));
  251. ops.checkType = checkEnumType(ops.enums, ops.type);
  252. return new Type(ops);
  253. };
  254. /**
  255. *
  256. *
  257. * Mysql SET datatype
  258. *
  259. * @function
  260. * @param {Object} options options for the SET data type.
  261. * @param {Array} options.enums the characters allowed in this SET
  262. * @param {Number} [options.length] the length of the field
  263. * @param {Boolean} [options.allowNull=true] should the field allow null
  264. * @param {Boolean} [options.default = null] default value of the field
  265. * @param {Boolean} [options.description = ""] description fo the field.
  266. *
  267. * @return {Type} A Type representing a SET column.
  268. *
  269. * @name SET
  270. * @memberOf moose.adapters.mysql.types
  271. *
  272. */
  273. exports.SET = function(options) {
  274. var ops = comb.merge({}, stringDefaults, {type : "SET", enums : []}, options || {});
  275. if (ops.enums && ops.enums.length > 64) {
  276. throw new Error("Max number of enum values is 64");
  277. }
  278. ops.setSql = getSetType(checkSetType(ops.enums, ops.type));
  279. ops.checkType = checkSetType(ops.enums, ops.type);
  280. return new Type(ops);
  281. };