Added socket.io
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
'use strict';
|
||||
const ps = require('../controller/pubsub');
|
||||
|
||||
|
||||
function ModelPs(model){
|
||||
const Model = model.constructor.name === 'Function' ? model : model.constructor
|
||||
|
||||
function getIndex(req, res){
|
||||
if(model[Model._key]) return model[Model._key];
|
||||
if(req && req[Model._key]) return req[Model._key];
|
||||
if(res && res[Model._key]) return res[Model._key];
|
||||
}
|
||||
|
||||
function publish(prop, res, req){
|
||||
if(!['add', 'create', 'update', 'remove'].includes(prop)) return;
|
||||
|
||||
ps.publish(`model:${Model.name}:${prop}:${getIndex(res, req)}`, res);
|
||||
}
|
||||
|
||||
return new Proxy(model, {
|
||||
construct(target, args) {
|
||||
let instance = ModelPs(new model(...args));
|
||||
|
||||
return instance;
|
||||
},
|
||||
get(target, propKey, receiver) {
|
||||
if(propKey == 'constructor') return target.constructor;
|
||||
if(propKey === 'isproxy') return 'YESSSSSSSSS'
|
||||
const targetValue = Reflect.get(target, propKey, receiver);
|
||||
if (typeof targetValue === 'function') {
|
||||
return function(...args){
|
||||
let res = targetValue.apply(this, args); // (A)
|
||||
if(targetValue.constructor.name === 'AsyncFunction'){
|
||||
res.then(function(res){
|
||||
publish(propKey, res, ...args);
|
||||
});
|
||||
}else{
|
||||
publish(propKey, res, ...args)
|
||||
}
|
||||
return res;
|
||||
}
|
||||
} else {
|
||||
return targetValue;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = ModelPs;
|
||||
Reference in New Issue
Block a user