diff --git a/src/helpers/create_index.js b/src/helpers/create_index.js new file mode 100644 index 0000000..fad7514 --- /dev/null +++ b/src/helpers/create_index.js @@ -0,0 +1,39 @@ +var schema = { + payload: { type: 'object', enabled: false }, + priority: { type: 'short' }, + process_timeout: { type: 'long' }, + process_expiration: { type: 'date' }, + created_at: { type: 'date' }, + started_at: { type: 'date' }, + completed_at: { type: 'date' }, + attempts: { type: 'short' }, + max_attempts: { type: 'short' }, + status: { type: 'string', index: 'not_analyzed' }, + output_content_type: { type: 'string', index: 'not_analyzed' }, + output: { type: 'object', enabled: false } +}; + +export default function createIndex(client, indexName) { + const indexBody = { + mappings: { + _default_: { + properties: schema + } + } + }; + + return client.indices.exists({ + index: indexName, + }) + .then((exists) => { + if (!exists) { + return client.indices.create({ + ignore: 400, + index: indexName, + body: indexBody + }) + .then(() => true); + } + return exists; + }); +} diff --git a/test/src/helpers/create_index.js b/test/src/helpers/create_index.js new file mode 100644 index 0000000..09e26e1 --- /dev/null +++ b/test/src/helpers/create_index.js @@ -0,0 +1,40 @@ +import expect from 'expect.js'; +import sinon from 'sinon'; +import createIndex from '../../../lib/helpers/create_index'; +import elasticsearchMock from '../../fixtures/elasticsearch'; + +describe('Create Index', function () { + let client; + let createSpy; + + beforeEach(function () { + client = new elasticsearchMock.Client(); + createSpy = sinon.spy(client.indices, 'create'); + }); + + it('should create the index', function () { + const indexName = 'test-index'; + const result = createIndex(client, indexName); + + return result + .then(function () { + sinon.assert.callCount(createSpy, 1); + expect(createSpy.getCall(0).args[0]).to.have.property('index', indexName); + }); + }); + + it('should create the default mappings', function () { + const indexName = 'test-index'; + const result = createIndex(client, indexName); + + return result + .then(function () { + const payload = createSpy.getCall(0).args[0]; + sinon.assert.callCount(createSpy, 1); + expect(payload).to.have.property('body'); + expect(payload.body).to.have.property('mappings'); + expect(payload.body.mappings).to.have.property('_default_'); + expect(payload.body.mappings._default_).to.have.property('properties'); + }); + }); +});