8
66%
MIT
ReasonML bindings to the mysql2 library.

NPM Build Status Coverage Status

bs-mysql2

ReasonML bindings to the mysql2 library.

This is a very rough implementation that will enable very simple use cases.

Initially this was just a copy of bs-mysql.

Why?

The main difference between these bindings and the bs-mysql bindings is the use of the mysql2 library / driver over the mysql (version 1) driver. You can see the reasoning behind the new mysql2 driver here: History and Why MySQL2

Version 2

Version 2 of this driver removed most of the API surface area. This is now intended as a module which better interfaces are built on top of, yet it still quite usable.

However, if you are looking for a higher level interface then you should look at the bs-sql-common library. This library can be used along side bs-sql-common as a data provider.

Status

Not all of the mysql2 library features are implemented but there is a usable implementation of the Promise based wrapper and Named Placeholders.

Usage

Standard Callback Interface

Standard Query Method

  • RE
  • ML
let conn
  = MySql2.Connection.connect(~host=127.0.0.1, ~port=3306, ~user="root", ());

MySql2.execute(conn, "SHOW DATABASES", None, res => {
    switch res {
    | `Error(e) => Js.log2("ERROR: ", e)
    | `Select(select) => Js.log2("SELECT: ", select)
    | `Mutation(mutation) => Js.log2("MUTATION: ", mutation)
    }
  MySql2.close(conn);
});
3010: <UNKNOWN SYNTAX ERROR>

Prepared Statements

Named Placeholders
  • RE
  • ML
let conn
  = MySql2.Connect.connect(~host=127.0.0.1, ~port=3306, ~user="root", ());

let named = MySql2.Params.named(
  Json.Encode.object_([
    ("x", Json.Encode.int(1)),
    ("y", Json.Encode.int(2)),
  ])
);

MySql2.execute(conn, "SELECT :x + :y AS result", Some(named), res => {
    switch res {
    | `Error(e) => Js.log2("ERROR: ", e)
    | `Select(select) => Js.log2("SELECT: ", select)
    | `Mutation(mutation) => Js.log2("MUTATION: ", mutation)
    }
  }
  MySql2.close(conn);
});
3010: <UNKNOWN SYNTAX ERROR>
Unnamed Placeholders
  • RE
  • ML
let conn
  = MySql2.Connection.connect(~host=127.0.0.1, ~port=3306, ~user="root", ());

let positional = MySql2.Params.positional(
  Belt_Array.map([|5, 6|], Json.Encode.int) |> Json.Encode.jsonArray
);

MySql2.execute(conn, "SELECT 1 + ? + ? AS result", Some(positional), res => {
    switch res {
    | `Error(e) => Js.log2("ERROR: ", e)
    | `Select(rows, meta) => Js.log2("SELECT: ", rows, meta)
    | `Mutation(count, id) => Js.log2("MUTATION: ", count, id)
    }
  }
  MySql2.close(conn);
});
3010: <UNKNOWN SYNTAX ERROR>

How do I install it?

Inside of a BuckleScript project:

yarn install --save bs-mysql2 @glennsl/bs-json

Then add bs-mysql2 and @glennsl/bs-json to your bs-dependencies in bsconfig.json:

{
  "bs-dependencies": ["bs-mysql2", "@glennsl/bs-json"]
}

How do I use it?

Use it in your project

See the Usage section above...

Run the examples

yarn run examples:simple
yarn run examples:prepared-statements

What's missing?

Mostly everything...