DB module

WORK IN PROGRESS

The module uses Mongoose framework. The connection configuration parameters must be set in the db key of the configuration properties.

Basic parameters

"db": {
  "host": "localhost",
  "db": "micro"
}

Full parameters

"db": {
  "host": "localhost",
  "db": "micro",
  "port": 27017,
  "user": "xxxx",
  "password": "xxxx",
  "debug": false
}

When the debug configuration is set to true, the framework will logs the commands sent to the database.

2016-05-18T23:22:01.321Z - debug: [db-mongo] reserves.find({"expirationTime":{"$lt":"2016-05-19T13:22:01.321Z"},"state":"ISSUED"})

Use

In the application, use the mongoose interface to register and use the models.

Register the model in a module (i.e.: cartModel.js):

function modelFactory(base) {
  const schema = base.db.Schema({
    userId: { type: String, required: true },
    items: [itemsSchema]
  });
  return base.db.model('Cart', schema);
}
module.exports = modelFactory;

and use the module directly:

const Cart = require('./models/cartModel')(base);

or using config parameters:

const Cart = require(base.config.get('models:cartModel'))(base);

To use the models, access the base.db property:

base.db.models.Cart
.find({id: 'ByQpDBcM'})
.exec()
.then(reserves => {
  // Do something
})
.catch(error => {
  base.logger.error(error);
});