mysql-live
Version:
Brings the server.publish and client.subscribe for live updates on mysql database. The only one Live Collections.
101 lines (100 loc) • 4.34 kB
JavaScript
/* Handler deals with socket, Receiver and Dispatcher. */
/* Receiver Accepts from client and do server side actions. */
/* Dispatcher Accepts from server, and push to the clients.*/
/* The Handler is a middleware between receiver,dispatcher,tableemmiter. */
/* Both Receiver and Dispatcher will have reference to the Handler class because:
The Handler is the only one class will have the nsp(Namespace) reference (nsp.on,nsp.emit).*/
/* Handler is also the only class which keep the collections dictionary, just that, nothing here to do with collections. */
/* TableEmmiter listens to server's database's tables events and push emits to the Dispatcher, then Dispatcher push these to the clients.*/
/* Handler initializes the Receiver, Dispatcher, TableEmmiter and the SocketPassport. */
/* Handler is initialized by the MysqlLiveServer. */
var Collection_1 = require("./Collection");
var Dispatcher_1 = require("./Dispatcher");
var TableEmmiter_1 = require("./TableEmmiter");
var Receiver_1 = require("./Receiver");
var SocketPassport_1 = require("./SocketPassport");
var LiveStore_1 = require("./LiveStore");
var MethodStore_1 = require("./MethodStore");
var node_mysql_wrapper_1 = require("node-mysql-wrapper");
var _ = require("lodash");
var Handler = (function () {
function Handler(nsp, db) {
var _this = this;
this.collections = {};
this.nsp = nsp;
this.store = new LiveStore_1.LiveStore(this);
this.methods = new MethodStore_1.MethodStore(this);
this.passport = new SocketPassport_1.default(this);
this.dispatcher = new Dispatcher_1.default(this, db);
this.db = db;
db.ready(function () {
_this.tableEmmiter = new TableEmmiter_1.default(_this);
_this.receiver = new Receiver_1.default(_this);
node_mysql_wrapper_1.Helper.forEachKey(_this.collections, function (colName) {
var col = _this.collections[colName];
col.setDatabaseTable(_this.db.table(col.table));
});
console.log("[MySQL] mysql-live has started.");
});
}
Handler.prototype.getCollection = function (collectionName) {
return this.collections[collectionName];
};
Handler.prototype.getCollectionsByTable = function (tableName) {
var _this = this;
var cols = [];
node_mysql_wrapper_1.Helper.forEachKey(this.collections, function (collectionName) {
var col = _this.collections[collectionName];
if (col.table === tableName) {
cols.push(col);
}
});
return cols;
};
Handler.prototype.getCollectionsByPrimaryKey = function (primaryKeyColumnName) {
var _this = this;
var cols = [];
node_mysql_wrapper_1.Helper.forEachKey(this.collections, function (collectionName) {
var col = _this.collections[collectionName];
if (col.primaryKeyColumn === primaryKeyColumnName) {
cols.push(col);
}
});
return cols;
};
Handler.prototype._registerCollection = function (collection) {
if (this.receiver !== undefined) {
collection.setDatabaseTable(this.db.table(collection.table));
}
this.collections[collection.name] = collection;
};
Handler.prototype.registerCollection = function (collectionName, tableName, isSingleItem) {
var _col = new Collection_1.Collection(collectionName, tableName, isSingleItem);
this._registerCollection(_col);
return _col;
};
Handler.prototype.getSocket = function (socketId) {
if (_.isString(socketId)) {
return this.nsp.connected[socketId];
}
else {
return socketId;
}
};
Handler.prototype.isProcedureCursor = function (cursor) {
return this.store.isProcedureCursor(cursor);
};
Handler.prototype.isFindCursor = function (cursor) {
return this.store.isFindCursor(cursor);
};
Object.defineProperty(Handler.prototype, "subscriptions", {
get: function () {
return this.store.subscriptions;
},
enumerable: true,
configurable: true
});
return Handler;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = Handler;