Ramon Snir
Ramon Snir

Reputation: 7560

Storing data structure in a MySql database using F#

I need to store large and complex data structure in a MySql database (I use F#). Right now I had two ideas:

Using only structs (limiting), then create a nativeptr<_> to the struct, convert to nativeptr and load all into a byte [] which is a MySql blob.

Using F# Reflection library to convert everything to and from JSON and store as string (slooow).

Are there better ways which I've missed? Which way of those two is better?

Thanks!

Upvotes: 1

Views: 558

Answers (1)

Tomas Petricek
Tomas Petricek

Reputation: 243061

I think storing F# data structures as binary blobs or as textual data is not a good idea - If you're using relational database, you should store the data into tables with (reasonable) columns.

The most straightforward way would be to use ADO.NET (MySQL has some providers that allow that). The code to call ADO.NET from F# will be essentially the same as code calling it from C#. See for example code in this tutorial (PDF) that uses MySqlConnection.

You can make reading of data a little nicer by using ? (dynamic) operator in F#. I wrote an example demonstrating this in MS SQL, but it can be adapted to work with MySQL too.

Finally, it appears that MySQL also has provider for Entity Framework (see here), so you may be able to use F# LINQ support (but the current version has somewhat limited support for EF queries, so it would work well only in simple scenarios).

Upvotes: 1

Related Questions