@itrocks/domain-components
v0.1.1
Published
Reusable business components to enrich domain entities with common attributes like active, name, and more
Maintainers
Readme
domain-components
Reusable domain mixins to enrich entities with common attributes (currently: active, name).
Installation
npm i @itrocks/domain-componentsUsage examples
Option A: Explicit in code
Use @Use when you want the composition to be explicit in source code.
import { Active } from '@itrocks/domain-components'
import { HasName } from '@itrocks/domain-components'
import { Use } from '@itrocks/use'
@Use(Active)
export class Project
{
// Project now has active: boolean
}
@Use(HasName)
export class Category
{
// Category now has name: string
}
@Use(Active, HasName)
export class User
{
// User now has both active: boolean, and name: string
}Option B: By project configuration
Use compose() when you want to decide compositions at application level or per deployment, without touching the library’s source code.
Call compose() before loading the modules you want to affect.
// main.ts
import { compose } from '@itrocks/compose'
compose(__dirname, {
// add mixins to the exported User class of @itrocks/user
'@itrocks/user': [
'@itrocks/domain-components:Active',
'@itrocks/domain-components:HasName'
]
})
// only import your app after compose() is called
await import('./app.js')Component reference
Active
Adds an active flag to your entity.
Properties:
active: boolean(default:false, @Required())
Example:
import { Active } from '@itrocks/domain-components'
import { Use } from '@itrocks/use'
@Use(Active)
export class Product
{
}
const product = new Product()
product.active = trueHasName
Adds a name field to your entity.
Properties:
name: string(default:'', @Required())
Example:
import { HasName } from '@itrocks/domain-components'
import { Use } from '@itrocks/use'
@Use(HasName)
export class Team
{
}
const team = new Team()
team.name = 'Red team'