4lessandrodev / rich-domain

A lib to help you create a robust project based on domain driven-design (ddd) principles with typescript and zero dependencies.
https://www.npmjs.com/package/rich-domain
MIT License
122 stars 5 forks source link

[1.23.0] - 2024-04-28 #150

Closed 4lessandrodev closed 5 months ago

4lessandrodev commented 5 months ago

Changes

Migrate from v1.22.1 to v1.23.0

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());