Skip to content

Commit

Permalink
ConfigValidator + prefix support for table names + v1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
juanhapes committed Sep 12, 2019
2 parents f4b729a + f8909a2 commit e2c7c13
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 19 deletions.
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
## [1.1.0] - 2019-09-12
### Added
- `ElasticSearchConfigError`
- `ElasticSearchConfigError`
- `ConfigValidator` module for config datatypes validation
- `prefix` support for table names

## [1.0.2] - 2019-08-29
### Fixed
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ Constructs the Elasticsearch driver instance, connected with the `config [Object
- user `[String]`: Elasticsearch user for the host.
- password `[String]`: Elasticsearch password for the host.
- limit `[Number]`: Default limit for getting operations.
- prefix `[String]`: Prefix for table name, if it's setted, any operation will use the model table name with the prefix. Ex: `table.prefix`.
- awsCredentials `[Boolean]`: Set to `true` if you need to use AWS credentials ENV variables for the host connection/authentication.

**Config usage:**
Expand All @@ -32,6 +33,7 @@ Constructs the Elasticsearch driver instance, connected with the `config [Object
user: 'Username', // Default empty
password: 'password', // Default empty
limit: 10, // Default 500
prefix: 'products', // Default empty
awsCredentials: true // Default false
}
```
Expand Down
7 changes: 5 additions & 2 deletions lib/elasticsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class ElasticSearch {
user: config.user || '',
password: config.password || '',
limit: config.limit || DEFAULT_LIMIT,
prefix: config.prefix || '',
awsCredentials: config.awsCredentials || false
};

Expand Down Expand Up @@ -122,12 +123,14 @@ class ElasticSearch {
}

/**
* Get the index name converted into snake_case
* Get the index name converted into snake_case is case of a prefix is setted it returns the index name with the prefix, concatenated with a dot.
* @param {Model} model Model instance
* @returns {String} Index/table name from the model, converted into snake_case
*/
_getIndex(model) {
return convertToSnakeCase(model.constructor.table);
return convertToSnakeCase(
this.config.prefix ? `${model.constructor.table}.${this.config.prefix}` : model.constructor.table
);
}

/**
Expand Down
22 changes: 11 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@janiscommerce/elasticsearch",
"version": "1.0.2",
"version": "1.1.0",
"description": "An AWS ElasticSearch Driver",
"main": "index.js",
"scripts": {
Expand All @@ -21,10 +21,10 @@
"eslint": "^5.3.0",
"eslint-config-airbnb-base": "^13.1.0",
"eslint-plugin-import": "^2.17.3",
"husky": "^3.0.3",
"husky": "^3.0.5",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
"sinon": "^7.3.2"
"sinon": "^7.4.2"
},
"files": [
"lib/"
Expand All @@ -33,7 +33,7 @@
"test": "tests"
},
"dependencies": {
"aws-sdk": "^2.518.0",
"aws-sdk": "^2.527.0",
"elasticsearch": "^15.0.0",
"http-aws-es": "^6.0.0",
"uuid": "^3.3.2"
Expand Down
20 changes: 20 additions & 0 deletions tests/elasticsearch-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,26 @@ describe('ElasticSearch', () => {
});
});

describe('_getIndex()', () => {

it('should return the table name from the model converted into snake_case', () => {

assert.deepStrictEqual(elastic._getIndex(model), 'my_table');

});

it('should return the table name with the prefix when it\'s setted in config', () => {

elastic.config.prefix = 'prefix';

assert.deepStrictEqual(elastic._getIndex(model), 'my_table.prefix');

delete elastic.config.prefix;

});

});

describe('buildIndex()', () => {

it('should create an index (if not exists) and put the mappings parsed from the model into it', async () => {
Expand Down

0 comments on commit e2c7c13

Please sign in to comment.