Created
October 1, 2020 10:12
-
-
Save knshiro/175bd5c2051de3d746babf9a92ba092d to your computer and use it in GitHub Desktop.
Firebase prefixed case sensitive query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import type firebase from "firebase"; | |
function findUpperBound(s: string): string { | |
if (!s.length) { | |
return s; | |
} | |
const c = String.fromCharCode(s.charCodeAt(s.length - 1) + 1); | |
return s.substring(0, s.length - 1) + c; | |
} | |
export function queryByPrefix( | |
query: firebase.firestore.Query<firebase.firestore.DocumentData>, | |
field: string, | |
prefix: string | |
): firebase.firestore.Query<firebase.firestore.DocumentData> { | |
return query | |
.where(field, ">=", prefix) | |
.where(field, "<", findUpperBound(prefix)) | |
.orderBy(field, "asc"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment