Skip to content

Refactor Models and Schemas to Use Mongoose #33

Description

@alvarobernal2412

This task aims to refactor the current schemas and models to use Mongoose for better maintainability and structure. Additionally, the /database/index.js file must be updated to correctly handle the insertion of collections with the new Mongoose-based models.

Current Schema Example (Simplified)

{
    "agreementId": {
        "type": "string"
    },
    "period": {
        "type": "object",
        "properties": {
            "from": {
                "type": "string"
            },
            "to": {
                "type": "string"
            }
        }
    },
    "billId": {
        "type": "string"
    },
    "state": {
        "type": "string"
    },
    "closeDate": {
        "type": "string"
    }
}

Refactored Simplified Schema (Mongoose Example)

import mongoose from 'mongoose';
const { Schema } = mongoose;

const AgreementSchema = new Schema({
    agreementId: { type: String, required: true },
    period: {
        from: { type: String, required: true },
        to: { type: String, required: true }
    },
    billId: { type: String, required: true },
    state: { type: String, required: true },
    closeDate: { type: String, required: true }
});

export default mongoose.model('Agreement', AgreementSchema);

Database Connection Refactor

The /database/index.js file must be updated to properly configure and insert collections using Mongoose models.

Current Code Snippet
function _connect(callback) {
  const instance = this;
  let databaseFullURL;
  if (process.env.NODE_ENV === 'production') {
    databaseFullURL = governify.infrastructure.getServiceURL('internal.database.mongo-registry') + '/' + config.database.name;
  } else if (process.env.NODE_ENV === 'ci') {
    logger.info('Using in-memory database for CI environment');
    memoryDB.connect();
    logger.info('In-memory MongoDB connected');
    if (callback) callback();
    return;
  } else {
    databaseFullURL = 'mongodb://' + process.env.MONGOADMIN + ':' + process.env.MONGOPASS + '@' + config.database.host + ':' + config.database.port + '/' + config.database.name + '?authSource=admin';
  }
  logger.info('Connecting to ' + databaseFullURL);

  mongoose.Promise = global.Promise;
  mongoose.connect(databaseFullURL)
    .then(() => {
      const db = mongoose.connection;
      logger.info('Connected to db!');
      instance.db = db;

      if (!instance.models) {
        instance.models = {};
        try {
          setupModel(instance, config.models.template.name, config.models.template.path, config.models.template.indexableParams);
          setupModel(instance, config.models.agreement.name, config.models.agreement.path);
          setupModel(instance, config.models.state.name, config.models.state.path);
          setupModel(instance, config.models.overrides.name, config.models.overrides.path);
          setupModel(instance, config.models.bills.name, config.models.bills.path);
        } catch (error) {
          logger.error('Error setting the /models files with /configuration files:', error);
          if (callback) {
            callback(error);
          }
          return;
        }
      }

      if (callback) {
        callback();
      }
    })
    .catch(err => {
      logger.error('Database connection error:', err);
      if (callback) {
        callback(err);
      }
    });
}
Recommended Refactor for /database/index.js
  1. Load all Mongoose models dynamically (e.g., AgreementSchema).
  2. Properly handle database connection, error handling, and ensure models are correctly initialized.
  3. Remove outdated setup logic tied to specific paths (setupModel).

Tasks

  • Refactor schemas to use Mongoose.

  • Update /database/index.js to support the new Mongoose-based models.

  • Test to ensure proper connection and insertion of collections in all environments:

    • Production
    • CI
    • Development

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions