forked from sequelize/umzug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MongoDBStorage.js
63 lines (57 loc) · 1.79 KB
/
MongoDBStorage.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
import _ from 'lodash';
import Storage from './Storage';
/**
* @class JSONStorage
*/
export default class MongoDBStorage extends Storage {
/**
* Constructs MongoDB collection storage.
*
* @param {Object} [options]
* Required either connection and collectionName OR collection
* @param {String} [options.connection] - a connection to target database established with MongoDB Driver
* @param {String} [options.collectionName] - name of migration collection in MongoDB
* @param {String} [options.collection] - reference to a MongoDB Driver collection
*/
constructor ({connection, collectionName, collection}) {
super();
this.connection = connection;
this.collection = collection;
this.collectionName = collectionName || 'migrations';
if (!this.connection && !this.collection) {
throw new Error('MongoDB Connection or Collection required');
}
if (!this.collection) {
this.collection = this.connection.collection(this.collectionName);
}
}
/**
* Logs migration to be considered as executed.
*
* @param {String} migrationName - Name of the migration to be logged.
* @returns {Promise}
*/
logMigration (migrationName) {
return this.collection.insertOne({migrationName});
}
/**
* Unlogs migration to be considered as pending.
*
* @param {String} migrationName - Name of the migration to be unlogged.
* @returns {Promise}
*/
unlogMigration (migrationName) {
return this.collection.removeOne({migrationName});
}
/**
* Gets list of executed migrations.
*
* @returns {Promise.<String[]>}
*/
executed () {
return this.collection.find({})
.sort({migrationName: 1})
.toArray()
.then((records) => _.map(records, 'migrationName'));
}
}