Skip to content

Commit 4469c58

Browse files
committed
Updating replaceWith to take an Expr
1 parent 129b62b commit 4469c58

File tree

3 files changed

+67
-8
lines changed

3 files changed

+67
-8
lines changed

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

+43-5
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ export class Pipeline implements ProtoSerializable<ProtoPipeline> {
599599
* // }
600600
*
601601
* // Emit parents as document.
602-
* firestore.pipeline().collection('people').replaceWith(field('parents'));
602+
* firestore.pipeline().collection('people').replaceWith('parents');
603603
*
604604
* // Output
605605
* // {
@@ -608,12 +608,50 @@ export class Pipeline implements ProtoSerializable<ProtoPipeline> {
608608
* // }
609609
* ```
610610
*
611-
* @param fieldValue The {@link Field} field containing the nested map.
611+
* @param fieldName The {@link Field} field containing the nested map.
612612
* @return A new {@code Pipeline} object with this stage appended to the stage list.
613613
*/
614-
replaceWith(fieldValue: Field | string): Pipeline {
615-
const fieldExpr =
616-
typeof fieldValue === 'string' ? field(fieldValue) : fieldValue;
614+
replaceWith(fieldName: string): Pipeline;
615+
616+
/**
617+
* Fully overwrites all fields in a document with those coming from a map.
618+
*
619+
* <p>This stage allows you to emit a map value as a document. Each key of the map becomes a field
620+
* on the document that contains the corresponding value.
621+
*
622+
* <p>Example:
623+
*
624+
* ```typescript
625+
* // Input.
626+
* // {
627+
* // 'name': 'John Doe Jr.',
628+
* // 'parents': {
629+
* // 'father': 'John Doe Sr.',
630+
* // 'mother': 'Jane Doe'
631+
* // }
632+
* // }
633+
*
634+
* // Emit parents as document.
635+
* firestore.pipeline().collection('people').replaceWith(map({
636+
* foo: 'bar',
637+
* info: {
638+
* name: field('name')
639+
* }
640+
* }));
641+
*
642+
* // Output
643+
* // {
644+
* // 'father': 'John Doe Sr.',
645+
* // 'mother': 'Jane Doe'
646+
* // }
647+
* ```
648+
*
649+
* @param expr An {@link Expr} that when returned evaluates to a map.
650+
* @return A new {@code Pipeline} object with this stage appended to the stage list.
651+
*/
652+
replaceWith(expr: Expr): Pipeline;
653+
replaceWith(value: Expr | string): Pipeline {
654+
const fieldExpr = typeof value === 'string' ? field(value) : value;
617655
this.readUserData('replaceWith', fieldExpr);
618656
return this._addStage(new Replace(fieldExpr, 'full_replace'));
619657
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -467,7 +467,7 @@ export class Replace implements Stage {
467467
name = 'replace_with';
468468

469469
constructor(
470-
private field: Field,
470+
private field: Expr,
471471
private mode:
472472
| 'full_replace'
473473
| 'merge_prefer_nest'

packages/firestore/test/integration/api/pipeline.test.ts

+23-2
Original file line numberDiff line numberDiff line change
@@ -1297,8 +1297,8 @@ apiDescribe.only('Pipelines', persistence => {
12971297
});
12981298
});
12991299

1300-
describe('replace stage', () => {
1301-
it('run pipleine with replace', async () => {
1300+
describe('replaceWith stage', () => {
1301+
it('run pipeline with replaceWith field name', async () => {
13021302
const snapshot = await execute(
13031303
firestore
13041304
.pipeline()
@@ -1312,6 +1312,27 @@ apiDescribe.only('Pipelines', persistence => {
13121312
others: { unknown: { year: 1980 } }
13131313
});
13141314
});
1315+
1316+
it('run pipeline with replaceWith Expr result', async () => {
1317+
const snapshot = await execute(
1318+
firestore
1319+
.pipeline()
1320+
.collection(randomCol.path)
1321+
.where(eq('title', "The Hitchhiker's Guide to the Galaxy"))
1322+
.replaceWith(
1323+
map({
1324+
foo: 'bar',
1325+
baz: {
1326+
title: field('title')
1327+
}
1328+
})
1329+
)
1330+
);
1331+
expectResults(snapshot, {
1332+
foo: 'bar',
1333+
baz: { title: "The Hitchhiker's Guide to the Galaxy" }
1334+
});
1335+
});
13151336
});
13161337

13171338
describe('sample stage', () => {

0 commit comments

Comments
 (0)