global::mysql

This plugin exposes methods to open a pool of connexions to a mysql database using Rhai.

fn connect

fn connect(parameters: Map) -> MySQL
Open a pool of connections to a MySQL database.
  • parameters - a map of the following parameters:
    • url - a string url to connect to the database.
    • timeout - time allowed between each query to the database. (default: 30s)
    • connections - Number of connections to open to the database. (default: 4)

A service used to query the database pointed by the url parameter.

  • The service failed to connect to the database.
// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_mysql" 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",
    connections: 1,
});

fn query

fn query(database: MySQL, query: SharedObject) -> Array
fn query(database: MySQL, query: String) -> Array
Query the database.
  • query - The query to execute.

A list of records.

Build a service in services/database.vsl;

// Import the plugin stored in the `plugins` directory.
import "plugins/libvsmtp_plugin_mysql" 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",
    connections: 1,
});

Query the database during filtering.

import "services/database" as srv;

#{
    connect: [
        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}`);
            }
        }
    ],
}