ElasticModels is a elasticsearch object modeling tool designed to work in and asynchronous environment. Builded for official elasticsearch client library Main inspiration was mongoose project.
WIP
WIP
First, we need to define a connection.
const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
const { schema } = require('./lib')(client, {});
Models are defined through the schema
interface.
const elasticsearch = require('elasticsearch');
const client = new elasticsearch.Client({
host: 'localhost:9200',
log: 'trace'
});
const { schema, types } = require('./lib')(client, {})
const { string, integer, object, array } = types
const video = schema('video', {
id: {
type: string
},
length: {
type: integer
},
description: {
type: string
},
descriptionShort: {
type: string,
dependencies: ['description'],
translation: description => description.subString(0, 20) + '...'
},
comments: { //Async field
type: array,
dependencies: ['videoId'],
translation: videoId => getVideoCommentsForId('id') // this returning Promise
}, {
index: 'video', // required! elastic search index
type: 'fullEpisode' // required! elasticsearch type
},
});
You have couple options to define field in schema. The most simple is
- Simple field. This field will looks if
id
exist in data and return value.
id: {
type: string,
}
- Field with string translation. If you need to get value from object type field you can use define field like this.
name: {
type: string,
translation: 'namespace.description' // You can use dot notation. :-)
}
- Field with string translation with fallback. If you need to get value from field/object and if not exist you want to fallback from another data.
description: {
type: string,
translation: ['namespace.description, default.description'],
}
- Field with function as translation. If you need to execute code before feeling a field. (Sync)
description: {
type: string,
dependencies: ['namespace.description, default.description'],
translate: (desc, defaultDesc, options) => {
if (desc) {
return desc;
} else if(defaultDesc) {
return defaultDesc;
} else {
return 'description not found :('
}
}
}
- Field with function as translation. If you need to populate field using another model or you need to pull data from different api
you can return
Promise
from translation function.
articles: {
type: array,
dependencies: ['articlesIds'],
translation: (articlesIds) => articleModel.find({id: articlesIds}).then(docs => docs.getDocuments)
}
Once we define model through schema('ModelName', mySchema)
, we can access it through the same function.
const video = schema('video', video);
video.find(query, options).then(documents => documents.getDocuments());
you can also findById
Important! .find
method returning documents
objects to access data you have to resolve documents.getDocuments()
You can extend model using .addMethod(name, func)
method.
const video = schema('video', video);
video.addMethod('findOnlyShortVideos', function () {
return this.find({
length: '..30000', // find videos what have length less then 30000ms.
})
});
video.methods.findOnlyShortVideos().then(result => {
console.log(result); // collection of videos what have length less then 30000ms.
});
Model have build in pagination.
const video = schema('video', video);
video.find({}, {
size: 30, // default 25
from: 0, // default 0
}).then(result => {
console.log(result); // first 30 objects
});
You can also delete extra method using .deleteMethod('findOnlyShortVideos')
WIP
WIP
The MIT License (MIT) Copyright (c) 2016 Mateusz Śpiewak.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.