All files model.js

97.56% Statements 40/41
88.89% Branches 16/18
100% Functions 9/9
97.5% Lines 39/40

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160    1x 1x 1x 1x 1x 1x               1x 121x   121x 41x   80x                 41x                                                                   41x         41x                     41x     41x                 41x 41x 41x 41x                                                 41x 41x   41x 41x 41x   41x 1x     41x                   41x   41x 38x     3x 3x 3x 2x   3x 1x   3x   3x    
'use strict'
 
const { get, set, isEmpty, isArray } = require('lodash')
const mongoose = require('mongoose')
const TYPES = mongoose.Schema.Types
const Schema = mongoose.Schema
const DEFAULT_COLLECTION_NAME = 'historyLogs'
mongoose.Promise = global.Promise
 
/**
 * Create and cache a history mongoose model
 * @param { mongoose } mongooseConnection - mongoose connection
 * @param { Object } [options]
 * @return {mongoose.Model} History Log Model
 */
module.exports.HistoryLogModel = function (mongooseConnection = {}, options = {}) {
  const collectionName = get(options, 'customCollectionName', DEFAULT_COLLECTION_NAME)
 
  if (!(collectionName in mongooseConnection.models)) {
    return initializeInstanceModel(collectionName, mongooseConnection, options)
  }
  return mongooseConnection.models[collectionName]
}
 
/**
 * @author Weligton Monteiro
 * @return { {schema} } - return new instance schema
 * @constructor
 */
function DefaultSchema () {
  return {
    documentNumber: { type: TYPES.String },
    action: { type: TYPES.String },
    method: { type: TYPES.String },
    user: { type: TYPES.Mixed },
    createdAt: { type: TYPES.Date, default: Date.now },
    module: { type: TYPES.String },
    changes: { type: [TYPES.Mixed] }
  }
}
 
/**
 * @author Welington Monteiro
 * @param { mongoose } mongooseInstance
 * @return { Object } - return options mongoose instance
 */
// function getOptionsVersionInstance (mongooseInstance) {
//   const version = parseInt(get(mongooseInstance, 'version'))
//   if (version < 5) {
//     return merge({}, { useMongoClient: true })
//   }
//   return merge({}, { useUnifiedTopology: true, useNewUrlParser: true, useFindAndModify: false, autoIndex: true, useCreateIndex: true })
// }
 
/**
 *
 * @param { String } collectionName - collection name of model
 * @param { mongoose } mongooseConnection - mongoose connection
 * @param { Object } options - options plugin schema
 * @return {*}
 */
function initializeInstanceModel (collectionName, mongooseConnection, options) {
  // TODO: removed in version v1.8.1 for problem multiples connections and timout exception
  // const connectionUri = get(options, 'connectionUri')
  const schema = initializeDefaultSchema(options)
 
  // if (connectionUri) {
  //   return otherConnection(collectionName, options)
  // }
  return getOrAddNewInstanceModel(mongooseConnection, collectionName, schema)
}
 
/**
 * @author Welington Monteiro
 * @param { mongoose } mongooseInstance - mongoose instance
 * @param { String } collectionName - collection name of model
 * @param { schema } schema - schema default
 * @return { Model} - return model instance
 */
function getOrAddNewInstanceModel (mongooseInstance, collectionName, schema) {
  Iif (mongooseInstance.models[collectionName]) {
    return mongooseInstance.models[collectionName]
  } else {
    return mongooseInstance.model(collectionName, schema, collectionName)
  }
}
 
/**
 * @author Welington Monteiro
 * @param { Object } options - options plugin schema
 */
function initializeDefaultSchema (options) {
  const defaultSchema = addNewPathsInSchema(new DefaultSchema(), options)
  const schema = new Schema(defaultSchema, { id: true, versionKey: false })
  ensureIndex(schema, options)
  return schema
}
 
/**
 * @author Welington Monteiro
 * @param { String } collectionName - collection name of model
 * @param { Object } options - options plugin schema
 * @return {*}
 */
// function otherConnection (collectionName, options) {
//   const connectionUri = get(options, 'connectionUri')
//   const dbOptions = getOptionsVersionInstance(mongoose)
//   const schema = initializeDefaultSchema(options)
//
//   const otherConnection = mongoose.createConnection(connectionUri, dbOptions)
//   return getOrAddNewInstanceModel(otherConnection, collectionName, schema)
// }
 
/**
 * @author Welington Monteiro
 * @param { Object } options - options plugin schema
 * @param { schema } schema - schema default
 * @return {*}
 */
function ensureIndex (schema, options) {
  const indexes = get(options, 'indexes')
  const addCollectionPaths = get(options, 'addCollectionPaths', [])
 
  schema.index({ module: 1 })
  schema.index({ method: 1 })
  schema.index({ documentNumber: 1 })
 
  if (!isEmpty(indexes) && isArray(addCollectionPaths)) {
    indexes.map((idx) => schema.index(idx))
  }
 
  return schema
}
 
/**
 * @author Welington Monteiro
 * @param { Object } options - options plugin schema
 * @param { Object } schema - schema default
 * @return { Object } - return schema
 */
function addNewPathsInSchema (schema, options) {
  const addCollectionPaths = get(options, 'addCollectionPaths', [])
 
  if (isEmpty(addCollectionPaths) || !isArray(addCollectionPaths)) {
    return schema
  }
 
  addCollectionPaths.map(({ key, value, defaultValue }) => {
    const newField = {}
    if (TYPES[value]) {
      newField.type = TYPES[value]
    }
    if (defaultValue !== undefined) {
      newField.default = defaultValue
    }
    set(schema, `${key}`, newField)
  })
  return schema
}