JSONStorage.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = void 0;
  6. var _bluebird = _interopRequireDefault(require("bluebird"));
  7. var _fs = _interopRequireDefault(require("fs"));
  8. var _path2 = _interopRequireDefault(require("path"));
  9. var _Storage = _interopRequireDefault(require("./Storage"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * @class JSONStorage
  13. */
  14. class JSONStorage extends _Storage.default {
  15. /**
  16. * Constructs JSON file storage.
  17. *
  18. * @param {Object} [options]
  19. * @param {String} [options.path='./umzug.json'] - Path to JSON file where
  20. * the log is stored. Defaults './umzug.json' relative to process' cwd.
  21. */
  22. constructor({
  23. path = _path2.default.resolve(process.cwd(), 'umzug.json')
  24. } = {}) {
  25. super();
  26. this.path = path;
  27. }
  28. /**
  29. * Logs migration to be considered as executed.
  30. *
  31. * @param {String} migrationName - Name of the migration to be logged.
  32. * @returns {Promise}
  33. */
  34. logMigration(migrationName) {
  35. const filePath = this.path;
  36. const readfile = _bluebird.default.promisify(_fs.default.readFile);
  37. const writefile = _bluebird.default.promisify(_fs.default.writeFile);
  38. return readfile(filePath).catch(() => '[]').then(content => JSON.parse(content)).then(content => {
  39. content.push(migrationName);
  40. return writefile(filePath, JSON.stringify(content, null, ' '));
  41. });
  42. }
  43. /**
  44. * Unlogs migration to be considered as pending.
  45. *
  46. * @param {String} migrationName - Name of the migration to be unlogged.
  47. * @returns {Promise}
  48. */
  49. unlogMigration(migrationName) {
  50. const filePath = this.path;
  51. const readfile = _bluebird.default.promisify(_fs.default.readFile);
  52. const writefile = _bluebird.default.promisify(_fs.default.writeFile);
  53. return readfile(filePath).catch(() => '[]').then(content => JSON.parse(content)).then(content => {
  54. content = content.filter(m => m !== migrationName);
  55. return writefile(filePath, JSON.stringify(content, null, ' '));
  56. });
  57. }
  58. /**
  59. * Gets list of executed migrations.
  60. *
  61. * @returns {Promise.<String[]>}
  62. */
  63. executed() {
  64. const filePath = this.path;
  65. const readfile = _bluebird.default.promisify(_fs.default.readFile);
  66. return readfile(filePath).catch(() => '[]').then(content => JSON.parse(content));
  67. }
  68. }
  69. exports.default = JSONStorage;