id
type BookProps = { title: BookTitle; soldBooksAmount: SoldBooksAmount;};class Book extends Entity<BookProps> { public static create(isbn: string, title: string, amount: number = 0): Result<Book> { return Result.combine({ id: Identifier.create(isbn, { name: 'Book.isbn [ID]' }), title: BookTitle.create(title), soldBooksAmount: SoldBooksAmount.create(amount), }).convertTo((validProps) => new this(validProps)); } get title() { return this.props.title.value; } get soldBooksAmount() { return this.props.soldBooksAmount.value; } increaseSoldBooksAmount() { SoldBooksAmount.create(this.soldBooksAmount + 1) .onSuccess((newAmout) => (this.props.soldBooksAmount = newAmout)) .onFail((error) => console.log('something strange happened: ' + error)); }}const book = Book.create('978-3-12-732320-7', 'How to create Entities');
ValueObjects
class BookTitle extends NonEmptyString { // recommended to access the options static readonly options: NonEmptyStringOptions = { name: 'Book.title' }; // required for clean creation public static create(title: string) { return super.create(title, BookTitle.options); }}class SoldBooksAmount extends Integer { public static create(amount: number) { return super.create(amount, { min: 0, name: 'Book.soldBooksAmount' }); }}
💬 The id of this Entity is an internal Identifier
Identifier
💬 Entities are compared based on their id.
Optional
Static
💬 Ensures that the given value is an instance of Entity
value
Entity
Generated using TypeDoc
An abstract Entity with an
id
, which can be serialized to its private attributesExample
Define a class for a book entity:
Define the
ValueObjects
associated to the book entity: