MySQL
MySQL is a relational database management system. This plugin provide a bridge for MySQL databases.
Usage
In this example, we will setup the plugin to be used in the SMTP SMTP Receiver service. First, create a connection pool to the MySQL database.
// /etc/vsmtp/smtp-receiver/services/db.rhai
// Import the plugin stored in the `plugins` directory.
import "plugins/libmysql_plugin" as mysql;
export const database = mysql::connect(#{
// Connect to a database on the system with the 'greylist-manager' user and 'my-password' password.
url: "mysql://localhost/?user=greylist-manager&password=my-password",
timeout: "1m",
// Number of connections to open in the pool.
connections: 4,
});
Then query the database in your rules.
import "services/db" as srv;
fn on_connect(ctx) {
ctx.run([
action "get records from my database" || {
// For the sake of this example, we assume that there is a populated
// table called 'my_table' in the database.
const records = srv::database.query("SELECT * FROM my_table");
// `records` is an array, we can run a for loop and print all records.
log("info", "fetching mysql records ...");
for record in records {
log("info", ` -> ${record}`);
}
}
]);
}