build-schema.js 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. // ██████╗ ██╗ ██╗██╗██╗ ██████╗ ███████╗ ██████╗██╗ ██╗███████╗███╗ ███╗ █████╗
  2. // ██╔══██╗██║ ██║██║██║ ██╔══██╗ ██╔════╝██╔════╝██║ ██║██╔════╝████╗ ████║██╔══██╗
  3. // ██████╔╝██║ ██║██║██║ ██║ ██║ ███████╗██║ ███████║█████╗ ██╔████╔██║███████║
  4. // ██╔══██╗██║ ██║██║██║ ██║ ██║ ╚════██║██║ ██╔══██║██╔══╝ ██║╚██╔╝██║██╔══██║
  5. // ██████╔╝╚██████╔╝██║███████╗██████╔╝ ███████║╚██████╗██║ ██║███████╗██║ ╚═╝ ██║██║ ██║
  6. // ╚═════╝ ╚═════╝ ╚═╝╚══════╝╚═════╝ ╚══════╝ ╚═════╝╚═╝ ╚═╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝
  7. //
  8. // Build a schema object that is suitable for using in a Create Table query.
  9. var _ = require('@sailshq/lodash');
  10. module.exports = function buildSchema(definition) {
  11. if (!definition) {
  12. throw new Error('Build Schema requires a valid definition.');
  13. }
  14. // ╔╗╔╔═╗╦═╗╔╦╗╔═╗╦ ╦╔═╗╔═╗ ┌┬┐┬ ┬┌─┐┌─┐
  15. // ║║║║ ║╠╦╝║║║╠═╣║ ║╔═╝║╣ │ └┬┘├─┘├┤
  16. // ╝╚╝╚═╝╩╚═╩ ╩╩ ╩╩═╝╩╚═╝╚═╝ ┴ ┴ ┴ └─┘
  17. // TODO: move this code inline to eliminate unnecessary function declaration
  18. var normalizeType = function normalizeType(type) {
  19. switch (type.toLowerCase()) {
  20. // Default types from sails-hook-orm (for automigrations)
  21. case '_number':
  22. return 'REAL';
  23. case '_numberkey':
  24. return 'INTEGER';
  25. case '_numbertimestamp':
  26. return 'BIGINT';
  27. case '_string':
  28. return 'VARCHAR(255)';
  29. case '_stringkey':
  30. return 'VARCHAR(255)';
  31. case '_stringtimestamp':
  32. return 'VARCHAR(255)';
  33. case '_boolean':
  34. return 'BOOLEAN';
  35. case '_json':
  36. return 'LONGTEXT';
  37. case '_ref':
  38. return 'LONGTEXT';
  39. // Sensible MySQL-specific defaults for common things folks might try to use.
  40. // (FUTURE: log warnings suggesting proper usage when any of these synonyms are invoked)
  41. case 'varchar':
  42. return 'VARCHAR(255)';
  43. default:
  44. return type;
  45. }
  46. };
  47. // Build up a string of column attributes
  48. var columns = _.map(definition, function map(attribute, name) {
  49. if (_.isString(attribute)) {
  50. var val = attribute;
  51. attribute = {};
  52. attribute.type = val;
  53. }
  54. var type = normalizeType(attribute.columnType);
  55. var nullable = attribute.notNull && 'NOT NULL';
  56. var unique = attribute.unique && 'UNIQUE';
  57. var autoIncrement = attribute.autoIncrement && 'AUTO_INCREMENT';
  58. return _.compact(['`' + name + '`', type, nullable, unique, autoIncrement]).join(' ');
  59. }).join(',');
  60. // Grab the Primary Key
  61. var primaryKeys = _.keys(_.pick(definition, function findPK(attribute) {
  62. return attribute.primaryKey;
  63. }));
  64. // Add the Primary Key to the definition
  65. var constraints = _.compact([
  66. primaryKeys.length && 'PRIMARY KEY (' + primaryKeys.join(',') + ')'
  67. ]).join(', ');
  68. var schema = _.compact([columns, constraints]).join(', ');
  69. return schema;
  70. };