bs-mysql2

10.0.2 • Public • Published

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 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.

NOTE: If you're trying to run the tests on macOS then you will need to: brew install watchman

Usage

Standard Callback Interface

Standard Query Method

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);
});
 

Prepared Statements

Named Placeholders
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);
});
Unnamed Placeholders
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);
});

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?

bs-mysql2 examples assume that there is:

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...

Versions

Current Tags

  • Version
    Downloads (Last 7 Days)
    • Tag
  • 10.0.2
    4
    • latest

Version History

Package Sidebar

Install

npm i bs-mysql2

Weekly Downloads

45

Version

10.0.2

License

MIT

Unpacked Size

48.5 kB

Total Files

26

Last publish

Collaborators

  • scull7