mysql-live
Version:
Brings the server.publish and client.subscribe for live updates on mysql database. The only one Live Collections.
100 lines (99 loc) • 4.45 kB
JavaScript
var _ = require("lodash");
var node_mysql_wrapper_1 = require("node-mysql-wrapper");
var Collection = (function () {
function Collection(collectionName, tableName, isJustOneObject) {
this.options = {};
this.disallowOptions = [];
this.__single_item_collection__ = false;
this.name = collectionName;
this.table = tableName;
if (isJustOneObject)
this.__single_item_collection__ = isJustOneObject;
}
Collection.prototype.isObject = function () {
return this.__single_item_collection__;
};
Collection.prototype.setDatabaseTable = function (table) {
this.databaseTable = table;
this.primaryKeyColumn = table.primaryKey;
};
Collection.prototype.allow = function (options) {
_.merge(this.options, options);
};
Collection.prototype.deny = function (options) {
var _this = this;
this.allow(options);
node_mysql_wrapper_1.Helper.forEachKey(options, function (optionKey) {
var indexExists = _this.disallowOptions.indexOf(optionKey);
if (indexExists !== -1) {
_this.disallowOptions = _this.disallowOptions.splice(indexExists, 1);
}
_this.disallowOptions.push(optionKey);
});
};
Collection.prototype.disallow = function (options) {
this.deny(options);
};
Collection.prototype.processCriteria = function (criteria, single) {
if (single === void 0) { single = false; }
var newCriteria = {};
if (criteria !== undefined) {
newCriteria = node_mysql_wrapper_1.Helper.copyObject(criteria);
if (_.isString(newCriteria) || _.isNumber(newCriteria)) {
newCriteria = {};
newCriteria[this.primaryKeyColumn] = newCriteria;
}
else if (criteria["build"] !== undefined) {
newCriteria = criteria["build"]();
if (single && newCriteria[node_mysql_wrapper_1.TABLE_RULES_PROPERTY] !== undefined && newCriteria[node_mysql_wrapper_1.TABLE_RULES_PROPERTY]['limit'] === undefined) {
newCriteria[node_mysql_wrapper_1.TABLE_RULES_PROPERTY]["limit"] = 1;
}
}
else {
if (this.__single_item_collection__ === true) {
if (newCriteria[node_mysql_wrapper_1.TABLE_RULES_PROPERTY] === undefined) {
newCriteria[node_mysql_wrapper_1.TABLE_RULES_PROPERTY] = {};
}
newCriteria[node_mysql_wrapper_1.TABLE_RULES_PROPERTY]["limit"] = 1;
}
}
}
return newCriteria;
};
Collection.prototype.find = function (criteria) {
var newCriteria = this.processCriteria(criteria, this.__single_item_collection__);
this.lastFindCriteria = newCriteria;
return { collectionName: this.name, criteria: newCriteria || {}, __find__: true };
};
Collection.prototype.findSingle = function (criteria) {
var newCriteria = this.processCriteria(criteria, true);
this.lastFindCriteria = newCriteria;
return { collectionName: this.name, criteria: newCriteria || {}, __find__: true };
};
Collection.prototype.noop = function () {
return { collectionName: this.name, criteria: undefined, __find__: true };
};
Collection.prototype.selector = function () {
return this.databaseTable.criteria;
};
Collection.prototype.fetch = function (criteriaRawJsObject, callback) {
return this.databaseTable.find(criteriaRawJsObject, callback);
};
Collection.prototype.fetchSingle = function (criteriaRawJsObject, callback) {
return this.databaseTable.findSingle(criteriaRawJsObject, callback);
};
Collection.prototype.save = function (criteriaRawJsObject, callback) {
return this.databaseTable.save(criteriaRawJsObject, callback);
};
Collection.prototype.insert = function (criteriaRawJsObject, callback) {
return this.save(criteriaRawJsObject, callback);
};
Collection.prototype.update = function (criteriaRawJsObject, callback) {
return this.save(criteriaRawJsObject, callback);
};
Collection.prototype.remove = function (criteriaOrID, callback) {
return this.databaseTable.remove(criteriaOrID, callback);
};
return Collection;
})();
exports.Collection = Collection;