<commit_before>'use strict'

const _ = require('lodash')
const mongoose = require('mongoose')
const DatastoreTrailpack = require('trailpack-datastore')
const lib = require('./lib')

/**
 * Mongoose integration for Trails. Allows mongoose to read its configration from the
 * trails datastore config, and auto-migrate on startup.
 */
module.exports = class MongooseTrailpack extends DatastoreTrailpack {

  /**
   * Ensure that this trailpack supports the configured migration
   */
  validate () {
    if (!_.includes([ 'none', 'drop', 'create' ], this.app.config.database.models.migrate)) {
      throw new Error('Migrate must be configured to either "create" or "drop"')
    }
  }

  configure () {
    this.app.config.database.orm = 'mongoose'

    this.mongoose = mongoose
  }

  /**
   * Initialize mongoose connections, and perform migrations.
   */
  initialize () {
    super.initialize()

    this.models = lib.Transformer.transformModels(this.app)

    this.orm = this.orm || {}
    this.connections = _.mapValues(this.app.config.database.stores, (_store, storeName) => {
      const store = _.merge({ }, _store)
      if (!_.isString(store.uri))
        throw new Error('Store have to contain "uri" option')

      if (!_.isObject(store.options))
        store.options = {}

      // Setup promise library for mongoose
      if (!store.options.promiseLibrary)
        store.options.promiseLibrary = global.Promise

      const connection = mongoose.createConnection(store.uri, store.options)
      const models = _.pickBy(this.models, { connection: storeName })

      _.map(models, model => {
        const schema = new mongoose.Schema(model.schema, model.schemaOptions)
        // Bind statics & methods
        schema.statics = model.statics
        schema.methods = model.methods

        model.onSchema(schema)

        //create model
        this.orm[model.globalId] = connection.model(model.globalId, schema, model.tableName)
      })

      return connection
    })

    this.app.orm = this.orm

    return this.migrate()
  }

  /**
   * Close all database connections
   */
  unload () {
    return Promise.all(
      _.map(this.connections, connection => {
        return new Promise((resolve, reject) => {
          connection.close((err) => {
            if (err)
              return reject(err)<commit_msg>fix undefined model in Footprint service<commit_after>'use strict'

const _ = require('lodash')
const mongoose = require('mongoose')
const DatastoreTrailpack = require('trailpack-datastore')
const lib = require('./lib')

/**
 * Mongoose integration for Trails. Allows mongoose to read its configration from the
 * trails datastore config, and auto-migrate on startup.
 */
module.exports = class MongooseTrailpack extends DatastoreTrailpack {

  /**
   * Ensure that this trailpack supports the configured migration
   */
  validate () {
    if (!_.includes([ 'none', 'drop', 'create' ], this.app.config.database.models.migrate)) {
      throw new Error('Migrate must be configured to either "create" or "drop"')
    }
  }

  configure () {
    this.app.config.database.orm = 'mongoose'

    this.mongoose = mongoose
  }

  /**
   * Initialize mongoose connections, and perform migrations.
   */
  initialize () {
    super.initialize()

    this.models = lib.Transformer.transformModels(this.app)

    this.orm = this.orm || {}
    this.connections = _.mapValues(this.app.config.database.stores, (_store, storeName) => {
      const store = _.merge({ }, _store)
      if (!_.isString(store.uri))
        throw new Error('Store have to contain "uri" option')

      if (!_.isObject(store.options))
        store.options = {}

      // Setup promise library for mongoose
      if (!store.options.promiseLibrary)
        store.options.promiseLibrary = global.Promise

      const connection = mongoose.createConnection(store.uri, store.options)
      const models = _.pickBy(this.models, { connection: storeName })

      _.map(models, model => {
        const schema = new mongoose.Schema(model.schema, model.schemaOptions)
        // Bind statics & methods
        schema.statics = model.statics
        schema.methods = model.methods

        model.onSchema(schema)

        //create model
        this.orm[model.identity] = connection.model(model.globalId, schema, model.tableName)
      })

      return connection
    })

    this.app.orm = this.orm

    return this.migrate()
  }

  /**
   * Close all database connections
   */
  unload () {
    return Promise.all(
      _.map(this.connections, connection => {
        return new Promise((resolve, reject) => {
          connection.close((err) => {
            if (err)
              return reject(err)
{'fraction_non_alphanumeric': 0.12156110044785669, 'fraction_numerical': 0.0, 'mean_word_length': 2.764044943820225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6549009', 'n_tokens_mistral': 1358, 'n_tokens_neox': 1346, 'n_words': 332}