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

domain-utilities

v1.9.1

Published

This npm package provides a set of utilities for building domain-driven design (DDD) applications. It includes essential classes and interfaces for defining aggregates, entities, value objects, domain events, decorators, use cases, and error handling. The

Downloads

19

Readme

DDD Utils

This npm package provides a set of utilities for building domain-driven design (DDD) applications. It includes essential classes and interfaces for defining aggregates, entities, value objects, domain events, decorators, use cases, and error handling. These utilities are designed to help you structure your application's domain logic effectively.

Installation

You can install this package via npm:

npm install domain-utilities

How to use this package

If you are starting out with DDD or want to learn more about it, I recommend reading @stemmlerjs excellent articles found on khalilstemmler.com and watching @CodeOpinion youtube channel, which contains many helpful videos.

Usage

Aggregate Root

import { AggregateRoot, Entity, UniqueIdentifier, DomainEvents } from 'domain-utilities';

class MyAggregate extends AggregateRoot<MyAggregateProps> {
  // Your custom methods and properties here

  public someAction() {
    // Perform actions and raise domain events if needed
    const domainEvent = new MyDomainEvent(/* event data */);
    this.addDomainEvent(domainEvent);
  }
}

// Usage example
const myAggregate = new MyAggregate(/* aggregate properties */);
myAggregate.someAction();

// Access domain events raised by the aggregate
const domainEvents = myAggregate.domainEvents;

Entity

import { Entity, UniqueIdentifier } from 'domain-utilities';

class MyEntity extends Entity<MyEntityProps> {
  // Your custom methods and properties here
}

// Usage example
const myEntity = new MyEntity(/* entity properties */);
const entityId = myEntity.id;

// Check equality of entities
const isEqual = myEntity.equals(anotherEntity);

Value Object

import { ValueObject } from 'domain-utilities';

class MyValueObject extends ValueObject<MyValueObjectProps> {
  // Your custom methods and properties here
}

// Usage example
const valueObject1 = new MyValueObject(/* value object properties */);
const valueObject2 = new MyValueObject(/* value object properties */);

// Check equality of value objects
const isEqual = valueObject1.equals(valueObject2);

Domain Events

import { DomainEvents, IDomainEvent } from 'domain-utilities';

// Register a handler for a specific domain event
DomainEvents.register((event: MyDomainEvent) => {
  // Handle the domain event
}, 'MyDomainEventClassName');

// Dispatch domain events
DomainEvents.dispatch(new MyDomainEvent(/* event data */));

// Dispatch domain events for a specific aggregate
DomainEvents.dispatchEventsForAggregate(aggregateId);

Decorators

import { OnEvent } from 'domain-utilities';

class MyEventHandler {
  @OnEvent('MyDomainEventClassName')
  handleMyDomainEvent(event: MyDomainEvent) {
    // Handle the domain event
  }
}

Use Cases

import { IUseCase } from 'domain-utilities';

class MyUseCase implements IUseCase<MyRequest, MyResponse> {
  async execute(request: MyRequest): Promise<MyResponse> {
    // Implement your use case logic
  }
}

Errors

import { ApplicationError, DomainValidationError, Either, left, right } from 'domain-utilities';

// Custom application error
throw new ApplicationError('An error occurred in the application.');

// Custom domain validation error
throw new DomainValidationError('Validation failed for a domain entity.');

// Either monad for error handling
const result: Either<ErrorType, SuccessType> = left(new ErrorType('Error message'));

Infrastructure

import { iMapper, iRepository } from 'domain-utilities';

// Define a custom mapper for persistence
class MyMapper implements iMapper<PersistenceType, AggregateType> {
  toPersistence(aggregateRoot: AggregateType): PersistenceType {
    // Map aggregate to persistence data
  }

  toDomain(persistence: PersistenceType): AggregateType {
    // Map persistence data to aggregate
  }
}

// Define a custom repository
class MyRepository implements iRepository<AggregateType> {
  async save(aggregateRoot: AggregateType): Promise<void> {
    // Save the aggregate to the database or storage
  }
}

License

This package is licensed under the MIT License.

Acknowledgments

This package is inspired by domain-driven design principles and patterns. It aims to provide a foundation for building robust and maintainable domain logic in your applications.