├── .autod.conf.js ├── .eslintignore ├── .eslintrc ├── .github └── PULL_REQUEST_TEMPLATE.md ├── .gitignore ├── .travis.yml ├── LICENSE ├── README.md ├── agent.js ├── app.js ├── appveyor.yml ├── config └── config.default.js ├── lib └── elasticsearch.js ├── package.json └── test ├── elasticsearch.test.js └── fixtures └── apps └── elasticsearch-test ├── app └── router.js ├── config └── config.unittest.js └── package.json /.autod.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | write: true, 5 | prefix: '^', 6 | plugin: 'autod-egg', 7 | test: [ 8 | 'test', 9 | 'benchmark', 10 | ], 11 | devdep: [ 12 | 'egg', 13 | 'egg-ci', 14 | 'egg-bin', 15 | 'autod', 16 | 'autod-egg', 17 | 'eslint', 18 | 'eslint-config-egg', 19 | 'supertest', 20 | 'webstorm-disable-index', 21 | ], 22 | exclude: [ 23 | './test/fixtures', 24 | './docs', 25 | './coverage', 26 | ], 27 | }; 28 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | coverage 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-egg", 3 | "rules": { 4 | "space-before-function-paren": [ 1, "always" ], 5 | "array-bracket-spacing": [ "error", "always" ], 6 | "comma-dangle": ["error", "never"] 7 | } 8 | } -------------------------------------------------------------------------------- /.github/PULL_REQUEST_TEMPLATE.md: -------------------------------------------------------------------------------- 1 | 10 | 11 | ##### Checklist 12 | 13 | 14 | - [ ] `npm test` passes 15 | - [ ] tests and/or benchmarks are included 16 | - [ ] documentation is changed or added 17 | - [ ] commit message follows commit guidelines 18 | 19 | ##### Affected core subsystem(s) 20 | 21 | 22 | 23 | ##### Description of change 24 | 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | logs/ 2 | npm-debug.log 3 | node_modules/ 4 | coverage/ 5 | .idea/ 6 | run/ 7 | .DS_Store 8 | *.swp 9 | package-lock.json 10 | *.tgz 11 | yarn.lock 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '6' 5 | - '8' 6 | install: 7 | - npm i npminstall && npminstall 8 | script: 9 | - npm run ci 10 | after_script: 11 | - npminstall codecov && codecov 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2017 Alibaba Group Holding Limited and other contributors. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # egg-elasticsearch 2 | 3 | [![NPM version][npm-image]][npm-url] 4 | [![build status][travis-image]][travis-url] 5 | [![Test coverage][codecov-image]][codecov-url] 6 | [![David deps][david-image]][david-url] 7 | [![Known Vulnerabilities][snyk-image]][snyk-url] 8 | [![npm download][download-image]][download-url] 9 | 10 | [npm-image]: https://img.shields.io/npm/v/egg-elasticsearch2.svg?style=flat-square 11 | [npm-url]: https://npmjs.org/package/egg-elasticsearch2 12 | [travis-image]: https://img.shields.io/travis/thonatos/egg-elasticsearch2.svg?style=flat-square 13 | [travis-url]: https://travis-ci.org/thonatos/egg-elasticsearch2 14 | [codecov-image]: https://img.shields.io/codecov/c/github/thonatos/egg-elasticsearch2.svg?style=flat-square 15 | [codecov-url]: https://codecov.io/github/thonatos/egg-elasticsearch2?branch=master 16 | [david-image]: https://img.shields.io/david/thonatos/egg-elasticsearch2.svg?style=flat-square 17 | [david-url]: https://david-dm.org/thonatos/egg-elasticsearch2 18 | [snyk-image]: https://snyk.io/test/npm/egg-elasticsearch2/badge.svg?style=flat-square 19 | [snyk-url]: https://snyk.io/test/npm/egg-elasticsearch2 20 | [download-image]: https://img.shields.io/npm/dm/egg-elasticsearch2.svg?style=flat-square 21 | [download-url]: https://npmjs.org/package/egg-elasticsearch2 22 | 23 | 26 | 27 | egg plugin for [elasticsearch](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html) 28 | 29 | ## Install 30 | 31 | ```bash 32 | $ npm i egg-elasticsearch2 --save 33 | ``` 34 | 35 | ## Usage 36 | 37 | ```js 38 | // {app_root}/config/plugin.js 39 | exports.elasticsearch2 = { 40 | enable: true, 41 | package: 'egg-elasticsearch2', 42 | }; 43 | ``` 44 | 45 | ## Configuration 46 | #### 单实例 47 | 48 | ```js 49 | // {app_root}/config/config.default.js 50 | exports.elasticsearch = { 51 | host: 'host:port', 52 | // more options: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html 53 | }; 54 | ``` 55 | 56 | #### 多实例 57 | ```js 58 | exports.elasticsearch = { 59 | clients: { 60 | es1: { 61 | host: 'host1:port', 62 | }, 63 | es2: { 64 | host: 'host2:port', 65 | } 66 | } 67 | }; 68 | ``` 69 | 70 | see [config/config.default.js](config/config.default.js) for more detail. 71 | 72 | ## Example 73 | #### 单实例 74 | ```js 75 | 'use strict'; 76 | 77 | module.exports = app => { 78 | app.get('/', function* () { 79 | try { 80 | const result = yield app.elasticsearch.search(); 81 | this.body = result; 82 | } catch (error) { 83 | this.status = 500; 84 | this.body = error; 85 | } 86 | }); 87 | }; 88 | ``` 89 | 90 | #### 多实例 91 | ```js 92 | 'use strict'; 93 | 94 | module.exports = app => { 95 | app.get('/', function* () { 96 | try { 97 | const result1 = yield app.elasticsearch.get('es1').search(); 98 | const result2 = yield app.elasticsearch.get('es2').search(); 99 | 100 | this.body = {result1, result2}; 101 | } catch (error) { 102 | this.status = 500; 103 | this.body = error; 104 | } 105 | }); 106 | }; 107 | ``` 108 | 109 | 110 | ## Questions & Suggestions 111 | 112 | Please open an issue [here](https://github.com/eggjs/egg/issues). 113 | 114 | ## License 115 | 116 | [MIT](LICENSE) 117 | -------------------------------------------------------------------------------- /agent.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const elasticsearch = require('./lib/elasticsearch'); 4 | 5 | module.exports = agent => { 6 | if (agent.config.elasticsearch.agent) elasticsearch(agent); 7 | }; 8 | -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const elasticsearch = require('./lib/elasticsearch'); 4 | 5 | module.exports = app => { 6 | elasticsearch(app); 7 | }; 8 | -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: '6' 4 | - nodejs_version: '8' 5 | 6 | install: 7 | - ps: Install-Product node $env:nodejs_version 8 | - npm i npminstall && node_modules\.bin\npminstall 9 | 10 | test_script: 11 | - node --version 12 | - npm --version 13 | - npm run test 14 | 15 | build: off 16 | -------------------------------------------------------------------------------- /config/config.default.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * egg-elasticsearch default config 5 | * https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html 6 | * @member Config#elasticsearch 7 | * @property {String} SOME_KEY - some description 8 | */ 9 | exports.elasticsearch = { 10 | default: { 11 | }, 12 | app: true, 13 | agent: false, 14 | 15 | host: '', 16 | }; 17 | -------------------------------------------------------------------------------- /lib/elasticsearch.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const elasticsearch = require('elasticsearch'); 5 | let count = 0; 6 | const createOneClient = (config, app) => { 7 | const {host} = config; 8 | // check key & secret 9 | assert(host, '[egg-elasticsearch] host is required.'); 10 | const client = new elasticsearch.Client(config); 11 | 12 | app.beforeStart(async () => { 13 | const index = count++; 14 | await client.ping( 15 | { 16 | // ping usually has a 3000ms timeout 17 | requestTimeout: 1000 18 | }, 19 | function (error) { 20 | if (error) { 21 | app.coreLogger.info('elasticsearch cluster is down!'); 22 | } else { 23 | app.coreLogger.info(`[egg-elasticsearch] instance[${index}] status Ok.`); 24 | } 25 | } 26 | ); 27 | }); 28 | return client; 29 | }; 30 | 31 | module.exports = (app) => { 32 | app.addSingleton('elasticsearch', createOneClient); 33 | }; 34 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "egg-elasticsearch2", 3 | "version": "2.0.1", 4 | "description": "elasticsearch for egg", 5 | "eggPlugin": { 6 | "name": "elasticsearch2" 7 | }, 8 | "keywords": [ 9 | "egg", 10 | "eggPlugin", 11 | "egg-plugin", 12 | "eggElasticsearch", 13 | "egg-elasticsearch" 14 | ], 15 | "dependencies": { 16 | "elasticsearch": "^15.3.1" 17 | }, 18 | "devDependencies": { 19 | "autod": "^3.0.1", 20 | "autod-egg": "^1.1.0", 21 | "egg": "^1.13.1", 22 | "egg-bin": "^3.7.0", 23 | "egg-ci": "^1.8.0", 24 | "egg-mock": "^3.17.0", 25 | "eslint": "^3.19.0", 26 | "eslint-config-egg": "^4.2.1", 27 | "supertest": "^3.0.0", 28 | "webstorm-disable-index": "^1.2.0" 29 | }, 30 | "engines": { 31 | "node": ">=6.0.0" 32 | }, 33 | "scripts": { 34 | "test": "npm run lint -- --fix && egg-bin pkgfiles && npm run test-local", 35 | "test-local": "egg-bin test", 36 | "cov": "egg-bin cov", 37 | "lint": "eslint .", 38 | "ci": "egg-bin pkgfiles --check && npm run lint && npm run cov", 39 | "pkgfiles": "egg-bin pkgfiles", 40 | "autod": "autod" 41 | }, 42 | "files": [ 43 | "config", 44 | "agent.js", 45 | "lib", 46 | "app.js" 47 | ], 48 | "ci": { 49 | "version": "6, 8" 50 | }, 51 | "repository": { 52 | "type": "git", 53 | "url": "git+https://github.com/eggjs/egg-elasticsearch.git" 54 | }, 55 | "bugs": { 56 | "url": "https://github.com/eggjs/egg/issues" 57 | }, 58 | "homepage": "https://github.com/eggjs/egg-elasticsearch#readme", 59 | "author": "thonatos", 60 | "license": "MIT" 61 | } 62 | -------------------------------------------------------------------------------- /test/elasticsearch.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const request = require('supertest'); 4 | const mm = require('egg-mock'); 5 | 6 | describe('test/elasticsearch.test.js', () => { 7 | let app; 8 | before(() => { 9 | app = mm.app({ 10 | baseDir: 'apps/elasticsearch-test', 11 | }); 12 | return app.ready(); 13 | }); 14 | 15 | after(() => app.close()); 16 | afterEach(mm.restore); 17 | 18 | it('should GET /', () => { 19 | return request(app.callback()) 20 | .get('/') 21 | .expect(200); 22 | }); 23 | }); 24 | -------------------------------------------------------------------------------- /test/fixtures/apps/elasticsearch-test/app/router.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = app => { 4 | app.get('/', function* () { 5 | try { 6 | const result = yield app.elasticsearch.search(); 7 | this.body = result; 8 | } catch (error) { 9 | this.status = 500; 10 | this.body = error; 11 | } 12 | }); 13 | }; 14 | -------------------------------------------------------------------------------- /test/fixtures/apps/elasticsearch-test/config/config.unittest.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | exports.keys = '123456'; 4 | 5 | exports.elasticsearch = { 6 | host: 'localhost:9200', 7 | // httpAuth: '', // user:pass 8 | }; 9 | -------------------------------------------------------------------------------- /test/fixtures/apps/elasticsearch-test/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "elasticsearch-test", 3 | "version": "0.0.1" 4 | } --------------------------------------------------------------------------------