yapi/server/utils/db.js

43 lines
827 B
JavaScript
Raw Normal View History

2017-07-03 16:16:05 +08:00
import mongoose from 'mongoose'
import yapi from '../yapi.js'
2017-07-03 16:16:05 +08:00
function model(model, schema){
return mongoose.model(model, schema, model)
}
2017-07-03 16:16:05 +08:00
function connect(){
2017-07-03 16:16:05 +08:00
mongoose.Promise = global.Promise;
let config = yapi.WEBCONFIG;
2017-07-03 16:16:05 +08:00
let db = mongoose.connect(`mongodb://${config.db.servername}:${config.db.port}/${config.db.DATABASE}`);
db.then(function (res) {
yapi.commons.log('mongodb load success...')
2017-07-03 16:16:05 +08:00
}, function (err) {
yapi.commons.log(err, 'Mongo connect error');
2017-07-03 16:16:05 +08:00
})
2017-07-03 16:16:05 +08:00
checkDatabase();
return db;
}
function checkDatabase(){
let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'))
2017-07-03 16:16:05 +08:00
if(!exist){
yapi.commons.log('lock is not exist')
2017-07-03 16:16:05 +08:00
}
}
yapi.db = model;
module.exports = {
model: model,
connect: connect
};