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 > https://github.com/mysqljs/mysql/blob/master/lib/Connection.js

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

Thanks,
Jay

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)
        
        //Js.log(conn)    
        connect(conn, (err) => {
            Js.log("Error during connection. ")
            Js.log(err)
            let message = err ? "failed": "connected"
            Js.log(message)
            query(conn, "SELECT message FROM greeting limit 1;", (err, results, _fields) =>{
                Js.log(err);
                Js.log(results[0].message);
                myValue.contents = results[0].message
                //Js.log(fields);
            });
        });
        myValue.contents
    }                      
}
2 Likes

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 = (
  Js.Nullable.t<Js.Exn.t>,
  Js.Nullable.t<array<row>>,
  Js.Nullable.t<fields>,
) => 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.

2 Likes