How to model a database table in Reason?


I am converting some Js code to Reason, and there some tabular datastructures, much like SQL database dumps.

On the Javascript side

Within a redux state tree there is a ‘datasets’ object, which is kinda like a relational database dump because it has rows, field names, and field types. Imagine the user uploads some CSV data and it gets parsed by javascript. There are be arbitrarily many rows and fields, and the fields can occur in any order in an indexed array. There is a runtime type detector that flags each field like “string”, “geojson” etc. (homegrown janky static typing haha). However, there known set of possible field types. At the top level it looks like this as json:

{ datasets: { "id" : { data: [...], fields: [...] }}}

In Reason Land

I’m not parsing the CSV data in Reason (yet). Rather I want to do some business logic on the datasets object in reason land. First, I want to parse the datasets object into strongly typed reason data. As soon as I realized the order of fields is unknown, and the number of fields is unknown, I ran into a bit of a mental block.

1st Idea: Use bs-json and start implementing Decoders to convert each level of the json object into a specific Reason type. Writing the decoders will result in the ultimate shape of the type definitions in reason.

2nd Idea: Is this an application for Functors? I want to inspect a json object, and output a new module which represents that dataset’s layout, field order and field types, exactly. So something that generates a new module is a Functor, right?

Do you recommend I be going down rabbit hole #1 or #2, a combination, or something else entirely?



The way I would model this would likely depend on usage. Can you show how this data structure is being used in the JS side?


@yawaramin thanks for your reply. This is React app, and the datasets stucture is used primarily as the source for a visualization, with filtering operations applied to it (filtering by attribute, or by time series). Also another component displays it in a datagrid type of view:



Cool. Is there a set schema (i.e. set of columns and their types) for the displayed table? Or can the data be anything at all with any number of columns? If the former, you can decode the JSON into a known data type. If the latter, you’ll need to try other strategies, e.g. treating everything as ‘text’.


@yawaramin The data can have any number of columns, and any order of columns, but each column has to be of a known type, like string, int, float, date, geojson, wkt. There is a type-detector package in javascript we are using. So basically the types well defined, but the length and types of the header and the rows isn’t known until the data is parsed at runtime.

I suspect this is a job for parametric data types, just haven’t been able to wrap my head around it yet.