All files model.js

100% Statements 38/38
90% Branches 18/20
100% Functions 3/3
100% Lines 36/36

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    1x 1x 1x 1x 1x 1x                     1x 158x 158x 158x 158x 158x   158x                   158x 158x 3x 3x 3x 2x   3x 1x   3x       158x   158x 158x 158x   158x 1x     158x 1x 1x 1x   157x 38x   119x       157x    
'use strict'
 
const { get, set, isEmpty, isArray } = require('lodash')
const mongoose = require('mongoose')
const TYPES = mongoose.Schema.Types
let historyLogModels = {}
const DEFAULT_COLLECTION_NAME = 'historyLogs'
const dbOptions = {
  autoIndex: true,
  useNewUrlParser: true,
  useFindAndModify: false,
  useUnifiedTopology: true
}
/**
 * Create and cache a history mongoose model
 * @param { Object } [options]
 * @return {mongoose.Model} History Log Model
 */
module.exports.HistoryLogModel = function (options) {
  const indexes = get(options, 'indexes')
  const connectionUri = get(options, 'connectionUri')
  const collectionName = get(options, 'customCollectionName', DEFAULT_COLLECTION_NAME)
  const addCollectionPaths = get(options, 'addCollectionPaths', [])
  Eif (process.env.NODE_ENV === 'test') historyLogModels = {}
 
  const schemaObject = {
    documentNumber: { type: TYPES.String, required: true },
    action: { type: TYPES.String, required: true },
    method: { type: TYPES.String, required: true },
    user: { type: TYPES.Mixed, require: true },
    createdAt: { type: TYPES.Date, required: true, default: Date.now },
    module: { type: TYPES.String, require: true },
    changes: { type: [TYPES.Mixed], required: true }
  }
 
  Eif (!(collectionName in historyLogModels)) {
    if (!isEmpty(addCollectionPaths) && isArray(addCollectionPaths)) {
      addCollectionPaths.map(({ key, value, defaultValue }) => {
        const newField = {}
        if (TYPES[value]) {
          newField.type = TYPES[value]
        }
        if (defaultValue !== undefined) {
          newField.default = defaultValue
        }
        set(schemaObject, `${key}`, newField)
      })
    }
 
    const schema = new mongoose.Schema(schemaObject, { id: true, versionKey: false })
 
    schema.index({ module: 1 })
    schema.index({ method: 1 })
    schema.index({ documentNumber: 1 })
 
    if (!isEmpty(indexes) && isArray(addCollectionPaths)) {
      indexes.map((idx) => schema.index(idx))
    }
 
    if (connectionUri) {
      const otherConnection = require('mongoose').createConnection(connectionUri, dbOptions)
      historyLogModels[collectionName] = otherConnection.model(collectionName, schema, collectionName)
      return historyLogModels[collectionName]
    }
    if (mongoose.models[collectionName]) {
      historyLogModels[collectionName] = mongoose.models[collectionName]
    } else {
      historyLogModels[collectionName] = mongoose.model(collectionName, schema, collectionName)
    }
  }
 
  return historyLogModels[collectionName]
}