mysql-live
Version:
Brings the server.publish and client.subscribe for live updates on mysql database. The only one Live Collections.
110 lines (109 loc) • 4.41 kB
JavaScript
///<reference path="../../typings/node-mysql-wrapper/node-mysql-wrapper.d.ts"/>
/// <reference path="../../typings/socket.io/socket.io.d.ts" />
var Handler_1 = require("./Handler");
var _ = require("lodash");
var node_mysql_wrapper_1 = require("node-mysql-wrapper");
var socketIO = require("socket.io");
var MysqlLiveServer = (function () {
function MysqlLiveServer(dbOrString, ioOrHttpServer) {
if (_.isString(dbOrString)) {
dbOrString = node_mysql_wrapper_1.wrap(dbOrString);
}
if (ioOrHttpServer["sockets"] === undefined) {
ioOrHttpServer = socketIO(ioOrHttpServer);
}
var nsp;
if (MysqlLiveServer.LiveServerCount === 0) {
nsp = ioOrHttpServer.of(MysqlLiveServer.LiveServerSocketNamespace);
}
else {
nsp = ioOrHttpServer.of(MysqlLiveServer.LiveServerSocketNamespace + (MysqlLiveServer.LiveServerCount++));
}
this.handler = new Handler_1.default(nsp, dbOrString);
}
MysqlLiveServer.prototype.Collection = function (collectionName, tableName, isSingleItem) {
if (isSingleItem === void 0) { isSingleItem = false; }
var col = this.handler.getCollection(collectionName);
if (col === undefined) {
col = this.handler.registerCollection(collectionName, tableName, isSingleItem || false);
}
return col;
};
MysqlLiveServer.prototype.Object = function (objectName, tableName) {
return this.Collection(objectName, tableName, true);
};
MysqlLiveServer.prototype.publish = function (publicationName, cbOrCursorOrCollection) {
this.handler.store.registerPublication(publicationName, cbOrCursorOrCollection);
};
MysqlLiveServer.prototype.insecure = function (allowAllByDefault) {
if (this.handler !== undefined && this.handler.passport !== undefined) {
this.handler.passport.insecure(allowAllByDefault);
}
};
MysqlLiveServer.prototype.methods = function (_methods) {
this.handler.methods.registerMethods(_methods);
};
MysqlLiveServer.prototype.getPassport = function (socket) {
try {
return this.handler.passport.getPassport(socket);
}
catch (ex) {
return undefined;
}
};
MysqlLiveServer.prototype.getSocket = function (socketId) {
return this.handler.getSocket(socketId);
};
MysqlLiveServer.prototype.setPassport = function (socket, passportObj) {
if (this.handler !== undefined && this.handler.passport !== undefined) {
this.handler.passport.addDecryptedPassport(socket, passportObj);
}
};
MysqlLiveServer.prototype.call = function (methodName) {
var args = [];
for (var _i = 1; _i < arguments.length; _i++) {
args[_i - 1] = arguments[_i];
}
return this.handler.methods.callMethodFromServer(methodName, args);
};
MysqlLiveServer.prototype.table = function (tableName) {
if (this.handler !== undefined && this.handler.db !== undefined && this.handler.db.isReady) {
return this.handler.db.table(tableName);
}
return undefined;
};
MysqlLiveServer.prototype.criteriaFor = function (tableName) {
if (this.handler !== undefined && this.handler.db !== undefined && this.handler.db.isReady) {
return this.handler.db.criteriaFor(tableName);
}
return undefined;
};
MysqlLiveServer.prototype.procedure = function (procedureName, params, canInsertCb) {
return {
name: procedureName,
params: params,
__procedure__: true,
canInsert: canInsertCb,
collectionNames: []
};
};
Object.defineProperty(MysqlLiveServer.prototype, "engine", {
get: function () {
return this.handler.nsp.server;
},
enumerable: true,
configurable: true
});
Object.defineProperty(MysqlLiveServer.prototype, "database", {
get: function () {
return this.handler.db;
},
enumerable: true,
configurable: true
});
MysqlLiveServer.LiveServerCount = 0;
MysqlLiveServer.LiveServerSocketNamespace = "/mysql";
return MysqlLiveServer;
})();
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = MysqlLiveServer;