mysql-live
Version:
Brings the server.publish and client.subscribe for live updates on mysql database. The only one Live Collections.
148 lines (147 loc) • 7.95 kB
JavaScript
///<reference path="../../typings/lodash/lodash.d.ts"/>
var LiveHelper_1 = require("./LiveHelper");
var node_mysql_wrapper_1 = require("node-mysql-wrapper");
var TableEmmiter = (function () {
function TableEmmiter(handler) {
this.handler = handler;
this.listeningTables = {};
}
TableEmmiter.prototype.addSubscriptionTo = function (tableName, subName) {
if (this.listeningTables[tableName] === undefined) {
this.listeningTables[tableName] = [];
this.listenToTable(tableName);
}
if (this.listeningTables[tableName].indexOf(subName) !== -1)
return;
this.listeningTables[tableName].push(subName);
};
TableEmmiter.prototype.listenToTable = function (tableName) {
var _this = this;
var table = this.handler.db.table(tableName);
table.on("INSERT", function (rows) {
rows.forEach(function (row) {
var objRow = table.objectFromRow(row);
_this.onTableRowInsert(table.name, objRow);
});
});
table.on("UPDATE", function (rows) {
rows.forEach(function (row) {
var rowUpdated = row["after"];
var selector = {};
selector[node_mysql_wrapper_1.Helper.toObjectProperty(table.primaryKey)] = rowUpdated[table.primaryKey];
var newItem = table.objectFromRow(rowUpdated);
_this.onTableRowUpdate(tableName, selector, newItem);
});
});
table.on("DELETE", function (rows) {
rows.forEach(function (row) {
var toBeRemovedCriteria = {};
toBeRemovedCriteria[node_mysql_wrapper_1.Helper.toObjectProperty(table.primaryKey)] = row[table.primaryKey];
_this.onTableRowRemove(tableName, toBeRemovedCriteria);
});
});
};
TableEmmiter.prototype.onTableRowInsert = function (tableName, newItem) {
var _this = this;
var table = this.handler.db.table(tableName);
this.listeningTables[tableName].forEach(function (subscriptionName) {
var subscriptions = _this.handler.store.subscriptions[subscriptionName];
if (subscriptions !== undefined) {
subscriptions.forEach(function (subscription) {
if (_this.handler.isFindCursor(subscription.cursor)) {
var colName = subscription.cursor.collectionName;
if (LiveHelper_1.default.canInsert(newItem, subscription.cursor.criteria)) {
if (_this.handler.collections[colName].table === tableName) {
_this.handler.dispatcher.emitInsertObject(subscription.subscriber, colName, newItem);
}
}
}
else if (_this.handler.isProcedureCursor(subscription.cursor)) {
if (subscription.cursor.canInsert !== undefined && subscription.cursor.canInsert(newItem) === true) {
subscription.cursor.collectionNames.forEach(function (colName) {
if (_this.handler.collections[colName].table === tableName) {
_this.handler.dispatcher.emitInsertObject(subscription.subscriber, colName, newItem);
}
});
}
}
});
}
});
};
TableEmmiter.prototype.onTableRowUpdate = function (tableName, selector, newItem) {
var _this = this;
var table = this.handler.db.table(tableName);
var newCriteriaForOneObject = {};
newCriteriaForOneObject[node_mysql_wrapper_1.Helper.toObjectProperty(table.primaryKey)] = newItem[node_mysql_wrapper_1.Helper.toObjectProperty(table.primaryKey)];
this.listeningTables[tableName].forEach(function (subscriptionName) {
var subscriptions = _this.handler.store.subscriptions[subscriptionName];
if (subscriptions !== undefined) {
subscriptions.forEach(function (subscription) {
if (_this.handler.isFindCursor(subscription.cursor)) {
var colName = subscription.cursor.collectionName;
if (_this.handler.dispatcher.handler.collections[colName].table === tableName) {
_this.handler.dispatcher.emitUpdateObject(subscription.subscriber, colName, newItem, selector);
}
}
else if (_this.handler.isProcedureCursor(subscription.cursor)) {
subscription.cursor.collectionNames.forEach(function (colName) {
if (_this.handler.dispatcher.handler.collections[colName].table === tableName) {
_this.handler.dispatcher.emitUpdateObject(subscription.subscriber, colName, newItem, selector);
}
});
}
});
}
});
};
TableEmmiter.prototype.onTableRowRemove = function (tableName, toBeRemovedCriteria) {
var _this = this;
this.listeningTables[tableName].forEach(function (subscriptionName) {
var subscriptions = _this.handler.store.subscriptions[subscriptionName];
if (subscriptions !== undefined) {
subscriptions.forEach(function (subscription) {
if (_this.handler.isFindCursor(subscription.cursor)) {
var colName = subscription.cursor.collectionName;
if (_this.handler.dispatcher.handler.collections[colName].table === tableName) {
_this.handler.dispatcher.emitRemoveObject(subscription.subscriber, colName, toBeRemovedCriteria);
}
}
else if (_this.handler.isProcedureCursor(subscription.cursor)) {
subscription.cursor.collectionNames.forEach(function (colName) {
if (_this.handler.dispatcher.handler.collections[colName].table === tableName) {
_this.handler.dispatcher.emitRemoveObject(subscription.subscriber, colName, toBeRemovedCriteria);
}
});
}
});
}
});
};
TableEmmiter.prototype.checkJoinedAndFetch = function (table, objRow, serverCollectionOnlyCriteria) {
return new Promise(function (resolve, reject) {
var newCriteriaForOneObject = {};
if (serverCollectionOnlyCriteria !== undefined) {
var primaryKeyOfObjValue = objRow[node_mysql_wrapper_1.Helper.toObjectProperty(table.primaryKey)];
newCriteriaForOneObject[node_mysql_wrapper_1.Helper.toObjectProperty(table.primaryKey)] = primaryKeyOfObjValue;
node_mysql_wrapper_1.Helper.forEachKey(serverCollectionOnlyCriteria, function (key) {
if (objRow[key] === undefined) {
var joinedTable = serverCollectionOnlyCriteria[key];
if (node_mysql_wrapper_1.Helper.hasRules(joinedTable) && joinedTable[node_mysql_wrapper_1.TABLE_RULES_PROPERTY]["table"] !== undefined) {
newCriteriaForOneObject[key] = joinedTable;
}
}
});
table.findSingle(newCriteriaForOneObject).then(function (_result) {
resolve(_result);
});
}
else {
resolve(objRow);
}
});
};
return TableEmmiter;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = TableEmmiter;