diff --git a/index.js b/index.js index 6b10d33..68c9bad 100644 --- a/index.js +++ b/index.js @@ -5,16 +5,16 @@ 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'); } @@ -22,10 +22,10 @@ function Cassandra (options) { 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); @@ -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); }); @@ -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; @@ -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)], @@ -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); } @@ -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(); @@ -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; diff --git a/package.json b/package.json index db2d4eb..679487d 100644 --- a/package.json +++ b/package.json @@ -23,7 +23,8 @@ }, "devDependencies": { "async": "^1.4.2", - "mocha": "^2.3.3" + "mocha": "^2.3.3", + "xo": "^0.11.2" }, "repository": { "type": "git", @@ -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" + ] } } diff --git a/test/config.js b/test/config.js index 65e12cb..7049301 100644 --- a/test/config.js +++ b/test/config.js @@ -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'))) { diff --git a/test/test.js b/test/test.js index 4cd3175..b937a93 100644 --- a/test/test.js +++ b/test/test.js @@ -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; @@ -44,12 +44,12 @@ 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); }); @@ -57,7 +57,7 @@ describe('Cassandra transport', function () { }); 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(); @@ -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);