add getInst method that create model singleton

This commit is contained in:
sean 2017-07-05 16:52:48 +08:00
parent ce399f4096
commit 904918b11b
4 changed files with 67 additions and 9 deletions

View File

@ -12,7 +12,7 @@ class groupController{
if(!params.group_name){
return ctx.body = yapi.commons.resReturn(null, 400, '组名不能为空');
}
var groupInst = new groupModel();
var groupInst = yapi.getInst(groupModel);
var checkRepeat = await groupInst.checkRepeat(params.group_name);
if(checkRepeat > 0){
@ -37,7 +37,7 @@ class groupController{
async list(ctx) {
try{
var groupInst = new groupModel();
var groupInst = yapi.getInst(groupModel);
let result = await groupInst.list();
ctx.body = yapi.commons.resReturn(result)
}catch(e){
@ -47,7 +47,7 @@ class groupController{
async del(ctx){
try{
var groupInst = new groupModel();
var groupInst = yapi.getInst(groupModel);
let id = ctx.request.body.id;
let result = await groupInst.del(id);
ctx.body = yapi.commons.resReturn(result)
@ -58,7 +58,7 @@ class groupController{
async up(ctx){
try{
var groupInst = new groupModel();
var groupInst = yapi.getInst(groupModel);
let id = ctx.request.body.id;
let data = {};
ctx.request.body.group_name && (data.group_name = ctx.request.body.group_name)

View File

@ -4,6 +4,7 @@ import prdConfig from './config.json'
import devConfig from './config.dev.json'
let args = process.argv.splice(2);
let isDev = args[0] === 'dev' ? true : false;
var insts = new Map();
const config = isDev ? devConfig : prdConfig;
const WEBROOT = path.resolve(__dirname, '..');
@ -12,6 +13,27 @@ const WEBROOT_RUNTIME = path.join(WEBROOT, 'runtime');
const WEBROOT_LOG = path.join(WEBROOT_RUNTIME, 'log');
const WEBCONFIG = config;
/**
* 获取一个model实例如果不存在则创建一个新的返回
* @param {*} m class
* @example
* yapi.getInst(groupModel, arg1, arg2)
*/
function getInst(m, ...args){
if(!insts.get(m)){
insts.set(m, new m(args))
}
return insts.get(m)
}
function delInst(m){
try{
insts.delete(m)
}catch(err){
console.error(err)
}
}
module.exports = {
fs: fs,
path: path,
@ -20,4 +42,7 @@ module.exports = {
WEBROOT_RUNTIME: WEBROOT_RUNTIME,
WEBROOT_LOG: WEBROOT_LOG,
WEBCONFIG: WEBCONFIG,
getInst: getInst,
delInst: delInst,
getInsts: insts
}

View File

@ -56,7 +56,7 @@ var groupController = function () {
return _context.abrupt('return', ctx.body = _yapi2.default.commons.resReturn(null, 400, '组名不能为空'));
case 3:
groupInst = new _group2.default();
groupInst = _yapi2.default.getInst(_group2.default);
_context.next = 6;
return groupInst.checkRepeat(params.group_name);
@ -120,7 +120,7 @@ var groupController = function () {
switch (_context2.prev = _context2.next) {
case 0:
_context2.prev = 0;
groupInst = new _group2.default();
groupInst = _yapi2.default.getInst(_group2.default);
_context2.next = 4;
return groupInst.list();
@ -161,7 +161,7 @@ var groupController = function () {
switch (_context3.prev = _context3.next) {
case 0:
_context3.prev = 0;
groupInst = new _group2.default();
groupInst = _yapi2.default.getInst(_group2.default);
id = ctx.request.body.id;
_context3.next = 5;
return groupInst.del(id);
@ -203,7 +203,7 @@ var groupController = function () {
switch (_context4.prev = _context4.next) {
case 0:
_context4.prev = 0;
groupInst = new _group2.default();
groupInst = _yapi2.default.getInst(_group2.default);
id = ctx.request.body.id;
data = {};

View File

@ -1,5 +1,9 @@
'use strict';
var _map = require('babel-runtime/core-js/map');
var _map2 = _interopRequireDefault(_map);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
@ -20,6 +24,7 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
var args = process.argv.splice(2);
var isDev = args[0] === 'dev' ? true : false;
var insts = new _map2.default();
var config = isDev ? _configDev2.default : _config2.default;
var WEBROOT = _path2.default.resolve(__dirname, '..');
@ -28,6 +33,31 @@ var WEBROOT_RUNTIME = _path2.default.join(WEBROOT, 'runtime');
var WEBROOT_LOG = _path2.default.join(WEBROOT_RUNTIME, 'log');
var WEBCONFIG = config;
/**
* 获取一个model实例如果不存在则创建一个新的返回
* @param {*} m class
* @example
* yapi.getInst(groupModel, arg1, arg2)
*/
function getInst(m) {
if (!insts.get(m)) {
for (var _len = arguments.length, args = Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++) {
args[_key - 1] = arguments[_key];
}
insts.set(m, new m(args));
}
return insts.get(m);
}
function delInst(m) {
try {
insts.delete(m);
} catch (err) {
console.error(err);
}
}
module.exports = {
fs: _fsExtra2.default,
path: _path2.default,
@ -35,5 +65,8 @@ module.exports = {
WEBROOT_SERVER: WEBROOT_SERVER,
WEBROOT_RUNTIME: WEBROOT_RUNTIME,
WEBROOT_LOG: WEBROOT_LOG,
WEBCONFIG: WEBCONFIG
WEBCONFIG: WEBCONFIG,
getInst: getInst,
delInst: delInst,
getInsts: insts
};