mirror of
https://github.com/YMFE/yapi.git
synced 2025-03-07 14:16:52 +08:00
feat: add mongo index
This commit is contained in:
parent
7fe29ba83c
commit
92d603db35
@ -3,10 +3,12 @@ import yapi from './yapi.js';
|
||||
import commons from './utils/commons';
|
||||
import dbModule from './utils/db.js';
|
||||
import userModel from './models/user.js';
|
||||
import mongoose from 'mongoose';
|
||||
|
||||
yapi.commons = commons;
|
||||
yapi.connect = dbModule.connect();
|
||||
|
||||
|
||||
function install() {
|
||||
let exist = yapi.commons.fileExist(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
|
||||
|
||||
@ -31,6 +33,98 @@ function setupSql() {
|
||||
up_time: yapi.commons.time()
|
||||
});
|
||||
|
||||
yapi.connect.then(function () {
|
||||
let userCol = mongoose.connection.db.collection('user')
|
||||
userCol.ensureIndex({
|
||||
username: 1
|
||||
})
|
||||
userCol.ensureIndex({
|
||||
email: 1
|
||||
}, {
|
||||
unique: true
|
||||
})
|
||||
|
||||
let projectCol = mongoose.connection.db.collection('project')
|
||||
projectCol.ensureIndex({
|
||||
uid: 1
|
||||
})
|
||||
projectCol.ensureIndex({
|
||||
name: 1
|
||||
})
|
||||
projectCol.ensureIndex({
|
||||
group_id: 1
|
||||
})
|
||||
|
||||
let logCol = mongoose.connection.db.collection('log')
|
||||
logCol.ensureIndex({
|
||||
uid: 1
|
||||
})
|
||||
|
||||
logCol.ensureIndex({
|
||||
typeid: 1,
|
||||
type: 1
|
||||
})
|
||||
|
||||
let interfaceColCol = mongoose.connection.db.collection('interface_col')
|
||||
interfaceColCol.ensureIndex({
|
||||
uid: 1
|
||||
})
|
||||
interfaceColCol.ensureIndex({
|
||||
project_id: 1
|
||||
})
|
||||
|
||||
let interfaceCatCol = mongoose.connection.db.collection('interface_cat')
|
||||
interfaceCatCol.ensureIndex({
|
||||
uid: 1
|
||||
})
|
||||
interfaceCatCol.ensureIndex({
|
||||
project_id: 1
|
||||
})
|
||||
|
||||
let interfaceCaseCol = mongoose.connection.db.collection('interface_case')
|
||||
interfaceCaseCol.ensureIndex({
|
||||
uid: 1
|
||||
})
|
||||
interfaceCaseCol.ensureIndex({
|
||||
col_id: 1
|
||||
})
|
||||
interfaceCaseCol.ensureIndex({
|
||||
project_id: 1
|
||||
})
|
||||
|
||||
let interfaceCol = mongoose.connection.db.collection('interface')
|
||||
interfaceCol.ensureIndex({
|
||||
uid: 1
|
||||
})
|
||||
interfaceCol.ensureIndex({
|
||||
path: 1,
|
||||
method: 1
|
||||
})
|
||||
interfaceCol.ensureIndex({
|
||||
project_id: 1
|
||||
})
|
||||
|
||||
let groupCol = mongoose.connection.db.collection('group')
|
||||
groupCol.ensureIndex({
|
||||
uid: 1
|
||||
})
|
||||
groupCol.ensureIndex({
|
||||
group_name: 1
|
||||
})
|
||||
|
||||
let avatarCol = mongoose.connection.db.collection('avatar')
|
||||
avatarCol.ensureIndex({
|
||||
uid: 1
|
||||
})
|
||||
|
||||
let followCol = mongoose.connection.db.collection('follow')
|
||||
followCol.ensureIndex({
|
||||
uid: 1
|
||||
})
|
||||
followCol.ensureIndex({
|
||||
project_id: 1
|
||||
})
|
||||
|
||||
result.then(function () {
|
||||
fs.ensureFileSync(yapi.path.join(yapi.WEBROOT_RUNTIME, 'init.lock'));
|
||||
console.log(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 成功`); // eslint-disable-line
|
||||
@ -39,6 +133,9 @@ function setupSql() {
|
||||
console.log(`初始化管理员账号 "${yapi.WEBCONFIG.adminAccount}" 失败, ${err.message}`); // eslint-disable-line
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
install();
|
@ -15,7 +15,7 @@ class baseModel{
|
||||
this.schema.plugin(autoIncrement.plugin, {
|
||||
model: this.name,
|
||||
field: this.getPrimaryKey(),
|
||||
startAt: 101,
|
||||
startAt: 11,
|
||||
incrementBy: yapi.commons.rand(1, 10)
|
||||
});
|
||||
}
|
||||
|
@ -12,7 +12,7 @@ function model(model, schema) {
|
||||
return yapi.connect.model(model, schema, model);
|
||||
}
|
||||
|
||||
function connect() {
|
||||
function connect(callback) {
|
||||
mongoose.Promise = global.Promise;
|
||||
|
||||
let config = yapi.WEBCONFIG;
|
||||
@ -28,6 +28,9 @@ function connect() {
|
||||
|
||||
db.then(function () {
|
||||
yapi.commons.log('mongodb load success...');
|
||||
if(typeof callback === 'function'){
|
||||
callback.call(db)
|
||||
}
|
||||
}, function (err) {
|
||||
yapi.commons.log(err, 'Mongo connect error');
|
||||
});
|
||||
@ -38,6 +41,7 @@ function connect() {
|
||||
|
||||
yapi.db = model;
|
||||
|
||||
|
||||
module.exports = {
|
||||
model: model,
|
||||
connect: connect
|
||||
|
@ -20,6 +20,10 @@ var _user = require('./models/user.js');
|
||||
|
||||
var _user2 = _interopRequireDefault(_user);
|
||||
|
||||
var _mongoose = require('mongoose');
|
||||
|
||||
var _mongoose2 = _interopRequireDefault(_mongoose);
|
||||
|
||||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
||||
|
||||
_yapi2.default.commons = _commons2.default;
|
||||
@ -49,6 +53,98 @@ function setupSql() {
|
||||
up_time: _yapi2.default.commons.time()
|
||||
});
|
||||
|
||||
_yapi2.default.connect.then(function () {
|
||||
var userCol = _mongoose2.default.connection.db.collection('user');
|
||||
userCol.ensureIndex({
|
||||
username: 1
|
||||
});
|
||||
userCol.ensureIndex({
|
||||
email: 1
|
||||
}, {
|
||||
unique: true
|
||||
});
|
||||
|
||||
var projectCol = _mongoose2.default.connection.db.collection('project');
|
||||
projectCol.ensureIndex({
|
||||
uid: 1
|
||||
});
|
||||
projectCol.ensureIndex({
|
||||
name: 1
|
||||
});
|
||||
projectCol.ensureIndex({
|
||||
group_id: 1
|
||||
});
|
||||
|
||||
var logCol = _mongoose2.default.connection.db.collection('log');
|
||||
logCol.ensureIndex({
|
||||
uid: 1
|
||||
});
|
||||
|
||||
logCol.ensureIndex({
|
||||
typeid: 1,
|
||||
type: 1
|
||||
});
|
||||
|
||||
var interfaceColCol = _mongoose2.default.connection.db.collection('interface_col');
|
||||
interfaceColCol.ensureIndex({
|
||||
uid: 1
|
||||
});
|
||||
interfaceColCol.ensureIndex({
|
||||
project_id: 1
|
||||
});
|
||||
|
||||
var interfaceCatCol = _mongoose2.default.connection.db.collection('interface_cat');
|
||||
interfaceCatCol.ensureIndex({
|
||||
uid: 1
|
||||
});
|
||||
interfaceCatCol.ensureIndex({
|
||||
project_id: 1
|
||||
});
|
||||
|
||||
var interfaceCaseCol = _mongoose2.default.connection.db.collection('interface_case');
|
||||
interfaceCaseCol.ensureIndex({
|
||||
uid: 1
|
||||
});
|
||||
interfaceCaseCol.ensureIndex({
|
||||
col_id: 1
|
||||
});
|
||||
interfaceCaseCol.ensureIndex({
|
||||
project_id: 1
|
||||
});
|
||||
|
||||
var interfaceCol = _mongoose2.default.connection.db.collection('interface');
|
||||
interfaceCol.ensureIndex({
|
||||
uid: 1
|
||||
});
|
||||
interfaceCol.ensureIndex({
|
||||
path: 1,
|
||||
method: 1
|
||||
});
|
||||
interfaceCol.ensureIndex({
|
||||
project_id: 1
|
||||
});
|
||||
|
||||
var groupCol = _mongoose2.default.connection.db.collection('group');
|
||||
groupCol.ensureIndex({
|
||||
uid: 1
|
||||
});
|
||||
groupCol.ensureIndex({
|
||||
group_name: 1
|
||||
});
|
||||
|
||||
var avatarCol = _mongoose2.default.connection.db.collection('avatar');
|
||||
avatarCol.ensureIndex({
|
||||
uid: 1
|
||||
});
|
||||
|
||||
var followCol = _mongoose2.default.connection.db.collection('follow');
|
||||
followCol.ensureIndex({
|
||||
uid: 1
|
||||
});
|
||||
followCol.ensureIndex({
|
||||
project_id: 1
|
||||
});
|
||||
|
||||
result.then(function () {
|
||||
_fsExtra2.default.ensureFileSync(_yapi2.default.path.join(_yapi2.default.WEBROOT_RUNTIME, 'init.lock'));
|
||||
console.log('\u521D\u59CB\u5316\u7BA1\u7406\u5458\u8D26\u53F7 "' + _yapi2.default.WEBCONFIG.adminAccount + '" \u6210\u529F'); // eslint-disable-line
|
||||
@ -57,6 +153,7 @@ function setupSql() {
|
||||
console.log('\u521D\u59CB\u5316\u7BA1\u7406\u5458\u8D26\u53F7 "' + _yapi2.default.WEBCONFIG.adminAccount + '" \u5931\u8D25, ' + err.message); // eslint-disable-line
|
||||
process.exit(0);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
install();
|
@ -37,7 +37,7 @@ var baseModel = function () {
|
||||
this.schema.plugin(_mongooseAutoIncrement2.default.plugin, {
|
||||
model: this.name,
|
||||
field: this.getPrimaryKey(),
|
||||
startAt: 101,
|
||||
startAt: 11,
|
||||
incrementBy: _yapi2.default.commons.rand(1, 10)
|
||||
});
|
||||
}
|
||||
|
@ -24,7 +24,7 @@ function model(model, schema) {
|
||||
return _yapi2.default.connect.model(model, schema, model);
|
||||
}
|
||||
|
||||
function connect() {
|
||||
function connect(callback) {
|
||||
_mongoose2.default.Promise = global.Promise;
|
||||
|
||||
var config = _yapi2.default.WEBCONFIG;
|
||||
@ -39,6 +39,9 @@ function connect() {
|
||||
|
||||
db.then(function () {
|
||||
_yapi2.default.commons.log('mongodb load success...');
|
||||
if (typeof callback === 'function') {
|
||||
callback.call(db);
|
||||
}
|
||||
}, function (err) {
|
||||
_yapi2.default.commons.log(err, 'Mongo connect error');
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user