Skip to content

Instantly share code, notes, and snippets.

@gaboelnuevo
Created September 19, 2018 07:23
add fullOuterJoin support to realm-query
RealmQuery.prototype.fullOuterJoin = function(query) {
const initialCriteria = this.criteria.slice();
// NOTE: We don't use arrow function because if the criteria of another RealmQuery is joined
// NOTE: we want 'this' instance to be used for the call such that values are pushed on to the correct instance.
if (initialCriteria.length) {
const q1 = function() {
return `(${initialCriteria
.map(wrapper => {
if (Array.isArray(wrapper)) {
return `(${wrapper
.map(_w => {
return _w.call(this);
})
.join(" ")})`;
}
return wrapper.call(this);
})
.join(" AND ")})`;
};
const q2 = function() {
return `(${query.criteria
.map(wrapper => {
if (Array.isArray(wrapper)) {
return `(${wrapper
.map(_w => {
return _w.call(this);
})
.join(" ")})`;
}
return wrapper.call(this);
})
.join(" AND ")})`;
};
const result = function() {
return [q1, q2]
.map(wrapper => {
return wrapper.call(this);
})
.join(" OR ");
};
this.criteria = [result];
} else {
this.criteria = query.criteria.slice();
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment