"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
var _ = require("lodash");
var RealmQuery = (function () {
function RealmQuery(objects) {
this.criteria = [];
this.inGroup = false;
this.objects = objects;
}
RealmQuery.prototype.addCriteria = function (critera) {
if (this.inGroup) {
var lastIndex = this.criteria.length - 1;
var currentGroup = this.criteria[lastIndex];
if (!currentGroup || !_.isArray(currentGroup)) {
this.criteria.push([]);
lastIndex += 1;
}
this.criteria[lastIndex].push(critera);
}
else {
this.criteria.push(critera);
}
return this;
};
RealmQuery.prototype.toString = function () {
var criteraStr = [];
for (var i in this.criteria) {
if (_.isString(this.criteria[i])) {
if (this.criteria[i] !== 'NOT') {
criteraStr.push(this.criteria[i]);
}
}
else {
criteraStr.push('(' + this.criteria[i].join(' ') + ')');
}
}
if (this.criteria.indexOf('NOT') > -1) {
criteraStr = ["NOT(" + criteraStr.join(' AND ') + ")"];
}
return criteraStr.join(' AND ');
};
RealmQuery.prototype.getFilteredObjects = function () {
var query = this.toString();
var results = this.objects;
if (query) {
results = results.filtered(query);
}
return results;
};
RealmQuery.prototype.average = function (fieldName) {
var results = this.getFilteredObjects();
return _.sumBy(_.toArray(results), fieldName) / results.length;
};
RealmQuery.prototype.beginGroup = function () {
this.inGroup = true;
return this;
};
RealmQuery.prototype.beginsWith = function (fieldName, value, casing) {
var op = casing ? 'BEGINSWITH[c]' : 'BEGINSWITH';
Eif (_.isString(value))
value = "\"" + value + "\"";
return this.addCriteria(fieldName + " " + op + " " + value);
};
RealmQuery.prototype.between = function (fieldName, from, to) {
return this.addCriteria(fieldName + " >= " + from + " AND " + fieldName + " <= " + to);
};
RealmQuery.prototype.contains = function (fieldName, value, casing) {
var op = casing ? 'CONTAINS[c]' : 'CONTAINS';
Eif (_.isString(value))
value = "\"" + value + "\"";
return this.addCriteria(fieldName + " " + op + " " + value);
};
RealmQuery.prototype.count = function () {
var results = this.getFilteredObjects();
return results.length;
};
RealmQuery.prototype.distinct = function (fieldName) {
var results = this.getFilteredObjects();
return _.uniqBy(_.toArray(results), fieldName);
};
RealmQuery.prototype.endGroup = function () {
this.inGroup = false;
return this;
};
RealmQuery.prototype.endsWith = function (fieldName, value, casing) {
var op = casing ? 'ENDSWITH[c]' : 'ENDSWITH';
Eif (_.isString(value))
value = "\"" + value + "\"";
return this.addCriteria(fieldName + " " + op + " " + value);
};
RealmQuery.prototype.equalTo = function (fieldName, value) {
if (_.isString(value))
value = "\"" + value + "\"";
return this.addCriteria(fieldName + " == " + value);
};
RealmQuery.prototype.findAll = function () {
var results = this.getFilteredObjects();
if (this.sortField) {
results = results.sorted(this.sortField, this.sortReverse);
}
return results;
};
RealmQuery.prototype.findFirst = function () {
var results = this.getFilteredObjects();
return results.length ? results[0] : undefined;
};
RealmQuery.prototype.greaterThan = function (fieldName, value) {
return this.addCriteria(fieldName + " > " + value);
};
RealmQuery.prototype.greaterThanOrEqualTo = function (fieldName, value) {
return this.addCriteria(fieldName + " >= " + value);
};
RealmQuery.prototype.in = function (fieldName, values) {
var criteria = [];
for (var i in values) {
var value = values[i];
if (_.isString(value)) {
value = "\"" + value + "\"";
}
criteria.push(fieldName + " == " + value);
}
return this.addCriteria('(' + criteria.join(' OR ') + ')');
};
RealmQuery.prototype.isEmpty = function (fieldName) {
throw new Error('Not yet supported "isEmpty"');
};
RealmQuery.prototype.isNotEmpty = function (fieldName) {
throw new Error('Not yet supported "isNotEmpty"');
};
RealmQuery.prototype.isNotNull = function (fieldName) {
throw new Error('Not yet supported "isNotNull"');
};
RealmQuery.prototype.isNull = function (fieldName) {
throw new Error('Not yet supported "isNull"');
};
RealmQuery.prototype.lessThan = function (fieldName, value) {
return this.addCriteria(fieldName + " < " + value);
};
RealmQuery.prototype.lessThanOrEqualTo = function (fieldName, value) {
return this.addCriteria(fieldName + " <= " + value);
};
RealmQuery.prototype.like = function (fieldName, value) {
throw new Error('Not yet supported "like"');
};
RealmQuery.prototype.max = function (fieldName) {
var results = this.getFilteredObjects();
return _.maxBy(_.toArray(results), fieldName);
};
RealmQuery.prototype.min = function (fieldName) {
var results = this.getFilteredObjects();
return _.minBy(_.toArray(results), fieldName);
};
RealmQuery.prototype.not = function () {
return this.addCriteria('NOT');
};
RealmQuery.prototype.notEqualTo = function (fieldName, value) {
if (_.isString(value))
value = "\"" + value + "\"";
return this.addCriteria(fieldName + " <> " + value);
};
RealmQuery.prototype.and = function () {
return this.addCriteria('AND');
};
RealmQuery.prototype.or = function () {
return this.addCriteria('OR');
};
RealmQuery.prototype.sum = function (fieldName) {
var results = this.getFilteredObjects();
return _.sumBy(_.toArray(results), fieldName);
};
RealmQuery.prototype.sort = function (fieldName, order) {
this.sortField = fieldName;
this.sortReverse = order === 'DESC' ? true : false;
return this;
};
/**
* Combine to another query
*/
RealmQuery.prototype.join = function (query) {
return this.addCriteria(query.toString());
};
RealmQuery.where = function (objects) {
return new RealmQuery(objects);
};
RealmQuery.create = function (objects) {
return new RealmQuery(objects);
};
return RealmQuery;
}());
exports.default = RealmQuery;
//# sourceMappingURL=realm-query.js.map |