elasticsearch-helper
Advanced tools
Comparing version 1.16.0 to 1.16.1
66
index.js
@@ -6,2 +6,3 @@ const ES = require('elasticsearch'); | ||
const Response = require('./class/response'); | ||
const Hit = require('./class/hit'); | ||
const QueryBuilder = require('./class/query-builder'); | ||
@@ -102,3 +103,3 @@ const AggregationBuilder = require('./class/aggregation-builder'); | ||
let onErrorMethod = err => err; | ||
let onUpsertData = null | ||
const onUpsertData = [] | ||
@@ -156,3 +157,4 @@ const promiseSerie = (arroPromises) => { | ||
func: fFunction, | ||
indexes: [] | ||
indexes: [], | ||
excludedIndexes: [] | ||
}; | ||
@@ -525,14 +527,21 @@ return this; | ||
if(["update", "index"].indexOf(sType) !== -1) { | ||
const data = this.onUpsertData || onUpsertData; | ||
if(data) { | ||
const {func, indexes} = data; | ||
if(!indexes.length || indexes.indexOf[this.sIndex] !== -1) { | ||
const data = this.onUpsertData || onUpsertData || [] ; | ||
data.forEach(d => { | ||
const {func, indexes, excludedIndexes} = d; | ||
if((!indexes.length || indexes.indexOf(this.sIndex) !== -1) && excludedIndexes.indexOf(this.sIndex) === -1) { | ||
return new Elasticsearch(this.sIndex, this.sType).id(this.sID).run() | ||
.then(oHit => oHit ? oHit.data() : null) | ||
.then(oData => { | ||
func(oData, this.oBody || this.oDoc) | ||
.then(oHit => oHit ? oHit : null) | ||
.then(oldHit => { | ||
const oCurrentHit = new Hit({ | ||
_id: this.sID, | ||
_type: this.sType, | ||
_index: this.sIndex, | ||
_source: this.oBody || this.oDoc | ||
}) | ||
func(oldHit, oCurrentHit) | ||
}) | ||
} | ||
} | ||
}) | ||
} | ||
@@ -583,8 +592,33 @@ }) | ||
}, | ||
onUpsert(fFuntion, indexes) { | ||
onUpsertData = { | ||
func: fFuntion, | ||
indexes: indexes || [] | ||
}; | ||
onUpsert(func, indexes = [], excludedIndexes = []) { | ||
onUpsertData.push({ | ||
func, | ||
indexes, | ||
excludedIndexes | ||
}); | ||
}, | ||
storeDocumentHistory(fromIndexes = [], toIndex) { | ||
toIndex = toIndex || "historical_data" | ||
onUpsertData.push({ | ||
func(before, after) { | ||
const date = new Date() | ||
new Elasticsearch(toIndex, "data") | ||
.id(`${after.index()}_${after.type()}_${after.id()}_${date.getTime()}`) | ||
.body({ | ||
index: after.index(), | ||
id: after.id(), | ||
type: after.type(), | ||
data: JSON.stringify(after.data()), | ||
metadata: { | ||
dateAdded: date.toISOString() | ||
} | ||
}) | ||
.run(true) | ||
}, | ||
indexes: fromIndexes, | ||
excludedIndexes: [toIndex] //Avoid recursive detection, | ||
}); | ||
}, | ||
AddClient, | ||
@@ -591,0 +625,0 @@ addClient: AddClient, |
{ | ||
"name": "elasticsearch-helper", | ||
"version": "1.16.0", | ||
"version": "1.16.1", | ||
"description": "A Nodejs module facilitating querying Elasticsearch clusters.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
51973
1156