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

Fix/change to obj implementation #149

Merged
merged 3 commits into from
Apr 28, 2024

Conversation

4lessandrodev
Copy link
Owner

[1.23.0] - 2024-04-28 #148

Changes

  • Removed the set method from value object instances.
  • Changed the way the toObject method works. Path shortcutting of property access has been removed when props have only one attribute.
  • Implemented some improvements in how value objects handle primitive values.

Migrate from v1.22.1 to v1.23.0

  • Break Change

If you are using the toObject method in production to create a model from Aggregate, Entity, or Value Object domain instances, it is important to note that the property access path is no longer shortened.

Now the model object follows exactly the contract defined in props.

For example:

If an object is defined in props, even if props contains only one property, if it is an object, the toObject method will generate a model according to props.

Before v1.22.1

type Props = { value: number };

class Price extends ValueObject<Props>{};

const price = new Price({ value: 200 });

console.log(price.toObject());

// > 200

After v1.23.0

type Props = { value: number };

class Price extends ValueObject<Props>{};

const price = new Price({ value: 200 });

console.log(price.toObject());

// > { value: 200 }

If you want to maintain the return with primitive value without it being an object, use props of primitive type.

class Price extends ValueObject<number>{};

const price = new Price(200);

console.log(price.toObject());

// > 200

Another alternative is to use an adapter.

class Adapter implements IAdapter<Domain, Model> {
    adapt(domain: Domain): Result<Model> {
        //...
    }
}

price.toObject(new Adapter());

@4lessandrodev 4lessandrodev merged commit b5afb6c into develop Apr 28, 2024
@4lessandrodev 4lessandrodev deleted the fix/change-to-obj-implementation branch April 28, 2024 19:09
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.

1 participant