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.
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 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.
Not all of the mysql2 library features are implemented but there is a usable implementation of the Promise based wrapper and Named Placeholders.
Faster / Better Performance (kind of get this for free)
MySQL Binary Log Protocol
Extended support for Encoding and Collation
Compression
SSL and Authentication Switch
Pooling
NOTE: If you're trying to run the tests on macOS then you will need to:
brew install watchman
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>
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>
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>
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"]
}
See the Usage section above...
yarn run examples:simple
yarn run examples:prepared-statements
Mostly everything...