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

demeine

v1.1.0

Published

DDDD - Distributed Domain Driven Design

Downloads

15,922

Readme

demeine

Demeine is a library supporting DDDD (Distributed Domain Drive Design).

Purpose

Provide the base building blocks for implementing DDD in a distributed environment. This repository contains an implementation of a repository, as well as a base aggregate that domain specific aggregates can extend. The flow of action → command → event → state update is supported.

Installation

npm install demeine

Usage

This example uses TapeWORM with its default in memory partition.

// User.ts
export const USER_AGGREGATE_TYPE = 'user';

export interface UserState {
  id: string;
  name: string;
}

export class User extends Aggregate<UserState> {
  constructor(commandSink?: CommandSink, eventHandler?: EventHandler, commandHandler?: CommandHandler) {
    super(commandSink, eventHandler, commandHandler);

    this._state = {
      id: this.id,
      name: ''
    };
  }

  register(id: string, name: string) {
    this.id = id;

    return this._sink({
      type: 'user.register.command',
      payload: { id, name },
      aggregateId: this.id
    });
  }

  processChangeName(command: Command<UserState>) {
    return this._apply(
      {
        type: 'location.name_changed.event',
        payload: command.payload,
        aggregateId:
        this.id
      },
      true
    );
  }

  applyNameChanged(event: Event<UserState>) {
    this._state.name = event.payload.name;
  }
}

// userFactory.ts
import { User, UserState, USER_AGGREGATE_TYPE } from './User';

export function userFactory(id: string) {
  const user = new User();

  user.id = id;
  user._state = { id, name: '' };
  user.type = USER_AGGREGATE_TYPE;

  return user;
}

// index.ts
import { Aggregate, Repository } from 'demeine';
import TapeWorm from 'tapeworm';
import { User, USER_AGGREGATE_TYPE } from './User';
import { userFactory } from './userFactory';

const tapeWorm = new TapeWorm();
const partition = await tapeWorm.openPartition('my_partition');

const userRepository = new Repository<User>(partition, USER_AGGREGATE_TYPE, userFactory);

// Not found, name: ''
const user = await userRepository.findById('123');
await user.register('123', 'Jeff');
await userRepository.save(user);

// Exists since save(user), name: 'Jeff'
const existingUser = await userRepository.findById('123');

Components

Aggregate

Base class for aggregate classes representing domain concepts.

clearUncommittedEvents

Remove the events created through calling the aggregate methods.

Is used in the save method on the Repository in order to remove local events after they've been committed.

const user = await userRepository.findById('123');

// Adds `user.registered.event` to uncommitted events
await user.register('123', 'Jeff');
// Removes the `registered` event
await user.clearUncommittedEvents();

await userRepository.save(user);

delete

Creates a $stream.deleted.event for the aggregate, which the persistence partition should handle by removing the aggregate data.

const user = await userRepository.findById('123');
await user.delete();
await userRepository.save(user);

// Should not exist anymore
const nonExistingUser = await userRepository.findById('123');

getUncommittedEvents

Retrieves the list of events created by calling aggregate methods. Prefer to use the async version, as this will throw if there are unprocessed commands.

const user = await userRepository.findById('123');
await user.register('123', 'Jeff');
await user.registerEmail('[email protected]');

// [ Event { type: 'user.registered.event' }, Event { type: 'user.email.registered.event' }]
const events = user.getUncommittedEvents();

getUncommittedEventsAsync

Retrieves the uncommitted events as soon as the processing queue is empty.

const user = await userRepository.findById('123');
await user.register('123', 'Jeff');
await user.registerEmail('[email protected]');

// [ Event { type: 'user.registered.event' }, Event { type: 'user.email.registered.event' }]
const events = await user.getUncommittedEventsAsync();

getVersion

Retrieves the version of the aggregate, including increments for the processed uncommitted events.

const user = await userRepository.findById('123');
// -1, non existing
const initialVersion = user.getVersion();

await user.register('123', 'Jeff');
await user.registerEmail('[email protected]');

// 2, set to 0 for initial + 2 increments for the events
const newVersion = user.getVersion();

Repository

You will need to provide a Partition to the Repository.

checkConcurrencyStrategy

Checks the concurrency strategy provided in the Repository constructor. Returns a Promise with a boolean stating whether it should throw an error or not. Defaults to resolving false if no concurrency strategy was provided.

Is used in the save method on the Repository in order to throw a concurrency error when there's a version mismatch.

const user = await userRepository.findById('123');
const initialVersion = user.getVersion();

await user.register('123', 'Jeff');
await user.registerEmail('[email protected]');

// 2, set to 0 for initial + 2 increments for the events
const newVersion = user.getVersion();

findById

Will look up an Aggregate in the Partition provided to the Repository by the aggregate's ID.

const user = await userRepository.findById('123');

findByQueryStreamWithSnapshot

Will create a rehydrated aggregate by looking up the events for a stream, and processing them. Returns the rehydrated aggregate.

Requires the persistence partition to implement queryStreamWithSnapshot.

const user = await userRepository.findById('123');

findBySnapshot

Will look up a snapshot for the aggregate and Will create a rehydrated aggregate by looking up the events for a stream, and processing them. Returns the rehydrated aggregate.

Requires the persistence partition to implement loadSnapshot & queryStream.

const user = await userRepository.findById('123');

findEventsById

Retrieves the committed (processed) events for the aggregate stream by the aggregate's ID.

// [{ type: 'user.registered.event' }. { type: 'user.email.registered.event' }]
const events = await userRepository.findEventsById('123');

save

Persists the aggregate including executed commands to the persistence partition.

// { id: '123', contact: [] }
const user = await userRepository.findById('123');
await user.registerEmail('[email protected]');
await userRepository.save(user);

// { id: '123', contact: [{ value: '[email protected]', type: 'email' }] }
const updatedUser = await userRepository.findById('123');