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

added linting through xo, initial linting fixes #6

Closed
wants to merge 1 commit into from
Closed
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
33 changes: 19 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@ var winston = require('winston');
var cql = require('cassandra-driver');

var defaultOptions = {
//column family to store the logs
// column family to store the logs
table: 'logs',
//determines if the partition key is changed per day or hour
// determines if the partition key is changed per day or hour
partitionBy: 'day',
consistency: cql.types.consistencies.quorum,
level: 'info',
name: 'cassandra'
};

function Cassandra (options) {
function Cassandra(options) {
if (!options) {
throw new Error('Transport options is required');
}
if (!options.keyspace) {
throw new Error('You must specify the options.keyspace');
}
this.options = Cassandra.extend({}, defaultOptions, options);
//winston options
// winston options
this.name = this.options.name;
this.level = this.options.level;
//create a queue object that will emit the event 'prepared'
// create a queue object that will emit the event 'prepared'
this.schemaStatus = new events.EventEmitter();
this.schemaStatus.setMaxListeners(0);
this.client = new cql.Client(this.options);
Expand All @@ -36,7 +36,9 @@ util.inherits(Cassandra, winston.Transport);
Cassandra.prototype.log = function (level, msg, meta, callback) {
var self = this;
this._ensureSchema(function (err) {
if (err) return callback(err, false);
if (err) {
return callback(err, false);
}
return self._insertLog(level, msg, meta, function (err) {
callback(err, !err);
});
Expand All @@ -49,8 +51,7 @@ Cassandra.prototype.log = function (level, msg, meta, callback) {
Cassandra.prototype.getKey = function () {
if (this.options.partitionBy === 'day') {
return new Date().toISOString().slice(0, 10);
}
else if (this.options.partitionBy === 'hour') {
} else if (this.options.partitionBy === 'hour') {
return new Date().toISOString().slice(0, 13);
}
return null;
Expand All @@ -64,7 +65,7 @@ Cassandra.prototype._insertLog = function (level, msg, meta, callback) {
if (!key) {
return callback(new Error('Partition ' + this.options.partitionBy + ' not supported'), false);
}
//execute as a prepared query as it would be executed multiple times
// execute as a prepared query as it would be executed multiple times
return this.client.execute(
'INSERT INTO ' + this.options.table + ' (key, date, level, message, meta) VALUES (?, ?, ?, ?, ?)',
[key, new Date(), level, msg, util.inspect(meta)],
Expand All @@ -77,7 +78,9 @@ Cassandra.prototype._insertLog = function (level, msg, meta, callback) {
* If its already being created, it queues until it is created (or fails).
*/
Cassandra.prototype._ensureSchema = function (callback) {
if (this.schemaStatus.err) return callback(this.schemaStatus.err);
if (this.schemaStatus.err) {
return callback(this.schemaStatus.err);
}
if (this.schemaStatus.created) {
return callback(null);
}
Expand Down Expand Up @@ -105,7 +108,9 @@ Cassandra.prototype._createSchema = function (callback) {
' (key text, date timestamp, level text, message text, meta text, PRIMARY KEY(key, date));';
var self = this;
this.client.execute(query, params, function (err, result) {
if (err) return callback(err);
if (err) {
return callback(err);
}
if (result.rows.length === 1) {
self.schemaStatus.created = true;
return callback();
Expand Down Expand Up @@ -133,10 +138,10 @@ Cassandra.extend = function (target) {
return target;
};

//Define as a property of winston transports for backward compatibility
// Define as a property of winston transports for backward compatibility
winston.transports.Cassandra = Cassandra;
module.exports = Cassandra;
//The rest of winston transports uses (module).name convention
//Create a field to allow consumers to interact in the same way
// The rest of winston transports uses (module).name convention
// Create a field to allow consumers to interact in the same way
module.exports.Cassandra = Cassandra;
module.exports.types = cql.types;
13 changes: 11 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@
},
"devDependencies": {
"async": "^1.4.2",
"mocha": "^2.3.3"
"mocha": "^2.3.3",
"xo": "^0.11.2"
},
"repository": {
"type": "git",
Expand All @@ -36,6 +37,14 @@
"node": ">=0.10.0"
},
"scripts": {
"test": "mocha test -R spec -t 5000"
"test": "mocha test -R spec -t 5000",
"lint": "xo --space *.js --env=node --env=mocha"
},
"xo": {
"space": true,
"envs": [
"node",
"mocha"
]
}
}
14 changes: 7 additions & 7 deletions test/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ var path = require('path');
var Cassandra = require('../index.js');

var config = {
"contactPoints": ["127.0.0.1"],
"port": 9042,
"username": "cassandra",
"password": "cassandra",
"keyspace": "logging",
"consistency": Cassandra.types.consistencies.one,
"level": "info"
contactPoints: ['127.0.0.1'],
port: 9042,
username: 'cassandra',
password: 'cassandra',
keyspace: 'logging',
consistency: Cassandra.types.consistencies.one,
level: 'info'
};

if (fs.existsSync(path.resolve(__dirname, './localConfig.json'))) {
Expand Down
14 changes: 7 additions & 7 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
var assert = require('assert');
//Node.js driver client class
// Node.js driver client class
var CassandraClient = require('cassandra-driver').Client;
var winston = require('winston');
var async = require('async');
//Transport class
// Transport class
var Cassandra = require('../index.js');
var config = require('./config.js');
var extend = Cassandra.extend;
Expand Down Expand Up @@ -44,20 +44,20 @@ describe('Cassandra transport', function () {
var queryGetLogs;

before(function (done) {
//drop and re create the keyspace
// drop and re create the keyspace
cqlClient.execute('DROP KEYSPACE ' + config.keyspace, function () {
//ignore the error: the keyspace could not exist
// ignore the error: the keyspace could not exist
var query =
"CREATE KEYSPACE " +
config.keyspace +
config.keyspace +
" WITH REPLICATION = { 'class' : 'SimpleStrategy', 'replication_factor' : 3 };";
cqlClient.execute(query, done);
});
queryGetLogs = 'SELECT * FROM ' + config.keyspace + '.' + cassandra.options.table + ' WHERE key = ? LIMIT 10';
});

it('should fail if the keyspace does not exists', function (done) {
var temp = new Cassandra(extend({}, options, {'keyspace': 'logging_123456'}));
var temp = new Cassandra(extend({}, options, {keyspace: 'logging_123456'}));
temp.log('info', 'message', 'metadata', function (err) {
assert.ok(err, 'It should yield an error');
done();
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('Cassandra transport', function () {
logger.log('info', 'Through winston without meta', next);
},
function (next) {
logger.log('info', 'Through winston with meta', {val:1}, next);
logger.log('info', 'Through winston with meta', {val: 1}, next);
}
], function (err) {
assert.ifError(err);
Expand Down