mysql-live
Version:
Brings the server.publish and client.subscribe for live updates on mysql database. The only one Live Collections.
163 lines (162 loc) • 8.23 kB
JavaScript
var _ = require("lodash");
var node_mysql_wrapper_1 = require("node-mysql-wrapper");
var Dispatcher = (function () {
function Dispatcher(handler, db) {
this.db = db;
this.handler = handler;
}
Object.defineProperty(Dispatcher.prototype, "subscriptions", {
get: function () {
return this.handler.subscriptions;
},
enumerable: true,
configurable: true
});
Dispatcher.prototype.emitSubscriptions = function (subscriptions, callbackClient) {
var _this = this;
var noPassportForOneOfTheseCollectionsFromSubscriptions = false;
if (subscriptions === undefined)
return;
subscriptions.forEach(function (subscription, index) {
if (noPassportForOneOfTheseCollectionsFromSubscriptions) {
callbackClient(["No Passport"]);
return;
}
if (_this.handler.store.isFindCursor(subscription.cursor)) {
var col = _this.handler.collections[subscription.cursor["collectionName"]];
if (col !== undefined) {
var hasRights = _this.handler.passport.hasRights(col, "subscribe", subscription.subscriber, subscription.name);
if (hasRights) {
if (index === subscriptions.length - 1) {
_this.publishFind(subscription, callbackClient);
}
else {
_this.publishFind(subscription);
}
_this.handler.tableEmmiter.addSubscriptionTo(col.table, subscription.name);
}
else {
if (_this.handler.passport.getPassport(subscription.subscriber) === undefined && index === subscriptions.length - 1) {
noPassportForOneOfTheseCollectionsFromSubscriptions = true;
if (index === subscriptions.length - 1) {
callbackClient(["No Passport"]);
}
}
}
}
}
else if (_this.handler.store.isProcedureCursor(subscription.cursor)) {
_this.publishProcedure(subscription, function (_items) {
if (_items && _items.length > 0 && _items[0] === 'No Passport' && noPassportForOneOfTheseCollectionsFromSubscriptions === false) {
noPassportForOneOfTheseCollectionsFromSubscriptions = true;
callbackClient(['No Passport']);
}
});
}
});
};
Dispatcher.prototype.publishFind = function (subscription, callbackClient) {
var _this = this;
var col = this.handler.collections[subscription.cursor["collectionName"]];
if (col === undefined)
return;
var tableName = col.table;
var criteria = subscription.cursor["criteria"];
this.db.table(tableName).find(criteria).then(function (results) {
if (callbackClient)
callbackClient(results);
_this.handler.getSocket(subscription.subscriber).emit("onPublicationReceive", {
name: subscription.name,
collectionName: col.name,
table: tableName,
primaryKeyColumn: node_mysql_wrapper_1.Helper.toObjectProperty(_this.db.table(tableName).primaryKey),
__single_item_collection__: col.isObject(),
items: results
});
});
};
Dispatcher.prototype.publishProcedure = function (subscription, callbackClient) {
var _this = this;
this.executeProcedureCursor(subscription.cursor, function (collectionAndItems) {
//...an dn petuxei to subscription.cursor.collectionNames = [colnames]..., na to kanw me to this.handler.subscriptions[subscription.name]...= [colnames], idi exoun ginei register, apto Receive.onSubscribe.
var noPassportForOneOfTheseCollectionsFromSubscriptions = false;
node_mysql_wrapper_1.Helper.forEachKey(collectionAndItems, function (collectionName) {
if (noPassportForOneOfTheseCollectionsFromSubscriptions === false) {
if (_.findIndex(subscription.cursor["collectionNames"], function (colName) { return colName === collectionName; }) === -1) {
subscription.cursor["collectionNames"].push(collectionName);
}
var col = _this.handler.collections[collectionName];
var hasRights = _this.handler.passport.hasRights(col, "subscribe", subscription.subscriber, subscription.name);
if (!hasRights) {
if (_this.handler.passport.getPassport(subscription.subscriber) === undefined) {
noPassportForOneOfTheseCollectionsFromSubscriptions = true;
}
}
else {
if (callbackClient)
callbackClient(collectionAndItems[collectionName]);
var traveler = {
name: subscription.name,
collectionName: col.name,
table: col.table,
primaryKeyColumn: node_mysql_wrapper_1.Helper.toObjectProperty(col.primaryKeyColumn),
__single_item_collection__: col.isObject(),
items: collectionAndItems[collectionName]
};
_this.handler.getSocket(subscription.subscriber).emit("onPublicationReceive", traveler);
_this.handler.tableEmmiter.addSubscriptionTo(col.table, subscription.name);
}
}
});
if (noPassportForOneOfTheseCollectionsFromSubscriptions) {
if (callbackClient)
callbackClient(["No Passport"]);
}
});
};
Dispatcher.prototype.executeProcedureCursor = function (cursor, callback) {
var _this = this;
this.handler.db.call(cursor.name, cursor.params, function (results) {
var collectionAndItems = {};
if (results && results.length > 0) {
_this.handler.db.connection.tables.forEach(function (table) {
var collectionsLinkedToTable = _this.handler.getCollectionsByTable(table.name);
results[0].forEach(function (res) {
if (Object.keys(res).indexOf(table.primaryKey) !== -1) {
var objToPush = table.objectFromRow(res);
collectionsLinkedToTable.forEach(function (col) {
if (collectionAndItems[col.name] === undefined) {
collectionAndItems[col.name] = [];
}
collectionAndItems[col.name].push(objToPush);
});
}
});
});
}
callback(collectionAndItems);
});
};
Dispatcher.prototype.emitInsertObject = function (socketId, collectionName, item) {
this.handler.getSocket(socketId).emit("onInsertObject", {
collectionName: collectionName,
newItem: item
});
};
Dispatcher.prototype.emitUpdateObject = function (socketId, collectionName, newItem, selector) {
this.handler.getSocket(socketId).emit("onUpdateObject", {
collectionName: collectionName,
newItem: newItem,
selector: selector
});
};
Dispatcher.prototype.emitRemoveObject = function (socketId, collectionName, selector) {
this.handler.getSocket(socketId).emit("onRemoveObject", {
collectionName: collectionName,
selector: selector
});
};
return Dispatcher;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Dispatcher;