remove Joi validation, add createClient, update tests

This commit is contained in:
2016-04-14 16:57:28 -07:00
parent cdbeef802f
commit 981bf09aab
5 changed files with 45 additions and 47 deletions

View File

@@ -0,0 +1,13 @@
import elasticsearch from 'elasticsearch';
export default function createClient(options) {
let client;
if (options instanceof elasticsearch.Client) {
client = options;
} else {
client = new elasticsearch.Client(options);
}
return client;
};

View File

@@ -1,17 +0,0 @@
import Joi from 'joi';
var schema = Joi.object({
url: Joi.string().uri({ scheme: ['http', 'https'] }).default('http://localhost:9200'),
index: Joi.string().required(),
interval: Joi.string().default('1w'),
timeout: Joi.number().min(10000).default(10000),
}).default();
const validateSchema = (options) => new Promise(function (resolve, reject) {
schema.validate(options, (err, settings) => {
if (err) return reject(err);
resolve(settings);
})
})
export default validateSchema;

View File

@@ -1,24 +1,26 @@
import events from 'events';
import validateSchema from './helpers/validate_schema';
import createClient from './helpers/create_client';
export default class Elastique extends events.EventEmitter {
constructor(options = {}) {
if (!options.index) throw new Error('Must specify an index to write to');
super();
this.ready = true;
// initalize the module
Promise.all([
validateSchema(options)
])
.then(([ settings ]) => {
this.settings = settings;
})
.catch((err) => {
this.ready = false;
this.emit('error', err);
throw err;
this.settings = {};
Object.keys(options, (key) => {
if (key !== 'client') this.settings[key] = options[key];
});
this.client = createClient(options.client);
}
add(type, payload, opts = {}) {
const options = Object.assign({
timeout: this.settings.timeout
}, opts);
}
}