@@ -1352,7 +1352,9 @@ describe("firestore-pipelines", () => {
13521352 conditional,
13531353 concat,
13541354 like,
1355- execute
1355+ execute,
1356+ variable,
1357+ equal
13561358 } = require ( "firebase/firestore/pipelines" ) ;
13571359
13581360 let app ;
@@ -3394,4 +3396,63 @@ describe("firestore-pipelines", () => {
33943396 // [END vector_length]
33953397 console . log ( result ) ;
33963398 }
3399+
3400+ // https://firebase.google.com/docs/firestore/pipelines/perform-joins-with-sub-pipelines
3401+ async function defineStage ( ) {
3402+ // [START define_example]
3403+ const result = await execute ( db . pipeline ( ) . collection ( "authors" )
3404+ . define (
3405+ field ( "id" ) . as ( "currentAuthorId" )
3406+ )
3407+ // ...
3408+ // [END define_example]
3409+ . addFields (
3410+ db . pipeline ( ) . collection ( "books" )
3411+ . where ( field ( "author_id" ) . equal ( variable ( "currentAuthorId" ) ) )
3412+ . aggregate (
3413+ field ( "rating" ) . average ( ) . as ( "avgRating" )
3414+ )
3415+ . toScalarExpression ( )
3416+ . as ( "averageBookRating" )
3417+ ) ) ;
3418+ console . log ( result ) ;
3419+ }
3420+
3421+ // https://firebase.google.com/docs/firestore/pipelines/perform-joins-with-sub-pipelines
3422+ async function toArrayExpressionStage ( ) {
3423+ // [START to_array_expression]
3424+ const projectTasks = await execute ( db . pipeline ( ) . collection ( "projects" )
3425+ . define (
3426+ field ( "id" ) . as ( "parentId" )
3427+ )
3428+ . addFields (
3429+ db . pipeline ( ) . collection ( "tasks" )
3430+ . where ( field ( "project_id" ) . equal ( variable ( "parentId" ) ) )
3431+ . select ( field ( "title" ) )
3432+ . toArrayExpression ( )
3433+ . as ( "taskTitles" )
3434+ ) ) ;
3435+ // [END to_array_expression]
3436+ console . log ( projectTasks ) ;
3437+ }
3438+
3439+ // https://firebase.google.com/docs/firestore/pipelines/perform-joins-with-sub-pipelines
3440+ async function toScalarExpressionStage ( ) {
3441+ // [START to_scalar_expression]
3442+ const result = await execute ( db . pipeline ( ) . collection ( "authors" )
3443+ . define (
3444+ field ( "id" ) . as ( "currentAuthorId" )
3445+ )
3446+ . addFields (
3447+ db . pipeline ( ) . collection ( "books" )
3448+ . where ( field ( "author_id" ) . equal ( variable ( "currentAuthorId" ) ) )
3449+ . aggregate (
3450+ field ( "rating" ) . average ( ) . as ( "avgRating" )
3451+ )
3452+ . toScalarExpression ( )
3453+ . as ( "averageBookRating" )
3454+ ) ) ;
3455+ // [END to_scalar_expression]
3456+ console . log ( result ) ;
3457+ }
33973458} ) ;
0 commit comments