0.6.1 • Published 4 years ago

@tpluscode/fun-ddr v0.6.1

Weekly downloads
1
License
MIT
Repository
github
Last release
4 years ago

Functional, domain-driven resources

Functional and unimposing foundation for architecture resembling CQRS; could be useful for DDD

Intro

By using fun-ddr (pronounced founder), you can reduce the complexity of individual pieces of your software and reconnect them with a little infrastructural code.

Quick guide

Implementing domain

  1. Define aggregate state
import { Entity } from '@tpluscode/fun-ddr'

interface Person extends Entity {
  firstName: string;
  lastName: string;
  spouseId?: string;
}
  1. Create a function to create a first aggregate root
import { initialize } from '@tpluscode/fun-ddr'

interface CreatePersonCommand {
  firstName: string;
  lastName: string;
}

const createPerson = initialize<Person, CreatePersonCommand>(cmd => {
  return {
    '@id': '/person/${cmd.firstName}/${cmd.firstName}',
    '@type': 'Person',
    firstName: cmd.firstName,
    lastName: cmd.lastName,
  }
})
  1. Create a function to mutate the state
import { mutate } from '@tpluscode/fun-ddr'

interface MarriageCommand = {
  spouseId: string;
  
}

const marry = mutate<Person, MarriageCommand>((self, command) => {
  if (self.spouseId) {
    throw new Error(`${self.firstName} ${self.lastName} is already married!`)
  }

  return {
    ...self,
    spouseId: command.spouseId,
  }
})
  1. Consume domain code in your application
import { Request, Response, NextFunction } from 'express'
import { people } from './repository/people'

function putPersonRequest (req: Request, res: Response, next: NextFunction) {
  createPerson({
    firstName: req.params.firstName,
    lastName: req.params.lastName,
  })
    .commit(people)
    .then(created => {
      res.status(201)
      res.setLink('Location', created['@id'])
      next()
    })
    .catch(next)
}

async function postMarriageRequest (req: Request, res: Response, next: NextFunction) {
  const person = await people.load(`/person/$req.params.firstName}/${req.params.lastName}`)
  
  person.mutate(marry)({ spouseId: 'some other person' })
    .commit(people)
    .then(() => {
      next()
    })
    .catch(next)
}
0.6.1

4 years ago

0.6.0

4 years ago

0.5.3

4 years ago

0.5.2

4 years ago

0.5.1

4 years ago

0.5.0

4 years ago

0.4.0

4 years ago

0.3.1

5 years ago

0.3.0

5 years ago

0.2.0

5 years ago

0.1.1

5 years ago

0.1.0

5 years ago