Skip to content

Commit

Permalink
Merge 4269002 into 8771995
Browse files Browse the repository at this point in the history
  • Loading branch information
guumaster committed Sep 12, 2019
2 parents 8771995 + 4269002 commit cb015e4
Show file tree
Hide file tree
Showing 13 changed files with 448 additions and 820 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,8 @@
"node": true,
"mocha": true
},
"extends": "standard"
"extends": "standard",
"rules": {
"no-prototype-builtins": 0
}
}
10 changes: 5 additions & 5 deletions lib/mapping-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ function getTypeFromPaths (paths, field) {
// @return the mapping
//
function getMapping (cleanTree, inPrefix) {
let mapping = {}
const mapping = {}
let value = []
let field = []
let prop = []
let implicitFields = []
const implicitFields = []
let hasEsIndex = false
const prefix = inPrefix !== '' ? `${inPrefix}.` : inPrefix

Expand Down Expand Up @@ -134,7 +134,7 @@ function getMapping (cleanTree, inPrefix) {
// @return the tree
//
function getCleanTree (tree, paths, inPrefix, isRoot) {
let cleanTree = {}
const cleanTree = {}
let type = ''
let value = {}
let field
Expand All @@ -143,7 +143,7 @@ function getCleanTree (tree, paths, inPrefix, isRoot) {
let subTree
let key
let geoFound = false
let prefix = inPrefix !== '' ? `${inPrefix}.` : inPrefix
const prefix = inPrefix !== '' ? `${inPrefix}.` : inPrefix

tree = cloneDeep(tree)
paths = cloneDeep(paths)
Expand Down Expand Up @@ -291,7 +291,7 @@ function Generator () {}
Generator.prototype.generateMapping = function generateMapping (schema) {
const cleanTree = getCleanTree(schema.tree, schema.paths, '', true)
delete cleanTree[schema.get('versionKey')]
let mapping = getMapping(cleanTree, '')
const mapping = getMapping(cleanTree, '')
return { properties: mapping }
}

Expand Down
73 changes: 32 additions & 41 deletions lib/mongoosastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function hydrate (res, model, options, cb) {

query.exec((err, docs) => {
let hits
let docsMap = {}
const docsMap = {}

if (err) {
return cb(err)
Expand Down Expand Up @@ -207,30 +207,30 @@ function deleteByMongoId (options, cb) {
}

function Mongoosastic (schema, pluginOpts) {
let options = pluginOpts || {}
const options = pluginOpts || {}

let bulkTimeout
let bulkBuffer = []
let esClient
let populate = options && options.populate
let mapping = generator.generateMapping(schema)
const populate = options && options.populate
const mapping = generator.generateMapping(schema)

let indexName = options && options.index
let typeName = options && options.type
let alwaysHydrate = options && options.hydrate
let defaultHydrateOptions = options && options.hydrateOptions
const alwaysHydrate = options && options.hydrate
const defaultHydrateOptions = options && options.hydrateOptions
let bulk = options && options.bulk
let filter = options && options.filter
let transform = options && options.transform
let routing = options && options.routing
const filter = options && options.filter
const transform = options && options.transform
const routing = options && options.routing

let customProperties = options && options.customProperties
let customSerialize = options && options.customSerialize
let forceIndexRefresh = options && options.forceIndexRefresh
let indexAutomatically = !(options && options.indexAutomatically === false)
let saveOnSynchronize = !(options && options.saveOnSynchronize === false)
const customProperties = options && options.customProperties
const customSerialize = options && options.customSerialize
const forceIndexRefresh = options && options.forceIndexRefresh
const indexAutomatically = !(options && options.indexAutomatically === false)
const saveOnSynchronize = !(options && options.saveOnSynchronize === false)

let bulkErrEm = new events.EventEmitter()
const bulkErrEm = new events.EventEmitter()

if (options.esClient) {
esClient = options.esClient
Expand Down Expand Up @@ -374,8 +374,6 @@ function Mongoosastic (schema, pluginOpts) {
* @param cb Function
*/
schema.methods.index = function schemaIndex (inOpts, inCb) {
let index
let type
let serialModel
let cb = inCb
let opts = inOpts
Expand All @@ -391,8 +389,8 @@ function Mongoosastic (schema, pluginOpts) {

setIndexNameIfUnset(this.constructor.modelName)

index = opts.index || indexName
type = opts.type || typeName
const index = opts.index || indexName
const type = opts.type || typeName

/**
* Serialize the model, and apply transformation
Expand Down Expand Up @@ -466,7 +464,6 @@ function Mongoosastic (schema, pluginOpts) {
schema.statics.esTruncate = function esTruncate (inOpts, inCb) {
let opts = inOpts
let cb = inCb
let esQuery

if (arguments.length < 2) {
cb = inOpts || nop
Expand All @@ -478,7 +475,7 @@ function Mongoosastic (schema, pluginOpts) {
opts.index = opts.index || indexName
opts.type = opts.type || typeName

esQuery = {
const esQuery = {
body: {
query: {
match_all: {}
Expand Down Expand Up @@ -512,16 +509,15 @@ function Mongoosastic (schema, pluginOpts) {
* @param query - query for documents you want to synchronize
*/
schema.statics.synchronize = function synchronize (inQuery, inOpts) {
let em = new events.EventEmitter()
const em = new events.EventEmitter()
let closeValues = []
let counter = 0
let stream
let query = inQuery || {}
let close = function close () {
const query = inQuery || {}
const close = function close () {
em.emit.apply(em, ['close'].concat(closeValues))
}

let _saveOnSynchronize = inOpts &&
const _saveOnSynchronize = inOpts &&
inOpts.saveOnSynchronize !== undefined ? inOpts.saveOnSynchronize : saveOnSynchronize

// Set indexing to be bulk when synchronizing to make synchronizing faster
Expand All @@ -534,7 +530,7 @@ function Mongoosastic (schema, pluginOpts) {

setIndexNameIfUnset(this.modelName)

stream = this.find(query).batchSize(bulk.batch).cursor()
const stream = this.find(query).batchSize(bulk.batch).cursor()

stream.on('data', doc => {
stream.pause()
Expand Down Expand Up @@ -568,9 +564,8 @@ function Mongoosastic (schema, pluginOpts) {
})

stream.on('close', (pA, pB) => {
let closeInterval
closeValues = [pA, pB]
closeInterval = setInterval(() => {
const closeInterval = setInterval(() => {
if (counter === 0 && bulkBuffer.length === 0) {
clearInterval(closeInterval)
close()
Expand Down Expand Up @@ -598,20 +593,18 @@ function Mongoosastic (schema, pluginOpts) {
schema.statics.search = function search (inQuery, inOpts, inCb) {
let cb = inCb
let opts = inOpts
let query = inQuery === null ? undefined : inQuery
let esSearch
let fullQuery
const query = inQuery === null ? undefined : inQuery

if (arguments.length === 2) {
cb = arguments[1]
opts = {}
}

fullQuery = {
const fullQuery = {
query: query
}

esSearch = schema.statics.esSearch.bind(this)
const esSearch = schema.statics.esSearch.bind(this)

return esSearch(fullQuery, opts, cb)
}
Expand All @@ -627,11 +620,10 @@ function Mongoosastic (schema, pluginOpts) {
* @param inCb - callback called with search results
*/
schema.statics.esSearch = function (inQuery, inOpts, inCb) {
let _this = this
const _this = this
let cb = inCb
let opts = inOpts
let esQuery
let query = inQuery === null ? undefined : inQuery
const query = inQuery === null ? undefined : inQuery

if (arguments.length === 2) {
cb = arguments[1]
Expand All @@ -642,7 +634,7 @@ function Mongoosastic (schema, pluginOpts) {

setIndexNameIfUnset(this.modelName)

esQuery = {
const esQuery = {
body: query,
index: opts.index || indexName,
type: opts.type || typeName
Expand Down Expand Up @@ -698,7 +690,6 @@ function Mongoosastic (schema, pluginOpts) {
schema.statics.esCount = function esCount (inQuery, inCb) {
let cb = inCb
let query = inQuery
let esQuery

setIndexNameIfUnset(this.modelName)

Expand All @@ -709,7 +700,7 @@ function Mongoosastic (schema, pluginOpts) {
}
}

esQuery = {
const esQuery = {
body: {
query: query
},
Expand All @@ -721,7 +712,7 @@ function Mongoosastic (schema, pluginOpts) {
}

schema.statics.flush = function flush (inCb) {
let cb = inCb || nop
const cb = inCb || nop

esClient.bulk({
body: bulkBuffer
Expand Down
6 changes: 3 additions & 3 deletions lib/serialize.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict'

module.exports = function serialize (model, mapping) {
let name, outModel
let name

function _serializeObject (object, mappingData) {
let serialized = {}
const serialized = {}
let field
let val
for (field in mappingData.properties) {
Expand All @@ -30,7 +30,7 @@ module.exports = function serialize (model, mapping) {
throw new Error('es_cast must be a function')
}

outModel = mapping.cast ? mapping.cast.call(this, model) : model
const outModel = mapping.cast ? mapping.cast.call(this, model) : model
if (typeof outModel === 'object' && outModel !== null) {
name = outModel.constructor.name
if (name === 'ObjectID') {
Expand Down

0 comments on commit cb015e4

Please sign in to comment.