Skip to content

mysql

The internal:mysql plugin executes queries against a MySQL database using the mysql2 driver.

x-plenum-upstream:
kind: "plugin"
plugin: "internal:mysql"
options:
host: "${{ env.DB_HOST }}"
port: "${{ env.DB_PORT }}"
database: "${{ env.DB_NAME }}"
user: "${{ env.DB_USER }}"
password: "${{ env.DB_PASSWORD }}"
permissions:
env: ["DB_HOST", "DB_PORT", "DB_NAME", "DB_USER", "DB_PASSWORD"]
net: ["${{ env.DB_HOST }}"]

Options passed to the plugin at registration become the connection pool configuration:

FieldTypeDefaultDescription
hoststring"localhost"Database host
portstring | number3306Database port
databasestring— (required)Database name
userstring— (required)Database user
passwordstring— (required)Database password
sslanyEnable SSL. When set, uses rejectUnauthorized: false
max_connectionsstring | number10Maximum pool size (connectionLimit)

The plugin verifies connectivity at startup by running SELECT 1. If the connection fails, the gateway refuses to start.

Pass query configuration via x-plenum-backend:

actions:
- target: "$.paths['/users'].get"
update:
x-plenum-backend:
query: "SELECT id, name, email FROM users"
- target: "$.paths['/users/{id}'].get"
update:
x-plenum-backend:
query: "SELECT id, name, email FROM users WHERE id = ?"
params:
- "${{path.id}}"
FieldTypeDefaultDescription
querystring— (required)SQL query with ? positional parameters
params(string | number)[][]Values for positional parameters. The gateway resolves ${{...}} tokens at request time
fieldsRecord<string, string>Map database column names to response field names
returnsstringJSON Pointer to extract from the result. "/0" returns the first row as an object instead of an array

Always put request-derived values in params, never inline in the query string. The gateway resolves ${{...}} tokens in params at request time and passes resolved values as native query parameters:

x-plenum-backend:
query: "SELECT id, name FROM users WHERE role = ? AND active = ?"
params:
- "${{query.role}}"
- "${{query.active}}"

MySQL uses ? placeholders. PostgreSQL uses $1, $2, … See the PostgreSQL plugin for positional syntax.

Rename columns in the response using fields:

x-plenum-backend:
query: "SELECT user_id, full_name FROM users"
fields:
user_id: "id"
full_name: "name"

Response becomes [{ "id": 1, "name": "Alice" }] instead of [{ "user_id": 1, "full_name": "Alice" }].

Use returns: "/0" to unwrap the result array when the query returns a single row:

x-plenum-backend:
query: "SELECT id, name FROM users WHERE id = ?"
params:
- "${{path.id}}"
returns: "/0"

Without "/0", the response is [{ "id": 1, "name": "Alice" }]. With it: { "id": 1, "name": "Alice" }.

If the row is not found, the plugin returns 404.

StatusCondition
200Query succeeded
404returns: "/0" used and no row found
500Pool not initialised, query error, or missing config

The 500 response body includes the error message from the driver.