xk6-sql
    Preparing search index...

    Interface Database

    Database is a database handle representing a pool of zero or more underlying connections.

     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");
    interface Database {
        close(): void;
        exec(query: string, ...args: any[]): Result;
        execWithTimeout(timeout: string, query: string, ...args: any[]): Result;
        query(query: string, ...args: any[]): Row[];
        queryWithTimeout(timeout: string, query: string, ...args: any[]): Row[];
    }
    Index

    Methods

    • Close the database and prevents new queries from starting.

      Close waits for all queries that have started processing on the server to finish.

      Returns void

       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 teardown() {
      db.close();
      }
    • Execute a query without returning any rows.

      Parameters

      • query: string

        the query to execute

      • ...args: any[]

        placeholder parameters in the query

      Returns Result

      summary of the executed SQL commands

       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
      );
      `);

      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`);
      }
    • Execute a query (with a timeout) without returning any rows. The timeout parameter is a duration string, a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

      Parameters

      • timeout: string

        the query timeout as a duration string

      • query: string

        the query to execute

      • ...args: any[]

        placeholder parameters in the query

      Returns Result

      summary of the executed SQL commands

       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
      );
      `);

      let result = db.execWithTimeout("10s", `
      INSERT INTO roster
      (given_name, family_name)
      VALUES
      ('Peter', 'Pan'),
      ('Wendy', 'Darling'),
      ('Tinker', 'Bell'),
      ('James', 'Hook');
      `);
      console.log(`${result.rowsAffected()} rows inserted`);
      }
    • Query executes a query that returns rows, typically a SELECT.

      Parameters

      • query: string

        the query to execute

      • ...args: any[]

        placeholder parameters in the query

      Returns Row[]

      rows of the query result

       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 default function () {
      let rows = db.query("SELECT * FROM roster WHERE given_name = $1;", "Peter");
      for (const row of results) {
      console.log(`${row.family_name}, ${row.given_name}`);
      }
      }
    • Query executes a query (with a timeout) that returns rows, typically a SELECT. The timeout parameter is a duration string, a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".

      Parameters

      • timeout: string

        the query timeout as a duration string

      • query: string

        the query to execute

      • ...args: any[]

        placeholder parameters in the query

      Returns Row[]

      rows of the query result

       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 default function () {
      let rows = db.queryWithTimeout("10s", "SELECT * FROM roster WHERE given_name = $1;", "Peter");
      for (const row of results) {
      console.log(`${row.family_name}, ${row.given_name}`);
      }
      }