import mongoose from 'mongoose';
import config from '../config.js';
import log4js from 'log4js';

const logger = log4js.getLogger('MongoDB');
logger.level = config.server.logLevel;

// Set strictQuery to false to prepare for Mongoose 7
mongoose.set('strictQuery', false);

function dbConnect() {
  logger.info(`Connecting to MongoDB...`);
  mongoose.connection.once('open', () => logger.info('Connected to MongoDB.'));
  return mongoose.connect(
    `mongodb://${config.database.mongo.link}/farmcontrol?retryWrites=true&w=majority`,
    {}
  );
}

export { dbConnect };
