Skip to content

Commit

Permalink
Merge b294668 into 1963139
Browse files Browse the repository at this point in the history
  • Loading branch information
okv committed Jan 10, 2020
2 parents 1963139 + b294668 commit 3645128
Show file tree
Hide file tree
Showing 21 changed files with 1,342 additions and 77 deletions.
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules
lib/migrationTemplate.js
35 changes: 35 additions & 0 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"extends": "airbnb-base",
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "script"
},
"rules": {
"comma-dangle": ["error", "never"],
"object-curly-spacing": ["error", "never"],
"indent": ["error", "tab"],
"no-tabs": 0,
"arrow-body-style": ["off"],
"arrow-parens": ["error", "always"],
"no-underscore-dangle": ["off"],
"camelcase": ["error", {
"properties": "never"
}],
"max-len": ["error", {
"code": 80,
"tabWidth": 1,
"ignoreTemplateLiterals": true
}],
"function-paren-newline": ["error", "consistent"],
"class-methods-use-this": [0],
"global-require": ["off"],
"prefer-destructuring": ["off"],
"strict": ["error", "global"],
"no-plusplus": ["error", {"allowForLoopAfterthoughts": true}],
"no-else-return": ["off"],
"no-lonely-if": ["off"],
"no-param-reassign": ["off"],
"consistent-return": ["off"],
"prefer-rest-params": ["off"]
}
}
2 changes: 2 additions & 0 deletions .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ coverage/
.npmrc
.travis.yml
.nycrc
.eslintignore
.eslintrc.json
27 changes: 14 additions & 13 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ function Adapter(params) {
this.helpers = helpers;
}

Adapter.prototype.getTemplatePath = function() {
Adapter.prototype.getTemplatePath = function getTemplatePath() {
return path.join(__dirname, 'migrationTemplate.js');
};

// mongodb driver 2.x returns `db`, 3.x returns `client` as a result of
// `MongoClient.connect`, this method sets client and db parsed from that
// polymorphic result
Adapter.prototype._setDbAndClient = function(dbOrClient) {
Adapter.prototype._setDbAndClient = function _setDbAndClient(dbOrClient) {
if (dbOrClient.collection) {
this.client = null;

Expand All @@ -37,7 +37,7 @@ Adapter.prototype._setDbAndClient = function(dbOrClient) {
}
};

Adapter.prototype.connect = function() {
Adapter.prototype.connect = function connect() {
return Promise.resolve()
.then(() => MongoClient.connect(this.params.url, this.params.options))
.then((dbOrClient) => {
Expand All @@ -51,7 +51,7 @@ Adapter.prototype.connect = function() {
});
};

Adapter.prototype.disconnect = function() {
Adapter.prototype.disconnect = function disconnect() {
if (this.client) {
return this.client.close();
} else if (this.db) {
Expand All @@ -61,19 +61,20 @@ Adapter.prototype.disconnect = function() {
}
};

Adapter.prototype.getExecutedMigrationNames = function() {
return Promise.resolve()
.then(() => this.collection.find({}).toArray())
.then((docs) => {
return docs.map((doc) => doc._id);
});
};
Adapter.prototype.getExecutedMigrationNames =
function getExecutedMigrationNames() {
return Promise.resolve()
.then(() => this.collection.find({}).toArray())
.then((docs) => {
return docs.map((doc) => doc._id);
});
};

Adapter.prototype.markExecuted = function(name) {
Adapter.prototype.markExecuted = function markExecuted(name) {
return this.collection.replaceOne({_id: name}, {_id: name}, {upsert: true});
};

Adapter.prototype.unmarkExecuted = function(name) {
Adapter.prototype.unmarkExecuted = function unmarkExecuted(name) {
return this.collection.deleteOne({_id: name});
};

Expand Down
52 changes: 27 additions & 25 deletions lib/helpers.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
'use strict';

exports.dropIndexIfExists = function(collection, index, callback) {
let indexName = '';
exports.dropIndexIfExists =
function dropIndexIfExists(collection, index, callback) {
let indexName = '';

return Promise.resolve()
.then(() => {
return Promise.resolve()
.then(() => {
// convert object to string coz `indexExists` doesn't`
// recognize objects
if (typeof index === 'object') {
Object.keys(index).forEach((key) => {
indexName += `_${key}_${index[key]}`;
});

// convert object to string coz `indexExists` don't recognize objects
if (typeof index === 'object') {
for (let key in index) {
indexName += '_' + key + '_' + index[key];
indexName = indexName.replace(/^_/, '');
} else {
indexName = index;
}
indexName = indexName.replace(/^_/, '');
} else {
indexName = index;
}

return collection.indexExists(indexName);
})
.then((indexExist) => {
if (indexExist) {
return collection.dropIndex(indexName);
}
})
.then(() => {
if (typeof callback === 'function') {
callback();
}
});
};
return collection.indexExists(indexName);
})
.then((indexExist) => {
if (indexExist) {
return collection.dropIndex(indexName);
}
})
.then(() => {
if (typeof callback === 'function') {
callback();
}
});
};
2 changes: 2 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
'use strict';

module.exports = require('./adapter');
Loading

0 comments on commit 3645128

Please sign in to comment.