Created
August 18, 2023 20:28
-
-
Save jose-almir/ddd9d8d51a7078283a921a8be2e265ab to your computer and use it in GitHub Desktop.
Filtering proof of concept pure firebase
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
export interface Criteria { | |
field: string; | |
operation: string; | |
value: any; | |
} |
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 { Criteria } from "./Criteria"; | |
import { Ordering } from "./Ordering"; | |
import { Pagination } from "./Pagination"; | |
import { PaginationResult } from "./PaginationResult"; | |
export interface FilteringService<T> { | |
filter(criterias: Criteria[], ordering?: Ordering, pagination?: Pagination): Promise<PaginationResult<T>>; | |
} |
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 { Influencer } from "../../models/Influencer"; | |
import { FilteringService } from "./FilteringService"; | |
import { | |
CollectionReference, | |
Firestore, | |
Query, | |
WhereFilterOp, | |
} from "firebase-admin/firestore"; | |
import { Criteria } from "./Criteria"; | |
import { PaginationResult } from "./PaginationResult"; | |
import { Ordering } from "./Ordering"; | |
import { Pagination } from "./Pagination"; | |
export class FirebaseFilteringService implements FilteringService<Influencer> { | |
private readonly INEQUALITY_OPERATORS = ["<", "<=", ">", ">=", "!="]; | |
private readonly collection: CollectionReference; | |
constructor(collection: string, private db: Firestore) { | |
this.collection = this.db.collection(collection); | |
} | |
async filter<T>( | |
criterias: Criteria[], | |
ordering?: Ordering, | |
pagination: Pagination = { limit: 10, nextToken: null } | |
): Promise<PaginationResult<T>> { | |
let query: Query = this.collection; | |
const inequalityCriterias = new Set( | |
criterias | |
.filter((c) => this.INEQUALITY_OPERATORS.includes(c.operation)) | |
.map((c) => c.field) | |
); | |
for (const field of inequalityCriterias) { | |
query = query.orderBy(field); | |
} | |
if (ordering) query = query.orderBy(ordering.field, ordering.direction); | |
for (const c of criterias) { | |
query = query.where(c.field, c.operation as WhereFilterOp, c.value); | |
} | |
if (pagination.nextToken) { | |
const nextDoc = await this.collection.doc(pagination.nextToken).get(); | |
query = query.startAfter(nextDoc); | |
} | |
if (pagination.limit) { | |
query = query.limit(pagination.limit); | |
} | |
const result = await query.get(); | |
return { | |
results: result.docs.map((d: any) => d.data() as T), | |
total: result.size, | |
nextToken: | |
result.size < pagination.limit | |
? null | |
: result.docs[result.docs.length - 1].id, | |
}; | |
} | |
} |
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
export interface Ordering { | |
field: string; | |
direction: "asc" | "desc"; | |
} |
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
export interface Pagination { | |
limit: number; | |
nextToken: string | null; | |
} |
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
export interface PaginationResult<T> { | |
results: T[]; | |
total: number; | |
nextToken: string | null; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment