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;
|
||||
@@ -22,7 +22,6 @@ class Table{
|
||||
|
||||
static async get(index){
|
||||
try{
|
||||
|
||||
if(typeof index === 'object'){
|
||||
index = index[this._key];
|
||||
}
|
||||
@@ -43,7 +42,7 @@ class Table{
|
||||
// back to native values.
|
||||
result = objValidate.parseFromString(this._keyMap, result);
|
||||
|
||||
return new this.prototype.constructor(result);
|
||||
return new this(result);
|
||||
|
||||
}catch(error){
|
||||
throw error;
|
||||
@@ -84,6 +83,10 @@ class Table{
|
||||
return out;
|
||||
}
|
||||
|
||||
static async create(...args){
|
||||
return this.add(...args);
|
||||
}
|
||||
|
||||
static async add(data){
|
||||
// Add a entry to this redis table.
|
||||
try{
|
||||
|
||||
Reference in New Issue
Block a user