Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ exports.elasticsearch2 = {
```js
// {app_root}/config/config.default.js
exports.elasticsearch = {
host: 'host:port',
client: {
host: 'host:port',
},
// more options: https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/configuration.html
};
```
Expand Down
7 changes: 2 additions & 5 deletions config/config.default.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@
* @property {String} SOME_KEY - some description
*/
exports.elasticsearch = {
default: {
},
client: {},
app: true,
agent: false,

host: '',
agent: false
};
10 changes: 10 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Client } from '@elastic/elasticsearch';

declare module 'egg' {
interface SingletonWrapper<T> {
get(name: string): T
}
interface Application {
elasticsearch: Client & SingletonWrapper<Client>;
}
}
29 changes: 12 additions & 17 deletions lib/elasticsearch.js
Original file line number Diff line number Diff line change
@@ -1,33 +1,28 @@
'use strict';

const assert = require('assert');
const elasticsearch = require('elasticsearch');
const elasticsearch = require('@elastic/elasticsearch');

let count = 0;
const createOneClient = (config, app) => {
const {host} = config;
// const { host } = config;
// check key & secret
assert(host, '[egg-elasticsearch] host is required.');
// assert(host, '[egg-elasticsearch] host is required.');
const client = new elasticsearch.Client(config);

app.beforeStart(async () => {
const index = count++;
await client.ping(
{
// ping usually has a 3000ms timeout
requestTimeout: 1000
},
function (error) {
if (error) {
app.coreLogger.info('elasticsearch cluster is down!');
} else {
app.coreLogger.info(`[egg-elasticsearch] instance[${index}] status Ok.`);
}
}
);
try {
await client.ping();
app.logger.info(`[egg-elasticsearch] instance[${index}] status Ok.`);
} catch (error) {
app.logger.error('[egg-elasticsearch] elasticsearch cluster is down with error: ', error);
throw error;
}
});
return client;
};

module.exports = (app) => {
module.exports = app => {
app.addSingleton('elasticsearch', createOneClient);
};
12 changes: 7 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egg-elasticsearch2",
"version": "2.0.1",
"name": "egg-elasticsearch",
"version": "3.0.0",
"description": "elasticsearch for egg",
"eggPlugin": {
"name": "elasticsearch2"
Expand All @@ -13,7 +13,7 @@
"egg-elasticsearch"
],
"dependencies": {
"elasticsearch": "^15.3.1"
"@elastic/elasticsearch": "^6.8.0"
},
"devDependencies": {
"autod": "^3.0.1",
Expand Down Expand Up @@ -43,7 +43,8 @@
"config",
"agent.js",
"lib",
"app.js"
"app.js",
"index.d.ts"
],
"ci": {
"version": "6, 8"
Expand All @@ -57,5 +58,6 @@
},
"homepage": "https://github.com/eggjs/egg-elasticsearch#readme",
"author": "thonatos",
"license": "MIT"
"license": "MIT",
"types": "index.d.ts"
}
2 changes: 1 addition & 1 deletion test/elasticsearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ describe('test/elasticsearch.test.js', () => {
let app;
before(() => {
app = mm.app({
baseDir: 'apps/elasticsearch-test',
baseDir: 'apps/elasticsearch-test'
});
return app.ready();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
exports.keys = '123456';

exports.elasticsearch = {
host: 'localhost:9200',
client: {
host: 'localhost:9200'
}
// httpAuth: '', // user:pass
};