Skip to main content

Sqlite

SQLite is a library that implements a small SQL database engine. This plugin provide a bridge for SQLite 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 SQLite database.

// /etc/vsmtp/smtp-receiver/services/db.rhai
// Import the plugin stored in the `plugins` directory.
import "plugins/libsqlite_plugin" as sqlite;

export const bridge = sqlite::connect(#{
// Connect to a database on the system with the path "./src/plugins/vsmtp-plugin-sqlite/greylist.db"
path: "./src/plugins/vsmtp-plugin-sqlite/greylist.db",
timeout: "1m",
connections: 4,
});
import "services/db" as db;

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 = db::bridge.query("SELECT * FROM my_table");

// `records` is an array, we can run a for loop and print all records.
log("info", "fetching sqlite records ...");
for record in records {
log("info", ` -> ${record}`);
}
}
]);
}