Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add linter #11

Merged
merged 8 commits into from
Jan 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ before_script:
- npm install mongodb@${MONGODB_DRIVER_VERSION}

script:
- npm run lint

- >
nodeVersionMajor=`node --version | grep -Eo 'v[0-9]+' | sed 's/v//'`;

Expand Down
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