Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(value-objects) - Add value objects to mikro-orm #5000

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

mlusca
Copy link
Contributor

@mlusca mlusca commented Dec 9, 2023

In this pull request, we add Value Objects to our project. Value Objects are a software design practice that allows us to encapsulate values ​​with specific behaviors and business rules in immutable objects.

The addition of these Value Objects brings several advantages to our project:

1.Encapsulation: Each Value Object encapsulates validation logic and behavior related to the value it represents. This makes our code more modular and easier to maintain.
2. Immutability: Value Objects are immutable, which means that once created, their values ​​cannot be changed.
This helps prevent bugs and makes our code more predictable.
3. Re-usability: Value Objects can be reused in different parts of our code, which helps reduce code duplication.

Utilization

@Entity()
class User {

  @PrimaryKey()
  id!: number;

  @Property()
  email: Email; // Value Object
}

Example of Value-Object

import { ValueObject } from './value-object';

const REGEX = /^[a-z0-9.]+@[a-z0-9]+\.[a-z]+(\.[a-z]+)?$/i;

export class Email extends ValueObject<string, Email> {

  protected validate(value: string): boolean {
    return REGEX.test(value);
  }

}

@mlusca mlusca changed the title Feature/value objects feat(value-objects) - Add value objects to mikro-orm Dec 9, 2023
Copy link

codecov bot commented Dec 9, 2023

Codecov Report

Attention: 4 lines in your changes are missing coverage. Please review.

Comparison is base (83aa6c8) 99.66% compared to head (ee70afb) 99.64%.
Report is 1 commits behind head on master.

Files Patch % Lines
packages/core/src/value-objects/value-object.ts 89.18% 2 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5000      +/-   ##
==========================================
- Coverage   99.66%   99.64%   -0.03%     
==========================================
  Files         221      225       +4     
  Lines       16204    16291      +87     
  Branches     3890     3909      +19     
==========================================
+ Hits        16150    16233      +83     
- Misses         54       56       +2     
- Partials        0        2       +2     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@B4nan
Copy link
Member

B4nan commented Dec 9, 2023

How is this different from embeddables (or custom types if it's about a single column, you can use those to validate the values too)? I don't think we need anything else for value objects. If something, we could improve the support for those two existing features, I don't want to add something "almost the same", it would only make things more confusing.

@mlusca
Copy link
Contributor Author

mlusca commented Dec 9, 2023

**B4nan ** commented Dec 9, 2023

@B4nan
For me the point would be:

  1. Simplicity. Just defining the type, without needing to customize the decorator, reducing the cognitive load.
  2. Centralized business rule. Embedables are not the same thing and in my opinion custom types were not made for that.

At the moment I thought about these two points, but I believe there could be more.
If you don't like it, feel free to close this PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants