Effect-TS / effect

An ecosystem of tools to build robust applications in TypeScript
https://effect.website
MIT License
7.63k stars 243 forks source link

From Discord: Suggestion for `withEncodingDefault` and effectful versions of defaults #3919

Open effect-bot opened 6 days ago

effect-bot commented 6 days ago

Summary

In this discussion, _adrian_g and patroza debate the pros and cons of using effectful defaults and encoding defaults within schemas when working with data entities such as users, especially in the context of the Effect-TS library.

Key points:

  1. Effectful Defaults: _adrian_g advocates for using effectful defaults, such as setting createdAt using a Clock during encoding. This approach encapsulates defaults within a schema, simplifying operations like persisting data.

  2. Encoding vs. Entity Construction: patroza opposes the idea of encoding defaults, suggesting entity construction should ensure a fully initialized state, like setting createdAt at instantiation, not encoding. He prefers entities to be self-contained and not reliant on external factors like database operations.

  3. Code First Philosophy: patroza emphasizes a "code first" approach, where database or framework concerns (such as default values) should not dictate code structure. He prefers deterministic and self-contained entity creation without relying on defaults added during encoding or persistence.

  4. Inconsistent State Avoidance: patroza argues against having optional fields like createdAt, as it implies entities might exist in an inconsistent state.

  5. Compatibility with Effect-TS: _adrian_g desires features like effectful constructors and defaults within the Effect-TS library, which would integrate well with Effect behaviors like controlled date-time handling.

  6. Alternative Perspectives: There’s a recognition that while patroza prefers immediate consistency in entity states, other developers, including Tim from the Effect-TS community, apply a database-managed default strategy during encoding.

Throughout the discussion, _adrian_g and patroza consider different perspectives on handling entity state and defaults, with an understanding that their preferences reflect broader architectural choices in software design.

Discord thread

https://discord.com/channels/795981131316985866/1304719283263246400

adrian-gierakowski commented 6 days ago

This should probably be converted into 2 separate issues:

  1. effecful Schema constructors and defaults
  2. decoding defaults and effecful versions of decoding\encoding defaults

if no one else beats me to it, I'll create these on Monday (unfortunately I'm not able to edit this one)