@fastify/elasticsearch 2.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/dependabot.yml +13 -0
- package/.github/workflows/ci.yml +49 -0
- package/LICENSE +21 -0
- package/README.md +110 -0
- package/es-docker.sh +10 -0
- package/index.js +43 -0
- package/package.json +45 -0
- package/test.js +104 -0
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
version: 2
|
|
2
|
+
updates:
|
|
3
|
+
- package-ecosystem: "github-actions"
|
|
4
|
+
directory: "/"
|
|
5
|
+
schedule:
|
|
6
|
+
interval: "monthly"
|
|
7
|
+
open-pull-requests-limit: 10
|
|
8
|
+
|
|
9
|
+
- package-ecosystem: "npm"
|
|
10
|
+
directory: "/"
|
|
11
|
+
schedule:
|
|
12
|
+
interval: "weekly"
|
|
13
|
+
open-pull-requests-limit: 10
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
name: CI
|
|
2
|
+
'on':
|
|
3
|
+
push:
|
|
4
|
+
paths-ignore:
|
|
5
|
+
- docs/**
|
|
6
|
+
- '*.md'
|
|
7
|
+
pull_request:
|
|
8
|
+
paths-ignore:
|
|
9
|
+
- docs/**
|
|
10
|
+
- '*.md'
|
|
11
|
+
jobs:
|
|
12
|
+
test:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
strategy:
|
|
15
|
+
matrix:
|
|
16
|
+
node-version:
|
|
17
|
+
- 10
|
|
18
|
+
- 12
|
|
19
|
+
- 14
|
|
20
|
+
- 16
|
|
21
|
+
services:
|
|
22
|
+
elasticsearch:
|
|
23
|
+
image: docker.elastic.co/elasticsearch/elasticsearch:7.10.0
|
|
24
|
+
ports:
|
|
25
|
+
- '9200:9200'
|
|
26
|
+
env:
|
|
27
|
+
discovery.type: single-node
|
|
28
|
+
steps:
|
|
29
|
+
- uses: actions/checkout@v3
|
|
30
|
+
- name: Use Node.js
|
|
31
|
+
uses: actions/setup-node@v3
|
|
32
|
+
with:
|
|
33
|
+
node-version: ${{ matrix.node-version }}
|
|
34
|
+
- name: Install Dependencies
|
|
35
|
+
run: |
|
|
36
|
+
npm install --ignore-scripts
|
|
37
|
+
- name: Run Tests
|
|
38
|
+
run: |
|
|
39
|
+
npm test
|
|
40
|
+
automerge:
|
|
41
|
+
needs: test
|
|
42
|
+
runs-on: ubuntu-latest
|
|
43
|
+
permissions:
|
|
44
|
+
pull-requests: write
|
|
45
|
+
contents: write
|
|
46
|
+
steps:
|
|
47
|
+
- uses: fastify/github-action-merge-dependabot@v3
|
|
48
|
+
with:
|
|
49
|
+
github-token: ${{ secrets.GITHUB_TOKEN }}
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018-2019 Fastify
|
|
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.
|
package/README.md
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
# fastify-elasticsearch
|
|
2
|
+
|
|
3
|
+

|
|
4
|
+
[](https://www.npmjs.com/package/fastify-elasticsearch)
|
|
5
|
+
[](https://snyk.io/test/github/fastify/fastify-elasticsearch)
|
|
6
|
+
[](https://standardjs.com/)
|
|
7
|
+
|
|
8
|
+
Fastify plugin for [Elasticsearch](https://www.elastic.co/elasticsearch/) for sharing the same ES client in every part of your server.
|
|
9
|
+
Under the hood, the official [elasticsearch](https://www.npmjs.com/package/@elastic/elasticsearch) module is used.
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
## Install
|
|
13
|
+
|
|
14
|
+
```
|
|
15
|
+
npm i fastify-elasticsearch
|
|
16
|
+
```
|
|
17
|
+
|
|
18
|
+
## Usage
|
|
19
|
+
Add it to your project with `register` and you are done!
|
|
20
|
+
The plugin accepts the [same options](https://github.com/elastic/elasticsearch-js#client-options) as the client.
|
|
21
|
+
|
|
22
|
+
```js
|
|
23
|
+
const fastify = require('fastify')()
|
|
24
|
+
|
|
25
|
+
fastify.register(require('fastify-elasticsearch'), { node: 'http://localhost:9200' })
|
|
26
|
+
|
|
27
|
+
fastify.get('/user', async function (req, reply) {
|
|
28
|
+
const { body } = await this.elastic.search({
|
|
29
|
+
index: 'tweets',
|
|
30
|
+
body: {
|
|
31
|
+
query: { match: { text: req.query.q }}
|
|
32
|
+
}
|
|
33
|
+
})
|
|
34
|
+
|
|
35
|
+
return body.hits.hits
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
fastify.listen(3000, err => {
|
|
39
|
+
if (err) throw err
|
|
40
|
+
})
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
By default, `fastify-elasticsearch` will try to ping the cluster as soon as you start Fastify, but in some cases pinging may not be supported due to the user permissions. If you want, you can disable the initial ping with the `healthcheck` option:
|
|
44
|
+
```js
|
|
45
|
+
fastify.register(require('fastify-elasticsearch'), {
|
|
46
|
+
node: 'http://localhost:9200',
|
|
47
|
+
healthcheck: false
|
|
48
|
+
})
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
If you need to connect to different clusters, you can also pass a `namespace` option:
|
|
52
|
+
```js
|
|
53
|
+
const fastify = require('fastify')()
|
|
54
|
+
|
|
55
|
+
fastify.register(require('fastify-elasticsearch'), {
|
|
56
|
+
node: 'http://localhost:9200',
|
|
57
|
+
namespace: 'cluster1'
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
fastify.register(require('fastify-elasticsearch'), {
|
|
61
|
+
node: 'http://localhost:9201',
|
|
62
|
+
namespace: 'cluster2'
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
fastify.get('/user', async function (req, reply) {
|
|
66
|
+
const { body } = await this.elastic.cluster1.search({
|
|
67
|
+
index: 'tweets',
|
|
68
|
+
body: {
|
|
69
|
+
query: { match: { text: req.query.q }}
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
|
|
73
|
+
return body.hits.hits
|
|
74
|
+
})
|
|
75
|
+
|
|
76
|
+
fastify.listen(3000, err => {
|
|
77
|
+
if (err) throw err
|
|
78
|
+
})
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
## Versioning
|
|
82
|
+
By default the latest and greatest version of the Elasticsearch client is used, see the [compatibility](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/introduction.html#_compatibility) table to understand if the embedded client is correct for you.
|
|
83
|
+
If it is not, you can pass a custom client via the `client` option.
|
|
84
|
+
```js
|
|
85
|
+
const fastify = require('fastify')()
|
|
86
|
+
const { Client } = require('@elastic/elasticsearch')
|
|
87
|
+
|
|
88
|
+
fastify.register(require('fastify-elasticsearch'), {
|
|
89
|
+
client: new Client({ node: 'http://localhost:9200' })
|
|
90
|
+
})
|
|
91
|
+
|
|
92
|
+
fastify.get('/user', async function (req, reply) {
|
|
93
|
+
const { body } = await this.elastic.search({
|
|
94
|
+
index: 'tweets',
|
|
95
|
+
body: {
|
|
96
|
+
query: { match: { text: req.query.q }}
|
|
97
|
+
}
|
|
98
|
+
})
|
|
99
|
+
|
|
100
|
+
return body.hits.hits
|
|
101
|
+
})
|
|
102
|
+
|
|
103
|
+
fastify.listen(3000, err => {
|
|
104
|
+
if (err) throw err
|
|
105
|
+
})
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
## License
|
|
109
|
+
|
|
110
|
+
Licensed under [MIT](./LICENSE).
|
package/es-docker.sh
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const fp = require('fastify-plugin')
|
|
4
|
+
const { Client } = require('@elastic/elasticsearch')
|
|
5
|
+
|
|
6
|
+
async function fastifyElasticsearch (fastify, options) {
|
|
7
|
+
const { namespace, healthcheck } = options
|
|
8
|
+
delete options.namespace
|
|
9
|
+
delete options.healthcheck
|
|
10
|
+
|
|
11
|
+
const client = options.client || new Client(options)
|
|
12
|
+
|
|
13
|
+
if (healthcheck !== false) {
|
|
14
|
+
await client.ping()
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
if (namespace) {
|
|
18
|
+
if (!fastify.elastic) {
|
|
19
|
+
fastify.decorate('elastic', {})
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
if (fastify.elastic[namespace]) {
|
|
23
|
+
throw new Error(`Elasticsearch namespace already used: ${namespace}`)
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fastify.elastic[namespace] = client
|
|
27
|
+
|
|
28
|
+
fastify.addHook('onClose', (instance, done) => {
|
|
29
|
+
instance.elastic[namespace].close(done)
|
|
30
|
+
})
|
|
31
|
+
} else {
|
|
32
|
+
fastify
|
|
33
|
+
.decorate('elastic', client)
|
|
34
|
+
.addHook('onClose', (instance, done) => {
|
|
35
|
+
instance.elastic.close(done)
|
|
36
|
+
})
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
module.exports = fp(fastifyElasticsearch, {
|
|
41
|
+
fastify: '3.x',
|
|
42
|
+
name: 'fastify-elasticsearch'
|
|
43
|
+
})
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@fastify/elasticsearch",
|
|
3
|
+
"version": "2.0.0",
|
|
4
|
+
"description": "Fastify plugin for elastic search",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"lint": "standard | snazzy",
|
|
8
|
+
"test": "npm run lint && npm run unit",
|
|
9
|
+
"unit": "tap test.js"
|
|
10
|
+
},
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "git+https://github.com/fastify/fastify-elasticsearch.git"
|
|
14
|
+
},
|
|
15
|
+
"keywords": [
|
|
16
|
+
"fastify",
|
|
17
|
+
"elastic",
|
|
18
|
+
"elasticsearch",
|
|
19
|
+
"elastic search",
|
|
20
|
+
"ES"
|
|
21
|
+
],
|
|
22
|
+
"author": "Tommaso Allevi - @allevo",
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"bugs": {
|
|
25
|
+
"url": "https://github.com/fastify/fastify-elasticsearch/issues"
|
|
26
|
+
},
|
|
27
|
+
"homepage": "https://github.com/fastify/fastify-elasticsearch#readme",
|
|
28
|
+
"engines": {
|
|
29
|
+
"node": ">=10.0.0"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@elastic/elasticsearch": "^7.1.0",
|
|
33
|
+
"fastify-plugin": "^3.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"fastify": "^3.0.0-rc.1",
|
|
37
|
+
"pre-commit": "^1.2.2",
|
|
38
|
+
"snazzy": "^9.0.0",
|
|
39
|
+
"standard": "^17.0.0",
|
|
40
|
+
"tap": "^16.0.0"
|
|
41
|
+
},
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/test.js
ADDED
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
'use strict'
|
|
2
|
+
|
|
3
|
+
const { test } = require('tap')
|
|
4
|
+
const { Client } = require('@elastic/elasticsearch')
|
|
5
|
+
const Fastify = require('fastify')
|
|
6
|
+
const fastifyElasticsearch = require('./index')
|
|
7
|
+
|
|
8
|
+
test('with reachable cluster', async t => {
|
|
9
|
+
const fastify = Fastify()
|
|
10
|
+
fastify.register(fastifyElasticsearch, { node: 'http://localhost:9200' })
|
|
11
|
+
|
|
12
|
+
await fastify.ready()
|
|
13
|
+
t.strictEqual(fastify.elastic.name, 'elasticsearch-js')
|
|
14
|
+
await fastify.close()
|
|
15
|
+
})
|
|
16
|
+
|
|
17
|
+
test('with unreachable cluster', async t => {
|
|
18
|
+
const fastify = Fastify()
|
|
19
|
+
fastify.register(fastifyElasticsearch, { node: 'http://localhost:9201' })
|
|
20
|
+
|
|
21
|
+
try {
|
|
22
|
+
await fastify.ready()
|
|
23
|
+
t.fail('should not boot successfully')
|
|
24
|
+
} catch (err) {
|
|
25
|
+
t.ok(err)
|
|
26
|
+
await fastify.close()
|
|
27
|
+
}
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
test('with unreachable cluster and healthcheck disabled', async t => {
|
|
31
|
+
const fastify = Fastify()
|
|
32
|
+
fastify.register(fastifyElasticsearch, {
|
|
33
|
+
node: 'http://localhost:9201',
|
|
34
|
+
healthcheck: false
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
try {
|
|
38
|
+
await fastify.ready()
|
|
39
|
+
t.strictEqual(fastify.elastic.name, 'elasticsearch-js')
|
|
40
|
+
} catch (err) {
|
|
41
|
+
t.fail('should not error')
|
|
42
|
+
}
|
|
43
|
+
await fastify.close()
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
test('namespaced', async t => {
|
|
47
|
+
const fastify = Fastify()
|
|
48
|
+
fastify.register(fastifyElasticsearch, {
|
|
49
|
+
node: 'http://localhost:9200',
|
|
50
|
+
namespace: 'cluster'
|
|
51
|
+
})
|
|
52
|
+
|
|
53
|
+
await fastify.ready()
|
|
54
|
+
t.strictEqual(fastify.elastic.cluster.name, 'elasticsearch-js')
|
|
55
|
+
await fastify.close()
|
|
56
|
+
})
|
|
57
|
+
|
|
58
|
+
test('namespaced (errored)', async t => {
|
|
59
|
+
const fastify = Fastify()
|
|
60
|
+
fastify.register(fastifyElasticsearch, {
|
|
61
|
+
node: 'http://localhost:9200',
|
|
62
|
+
namespace: 'cluster'
|
|
63
|
+
})
|
|
64
|
+
|
|
65
|
+
fastify.register(fastifyElasticsearch, {
|
|
66
|
+
node: 'http://localhost:9200',
|
|
67
|
+
namespace: 'cluster'
|
|
68
|
+
})
|
|
69
|
+
|
|
70
|
+
try {
|
|
71
|
+
await fastify.ready()
|
|
72
|
+
t.fail('should not boot successfully')
|
|
73
|
+
} catch (err) {
|
|
74
|
+
t.ok(err)
|
|
75
|
+
await fastify.close()
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
test('custom client', async t => {
|
|
80
|
+
const client = new Client({
|
|
81
|
+
node: 'http://localhost:9200',
|
|
82
|
+
name: 'custom'
|
|
83
|
+
})
|
|
84
|
+
|
|
85
|
+
const fastify = Fastify()
|
|
86
|
+
fastify.register(fastifyElasticsearch, { client })
|
|
87
|
+
|
|
88
|
+
await fastify.ready()
|
|
89
|
+
t.strictEqual(fastify.elastic.name, 'custom')
|
|
90
|
+
await fastify.close()
|
|
91
|
+
})
|
|
92
|
+
|
|
93
|
+
test('Missing configuration', async t => {
|
|
94
|
+
const fastify = Fastify()
|
|
95
|
+
fastify.register(fastifyElasticsearch)
|
|
96
|
+
|
|
97
|
+
try {
|
|
98
|
+
await fastify.ready()
|
|
99
|
+
t.fail('should not boot successfully')
|
|
100
|
+
} catch (err) {
|
|
101
|
+
t.ok(err)
|
|
102
|
+
await fastify.close()
|
|
103
|
+
}
|
|
104
|
+
})
|