Skip to content

Commit 28fbbae

Browse files
committed
add subquery snippets
1 parent 59fd29c commit 28fbbae

File tree

5 files changed

+116
-2
lines changed

5 files changed

+116
-2
lines changed

firestore-next/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
},
77
"license": "Apache-2.0",
88
"dependencies": {
9-
"firebase": "eap-firestore-pipelines",
9+
"firebase": "^12.12.0",
1010
"geofire-common": "^6.0.0"
1111
},
1212
"devDependencies": {

firestore-next/test.firestore.js

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./firestore-next/test.firestore.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START define_example_modular]
8+
const result = await execute(db.pipeline().collection("authors")
9+
.define(
10+
field("id").as("currentAuthorId")
11+
)
12+
// ...
13+
// [END define_example_modular]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./firestore-next/test.firestore.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START to_array_expression_modular]
8+
const projectTasks = await execute(db.pipeline().collection("projects")
9+
.define(
10+
field("id").as("parentId")
11+
)
12+
.addFields(
13+
db.pipeline().collection("tasks")
14+
.where(field("project_id").equal(variable("parentId")))
15+
.select(field("title"))
16+
.toArrayExpression()
17+
.as("taskTitles")
18+
));
19+
// [END to_array_expression_modular]
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// This snippet file was generated by processing the source file:
2+
// ./firestore-next/test.firestore.js
3+
//
4+
// To update the snippets in this file, edit the source and then run
5+
// 'npm run snippets'.
6+
7+
// [START to_scalar_expression_modular]
8+
const result = await execute(db.pipeline().collection("authors")
9+
.define(
10+
field("id").as("currentAuthorId")
11+
)
12+
.addFields(
13+
db.pipeline().collection("books")
14+
.where(field("author_id").equal(variable("currentAuthorId")))
15+
.aggregate(
16+
field("rating").average().as("avgRating")
17+
)
18+
.toScalarExpression()
19+
.as("averageBookRating")
20+
));
21+
// [END to_scalar_expression_modular]

0 commit comments

Comments
 (0)