Skip to content

Instantly share code, notes, and snippets.

@yingray
Last active March 7, 2019 16:43
Show Gist options
  • Save yingray/2c068ebee0beb5168f206d164433649f to your computer and use it in GitHub Desktop.
Save yingray/2c068ebee0beb5168f206d164433649f to your computer and use it in GitHub Desktop.
Firebase ref with pagination for firebase-server
// obj === parsed.d.b.q
const paginateRef = (ref, obj = {}) => {
Object.entries(obj).forEach(([key, value]) => {
switch (key) {
case "sp": {
ref = ref.startAt(value);
break;
}
case "ep": {
ref = ref.endAt(value);
break;
}
case "i": {
if (value === ".key") {
ref = ref.orderByKey();
} else if (value === ".value") {
ref = ref.orderByValue();
} else {
ref = ref.orderByChild(value);
}
break;
}
case "l": {
ref = obj.vf === "l" ? ref.limitToFirst(value) : ref.limitToLast(value);
break;
}
default: {
break;
}
}
});
return ref;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment