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

sum avg #7502

Merged
merged 14 commits into from
Oct 10, 2023
Merged

sum avg #7502

Show file tree
Hide file tree
Changes from 10 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
5 changes: 5 additions & 0 deletions .changeset/beige-oranges-eat.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@firebase/firestore": minor
MarkDuckworth marked this conversation as resolved.
Show resolved Hide resolved
---

Support sum and average aggregations.
21 changes: 20 additions & 1 deletion common/api-review/firestore-lite.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ export type AddPrefixToKeys<Prefix extends string, T extends Record<string, unkn

// @public
export class AggregateField<T> {
readonly aggregateType: AggregateType;
MarkDuckworth marked this conversation as resolved.
Show resolved Hide resolved
readonly type = "AggregateField";
}

// @public
export type AggregateFieldType = AggregateField<number | null>;
export function aggregateFieldEqual(left: AggregateField<unknown>, right: AggregateField<unknown>): boolean;

// @public
export type AggregateFieldType = ReturnType<typeof sum> | ReturnType<typeof average> | ReturnType<typeof count>;

// @public
export class AggregateQuerySnapshot<AggregateSpecType extends AggregateSpec, AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData> {
Expand All @@ -46,6 +50,9 @@ export type AggregateSpecData<T extends AggregateSpec> = {
[P in keyof T]: T[P] extends AggregateField<infer U> ? U : never;
};

// @public
export type AggregateType = 'count' | 'avg' | 'sum';

// @public
export function and(...queryConstraints: QueryFilterConstraint[]): QueryCompositeFilterConstraint;

Expand All @@ -55,6 +62,9 @@ export function arrayRemove(...elements: unknown[]): FieldValue;
// @public
export function arrayUnion(...elements: unknown[]): FieldValue;

// @public
export function average(field: string | FieldPath): AggregateField<number | null>;

// @public
export class Bytes {
static fromBase64String(base64: string): Bytes;
Expand Down Expand Up @@ -95,6 +105,9 @@ export function connectFirestoreEmulator(firestore: Firestore, host: string, por
mockUserToken?: EmulatorMockTokenOptions | string;
}): void;

// @public
export function count(): AggregateField<number>;

// @public
export function deleteDoc<AppModelType, DbModelType extends DocumentData>(reference: DocumentReference<AppModelType, DbModelType>): Promise<void>;

Expand Down Expand Up @@ -201,6 +214,9 @@ export class GeoPoint {
};
}

// @public
export function getAggregate<AggregateSpecType extends AggregateSpec, AppModelType, DbModelType extends DocumentData>(query: Query<AppModelType, DbModelType>, aggregateSpec: AggregateSpecType): Promise<AggregateQuerySnapshot<AggregateSpecType, AppModelType, DbModelType>>;

// @public
export function getCount<AppModelType, DbModelType extends DocumentData>(query: Query<AppModelType, DbModelType>): Promise<AggregateQuerySnapshot<{
count: AggregateField<number>;
Expand Down Expand Up @@ -388,6 +404,9 @@ export function startAt<AppModelType, DbModelType extends DocumentData>(snapshot
// @public
export function startAt(...fieldValues: unknown[]): QueryStartAtConstraint;

// @public
export function sum(field: string | FieldPath): AggregateField<number>;

// @public
export function terminate(firestore: Firestore): Promise<void>;

Expand Down
21 changes: 20 additions & 1 deletion common/api-review/firestore.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,15 @@ export type AddPrefixToKeys<Prefix extends string, T extends Record<string, unkn

// @public
export class AggregateField<T> {
readonly aggregateType: AggregateType;
readonly type = "AggregateField";
}

// @public
export type AggregateFieldType = AggregateField<number | null>;
export function aggregateFieldEqual(left: AggregateField<unknown>, right: AggregateField<unknown>): boolean;

// @public
export type AggregateFieldType = ReturnType<typeof sum> | ReturnType<typeof average> | ReturnType<typeof count>;

// @public
export class AggregateQuerySnapshot<AggregateSpecType extends AggregateSpec, AppModelType = DocumentData, DbModelType extends DocumentData = DocumentData> {
Expand All @@ -46,6 +50,9 @@ export type AggregateSpecData<T extends AggregateSpec> = {
[P in keyof T]: T[P] extends AggregateField<infer U> ? U : never;
};

// @public
export type AggregateType = 'count' | 'avg' | 'sum';

// @public
export function and(...queryConstraints: QueryFilterConstraint[]): QueryCompositeFilterConstraint;

Expand All @@ -55,6 +62,9 @@ export function arrayRemove(...elements: unknown[]): FieldValue;
// @public
export function arrayUnion(...elements: unknown[]): FieldValue;

// @public
export function average(field: string | FieldPath): AggregateField<number | null>;

// @public
export class Bytes {
static fromBase64String(base64: string): Bytes;
Expand Down Expand Up @@ -101,6 +111,9 @@ export function connectFirestoreEmulator(firestore: Firestore, host: string, por
mockUserToken?: EmulatorMockTokenOptions | string;
}): void;

// @public
export function count(): AggregateField<number>;

// @public
export function deleteDoc<AppModelType, DbModelType extends DocumentData>(reference: DocumentReference<AppModelType, DbModelType>): Promise<void>;

Expand Down Expand Up @@ -251,6 +264,9 @@ export class GeoPoint {
};
}

// @public
export function getAggregateFromServer<AggregateSpecType extends AggregateSpec, AppModelType, DbModelType extends DocumentData>(query: Query<AppModelType, DbModelType>, aggregateSpec: AggregateSpecType): Promise<AggregateQuerySnapshot<AggregateSpecType, AppModelType, DbModelType>>;

// @public
export function getCountFromServer<AppModelType, DbModelType extends DocumentData>(query: Query<AppModelType, DbModelType>): Promise<AggregateQuerySnapshot<{
count: AggregateField<number>;
Expand Down Expand Up @@ -644,6 +660,9 @@ export function startAt<AppModelType, DbModelType extends DocumentData>(snapshot
// @public
export function startAt(...fieldValues: unknown[]): QueryStartAtConstraint;

// @public
export function sum(field: string | FieldPath): AggregateField<number>;

// @public
export type TaskState = 'Error' | 'Running' | 'Success';

Expand Down
11 changes: 11 additions & 0 deletions docs-devsite/firestore_.aggregatefield.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,19 @@ export declare class AggregateField<T>

| Property | Modifiers | Type | Description |
| --- | --- | --- | --- |
| [aggregateType](./firestore_.aggregatefield.md#aggregatefieldaggregatetype) | | [AggregateType](./firestore_.md#aggregatetype) | Indicates the aggregation operation of this AggregateField. |
| [type](./firestore_.aggregatefield.md#aggregatefieldtype) | | (not declared) | A type string to uniquely identify instances of this class. |

## AggregateField.aggregateType

Indicates the aggregation operation of this AggregateField.

<b>Signature:</b>

```typescript
readonly aggregateType: AggregateType;
```

## AggregateField.type

A type string to uniquely identify instances of this class.
Expand Down
Loading
Loading