Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow use of orderBy and where Query Constraints on multiple fields #9

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/lib/firebase/firestore/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>;
startAfter?: unknown;
startAt?: unknown;
where?: { fieldPath: string; filter: WhereFilterOp; value: unknown };
where?: Array<{ fieldPath: string; filter: WhereFilterOp; value: unknown }>;
}

export interface QueryConfig {
Expand Down
50 changes: 36 additions & 14 deletions src/lib/firebase/firestore/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
Expand Down