Reason and databases


#1

Hi All,

I have just started to take a serious look at Reason and really like what I have seen so far. However, I am a bit concerned about the availability of DB drivers/connectors for using it as a backend language since OCaml’s choices seem rather limited.

I guess as long as one compiles to JS/Node this problem can be avoided since all DBs with native Node drivers are available? Please correct me if I’m missing something here.

And please let me know if you can think of strategies to work around the driver issue if the eventual goal is to compile to native. I wonder how feasible is it to call a Node instance containing DB calls or even embedding a V8 instance, but a less crazy solution would obviously be much preferred.

Thanks,
Hans


#2

If you are looking for native; there are quite a few suggestions here: https://discuss.ocaml.org/t/databases-and-ocaml/913.

I wrote a NodeJS abstraction package: https://github.com/scull7/bs-sql-common
There are drivers for:

  1. Sqlite: https://github.com/scull7/bs-sqlite
  2. MySql/MariaDb: https://github.com/scull7/bs-mysql2/

#3

Thank you for your reply, the thread on the OCaml forum was an interesting read and it’s reassuring that Postgres is a solid option.

I should have mentioned that an upcoming project calls for a graph DB and while there are few candidates currently evaluated, none offer OCaml driver. So basically I’m trying to figure out the sanest way to access a DB without a native driver. Going the JS route and worrying about native if it becomes an issue seems like the best option for now.


#4

I think initially JS compilation is a good option. Can get your prototype up and running and then sort out native compilation if performance problems necessitate.


#5

Agreed with @scull7. I was recently experimenting with Postgres and Reason, and things were working well using https://www.npmjs.com/package/bs-knex (which is still in early stages and needs improvements, but is a good start). We’re currently researching https://www.prisma.io/ as a possibility and it’s very interesting, as well. Though I understand that’s an entire service and probably not what you’re looking for.


#6

Just an update here, I’ve been having a good time writing straight SQL and using node-postgres in conjunction with yesql to make positioning arguments easier.

Here’s the simple wrapper I’ve been using:

type client;
[@bs.module "pg"] [@bs.new]
external makePool: {. "connectionString": string} => pool = "Pool";

type dbResult = {. "rows": array(Js.Json.t)};

module Types = {
  type t;

  [@bs.val] external parseInt: string => int = "";
  [@bs.module "pg"] external types: t = "";
  [@bs.send] external setTypeParser: (t, int, string => 'a) => unit = "";

  types->setTypeParser(20, parseInt);
};

type queryPayload;

type queryTemplate('a) = Js.t('a) => queryPayload;
[@bs.module "yesql"] external _payload: string => queryTemplate('a) = "pg";

let payload = (queryString, variables) => _payload(queryString, variables);