The fastest [1] JavaScript MySQL client.
import { Connection } from "https://deno.land/x/mysql_native/mod.ts";
const conn = new Connection();
conn.connect({
unixSocket: "/tmp/mysql.sock",
// ...
//
// setup connection pool
// pool: 3,
});
// -- Connection methods
conn.execute("INSERT INTO test VALUES (1, 'deno')");
const result = conn.query("SELECT * FROM test");
result.all(); // [ [ 1, "deno" ] ]
const iter = conn.query("SELECT * FROM test");
for (const row of iter) {
row; // [1, "deno"]
}
conn.close();
// -- Prepared statements
const stmt = conn.prepare("INSERT INTO test2 VALUES (?, ?)");
stmt.run(2, "deno");
stmt.all();
stmt.close();
// -- Transactions
const tx = conn.tx((conn) => {
conn.execute("...");
return conn.query("...");
});
It exposes a synchronous & minimal API.
A non-blocking async API is planned but for now use the battle tested
denodrivers/mysql
module.
- Performance
- Prepared statements
- Bind parameters
- Non blocking API
- SSL
- Connection pooling
mysql_native
dispatches almost zero-overhead calls to libmysqlclient
using
Deno FFI.
sys.ts
provides direct access to unsafe libmysqlcient
API.
import { mysql_stmt_init } from "https://deno.land/x/mysql_native/sys.ts";
const handle = mysql_stmt_init(null);
mysql_real_connect(
handle,
encode("host\0"),
encode("user\0"),
encode("password\0"),
encode("db\0"),
port,
socket ? encode("socketPath\0") : null,
0,
);
mysql_real_query(handle, encode("SELECT * FROM test"), 18);