2017-07-03 16:16:05 +08:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
var _mongoose = require('mongoose');
|
|
|
|
|
|
|
|
var _mongoose2 = _interopRequireDefault(_mongoose);
|
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var _yapi = require('../yapi.js');
|
|
|
|
|
|
|
|
var _yapi2 = _interopRequireDefault(_yapi);
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
var _mongooseAutoIncrement = require('mongoose-auto-increment');
|
|
|
|
|
|
|
|
var _mongooseAutoIncrement2 = _interopRequireDefault(_mongooseAutoIncrement);
|
|
|
|
|
2017-07-03 16:16:05 +08:00
|
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
function model(model, schema) {
|
2017-07-05 16:17:58 +08:00
|
|
|
if (schema instanceof _mongoose2.default.Schema === false) {
|
|
|
|
schema = new _mongoose2.default.Schema(schema);
|
|
|
|
}
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
schema.set('autoIndex', false);
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
return _yapi2.default.connect.model(model, schema, model);
|
2017-07-04 16:43:07 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
function connect() {
|
2017-07-03 16:16:05 +08:00
|
|
|
_mongoose2.default.Promise = global.Promise;
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
var config = _yapi2.default.WEBCONFIG;
|
2017-07-26 11:26:28 +08:00
|
|
|
var options = {};
|
2017-07-26 22:03:18 +08:00
|
|
|
|
2017-07-26 11:26:28 +08:00
|
|
|
if (config.user) {
|
2017-07-27 15:50:04 +08:00
|
|
|
options.user = config.db.user;
|
|
|
|
options.pass = config.db.pass;
|
2017-07-26 11:26:28 +08:00
|
|
|
}
|
2017-07-04 16:43:07 +08:00
|
|
|
|
2017-07-26 11:26:28 +08:00
|
|
|
var db = _mongoose2.default.connect('mongodb://' + config.db.servername + ':' + config.db.port + '/' + config.db.DATABASE, options);
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-27 15:50:04 +08:00
|
|
|
db.then(function () {
|
2017-07-04 16:43:07 +08:00
|
|
|
_yapi2.default.commons.log('mongodb load success...');
|
2017-07-03 16:16:05 +08:00
|
|
|
}, function (err) {
|
2017-07-04 16:43:07 +08:00
|
|
|
_yapi2.default.commons.log(err, 'Mongo connect error');
|
2017-07-03 16:16:05 +08:00
|
|
|
});
|
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
_mongooseAutoIncrement2.default.initialize(db);
|
2017-07-03 16:16:05 +08:00
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
_yapi2.default.db = model;
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
model: model,
|
|
|
|
connect: connect
|
|
|
|
};
|