npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@nunw/domain-events-for-firestore

v1.0.0

Published

aaa

Downloads

4

Readme

Domain Events for Firestore

This is a library that supports the implementation of domain event processing utilizing Firestore's transaction feature.

  • It is a simple Pub/Sub mechanism for Events
  • In order to make it easy to handle Firestore's transaction restriction, "Read operations must be performed before write operations", it provides an execution flow that separates data reading and writing.
  • You can combine and execute multiple update logics
  • If an error occurs during processing, a rollback will be performed
  • If a recoverable error occurs, the process will be retried

Example

This is an example of a process that reads certain data and updates that data based on it.

const firestore = getFirestore();

const publisher = new DomainEventPublisher(firestore);

class ExampleUpdateEvent extends AbstractDomainEvent {
  constructor(
    public readonly target: DocumentReference,
    public readonly incrementValue: number,
  ) {
    super();
  }
}

const subscriber: DomainEventSubscriber = {
  onEvent(event: DomainEvent): DomainEventHandler | undefined {
    if (event instanceof ExampleUpdateEvent) {
      return new (class extends TransactionDomainEventHandler {
        private currentValue: number | undefined = undefined;

        async prepareHandleEvent(context: ReadContext): Promise<void> {
          const result = await context.get(event.target);
          this.currentValue = result.data()?.value;
        }

        async handleEvent(context: WriteContext): Promise<void> {
          context.set(event.target, {
            value: (this.currentValue ?? 0) + event.incrementValue,
          });
        }
      })();
    } else {
      return undefined;
    }
  },
};

publisher.addSubscriber(subscriber);

await publisher.publish(
  new ExampleUpdateEvent(
    firestore.collection("transactionExample").doc("value1"),
    1,
  ),
);

The entire code can be found in example/src/transaction-update.ts.

Installation

npm install @nunw/domain-events-for-firestore

Explanation

This library processes in the following flow.

  1. (Preparation) Register DomainEventSubscriber in DomainEventPublisher
  2. Publish an event in some processing (DomainEventPublisher.publish)
  3. DomainEventSubscriber is called sequentially and DomainEventHandler is created
  4. DomainEventHandler's prepareHandleEvent is called for data reading
  5. DomainEventHandler's handleEvent is called for data writing

If an error occurs during the process, Firestore's commit() is not called, so you can safely interrupt the process.

DomainEventSubscriber is an interface as follows, and you register it with DomainEventPublisher's addSubscriber.

export interface DomainEventSubscriber {
  onEvent(event: DomainEvent): DomainEventHandler | undefined;
}

When an event is published, onEvent of all subscribers is called. If you perform processing based on the event, return DomainEventHandler.

DomainEventHandler is an abstract class and there are the following types

| Class name | Readable | Writable | Method provided | | :---------------------------: | :------: | :------: | :----------------------------------------------------------------------------------------------------------------- | | SimpleDomainEventHandler | ✗ | ✗ | handleEvent(): Promise<void> | | TransactionDomainEventHandler | ✓ | ✓ | prepareHandleEvent(context: ReadContext): Promise<void> handleEvent(context: WriteContext): Promise<void> | | BatchDomainEventHandler | ✗ | ✓ | handleEvent(context: WriteContext): Promise<void> | | ReadDomainEventHandler | ✓ | ✗ | prepareHandleEvent(context: ReadContext): Promise<void> handleEvent(): Promise<void> |

WriteContext is an object that can only perform write operations against Firestore. ReadContext is an object that can only perform read operations against Firestore.

Any DomainEventHandler can override onSuccess(): Promise<void> or rollback(): Promise<void> to describe post-processing at the time of success or failure.

Usage

Please refer to example/src.