Skip to content

Commit 44c234c

Browse files
feat(firestore): add the parent expression (#9773)
* Add support for parent expression * Add documentation and fix formatting * Add changeset * Change path in tests as suggested by gemini auto review * Update docs-devsite/firestore_pipelines.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update docs-devsite/firestore_pipelines.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update docs-devsite/firestore_lite_pipelines.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update docs-devsite/firestore_lite_pipelines.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update docs-devsite/firestore_pipelines.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * Update docs-devsite/firestore_pipelines.md Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com> * fix formatting and documentation * Update documentation to make the return value clearer * remove the ts ignore notation * Remove beta notation and timeout for tests and regenerate documentation --------- Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
1 parent b2c4646 commit 44c234c

12 files changed

Lines changed: 313 additions & 0 deletions

File tree

.changeset/cold-moles-compare.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@firebase/firestore': minor
3+
'firebase': minor
4+
---
5+
6+
Add support for the parent expression

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ export abstract class Expression {
719719
/* Excluded from this release type: _readUserData */
720720
notEqualAny(arrayExpression: Expression): BooleanExpression;
721721
/* Excluded from this release type: _readUserData */
722+
parent(): FunctionExpression;
723+
/* Excluded from this release type: _readUserData */
722724
pow(exponent: Expression): FunctionExpression;
723725
/* Excluded from this release type: _readUserData */
724726
pow(exponent: number): FunctionExpression;
@@ -1206,6 +1208,14 @@ export class Ordering {
12061208
readonly expr: Expression;
12071209
}
12081210

1211+
// @public
1212+
function parent_2(documentPath: string | DocumentReference): FunctionExpression;
1213+
1214+
// @public
1215+
function parent_2(documentPathExpr: Expression): FunctionExpression;
1216+
1217+
export { parent_2 as parent }
1218+
12091219
// @public
12101220
export class Pipeline {
12111221
addFields(field: Selectable, ...additionalFields: Selectable[]): Pipeline;

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,6 +722,8 @@ export abstract class Expression {
722722
/* Excluded from this release type: _readUserData */
723723
notEqualAny(arrayExpression: Expression): BooleanExpression;
724724
/* Excluded from this release type: _readUserData */
725+
parent(): FunctionExpression;
726+
/* Excluded from this release type: _readUserData */
725727
pow(exponent: Expression): FunctionExpression;
726728
/* Excluded from this release type: _readUserData */
727729
pow(exponent: number): FunctionExpression;
@@ -1209,6 +1211,14 @@ export class Ordering {
12091211
readonly expr: Expression;
12101212
}
12111213

1214+
// @public
1215+
function parent_2(documentPath: string | DocumentReference): FunctionExpression;
1216+
1217+
// @public
1218+
function parent_2(documentPathExpr: Expression): FunctionExpression;
1219+
1220+
export { parent_2 as parent }
1221+
12121222
// @public (undocumented)
12131223
export class Pipeline {
12141224
addFields(field: Selectable, ...additionalFields: Selectable[]): Pipeline;

docs-devsite/firestore_lite_pipelines.expression.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export declare abstract class Expression
146146
| [notEqual(value)](./firestore_lite_pipelines.expression.md#expressionnotequal) | | Creates an expression that checks if this expression is not equal to a constant value. |
147147
| [notEqualAny(values)](./firestore_lite_pipelines.expression.md#expressionnotequalany) | | Creates an expression that checks if this expression is not equal to any of the provided values or expressions. |
148148
| [notEqualAny(arrayExpression)](./firestore_lite_pipelines.expression.md#expressionnotequalany) | | Creates an expression that checks if this expression is not equal to any of the values in the evaluated expression. |
149+
| [parent()](./firestore_lite_pipelines.expression.md#expressionparent) | | Creates an expression that returns the parent document reference of a document reference. |
149150
| [pow(exponent)](./firestore_lite_pipelines.expression.md#expressionpow) | | Creates an expression that returns the value of this expression raised to the power of another expression. |
150151
| [pow(exponent)](./firestore_lite_pipelines.expression.md#expressionpow) | | Creates an expression that returns the value of this expression raised to the power of a constant value. |
151152
| [regexContains(pattern)](./firestore_lite_pipelines.expression.md#expressionregexcontains) | | Creates an expression that checks if a string contains a specified regular expression as a substring. |
@@ -3475,6 +3476,30 @@ field("status").notEqualAny(field('rejectedStatuses'));
34753476

34763477
```
34773478
3479+
## Expression.parent()
3480+
3481+
Creates an expression that returns the parent document reference of a document reference.
3482+
3483+
<b>Signature:</b>
3484+
3485+
```typescript
3486+
parent(): FunctionExpression;
3487+
```
3488+
<b>Returns:</b>
3489+
3490+
[FunctionExpression](./firestore_lite_pipelines.functionexpression.md#functionexpression_class)
3491+
3492+
A new [Expression](./firestore_pipelines.expression.md#expression_class) representing the parent operation.
3493+
3494+
### Example
3495+
3496+
3497+
```typescript
3498+
// Get the parent document reference of a document reference.
3499+
field("__path__").parent();
3500+
3501+
```
3502+
34783503
## Expression.pow()
34793504
34803505
Creates an expression that returns the value of this expression raised to the power of another expression.

docs-devsite/firestore_lite_pipelines.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ https://github.com/firebase/firebase-js-sdk
6868
| [switchOn(condition, result, others)](./firestore_lite_pipelines.md#switchon_02b8caf) | Creates an expression that evaluates to the result corresponding to the first true condition. |
6969
| <b>function(documentPath, ...)</b> |
7070
| [documentId(documentPath)](./firestore_lite_pipelines.md#documentid_cef293c) | Creates an expression that returns the document ID from a path. |
71+
| [parent\_2(documentPath)](./firestore_lite_pipelines.md#parent_2_cef293c) | Creates an expression that returns the parent document reference of a document reference. |
7172
| <b>function(documentPathExpr, ...)</b> |
7273
| [documentId(documentPathExpr)](./firestore_lite_pipelines.md#documentid_9a69021) | Creates an expression that returns the document ID from a path. |
74+
| [parent\_2(documentPathExpr)](./firestore_lite_pipelines.md#parent_2_9a69021) | Creates an expression that returns the parent document reference of a document reference. |
7375
| <b>function(element, ...)</b> |
7476
| [notEqualAny(element, values)](./firestore_lite_pipelines.md#notequalany_c2c5bcb) | Creates an expression that checks if an expression is not equal to any of the provided values or expressions. |
7577
| [notEqualAny(element, arrayExpression)](./firestore_lite_pipelines.md#notequalany_16b2851) | Creates an expression that checks if an expression is not equal to any of the provided values or expressions. |
@@ -1889,6 +1891,37 @@ documentId(myDocumentReference);
18891891

18901892
```
18911893

1894+
### parent\_2(documentPath) {:#parent_2_cef293c}
1895+
1896+
Creates an expression that returns the parent document reference of a document reference.
1897+
1898+
<b>Signature:</b>
1899+
1900+
```typescript
1901+
declare function parent_2(documentPath: string | DocumentReference): FunctionExpression;
1902+
```
1903+
1904+
#### Parameters
1905+
1906+
| Parameter | Type | Description |
1907+
| --- | --- | --- |
1908+
| documentPath | string \| [DocumentReference](./firestore_lite.documentreference.md#documentreference_class) | A string path or DocumentReference to get the parent from. |
1909+
1910+
<b>Returns:</b>
1911+
1912+
[FunctionExpression](./firestore_lite_pipelines.functionexpression.md#functionexpression_class)
1913+
1914+
A new [Expression](./firestore_pipelines.expression.md#expression_class) representing the parent operation.
1915+
1916+
### Example
1917+
1918+
1919+
```typescript
1920+
// Get the parent document reference of a document reference.
1921+
parent(myDocumentReference);
1922+
1923+
```
1924+
18921925
## function(documentPathExpr, ...)
18931926

18941927
### documentId(documentPathExpr) {:#documentid_9a69021}
@@ -1922,6 +1955,37 @@ documentId(field("__path__"));
19221955

19231956
```
19241957

1958+
### parent\_2(documentPathExpr) {:#parent_2_9a69021}
1959+
1960+
Creates an expression that returns the parent document reference of a document reference.
1961+
1962+
<b>Signature:</b>
1963+
1964+
```typescript
1965+
declare function parent_2(documentPathExpr: Expression): FunctionExpression;
1966+
```
1967+
1968+
#### Parameters
1969+
1970+
| Parameter | Type | Description |
1971+
| --- | --- | --- |
1972+
| documentPathExpr | [Expression](./firestore_lite_pipelines.expression.md#expression_class) | An Expression evaluating to a document reference. |
1973+
1974+
<b>Returns:</b>
1975+
1976+
[FunctionExpression](./firestore_lite_pipelines.functionexpression.md#functionexpression_class)
1977+
1978+
A new [Expression](./firestore_pipelines.expression.md#expression_class) representing the parent operation.
1979+
1980+
### Example
1981+
1982+
1983+
```typescript
1984+
// Get the parent document reference of a document reference.
1985+
parent(field("__path__"));
1986+
1987+
```
1988+
19251989
## function(element, ...)
19261990

19271991
### notEqualAny(element, values) {:#notequalany_c2c5bcb}

docs-devsite/firestore_pipelines.expression.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ export declare abstract class Expression
146146
| [notEqual(value)](./firestore_pipelines.expression.md#expressionnotequal) | | Creates an expression that checks if this expression is not equal to a constant value. |
147147
| [notEqualAny(values)](./firestore_pipelines.expression.md#expressionnotequalany) | | Creates an expression that checks if this expression is not equal to any of the provided values or expressions. |
148148
| [notEqualAny(arrayExpression)](./firestore_pipelines.expression.md#expressionnotequalany) | | Creates an expression that checks if this expression is not equal to any of the values in the evaluated expression. |
149+
| [parent()](./firestore_pipelines.expression.md#expressionparent) | | Creates an expression that returns the parent document reference of a document reference. |
149150
| [pow(exponent)](./firestore_pipelines.expression.md#expressionpow) | | Creates an expression that returns the value of this expression raised to the power of another expression. |
150151
| [pow(exponent)](./firestore_pipelines.expression.md#expressionpow) | | Creates an expression that returns the value of this expression raised to the power of a constant value. |
151152
| [regexContains(pattern)](./firestore_pipelines.expression.md#expressionregexcontains) | | Creates an expression that checks if a string contains a specified regular expression as a substring. |
@@ -3475,6 +3476,30 @@ field("status").notEqualAny(field('rejectedStatuses'));
34753476

34763477
```
34773478
3479+
## Expression.parent()
3480+
3481+
Creates an expression that returns the parent document reference of a document reference.
3482+
3483+
<b>Signature:</b>
3484+
3485+
```typescript
3486+
parent(): FunctionExpression;
3487+
```
3488+
<b>Returns:</b>
3489+
3490+
[FunctionExpression](./firestore_pipelines.functionexpression.md#functionexpression_class)
3491+
3492+
A new [Expression](./firestore_pipelines.expression.md#expression_class) representing the parent operation.
3493+
3494+
### Example
3495+
3496+
3497+
```typescript
3498+
// Get the parent document reference of a document reference.
3499+
field("__path__").parent();
3500+
3501+
```
3502+
34783503
## Expression.pow()
34793504
34803505
Creates an expression that returns the value of this expression raised to the power of another expression.

docs-devsite/firestore_pipelines.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,10 @@ https://github.com/firebase/firebase-js-sdk
6868
| [switchOn(condition, result, others)](./firestore_pipelines.md#switchon_02b8caf) | Creates an expression that evaluates to the result corresponding to the first true condition. |
6969
| <b>function(documentPath, ...)</b> |
7070
| [documentId(documentPath)](./firestore_pipelines.md#documentid_cef293c) | Creates an expression that returns the document ID from a path. |
71+
| [parent\_2(documentPath)](./firestore_pipelines.md#parent_2_cef293c) | Creates an expression that returns the parent document reference of a document reference. |
7172
| <b>function(documentPathExpr, ...)</b> |
7273
| [documentId(documentPathExpr)](./firestore_pipelines.md#documentid_9a69021) | Creates an expression that returns the document ID from a path. |
74+
| [parent\_2(documentPathExpr)](./firestore_pipelines.md#parent_2_9a69021) | Creates an expression that returns the parent document reference of a document reference. |
7375
| <b>function(element, ...)</b> |
7476
| [notEqualAny(element, values)](./firestore_pipelines.md#notequalany_c2c5bcb) | Creates an expression that checks if an expression is not equal to any of the provided values or expressions. |
7577
| [notEqualAny(element, arrayExpression)](./firestore_pipelines.md#notequalany_16b2851) | Creates an expression that checks if an expression is not equal to any of the provided values or expressions. |
@@ -1894,6 +1896,37 @@ documentId(myDocumentReference);
18941896

18951897
```
18961898

1899+
### parent\_2(documentPath) {:#parent_2_cef293c}
1900+
1901+
Creates an expression that returns the parent document reference of a document reference.
1902+
1903+
<b>Signature:</b>
1904+
1905+
```typescript
1906+
declare function parent_2(documentPath: string | DocumentReference): FunctionExpression;
1907+
```
1908+
1909+
#### Parameters
1910+
1911+
| Parameter | Type | Description |
1912+
| --- | --- | --- |
1913+
| documentPath | string \| [DocumentReference](./firestore_.documentreference.md#documentreference_class) | A string path or DocumentReference to get the parent from. |
1914+
1915+
<b>Returns:</b>
1916+
1917+
[FunctionExpression](./firestore_pipelines.functionexpression.md#functionexpression_class)
1918+
1919+
A new [Expression](./firestore_pipelines.expression.md#expression_class) representing the parent operation.
1920+
1921+
### Example
1922+
1923+
1924+
```typescript
1925+
// Get the parent document reference of a document reference.
1926+
parent(myDocumentReference);
1927+
1928+
```
1929+
18971930
## function(documentPathExpr, ...)
18981931

18991932
### documentId(documentPathExpr) {:#documentid_9a69021}
@@ -1927,6 +1960,37 @@ documentId(field("__path__"));
19271960

19281961
```
19291962

1963+
### parent\_2(documentPathExpr) {:#parent_2_9a69021}
1964+
1965+
Creates an expression that returns the parent document reference of a document reference.
1966+
1967+
<b>Signature:</b>
1968+
1969+
```typescript
1970+
declare function parent_2(documentPathExpr: Expression): FunctionExpression;
1971+
```
1972+
1973+
#### Parameters
1974+
1975+
| Parameter | Type | Description |
1976+
| --- | --- | --- |
1977+
| documentPathExpr | [Expression](./firestore_pipelines.expression.md#expression_class) | An Expression evaluating to a document reference. |
1978+
1979+
<b>Returns:</b>
1980+
1981+
[FunctionExpression](./firestore_pipelines.functionexpression.md#functionexpression_class)
1982+
1983+
A new [Expression](./firestore_pipelines.expression.md#expression_class) representing the parent operation.
1984+
1985+
### Example
1986+
1987+
1988+
```typescript
1989+
// Get the parent document reference of a document reference.
1990+
parent(field("__path__"));
1991+
1992+
```
1993+
19301994
## function(element, ...)
19311995

19321996
### notEqualAny(element, values) {:#notequalany_c2c5bcb}

packages/firestore/lite/pipelines/pipelines.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ export {
112112
map,
113113
mod,
114114
documentId,
115+
parent,
115116
equal,
116117
notEqual,
117118
lessThan,

packages/firestore/src/api_pipelines.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ export {
163163
mapRemove,
164164
mapMerge,
165165
documentId,
166+
parent,
166167
substring,
167168
countDistinct,
168169
ceil,

packages/firestore/src/lite-api/expressions.ts

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2612,6 +2612,22 @@ export abstract class Expression implements ProtoValueSerializable, UserData {
26122612
return new FunctionExpression('document_id', [this], 'documentId');
26132613
}
26142614

2615+
/**
2616+
*
2617+
* Creates an expression that returns the parent document reference of a document reference.
2618+
*
2619+
* @example
2620+
* ```typescript
2621+
* // Get the parent document reference of a document reference.
2622+
* field("__path__").parent();
2623+
* ```
2624+
*
2625+
* @returns A new {@link @firebase/firestore/pipelines#Expression} representing the parent operation.
2626+
*/
2627+
parent(): FunctionExpression {
2628+
return new FunctionExpression('parent', [this], 'parent');
2629+
}
2630+
26152631
/**
26162632
*
26172633
* Creates an expression that returns a substring of the results of this expression.
@@ -4739,6 +4755,45 @@ export function documentId(
47394755
return documentPathExpr.documentId();
47404756
}
47414757

4758+
/**
4759+
*
4760+
* Creates an expression that returns the parent document reference of a document reference.
4761+
*
4762+
* @example
4763+
* ```typescript
4764+
* // Get the parent document reference of a document reference.
4765+
* parent(myDocumentReference);
4766+
* ```
4767+
*
4768+
* @param documentPath - A string path or DocumentReference to get the parent from.
4769+
* @returns A new {@link @firebase/firestore/pipelines#Expression} representing the parent operation.
4770+
*/
4771+
export function parent(
4772+
documentPath: string | DocumentReference
4773+
): FunctionExpression;
4774+
4775+
/**
4776+
*
4777+
* Creates an expression that returns the parent document reference of a document reference.
4778+
*
4779+
* @example
4780+
* ```typescript
4781+
* // Get the parent document reference of a document reference.
4782+
* parent(field("__path__"));
4783+
* ```
4784+
*
4785+
* @param documentPathExpr - An Expression evaluating to a document reference.
4786+
* @returns A new {@link @firebase/firestore/pipelines#Expression} representing the parent operation.
4787+
*/
4788+
export function parent(documentPathExpr: Expression): FunctionExpression;
4789+
4790+
export function parent(
4791+
documentPath: Expression | string | DocumentReference
4792+
): FunctionExpression {
4793+
const documentPathExpr = valueToDefaultExpr(documentPath);
4794+
return documentPathExpr.parent();
4795+
}
4796+
47424797
/**
47434798
*
47444799
* Creates an expression that returns a substring of a string or byte array.

0 commit comments

Comments
 (0)