Fp-ts: Should be Option persisted?

Created on 1 Dec 2019  路  4Comments  路  Source: gcanti/fp-ts

馃摉 Documentation

One thing is not clear to me. I understand why Option is better than null/undefined. But should we persist (save to db etc.) our model as serialized Option or with elvis operator and restore it to Option later on load? What fp-ts people do?

Maybe related: https://github.com/gcanti/io-ts#mixing-required-and-optional-props

Most helpful comment

I normally use the optionFromNullable type for serialization but note it does not come for free. You loose the ability to store constructs like Option<Option<unknown>>. The benefit of going for a null is that your data can be queried natively by most of the database engines out there.

All 4 comments

I normally use the optionFromNullable type for serialization but note it does not come for free. You loose the ability to store constructs like Option<Option<unknown>>. The benefit of going for a null is that your data can be queried natively by most of the database engines out there.

This is a good answer, thank you. Just curious, when it can happen Option<Option<unknown>>? It looks like a bug to me, but I am still a beginner.

I use this construct when I want to build a datastructure like in remote-data-ts but don't want to include an addtional library

type RemoteData<E, A> = Option<Option<Either<E, A>>

const init: Option<Option<never>> = none
const pending: Option<Option<never>> = some(none)
const success<A>(a: A): Option<Option<Either<never, A>>> = some(some(right(a)))
const failure<E>(e: E): Option<Option<Either<E, never>>> = some(some(left(e)))

const remoteData = getEitherM(getOptionM(option))

Coming from an OOP perspective this seems odd but when diving deeper in FP it is just again composition of stuff that is already there.

Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

mustafaekim picture mustafaekim  路  4Comments

mohsensaremi picture mohsensaremi  路  3Comments

amaurymartiny picture amaurymartiny  路  4Comments

steida picture steida  路  4Comments

vicrac picture vicrac  路  4Comments