question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Best approach to update sql table with many optional fields with different types?

See original GitHub issue

I am trying out this lib on a project with Giraffe.

One use case is that a web app needs to submit an update request with a dozen fields that are mostly optional. The request comes in in JSON through JS fetch. The REST api handler will bind the payload to a record with optional fields defined as int option or string option. If a field comes in as None, then the update sql should skip the field.

Is there a recommended approach to model the request payload and build up the update sql in a unified way?

It is easy with dynamical language such as js or Clojure. But I am not sure if there is a good way with F#.

Thanks in advance and my apology if it is a naive question. I did a search but couldn’t find out good answers.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
lanyusancommented, Aug 3, 2021

Thanks. Would try Analyzer later.

0reactions
Zaid-Ajajcommented, Aug 2, 2021

Well, there are a couple of reasons. Mainly because when you couple types (code) to database tables, that doesn’t necessarily make your code “type-safe” because now changes in the code break the application if these changes aren’t also reflected the database. This is something that the compiler cannot enforce so although your program compiles and maybe your unit tests will work too, the program will break in production because it relied on types being in sync with the database.

I’m not saying reflection is always bad, only prefer not to use it when possible or be very mindful of the points of failure.

Another reason not to use reflection is to be able to easily map table columns to other types, for example reading a jsonb column into string first, then converting it to JToken (from Newtonsoft.Json library) or reading timestamp as ZonedDateTime without extra plugins.

Finally, using this methodology of writing the data access layer makes the code very simple and allows for tools to better work with it: checkout Npgsql.FSharp.Analyzer

Hope this helps, let me know if you have more questions 😄

Read more comments on GitHub >

github_iconTop Results From Across the Web

Update multiple rows with different, optional values for ...
I want to be able to update multiple columns of multiple rows at a time, but not necessarily to update every column of...
Read more >
What is the best way to store optional values in table?
The column type is varchar (max) . In other words, this column is to store values ​​that are certain business characteristics of the...
Read more >
SQL Update statement Performance Tips
Best practices to improve SQL update statement performance ... The query we will tackle below updates all rows in a heap table with...
Read more >
How to handle a lot of optional columns in a large table?
From a design perspective using sharding the contents are split amongst multiple tables based on the partitioning scheme but you query it as...
Read more >
How to Update Multiple Columns in Single ...
In this article, we will see, how to update multiple columns in a single statement in SQL. We can update multiple columns by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found