Skip to content

Commit cecd028

Browse files
authored
feat(firestore): Add support for pipeline subqueries (#9720)
1 parent 31bddcd commit cecd028

27 files changed

Lines changed: 3110 additions & 77 deletions

.changeset/ten-pugs-tie.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'firebase': minor
3+
'@firebase/firestore': minor
4+
---
5+
6+
Add subquery support to Pipelines.

common/api-review/firestore-lite-pipelines.api.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,20 @@ export function countDistinct(expr: Expression | string): AggregateFunction;
364364
// @public
365365
export function countIf(booleanExpr: BooleanExpression): AggregateFunction;
366366

367+
// @public
368+
export function currentDocument(): Expression;
369+
367370
// @public
368371
export function currentTimestamp(): FunctionExpression;
369372

370373
// @public
371374
export type DatabaseStageOptions = StageOptions & {};
372375

376+
// @public
377+
export type DefineStageOptions = StageOptions & {
378+
variables: AliasedExpression[];
379+
};
380+
373381
// @public
374382
export function descending(expr: Expression): Ordering;
375383

@@ -619,6 +627,8 @@ export abstract class Expression {
619627
/* Excluded from this release type: _readUserData */
620628
floor(): FunctionExpression;
621629
/* Excluded from this release type: _readUserData */
630+
getField(key: string | Expression): Expression;
631+
/* Excluded from this release type: _readUserData */
622632
greaterThan(expression: Expression): BooleanExpression;
623633
/* Excluded from this release type: _readUserData */
624634
greaterThan(value: unknown): BooleanExpression;
@@ -825,7 +835,7 @@ export abstract class Expression {
825835
}
826836

827837
// @public
828-
export type ExpressionType = 'Field' | 'Constant' | 'Function' | 'AggregateFunction' | 'ListOfExpressions' | 'AliasedExpression';
838+
export type ExpressionType = 'Field' | 'Constant' | 'Function' | 'AggregateFunction' | 'ListOfExpressions' | 'AliasedExpression' | 'Variable' | 'PipelineValue';
829839

830840
// @public
831841
export class Field extends Expression implements Selectable {
@@ -1202,6 +1212,8 @@ export class Pipeline {
12021212
addFields(options: AddFieldsStageOptions): Pipeline;
12031213
aggregate(accumulator: AliasedAggregate, ...additionalAccumulators: AliasedAggregate[]): Pipeline;
12041214
aggregate(options: AggregateStageOptions): Pipeline;
1215+
define(aliasedExpression: AliasedExpression, ...additionalExpressions: AliasedExpression[]): Pipeline;
1216+
define(options: DefineStageOptions): Pipeline;
12051217
distinct(group: string | Selectable, ...additionalGroups: Array<string | Selectable>): Pipeline;
12061218
distinct(options: DistinctStageOptions): Pipeline;
12071219
findNearest(options: FindNearestStageOptions): Pipeline;
@@ -1225,6 +1237,8 @@ export class Pipeline {
12251237
select(options: SelectStageOptions): Pipeline;
12261238
sort(ordering: Ordering, ...additionalOrderings: Ordering[]): Pipeline;
12271239
sort(options: SortStageOptions): Pipeline;
1240+
toArrayExpression(): Expression;
1241+
toScalarExpression(): Expression;
12281242
union(other: Pipeline): Pipeline;
12291243
union(options: UnionStageOptions): Pipeline;
12301244
unnest(selectable: Selectable, indexField?: string): Pipeline;
@@ -1496,6 +1510,17 @@ export function stringReverse(stringExpression: Expression): FunctionExpression;
14961510
// @public
14971511
export function stringReverse(field: string): FunctionExpression;
14981512

1513+
// @public
1514+
export function subcollection(path: string): Pipeline;
1515+
1516+
// @public
1517+
export function subcollection(options: SubcollectionStageOptions): Pipeline;
1518+
1519+
// @public
1520+
export type SubcollectionStageOptions = StageOptions & {
1521+
path: string;
1522+
};
1523+
14991524
// @public
15001525
export function substring(field: string, position: number, length?: number): FunctionExpression;
15011526

@@ -1544,7 +1569,7 @@ export function timestampAdd(timestamp: Expression, unit: TimeUnit, amount: numb
15441569
// @public
15451570
export function timestampAdd(fieldName: string, unit: TimeUnit, amount: number): FunctionExpression;
15461571

1547-
// @public
1572+
// @public (undocumented)
15481573
export function timestampDiff(endFieldName: string, startFieldName: string, unit: TimeUnit | Expression): FunctionExpression;
15491574

15501575
// @public
@@ -1678,6 +1703,9 @@ export type UnnestStageOptions = StageOptions & {
16781703
indexField?: string;
16791704
};
16801705

1706+
// @public
1707+
export function variable(name: string): Expression;
1708+
16811709
// @public
16821710
export function vectorLength(vectorExpression: Expression): FunctionExpression;
16831711

common/api-review/firestore-pipelines.api.md

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,12 +364,20 @@ export function countDistinct(expr: Expression | string): AggregateFunction;
364364
// @public
365365
export function countIf(booleanExpr: BooleanExpression): AggregateFunction;
366366

367+
// @public
368+
export function currentDocument(): Expression;
369+
367370
// @public
368371
export function currentTimestamp(): FunctionExpression;
369372

370373
// @public
371374
export type DatabaseStageOptions = StageOptions & {};
372375

376+
// @public
377+
export type DefineStageOptions = StageOptions & {
378+
variables: AliasedExpression[];
379+
};
380+
373381
// @public
374382
export function descending(expr: Expression): Ordering;
375383

@@ -622,6 +630,8 @@ export abstract class Expression {
622630
/* Excluded from this release type: _readUserData */
623631
floor(): FunctionExpression;
624632
/* Excluded from this release type: _readUserData */
633+
getField(key: string | Expression): Expression;
634+
/* Excluded from this release type: _readUserData */
625635
greaterThan(expression: Expression): BooleanExpression;
626636
/* Excluded from this release type: _readUserData */
627637
greaterThan(value: unknown): BooleanExpression;
@@ -828,7 +838,7 @@ export abstract class Expression {
828838
}
829839

830840
// @public
831-
export type ExpressionType = 'Field' | 'Constant' | 'Function' | 'AggregateFunction' | 'ListOfExpressions' | 'AliasedExpression';
841+
export type ExpressionType = 'Field' | 'Constant' | 'Function' | 'AggregateFunction' | 'ListOfExpressions' | 'AliasedExpression' | 'Variable' | 'PipelineValue';
832842

833843
// @public
834844
export class Field extends Expression implements Selectable {
@@ -1206,6 +1216,10 @@ export class Pipeline {
12061216
addFields(options: AddFieldsStageOptions): Pipeline;
12071217
aggregate(accumulator: AliasedAggregate, ...additionalAccumulators: AliasedAggregate[]): Pipeline;
12081218
aggregate(options: AggregateStageOptions): Pipeline;
1219+
// (undocumented)
1220+
define(aliasedExpression: AliasedExpression, ...additionalExpressions: AliasedExpression[]): Pipeline;
1221+
// (undocumented)
1222+
define(options: DefineStageOptions): Pipeline;
12091223
distinct(group: string | Selectable, ...additionalGroups: Array<string | Selectable>): Pipeline;
12101224
// (undocumented)
12111225
distinct(options: DistinctStageOptions): Pipeline;
@@ -1235,6 +1249,10 @@ export class Pipeline {
12351249
sort(ordering: Ordering, ...additionalOrderings: Ordering[]): Pipeline;
12361250
// (undocumented)
12371251
sort(options: SortStageOptions): Pipeline;
1252+
// (undocumented)
1253+
toArrayExpression(): Expression;
1254+
// (undocumented)
1255+
toScalarExpression(): Expression;
12381256
union(other: Pipeline): Pipeline;
12391257
// (undocumented)
12401258
union(options: UnionStageOptions): Pipeline;
@@ -1520,6 +1538,17 @@ export function stringReverse(stringExpression: Expression): FunctionExpression;
15201538
// @public
15211539
export function stringReverse(field: string): FunctionExpression;
15221540

1541+
// @public
1542+
export function subcollection(path: string): Pipeline;
1543+
1544+
// @public
1545+
export function subcollection(options: SubcollectionStageOptions): Pipeline;
1546+
1547+
// @public
1548+
export type SubcollectionStageOptions = StageOptions & {
1549+
path: string;
1550+
};
1551+
15231552
// @public
15241553
export function substring(field: string, position: number, length?: number): FunctionExpression;
15251554

@@ -1568,7 +1597,7 @@ export function timestampAdd(timestamp: Expression, unit: TimeUnit, amount: numb
15681597
// @public
15691598
export function timestampAdd(fieldName: string, unit: TimeUnit, amount: number): FunctionExpression;
15701599

1571-
// @public
1600+
// @public (undocumented)
15721601
export function timestampDiff(endFieldName: string, startFieldName: string, unit: TimeUnit | Expression): FunctionExpression;
15731602

15741603
// @public
@@ -1702,6 +1731,9 @@ export type UnnestStageOptions = StageOptions & {
17021731
indexField?: string;
17031732
};
17041733

1734+
// @public
1735+
export function variable(name: string): Expression;
1736+
17051737
// @public
17061738
export function vectorLength(vectorExpression: Expression): FunctionExpression;
17071739

docs-devsite/firestore_lite_pipelines.expression.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export declare abstract class Expression
100100
| [exp()](./firestore_lite_pipelines.expression.md#expressionexp) | | Creates an expression that computes e to the power of this expression. |
101101
| [first()](./firestore_lite_pipelines.expression.md#expressionfirst) | | Creates an aggregation that finds the first value of an expression across multiple stage inputs. |
102102
| [floor()](./firestore_lite_pipelines.expression.md#expressionfloor) | | Creates an expression that computes the floor of a numeric value. |
103+
| [getField(key)](./firestore_lite_pipelines.expression.md#expressiongetfield) | | Creates an expression that returns the value of a field from the document that results from the evaluation of this expression. |
103104
| [greaterThan(expression)](./firestore_lite_pipelines.expression.md#expressiongreaterthan) | | Creates an expression that checks if this expression is greater than another expression. |
104105
| [greaterThan(value)](./firestore_lite_pipelines.expression.md#expressiongreaterthan) | | Creates an expression that checks if this expression is greater than a constant value. |
105106
| [greaterThanOrEqual(expression)](./firestore_lite_pipelines.expression.md#expressiongreaterthanorequal) | | Creates an expression that checks if this expression is greater than or equal to another expression. |
@@ -2096,6 +2097,37 @@ field("price").floor();
20962097
20972098
```
20982099

2100+
## Expression.getField()
2101+
2102+
Creates an expression that returns the value of a field from the document that results from the evaluation of this expression.
2103+
2104+
<b>Signature:</b>
2105+
2106+
```typescript
2107+
getField(key: string | Expression): Expression;
2108+
```
2109+
2110+
#### Parameters
2111+
2112+
| Parameter | Type | Description |
2113+
| --- | --- | --- |
2114+
| key | string \| [Expression](./firestore_lite_pipelines.expression.md#expression_class) | The field to access in the document. |
2115+
2116+
<b>Returns:</b>
2117+
2118+
[Expression](./firestore_lite_pipelines.expression.md#expression_class)
2119+
2120+
A new `Expression` representing the value of the field in the document.
2121+
2122+
### Example
2123+
2124+
2125+
```typescript
2126+
// Get the value of the "city" field in the "address" document.
2127+
field("address").getField("city")
2128+
2129+
```
2130+
20992131
## Expression.greaterThan()
21002132

21012133
Creates an expression that checks if this expression is greater than another expression.

0 commit comments

Comments
 (0)