forked from sequelize/umzug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JSONStorage.js
78 lines (71 loc) · 2.15 KB
/
JSONStorage.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import _ from 'lodash';
import Bluebird from 'bluebird';
import fs from 'fs';
import _path from 'path';
import Storage from './Storage';
/**
* @class JSONStorage
*/
export default class JSONStorage extends Storage {
/**
* Constructs JSON file storage.
*
* @param {Object} [options]
* @param {String} [options.path='./umzug.json'] - Path to JSON file where
* the log is stored. Defaults './umzug.json' relative to process' cwd.
*/
constructor ({ path = _path.resolve(process.cwd(), 'umzug.json') } = {}) {
super();
this.path = path;
}
/**
* Logs migration to be considered as executed.
*
* @param {String} migrationName - Name of the migration to be logged.
* @returns {Promise}
*/
logMigration (migrationName) {
let filePath = this.path;
let readfile = Bluebird.promisify(fs.readFile);
let writefile = Bluebird.promisify(fs.writeFile);
return readfile(filePath)
.catch(function () { return '[]'; })
.then(function (content) { return JSON.parse(content); })
.then(function (content) {
content.push(migrationName);
return writefile(filePath, JSON.stringify(content, null, ' '));
});
}
/**
* Unlogs migration to be considered as pending.
*
* @param {String} migrationName - Name of the migration to be unlogged.
* @returns {Promise}
*/
unlogMigration (migrationName) {
let filePath = this.path;
let readfile = Bluebird.promisify(fs.readFile);
let writefile = Bluebird.promisify(fs.writeFile);
return readfile(filePath)
.catch(function () { return '[]'; })
.then(function (content) { return JSON.parse(content); })
.then(function (content) {
content = _.without(content, migrationName);
return writefile(filePath, JSON.stringify(content, null, ' '));
});
}
/**
* Gets list of executed migrations.
*
* @returns {Promise.<String[]>}
*/
executed () {
let filePath = this.path;
let readfile = Bluebird.promisify(fs.readFile);
return readfile(filePath)
.catch(function () { return '[]'; })
.then(function (content) {
return JSON.parse(content);
});
}
}