Effect CommunityEC
Effect Community17mo ago
6 replies
TashTolo

Understanding Encode and Decode for User Data Transformation and Event Publishing

I'm trying to get my head around decode and enconde.
My scenario is that I have some user data I grab from DB and that becomes a User type. Then I transform this user data and publish this to event bus for someone else to consume. My issue is about sending this data.
I started with:

export class UserTelemetry extends SCH.Class<UserTelemetry>('UserTelemetry')({
  user_email: SCH.propertySignature(SCH.String).pipe(SCH.fromKey('email')),
  user_accountType: SCH.propertySignature(SCH.Enums(AccountType)).pipe(SCH.fromKey('accountType')),
  user_distributionStatus: SCH.propertySignature(SCH.String).pipe(
    SCH.fromKey('distributionStatus')
  ),
}) {}

This allows me to yield* SCH.decode(UserTelemetry)(user) and send the result to the event bus.

But then looking at the docs, decode is for when receiving data from outside and encode to send the data.

So I changed the schema to:

export class UserTelemetry extends SCH.Class<UserTelemetry>('UserTelemetry')({
  email: SCH.propertySignature(SCH.String).pipe(SCH.fromKey('user_email')),
  accountType: SCH.propertySignature(SCH.Enums(AccountType)).pipe(SCH.fromKey('user_accountType')),
  distributionStatus: SCH.propertySignature(SCH.String).pipe(
    SCH.fromKey('user_distributionStatus')
  ),
}) {}

with yield* SCH.encode(UserTelemetry)(user).

The reason why I started with the first approach is because I wouldn't ever receive that user_*** format in order to decode.

But semantically the secod approach is correct??!!!

Am I on the right track with the second option?
Was this page helpful?