index.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. /**
  2. * Module dependencies
  3. */
  4. var _ = require('@sailshq/lodash');
  5. var runAlterStrategy = require('./private/run-alter-strategy');
  6. var runDropStrategy = require('./private/run-drop-strategy');
  7. var runSafeStrategy = require('./private/run-safe-strategy');
  8. /**
  9. * runAutoMigrations()
  10. *
  11. * Auto-migrate all models in this orm using the given strategy.
  12. *
  13. * @param {[type]} strategy [description]
  14. * @param {[type]} orm [description]
  15. * @param {Function} cb [description]
  16. * @return {[type]} [description]
  17. */
  18. module.exports = function runAutoMigrations(strategy, orm, cb) {
  19. // ╦ ╦╔═╗╦ ╦╔╦╗╔═╗╔╦╗╔═╗ ┌─┐┌┬┐┬─┐┌─┐┌┬┐┌─┐┌─┐┬ ┬
  20. // ╚╗╔╝╠═╣║ ║ ║║╠═╣ ║ ║╣ └─┐ │ ├┬┘├─┤ │ ├┤ │ ┬└┬┘
  21. // ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ └─┘ ┴ ┴└─┴ ┴ ┴ └─┘└─┘ ┴
  22. if (!_.isString(strategy)) {
  23. return cb(new Error('Strategy must be one of: `alter`, `drop`, or `safe`.'));
  24. }
  25. // ╦ ╦╔═╗╦ ╦╔╦╗╔═╗╔╦╗╔═╗ ┌─┐┬─┐┌┬┐
  26. // ╚╗╔╝╠═╣║ ║ ║║╠═╣ ║ ║╣ │ │├┬┘│││
  27. // ╚╝ ╩ ╩╩═╝╩═╩╝╩ ╩ ╩ ╚═╝ └─┘┴└─┴ ┴
  28. if (!orm || !_.isObject(orm)) {
  29. return cb(new Error('ORM must be an initialized Waterline ORM instance.'));
  30. }
  31. // Ensure a callback function exists
  32. if (!cb || !_.isFunction(cb)) {
  33. throw new Error('Missing callback argument.');
  34. }
  35. // ╦═╗╦ ╦╔╗╔ ┌┬┐┬┌─┐┬─┐┌─┐┌┬┐┬┌─┐┌┐┌ ┌─┐┌┬┐┬─┐┌─┐┌┬┐┌─┐┌─┐┬ ┬
  36. // ╠╦╝║ ║║║║ │││││ ┬├┬┘├─┤ │ ││ ││││ └─┐ │ ├┬┘├─┤ │ ├┤ │ ┬└┬┘
  37. // ╩╚═╚═╝╝╚╝ ┴ ┴┴└─┘┴└─┴ ┴ ┴ ┴└─┘┘└┘ └─┘ ┴ ┴└─┴ ┴ ┴ └─┘└─┘ ┴
  38. switch(strategy){
  39. case 'alter': runAlterStrategy(orm, cb); break;
  40. case 'drop': runDropStrategy(orm, cb); break;
  41. case 'safe': runSafeStrategy(orm, cb); break;
  42. default: return cb(new Error('Strategy must be one of: `alter`, `drop`, or `safe`.'));
  43. }
  44. };