@@ -29,6 +29,7 @@ import {
2929} from './pipeline-util' ;
3030import { HasUserData , Serializer , validateUserInput } from '../serializer' ;
3131import { cast } from '../util' ;
32+ import { Pipeline } from './pipelines' ;
3233import { GeoPoint } from '../geo-point' ;
3334import { OptionsUtil } from './options-util' ;
3435
@@ -3075,6 +3076,22 @@ export abstract class Expression
30753076 ] ) . asBoolean ( ) ;
30763077 }
30773078
3079+ /**
3080+ * Creates an expression that returns the value of a field from the document that results from the evaluation of this expression.
3081+ *
3082+ * @example
3083+ * ```typescript
3084+ * // Get the value of the "city" field in the "address" document.
3085+ * field("address").getField("city")
3086+ * ```
3087+ *
3088+ * @param key The field to access in the document.
3089+ * @returns A new `Expression` representing the value of the field in the document.
3090+ */
3091+ getField ( key : string | Expression ) : Expression {
3092+ return new FunctionExpression ( 'get_field' , [ this , valueToDefaultExpr ( key ) ] ) ;
3093+ }
3094+
30783095 // /**
30793096 // * Evaluates if the result of this `expression` is between
30803097 // * the `lowerBound` (inclusive) and `upperBound` (inclusive).
@@ -10412,6 +10429,184 @@ export function isType(
1041210429 return fieldOrExpression ( fieldNameOrExpression ) . isType ( type ) ;
1041310430}
1041410431
10432+ /**
10433+ * Creates an expression that gets a field from this map (object).
10434+ *
10435+ * @example
10436+ * ```typescript
10437+ * // Get the value of the "city" field in the "address" document.
10438+ * getField(field("address"), "city")
10439+ * ```
10440+ *
10441+ * @param expression The expression evaluating to the map from which the field will be extracted.
10442+ * @param key The field to access in the document.
10443+ * @returns A new `Expression` representing the value of the field in the document.
10444+ */
10445+ export function getField ( expression : Expression , key : string ) : Expression ;
10446+ /**
10447+ * Creates an expression that gets a field from this map (object).
10448+ *
10449+ * @example
10450+ * ```typescript
10451+ * // Get the value of the "city" field in the "address" document.
10452+ * getField(field("address"), "city")
10453+ * ```
10454+ *
10455+ * @param expression The expression evaluating to the map from which the field will be extracted.
10456+ * @param keyExpr The expression representing the key to access in the document.
10457+ * @returns A new `Expression` representing the value of the field in the document.
10458+ */
10459+ export function getField (
10460+ expression : Expression ,
10461+ keyExpr : Expression ,
10462+ ) : Expression ;
10463+ /**
10464+ * Creates an expression that returns the value of a field from the document with the given field name.
10465+ *
10466+ * @example
10467+ * ```typescript
10468+ * // Get the value of the "city" field in the "address" document.
10469+ * getField("address", "city")
10470+ * ```
10471+ *
10472+ * @param fieldName The name of the field containing the map/document.
10473+ * @param key The key to access.
10474+ * @returns A new `Expression` representing the value of the field in the document.
10475+ */
10476+ export function getField ( fieldName : string , key : string ) : Expression ;
10477+ /**
10478+ * Creates an expression that returns the value of a field from the document with the given field name.
10479+ *
10480+ * @example
10481+ * ```typescript
10482+ * // Get the value of the "city" field in the "address" document.
10483+ * getField("address", variable("addressField"))
10484+ * ```
10485+ *
10486+ * @param fieldName The name of the field containing the map/document.
10487+ * @param keyExpr The key expression to access.
10488+ * @returns A new `Expression` representing the value of the field in the document.
10489+ */
10490+ export function getField ( fieldName : string , keyExpr : Expression ) : Expression ;
10491+ export function getField (
10492+ fieldOrExpr : string | Expression ,
10493+ keyOrExpr : string | Expression ,
10494+ ) : Expression {
10495+ return fieldOrExpression ( fieldOrExpr ) . getField ( keyOrExpr ) ;
10496+ }
10497+
10498+ /**
10499+ * @internal
10500+ * Expression representing a variable reference. This evaluates to the value of a variable
10501+ * defined in a pipeline.
10502+ */
10503+ export class VariableExpression extends Expression {
10504+ expressionType : firestore . Pipelines . ExpressionType = 'Variable' ;
10505+
10506+ /**
10507+ * @hideconstructor
10508+ */
10509+ constructor ( private readonly name : string ) {
10510+ super ( ) ;
10511+ }
10512+
10513+ /**
10514+ * @internal
10515+ */
10516+ _toProto ( _serializer : Serializer ) : api . IValue {
10517+ return {
10518+ variableReferenceValue : this . name ,
10519+ } ;
10520+ }
10521+
10522+ /**
10523+ * @internal
10524+ */
10525+ _validateUserData ( _ignoreUndefinedProperties : boolean ) : void { }
10526+ }
10527+
10528+ /**
10529+ * Creates an expression that retrieves the value of a variable bound via `define()`.
10530+ *
10531+ * @example
10532+ * ```typescript
10533+ * db.pipeline().collection("products")
10534+ * .define(
10535+ * field("price").multiply(0.9).as("discountedPrice"),
10536+ * field("stock").add(10).as("newStock")
10537+ * )
10538+ * .where(variable("discountedPrice").lessThan(100))
10539+ * .select(field("name"), variable("newStock"));
10540+ * ```
10541+ *
10542+ * @param name - The name of the variable to retrieve.
10543+ * @returns An `Expression` representing the variable's value.
10544+ */
10545+ export function variable ( name : string ) : Expression {
10546+ return new VariableExpression ( name ) ;
10547+ }
10548+
10549+ /**
10550+ * Creates an expression that represents the current document being processed.
10551+ *
10552+ * @example
10553+ * ```typescript
10554+ * // Define the current document as a variable "doc"
10555+ * firestore.pipeline().collection("books")
10556+ * .define(currentDocument().as("doc"))
10557+ * // Access a field from the defined document variable
10558+ * .select(variable("doc").getField("title"));
10559+ * ```
10560+ *
10561+ * @returns An `Expression` representing the current document.
10562+ */
10563+ export function currentDocument ( ) : Expression {
10564+ return new FunctionExpression ( 'current_document' , [ ] ) ;
10565+ }
10566+
10567+ /**
10568+ * @internal
10569+ */
10570+ class PipelineValueExpression extends Expression {
10571+ expressionType : firestore . Pipelines . ExpressionType = 'PipelineValue' ;
10572+
10573+ /**
10574+ * @hideconstructor
10575+ */
10576+ constructor ( private readonly pipeline : firestore . Pipelines . Pipeline ) {
10577+ super ( ) ;
10578+ }
10579+
10580+ /**
10581+ * @internal
10582+ */
10583+ _toProto ( serializer : Serializer ) : api . IValue {
10584+ return {
10585+ // Casting to bypass type checking becuase _validateUserData does not exist in the public types
10586+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10587+ pipelineValue : ( this . pipeline as Pipeline ) . _toProto ( serializer ) ,
10588+ } ;
10589+ }
10590+
10591+ /**
10592+ * @internal
10593+ */
10594+ _validateUserData ( _ignoreUndefinedProperties : boolean ) : void {
10595+ // Casting to bypass type checking becuase _validateUserData does not exist in the public types
10596+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
10597+ ( this . pipeline as any ) . _validateUserData ( 'PipelineValueExpression' ) ;
10598+ }
10599+ }
10600+
10601+ /**
10602+ * @internal
10603+ */
10604+ export function pipelineValue (
10605+ pipeline : firestore . Pipelines . Pipeline ,
10606+ ) : Expression {
10607+ return new PipelineValueExpression ( pipeline ) ;
10608+ }
10609+
1041510610// /**
1041610611// * @beta
1041710612// * Perform a full-text search on the specified field.
0 commit comments