2017-07-03 16:16:05 +08:00
|
|
|
import mongoose from 'mongoose'
|
2017-07-04 16:43:07 +08:00
|
|
|
import yapi from '../yapi.js'
|
2017-07-05 16:17:58 +08:00
|
|
|
import autoIncrement from 'mongoose-auto-increment'
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
function model(model, schema){
|
2017-07-05 16:17:58 +08:00
|
|
|
if(schema instanceof mongoose.Schema === false){
|
|
|
|
schema = new mongoose.Schema(schema);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
schema.set('autoIndex', false);
|
|
|
|
return yapi.connect.model(model, schema, model)
|
2017-07-04 16:43:07 +08:00
|
|
|
}
|
2017-07-03 16:16:05 +08:00
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
function connect(){
|
2017-07-03 16:16:05 +08:00
|
|
|
mongoose.Promise = global.Promise;
|
2017-07-04 16:43:07 +08:00
|
|
|
let config = yapi.WEBCONFIG;
|
2017-07-05 17:59:53 +08:00
|
|
|
|
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) {
|
2017-07-04 16:43:07 +08:00
|
|
|
yapi.commons.log('mongodb load success...')
|
2017-07-03 16:16:05 +08:00
|
|
|
}, function (err) {
|
2017-07-04 16:43:07 +08:00
|
|
|
yapi.commons.log(err, 'Mongo connect error');
|
2017-07-03 16:16:05 +08:00
|
|
|
})
|
|
|
|
|
2017-07-05 16:17:58 +08:00
|
|
|
autoIncrement.initialize(db);
|
2017-07-04 16:43:07 +08:00
|
|
|
|
2017-07-03 16:16:05 +08:00
|
|
|
return db;
|
|
|
|
}
|
|
|
|
|
2017-07-04 16:43:07 +08:00
|
|
|
yapi.db = model;
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
model: model,
|
|
|
|
connect: connect
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|