Quantcast
Channel: ReScript Forum - Latest posts
Viewing all articles
Browse latest Browse all 1774

Ergonomic bindings to database drivers are so frustrating

$
0
0

in JS (might be a little pseudo)

  let sql = `insert into something (id, name, parentId) values (?,?,?)`
  await conn.execute(sql, [1, "blah", 2])

Its that damn array. It can contain numbers, strings, undefined, null, whatever.

One trick I found is that mysql drivers dont care if its a string or number, it will do the conversion automatically (not sure about postgres), so we can pass numbers and strings as a array.

But if we want strings and undefines things get ugly (undefines translate to NULL by the driver)

Best I came up with is

@unboxed type intOrStr = I(int) | S(string)
type maybeIntOrStr = option<intOrStr>
type multiTypeArray = array<maybeIntOrStr>

and a function to generate it like this, (if ~z can be undefined (NULL in the database))

let f = (~x: int, ~y: string, ~z: option<intOrStr>=None): multiTypeArray => {
  [Some(I(x)), Some(S(y)), z]
}

let r = f(~x=1, ~y="two")
Console.log(r) //=> [ 1, 'two', undefined ]
let r = f(~x=1, ~y="two", ~z=Some(I(100)))
Console.log(r) //=> [ 1, 'two', 100 ]

gross

Am I missing something? Is there some kind of variant decorator combo like

@unboxed type intOrStrOrUndefined = I(int) | S(string) | Undefined

Viewing all articles
Browse latest Browse all 1774

Trending Articles