Create a series of actions that do not depend on using only one occurrence of the WriteBatch class

My goal is to create a series of batch actions using functions that do not require a specific instance of WriteBatch. Currently, I am passing an instance of the WriteBatch class to the functions so they can utilize the .set(), .update(), or .delete() methods on the batch.

Consider my code snippet:

export class BatchHelperService {

  constructor(private afStore: AngularFirestore) { }

  executeBatchActions(batchActions: BaseBatchAction[]): Observable<void> {
    const batch = this.afStore.firestore.batch();
    batchActions.forEach(action => action.attachActionToBatch(batch));
    return from(batch.commit());
  }
}

In this code, I have a function that takes an array of a base class BaseBatchAction. There are three derived classes that represent different types of batch actions (set, update, delete), all implementing the attachActionToBatch() method requiring a pre-existing WriteBatch object.

I aim to eliminate this reliance as it goes against clean coding principles for monadic functions (avoiding the use of output arguments). Is there a way to achieve this with the SDK?

Answer №1

To efficiently interact with Firestore, utilizing WriteBatch is essential. There are no shortcuts when it comes to executing a batch write in Firestore; this structure will always be an integral part of the process.

However, if you aim to enhance code organization and maintain a clear separation of responsibilities, consider developing a custom batch abstraction or encapsulation mechanism. This could involve creating a wrapper object that conceals Firestore APIs from external users while internally leveraging them for operations. By passing around these wrapper objects and structuring your code accordingly, you can establish a more manageable approach. While BaseBatchAction is a step in the right direction, expanding the abstraction to cover the entire batch process—not just individual changes—may provide further benefits.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there documentation available for the gcloud output formats, such as the JSON output for each command?

As I work to script the gcloud tool in a TypeScript-aware JavaScript environment known as SLIME, I am utilizing the --format json feature for formatting. The integration is smooth, but I find myself manual analyzing the JSON output of each command to und ...

Identifying Data Types in Typescript Using a Union Type Guard

I came across this interesting type guard: enum X { A = "1" } const isNullableX = (value: any): value is X | null => false let bar: string | null = '' if (isNullableX(bar)) { console.log(bar) } Surprisingly, in the last con ...

Tips for showing solely the current page number within angular pagination

HTML : <!-- pagination controls --> <div class="pagination-container"> <pagination-controls (pageChange)="onPageChange($event)" [maxSize]="1" [id]="config.id" [directionLinks]="true">< ...

The requested module cannot be located, were you referring to "js" instead?

I am currently developing a React application using webpack and typescript. I have integrated the dependency react-financial-charts into my project, and it is properly specified in the package.json. Inside the node_modules directory, there are two folders ...

Why aren't the validations being set when creating Angular forms using formControl.values?

I had to structure the form in a specific way in my app.ts file -> courseNameControl = new FormControl("", [Validators.required,Validators.minLength(2)]); contentControl = new FormControl("", Validators.required); form = { cours ...

What is the rationale behind ngOnInit not being a private method in Angular?

After extensively checking both code samples and even the official documentation, I am still unable to find the information I need. Turning to Google has also yielded no results. The question that baffles me is: ngOnInit() { ... } Why do we use this syn ...

Could you please explain the significance of /** @docs-private */ in Angular's TypeScript codebase?

After browsing through the @angular/cdk code on GitHub, I came across a puzzling "docs-private" comment. Can anyone explain its significance to me? Link to Code * In this base class for CdkHeaderRowDef and CdkRowDef, the columns inputs are checked for ...

The error message in Angular states "Unable to destructure the 'country' property of an intermediate value as it is null"

I recently started an Angular project from scratch. When I run the default application that is created alongside the project (which is currently empty), I encounter an error in the console: "Uncaught (in promise) TypeError: Cannot destructure property &apo ...

Error in Angular2 routes: Unable to access the 'forRoot' property

angular2 rc.5 I encountered a problem with TypeError: Cannot read property 'forRoot' of undefined(…) after investigating, I discovered that RouterModule is appearing as undefined during execution. This is my app.route.ts: import {Routes, Rou ...

What is the best way to manage the connections in my D3 tree chart?

I've been working on customizing a tool from an open source library called angular-d3-tree, but I'm struggling with getting the links to connect properly in my D3 tree structure. This is what my current tree layout looks like: https://i.stack.im ...

Using Angular 2 to showcase icons in the navbar post authentication

The structure of my components is as follows: The app component contains a navigation bar and router outlet. The navigation bar includes a logo, generic links, and specific links that are only shown after user login and authentication. The router outlet de ...

Testing the branch count of optional chaining in Typescript

I am struggling to grasp the concept of branch coverage, especially when it involves optional chaining in TypeScript. Below is my code snippet: type testingType = { b?: { a?: number }; }; export function example(input: testingType) { return input. ...

Retrieving selected values from an ngx dropdown list

I am having trouble implementing ngx dropdown list in this way: <ngx-dropdown-list [items]="categoryItems" id="categoriesofdata" [multiSelection]="true" [placeHolder]="'Select categories'"></ngx-dropdown-list> ...

Troubleshooting Appium error management is ineffective

As a newcomer to appium, I might have made some mistakes. I'm encountering a problem with appium while using wdio and jasmine. it('wtf', (done) => { client.init().element('someName').getText() // ^ here ...

Developing with Electron JS and TypeScript - Leveraging TS-Node in the primary process

Is there a way to modify the code below in order for the electron main process to utilize Typescript by using ts-node? "scripts": { "shell": "cross-env NODE_ENV=development electron ts-node ./app/main.ts" } ...

Encountering an unforeseen exception in the intercom system

After updating my Angular 13 app to version 17, I encountered the following error. How can this issue be fixed? Error: Unexpected value 'IntercomModule' imported by the module 'AppModule'. Please add an @NgModule annotation. ng-interc ...

Methods for transforming a TypeScript class instance containing getter/setter properties into a JSON format for storage within a MySQL database

I am currently working on a TypeScript class that includes a getter and setter method: export class KitSection { uid: string; order: number; set layout(layout: KitLayout) { this._layout = new KitLayout(layout); } get layout( ...

An Angular CDK overlay conflict occurring within a nested component

Incorporating the Angular CDK overlay into my project, I've successfully implemented a modal drawer and tooltips. However, I've encountered an issue when trying to close the drawer while a tooltip is still active within it. Upon pressing Escape ...

Enhance your Flatlist re-rendering skills in React Native by utilizing componentWillRecieveProps!

UNSAFE_componentWillMount(){ this.props.retrieveEmployeeData(); } displayEmployeeRow(employee){ return <ListItem employee={employee}/>; } render(){ return( <FlatList style={{flex:1,height:100}} dat ...

Angular: Granting an external module access to a provider

One of the modules I imported provides a service with an optional dependency. Although it being optional didn't affect my application, as it just prevented any errors from occurring when not present. Here's an example: import { FooModule } from ...