xk6-sql

Use SQL databases from k6 tests.

xk6-sql is a Grafana k6 extension that enables the use of SQL databases in k6 tests.

In order to use the xk6-sql API, in addition to the k6/x/sql module, it is also necessary to import at least one driver module. The default export of the driver module is a driver identifier symbol, which should be passed as a parameter of the open() function.

The driver module is typically available at k6/x/sql/driver/FOO, where FOO is the name of the driver.

 import sql from "k6/x/sql";

// the actual database driver should be used instead of ramsql
import driver from "k6/x/sql/driver/ramsql";

const db = sql.open(driver, "roster_db");

export function setup() {
db.exec(`
CREATE TABLE IF NOT EXISTS roster
(
id INTEGER PRIMARY KEY AUTOINCREMENT,
given_name VARCHAR NOT NULL,
family_name VARCHAR NOT NULL
);
`);
}

export function teardown() {
db.close();
}

export default function () {
let result = db.exec(`
INSERT INTO roster
(given_name, family_name)
VALUES
('Peter', 'Pan'),
('Wendy', 'Darling'),
('Tinker', 'Bell'),
('James', 'Hook');
`);
console.log(`${result.rowsAffected()} rows inserted`);

let rows = db.query("SELECT * FROM roster WHERE given_name = $1;", "Peter");
for (const row of rows) {
console.log(`${row.family_name}, ${row.given_name}`);
}
}

Index

Interfaces

Functions