database.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. var _process = _interopRequireDefault(require("process"));
  3. var _yargs = require("../core/yargs");
  4. var _migrator = require("../core/migrator");
  5. var _helpers = _interopRequireDefault(require("../helpers"));
  6. var _lodash = require("lodash");
  7. var _picocolors = _interopRequireDefault(require("picocolors"));
  8. function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
  9. const Sequelize = _helpers.default.generic.getSequelize();
  10. exports.builder = yargs => (0, _yargs._baseOptions)(yargs).option('charset', {
  11. describe: 'Pass charset option to dialect, MYSQL only',
  12. type: 'string'
  13. }).option('collate', {
  14. describe: 'Pass collate option to dialect',
  15. type: 'string'
  16. }).option('encoding', {
  17. describe: 'Pass encoding option to dialect, PostgreSQL only',
  18. type: 'string'
  19. }).option('ctype', {
  20. describe: 'Pass ctype option to dialect, PostgreSQL only',
  21. type: 'string'
  22. }).option('template', {
  23. describe: 'Pass template option to dialect, PostgreSQL only',
  24. type: 'string'
  25. }).argv;
  26. exports.handler = async function (args) {
  27. const command = args._[0];
  28. // legacy, gulp used to do this
  29. await _helpers.default.config.init();
  30. const sequelize = getDatabaseLessSequelize();
  31. const config = _helpers.default.config.readConfig();
  32. const options = (0, _lodash.pick)(args, ['charset', 'collate', 'encoding', 'ctype', 'template']);
  33. const queryInterface = sequelize.getQueryInterface();
  34. const queryGenerator = queryInterface.queryGenerator || queryInterface.QueryGenerator;
  35. const query = getCreateDatabaseQuery(sequelize, config, options);
  36. switch (command) {
  37. case 'db:create':
  38. await sequelize.query(query, {
  39. type: sequelize.QueryTypes.RAW
  40. }).catch(e => _helpers.default.view.error(e));
  41. _helpers.default.view.log('Database', _picocolors.default.blueBright(config.database), 'created.');
  42. break;
  43. case 'db:drop':
  44. await sequelize.query(`DROP DATABASE IF EXISTS ${queryGenerator.quoteIdentifier(config.database)}`, {
  45. type: sequelize.QueryTypes.RAW
  46. }).catch(e => _helpers.default.view.error(e));
  47. _helpers.default.view.log('Database', _picocolors.default.blueBright(config.database), 'dropped.');
  48. break;
  49. }
  50. _process.default.exit(0);
  51. };
  52. function getCreateDatabaseQuery(sequelize, config, options) {
  53. const queryInterface = sequelize.getQueryInterface();
  54. const queryGenerator = queryInterface.queryGenerator || queryInterface.QueryGenerator;
  55. switch (config.dialect) {
  56. case 'postgres':
  57. case 'postgres-native':
  58. return 'CREATE DATABASE ' + queryGenerator.quoteIdentifier(config.database) + (options.encoding ? ' ENCODING = ' + queryGenerator.quoteIdentifier(options.encoding) : '') + (options.collate ? ' LC_COLLATE = ' + queryGenerator.quoteIdentifier(options.collate) : '') + (options.ctype ? ' LC_CTYPE = ' + queryGenerator.quoteIdentifier(options.ctype) : '') + (options.template ? ' TEMPLATE = ' + queryGenerator.quoteIdentifier(options.template) : '');
  59. case 'mysql':
  60. return 'CREATE DATABASE IF NOT EXISTS ' + queryGenerator.quoteIdentifier(config.database) + (options.charset ? ' DEFAULT CHARACTER SET ' + queryGenerator.quoteIdentifier(options.charset) : '') + (options.collate ? ' DEFAULT COLLATE ' + queryGenerator.quoteIdentifier(options.collate) : '');
  61. case 'mssql':
  62. return "IF NOT EXISTS (SELECT * FROM sys.databases WHERE name = N'" + config.database + "')" + ' BEGIN' + ' CREATE DATABASE ' + queryGenerator.quoteIdentifier(config.database) + (options.collate ? ' COLLATE ' + options.collate : '') + ' END;';
  63. default:
  64. _helpers.default.view.error(`Dialect ${config.dialect} does not support db:create / db:drop commands`);
  65. return 'CREATE DATABASE ' + queryGenerator.quoteIdentifier(config.database);
  66. }
  67. }
  68. function getDatabaseLessSequelize() {
  69. let config = null;
  70. try {
  71. config = _helpers.default.config.readConfig();
  72. } catch (e) {
  73. _helpers.default.view.error(e);
  74. }
  75. config = (0, _lodash.cloneDeep)(config);
  76. config = (0, _lodash.defaults)(config, {
  77. logging: _migrator.logMigrator
  78. });
  79. switch (config.dialect) {
  80. case 'postgres':
  81. case 'postgres-native':
  82. config.database = 'postgres';
  83. break;
  84. case 'mysql':
  85. delete config.database;
  86. break;
  87. case 'mssql':
  88. config.database = 'master';
  89. break;
  90. default:
  91. _helpers.default.view.error(`Dialect ${config.dialect} does not support db:create / db:drop commands`);
  92. }
  93. try {
  94. return new Sequelize(config);
  95. } catch (e) {
  96. _helpers.default.view.error(e);
  97. }
  98. }