Effect CommunityEC
Effect Community2y ago
9 replies
bsluther

Optimizing Schema Duplication for Optional Fields

Any suggestions on how to "map" over a schema to make every field optional with a default value? The following works, but I just defined an entirely new schema rather than composing or transforming. Maybe that's fine? The two could get out of sync, but if they do the types wont match up.

export class TemporalTable extends S.Class<TemporalTable>('TemporalTable')({
  id: TemporalId,
  attributionId: AttributionId,
  createdAt: SqliteDateTime,
  start: S.NullOr(SqliteDateTime),
  startPrecision: Precision,
  end: S.NullOr(SqliteDateTime),
  endPrecision: Precision,
  ordinal: S.NullOr(S.Number)
}) {}

export class InsertTemporalParams extends S.Class<InsertTemporalParams>('InsertTemporalParams') ({
  id: S.optional(TemporalId, { default: () => makeId('Temporal')}),
  attributionId: S.optional(AttributionId, { default: () => makeId('Attribution') }),
  createdAt: S.optional(SqliteDateTime, { default: sqliteDateTimeNow }),
  start: S.optional(S.NullOr(SqliteDateTime), { default: () => null }),
  startPrecision: S.optional(Precision, { default: () => 'empty'}),
  end: S.optional(S.NullOr(SqliteDateTime), { default: () => null }),
  endPrecision: S.optional(Precision, { default: () => 'empty'}),
  ordinal: S.optional(S.NullOr(S.Number), { default: () => null })
}) {}


Basically I'm wondering if there is an ergonomic way to create InsertTemporalParams based on TemporalTable. I kinda think it works fine to define them separately, but wonder if I'm missing something.
Was this page helpful?