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

fp-ddd

v0.0.9

Published

A starter project for functional domain driven projects

Downloads

22

Readme

FP-DDD

The fp-ddd is a TypeScript library designed to offer a solid foundation for working with Domain-Driven Design (DDD) concepts such as entities, value objects, and aggregates. This package leverages the power of functional programming to ensure immutability and robust domain validation through the use of fp-ts and lodash.

Features

  • Entity Base Class: Abstract class for creating entities that are distinct by their state.
  • Value Object Base Class: Abstract class for creating value objects which are immutable and validated upon creation.

Installation

Install the package using npm:

    npm install fp-ddd

Usage

Below are simplified examples of how you might implement and utilize the base classes provided by fp-ddd.

Creating an Entity

import { Entity } from 'fp-ddd';
import { Either } from "fp-ts/lib/Either";
import Failure from "./failure";

class User extends Entity<UserProps> {
  constructor(public readonly values: Either<Failure<UserProps>, UserProps>) {
    super();
  }

  get name(): string {
    return this.getOrCrash().name;
  }
}

// Usage
const user = new User(right({ name: 'John Doe', email: '[email protected]' }));
console.log(user.name); // Outputs: John Doe

Creating a Value Object

import { ValueObject } from 'fp-ddd';
import { Either, right } from "fp-ts/lib/Either";
import Failure from "./failure";

class Email extends ValueObject<string> {
  constructor(public readonly value: Either<Failure<string>, string>) {
    super();
  }

  get email(): string {
    return this.getOrCrash();
  }
}

// Usage
const email = new Email(right('[email protected]'));
console.log(email.email); // Outputs: [email protected]

API Reference

  • isValid(): Checks if the contained value passes the domain rules.
  • getOrCrash(): Forcibly extracts the value or throws an error if the validation fails.
  • isEqual(other): Compares two instances to determine if they are equal based on their state.

Background

The Fusion of Functional Programming and Domain-Driven Design

Functional Programming (FP) and Domain-Driven Design (DDD) are two powerful paradigms in software development that, when combined, offer a robust approach to building scalable, maintainable, and business-focused software systems.

Functional Programming (FP) is a programming paradigm that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data. It promotes a declarative coding style, where the focus is on "what to solve" rather than "how to solve it." FP brings several benefits to software development, including more predictable code, easier testing, and improved modularity.

Domain-Driven Design (DDD) is a methodology for developing software that meets core business objectives by modeling software after real-world business domains. DDD emphasizes understanding the domain deeply and reflecting this understanding in the software architecture, helping to create software that is deeply aligned with the business needs and terminology.

When combined, FP and DDD offer a unique advantage: the clarity, simplicity, and immutability of FP complement the rich, model-driven focus of DDD. This synergy helps to:

  • Enhance Domain Modeling: The immutable nature of FP aligns well with the concept of Value Objects in DDD, making it easier to reason about the state changes within the domain.
  • Reduce Side Effects: FP's emphasis on pure functions minimizes side effects, which is critical in complex domain models where unexpected state changes can lead to bugs.
  • Improve Code Base Scalability and Maintainability: The modular nature of FP, combined with the strategic design enforced by DDD, results in a codebase that is easier to extend and maintain. The fp-ddd package is designed to leverage the strengths of both paradigms. It provides foundational classes and utilities that facilitate the implementation of domain models, ensuring that your software not only meets the technical best practices of FP but also aligns closely with your business strategies through DDD.

By using fp-ddd, developers can focus more on the strategic design of their domain while benefiting from the safety and robustness offered by functional programming.

Contributing

Contributions are welcome! Please feel free to submit pull requests or open issues to discuss proposed changes or enhancements.

License

This project is licensed under the MIT License - see the LICENSE file for details.