Using ReScript on Server side/Backend Service

Hi All,

I am trying do POC on using ReScript with NodeJS. So far I was able to connect to backend MySQL database and return simple “hello from database”. But code to access DB is in commonjs format.

Now I am trying ti write a module for Database connectivity, query etc. Just wanted to know if anyone done this or is this make sense.

Also I am trying to follow guide here > Bind to JS Function | ReScript Language Manual.

See section => “Solution: Use Guaranteed Uncurrying”.

Right now just trying to write connect method using above method in DB module.

Please see line 64 >

I am trying to figure out, but just wanted to know your thoughts on this.


Hi @jaravan, can you share example JS code you’re using to connect to MySQL?

Okay, so I was able to connect and query database. I am new to ReScript, so yet to figure out best practices, but this code works.

module DB = {

    type rowDataPacket = {
        message: string

    type connectionConfig = {
        host: string,
        user: string,
        password: string,
        database: string
    type connection

    //NOTE: A labeled parameter starts with a `~`.
    @module("mysql") external createConnection: (~config: connectionConfig) => connection = "createConnection"

    @send external connect: (connection,  @uncurry ('a => unit))  => unit = "connect"

    @send external query: (connection,  string, @uncurry (('a, 'b, 'c) => unit))  => unit = "query"
    let testDB = () => {
        let myValue = ref("")

        let config : connectionConfig = {
            host     : "localhost",
            user     : "USERNAME",
            password : "PASSWORD",
            database : "DBNAME"
        let conn = createConnection(~config = config)
        connect(conn, (err) => {
            Js.log("Error during connection. ")
            let message = err ? "failed": "connected"
            query(conn, "SELECT message FROM greeting limit 1;", (err, results, _fields) =>{
                myValue.contents = results[0].message

Nice work @jaravan

Next you might like to try improve the types on your callbacks.

For example, for your connect() function:

type connectCallback = Js.Nullable.t<Js.Exn.t> => unit
@send external connect: (connection, connectCallback) => unit = "connect"

And your query() function:

type row
type fields
type queryCallback = (
) => unit
@send external query: (connection, string, queryCallback) => unit = "query"

You can convert the Nullable values into option values using Js.Nullable.toOption() so you can then use switch on the result.