From 05fabcf891ef2c53831eebacb9eb4524915c8c3b Mon Sep 17 00:00:00 2001 From: GogoVega <92022724+GogoVega@users.noreply.github.com> Date: Fri, 16 Aug 2024 11:55:21 +0200 Subject: [PATCH 1/2] Allow use of Range Constraints on multiple fields --- src/lib/firebase/firestore/types.ts | 4 +-- src/lib/firebase/firestore/utils.ts | 51 +++++++++++++++++++++-------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/src/lib/firebase/firestore/types.ts b/src/lib/firebase/firestore/types.ts index 5a64792..4dc7f9b 100644 --- a/src/lib/firebase/firestore/types.ts +++ b/src/lib/firebase/firestore/types.ts @@ -44,12 +44,12 @@ export interface Constraint { endBefore?: unknown; limitToFirst?: number; limitToLast?: number; - orderBy?: { fieldPath: string; direction?: OrderByDirection }; + orderBy?: Array<{ fieldPath: string; direction?: OrderByDirection }>; offset?: number; select?: string | Array; startAfter?: unknown; startAt?: unknown; - where?: { fieldPath: string; filter: WhereFilterOp; value: unknown }; + where?: Array<{ fieldPath: string; filter: WhereFilterOp; value: unknown }>; } export interface QueryConfig { diff --git a/src/lib/firebase/firestore/utils.ts b/src/lib/firebase/firestore/utils.ts index af4726f..bb340fe 100644 --- a/src/lib/firebase/firestore/utils.ts +++ b/src/lib/firebase/firestore/utils.ts @@ -107,16 +107,27 @@ function applyQueryConstraints(constraints: Constraint = {}, query?: AdminQuery) throw new Error("Offset Query Constraint not available for this SDK."); } break; - case "orderBy": - if (typeof value.fieldPath !== "string") - throw new TypeError(`The fieldPath value of the "${method}" constraint must be a string!`); + case "orderBy": { + if (typeof value !== "object") throw new TypeError(`The value of the "${method}" constraint must be an array!`); - if (query) { - query.orderBy(value.fieldPath, value.direction); - } else { - queryConstraints.push(orderBy(value.fieldPath, value.direction)); + let valArray = value; + if (!Array.isArray(value)) { + valArray = [value]; } + + valArray.forEach((val) => { + if (typeof val.fieldPath !== "string") + throw new TypeError(`The fieldPath value of the "${method}" constraint must be a string!`); + + if (query) { + query.orderBy(val.fieldPath, val.direction); + } else { + queryConstraints.push(orderBy(val.fieldPath, val.direction)); + } + }); + break; + } case "select": if (typeof value === "string" || (typeof value === "object" && value && Array.isArray(value))) { if (query) { @@ -128,16 +139,27 @@ function applyQueryConstraints(constraints: Constraint = {}, query?: AdminQuery) throw new TypeError(`The value of the "${method}" constraint must be a string or a string array!`); } break; - case "where": - if (typeof value.fieldPath !== "string") - throw new TypeError(`The fieldPath value of the "${method}" constraint must be a string!`); + case "where": { + if (typeof value !== "object") throw new TypeError(`The value of the "${method}" constraint must be an array!`); - if (query) { - query.where(value.fieldPath, value.filter, value.value); - } else { - queryConstraints.push(where(value.fieldPath, value.filter, value.value)); + let valArray = value; + if (!Array.isArray(value)) { + valArray = [value]; } + + valArray.forEach((val) => { + if (typeof val.fieldPath !== "string") + throw new TypeError(`The fieldPath value of the "${method}" constraint must be a string!`); + + if (query) { + query.where(val.fieldPath, val.filter, val.value); + } else { + queryConstraints.push(where(val.fieldPath, val.filter, val.value)); + } + }); + break; + } default: continue; } @@ -311,3 +333,4 @@ class SpecialFieldValue { } export { applyQueryConstraints, documentFrom, queryFrom, Snapshot, SpecialFieldValue }; +//export { FieldValue } from "firebase-admin/firestore" From 9a7e55d82abf7838adbed8ce3f135f0726127bd9 Mon Sep 17 00:00:00 2001 From: GogoVega <92022724+GogoVega@users.noreply.github.com> Date: Fri, 16 Aug 2024 12:02:29 +0200 Subject: [PATCH 2/2] Clean up comments --- src/lib/firebase/firestore/utils.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/lib/firebase/firestore/utils.ts b/src/lib/firebase/firestore/utils.ts index bb340fe..69c961b 100644 --- a/src/lib/firebase/firestore/utils.ts +++ b/src/lib/firebase/firestore/utils.ts @@ -333,4 +333,3 @@ class SpecialFieldValue { } export { applyQueryConstraints, documentFrom, queryFrom, Snapshot, SpecialFieldValue }; -//export { FieldValue } from "firebase-admin/firestore"