i18nucleus-mongoose
Version:
Mongoose storage middleware for i18nucleus
44 lines (39 loc) • 1.74 kB
JavaScript
/*
* Available options:
* - termsModelName: String - Optional. If not provided defaults to `i18n`
* - mongoose: Mongoose object - Optional. A mongoose object (containig already defined connection) from the client.
* Its connection state can either be idle, connecting or connected.
* - privateConnect: Boolean - Optional. If set to True AND no `mongoose` option provided, create new DB connection
* with connection options provided from options below.
* - db: String - Optional. Database name. Can be included in connectionString option.
* If both db and connectionString option are missing, it defaults to `i18nucleus`.
* - connectionString: String - Optional. Connection string for connecting to Server.
* If missing defaults to localhost. It must follow the rules of mongoose.
* - connectOptions: Object - Optional. Connection options for database connection.
*/
var mongoose = require('mongoose');
exports = module.exports = function (options, utils) {
options = options || {};
options.termsModelName = options.termsModelName || 'i18n';
if (options.mongoose) {
return {
err: null
, options: options
};
} else if (options.privateConnect) {
options.db = options.db || 'i18nucleus';
options.connectionString = options.connectionString || 'mongodb://localhost/' + options.db;
mongoose.connect(options.connectionString, options.connectOptions, function (err) {
if (err) console.log('ERROR CONNECTING TO DATABASE:'.red, err.errmsg);
});
options.mongoose = mongoose;
return {
err: null
, options: options
};
} else {
return {
err: 'No mongoose instance provided and option `privateConnect` not set'
};
}
};