зеркало из https://github.com/ojizero/portal
11 изменённых файлов: 1897 добавлений и 14 удалений
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
# Portal sample client - Elasticsearch |
||||
|
||||
This is a sample client build for Elasticsearch using Portal, is provides an API to create index, insert a document and read a document. |
||||
|
||||
## Usage |
||||
|
||||
``` |
||||
npm start # starts a docker container with elasticsearch instance |
||||
|
||||
node ./index.js # run the sample code |
||||
|
||||
npm stop # stop and remove the docker container |
||||
``` |
@ -0,0 +1,71 @@
@@ -0,0 +1,71 @@
|
||||
const { |
||||
Joi, |
||||
createPortalClient, |
||||
} = require('@ojizero/portal') |
||||
|
||||
function makeESClient (host) { |
||||
const client = createPortalClient({ |
||||
baseUrl: host, |
||||
onError: 'resolve', |
||||
}) |
||||
|
||||
return { |
||||
createIndex: client.route({ |
||||
path: '/:indexName', |
||||
method: 'PUT', |
||||
}), |
||||
getDocument: client.route({ |
||||
path: '/:indexName/:docType/:docId', |
||||
method: 'GET', |
||||
}), |
||||
addDocument: client.route({ |
||||
path: '/:indexName/:docType/:docId', |
||||
method: 'POST', |
||||
}) |
||||
// TODO: elastic's bulk API currently is incompatible with portal :D
|
||||
// bulkOperation: client.route({
|
||||
// path: '/_bulk',
|
||||
// method: 'POST',
|
||||
// // You can pass any Joi based schema for validation
|
||||
// // Or you can use the simplified syntax provided by portal
|
||||
// // Or you can use any custom object withe a `validate` method
|
||||
// body: Joi.string().required(),
|
||||
// // Joi.object({
|
||||
// // body: Joi.array().items(
|
||||
// // Joi.object({
|
||||
// // index: Joi.object({
|
||||
// // _index: Joi.string().required(),
|
||||
// // _type: Joi.string().required(),
|
||||
// // _id: Joi.string(),
|
||||
// // }).required(),
|
||||
// // }),
|
||||
// // Joi.object(),
|
||||
// // ).required()
|
||||
// // }).required(),
|
||||
// // headers: {
|
||||
// // 'Content-Type': 'application/text',
|
||||
// // }
|
||||
// }),
|
||||
} |
||||
} |
||||
|
||||
module.exports = makeESClient |
||||
|
||||
if (require.main === module) { |
||||
(async function () { |
||||
const client = makeESClient('http://localhost:9200') |
||||
let response |
||||
|
||||
response = await client.createIndex('test-index') |
||||
delete response._rawResponse // This is a HUGE object
|
||||
console.log({ createIndex: { stringifiedResponse: JSON.stringify(response) } }) |
||||
|
||||
response = await client.addDocument('test-index', 'test-type', 'test-id', { a: { test: 'document' } }) |
||||
delete response._rawResponse // This is a HUGE object
|
||||
console.log({ addDocument: { stringifiedResponse: JSON.stringify(response) } }) |
||||
|
||||
response = await client.getDocument('test-index', 'test-type', 'test-id') |
||||
delete response._rawResponse // This is a HUGE object
|
||||
console.log({ getDocument: { stringifiedResponse: JSON.stringify(response) } }) |
||||
})() |
||||
} |
Разница между файлами не показана из-за своего большого размера
Загрузить разницу
@ -0,0 +1,15 @@
@@ -0,0 +1,15 @@
|
||||
{ |
||||
"name": "sample-elasticsearch-client", |
||||
"version": "1.0.0", |
||||
"description": "Sample library demoing portal", |
||||
"private": true, |
||||
"main": "index.js", |
||||
"scripts": { |
||||
"start": "docker run --publish '9200:9200' --name elastic_portal_demo --detach elasticsearch:6.5.4 && while ! curl localhost:9200; do sleep 10; done", |
||||
"stop": "docker kill elastic_portal_demo && docker rm elastic_portal_demo" |
||||
}, |
||||
"license": "MIT", |
||||
"dependencies": { |
||||
"@ojizero/portal": "../../" |
||||
} |
||||
} |
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
{ |
||||
"name": "sample-rest-api", |
||||
"version": "1.0.0", |
||||
"description": "Sample library demoing portal", |
||||
"private": true, |
||||
"main": "index.js", |
||||
"scripts": {}, |
||||
"license": "MIT", |
||||
"dependencies": { |
||||
"@ojizero/portal": "../../" |
||||
} |
||||
} |
Загрузка…
Ссылка в новой задаче