yapi/server_dist/utils/db.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2017-07-03 16:16:05 +08:00
'use strict';
var _mongoose = require('mongoose');
var _mongoose2 = _interopRequireDefault(_mongoose);
var _yapi = require('../yapi.js');
var _yapi2 = _interopRequireDefault(_yapi);
2017-07-03 16:16:05 +08:00
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function model(model, schema) {
return _mongoose2.default.model(model, schema, model);
}
function connect() {
2017-07-03 16:16:05 +08:00
_mongoose2.default.Promise = global.Promise;
var config = _yapi2.default.WEBCONFIG;
2017-07-03 16:16:05 +08:00
var db = _mongoose2.default.connect('mongodb://' + config.db.servername + ':' + config.db.port + '/' + config.db.DATABASE);
db.then(function (res) {
_yapi2.default.commons.log('mongodb load success...');
2017-07-03 16:16:05 +08:00
}, function (err) {
_yapi2.default.commons.log(err, 'Mongo connect error');
2017-07-03 16:16:05 +08:00
});
checkDatabase();
return db;
}
function checkDatabase() {
var exist = _yapi2.default.commons.fileExist(_yapi2.default.path.join(_yapi2.default.WEBROOT_RUNTIME, 'init.lock'));
2017-07-03 16:16:05 +08:00
if (!exist) {
_yapi2.default.commons.log('lock is not exist');
2017-07-03 16:16:05 +08:00
}
}
_yapi2.default.db = model;
module.exports = {
model: model,
connect: connect
};