Skip to content
GlxymeshDocs
glxymesh.com
Select theme
Open console
Reading time 4 min

Connect to a database

Reach Postgres, MySQL, Redis, MongoDB and other TCP services from a Glxymesh function tool, with the address approved in tool.yml and the password passed in from the vault.

A function tool can open TCP connections, so it can talk to a database or any other service that doesn’t speak HTTPS: Postgres, MySQL, Redis, MongoDB and the like. Like requests, every connection goes through the egress gateway, which holds it to the addresses in tool.yml, refuses private addresses and logs each connection with its bytes and duration.

tools/list-orders/tool.yml
spec: glxymesh/tool@v1
name: list-orders
kind: function
description: Lists a customer's orders from the shop's database, newest first.
read_only: true
inputs:
customer: {type: string, description: "The customer's email, e.g. ada@example.com"}
network:
connect: ["db.example.com:5432"]
secrets:
PG_URL:
hosts: [db.example.com]
mode: pass-in
  • network.connect lists host:port pairs the code may connect to, up to 10. The host is a name or an IP address. A person approves each new one before it goes live, as with hosts.
  • The password, or a whole connection string, is a passed-in key: mode: pass-in. Unlike other keys, its value reaches your code, so a person approves it explicitly before the code can read it. It is fetched for each call and never stored in the function’s configuration.
  • Never take a connection string or password as an input: store it on the Secrets page.

For a database on your own server, keep its address in the vault, as you would for an API on your own server:

network:
connect: ["{PG_HOST}:5432"]
secrets:
PG_URL: {hosts: ["{PG_HOST}"], mode: pass-in}

Store the server’s name or public IP as PG_HOST and the connection string as PG_URL. The connection string can name the server itself: a connection to the address PG_HOST holds is allowed too.

Give the driver the platform’s connection instead of letting it open its own.

tools/list-orders/tool.go
package listorders
import (
"github.com/jackc/pgx/v5"
"glxymesh.com/sdk/glxy"
)
func Run(ctx *glxy.Ctx, in Input) (any, error) {
dsn, err := ctx.SecretValue("PG_URL")
if err != nil {
return nil, err
}
cfg, err := pgx.ParseConfig(dsn)
if err != nil {
return nil, glxy.Errorf("PG_URL isn't a Postgres connection string: %v", err)
}
cfg.DialFunc = ctx.DialContext
conn, err := pgx.ConnectConfig(ctx, cfg)
if err != nil {
return nil, err
}
defer conn.Close(ctx)
rows, _ := conn.Query(ctx, "SELECT id, total FROM orders WHERE customer = $1 ORDER BY created_at DESC LIMIT 20", in.Customer)
return pgx.CollectRows(rows, pgx.RowToMap)
}

ctx.DialContext has the signature drivers take as a dial hook: pgx’s DialFunc, go-redis’s Dialer, and others. Imports resolve when the platform builds, so go.mod needs no extra lines.

Connections end with the call: open one per call and close it.

Keep TLS on. With Go’s ctx.DialContext the driver still knows the real server name, so sslmode=verify-full works. With ctx.forward the driver connects to 127.0.0.1: tell it the server’s name for verification where it can (pg’s ssl.servername), or use sslmode=require.

gxmesh call and gxmesh dev connect the same way, with the keys from .env.local. Private addresses are refused locally too; for a database on your machine or your network, add --allow-private:

Terminal window
gxmesh call list-orders '{"customer":"ada@example.com"}' --allow-private

Describe the tools and say where the connection string lives, for example “Postgres tools for my own database: read orders by customer and mark one shipped. Keep the connection string in the vault.” Glxymesh writes function tools with network.connect and a passed-in key, and you approve both before anything goes live. In a Go project the driver is ready at once; in TypeScript and Python, pin the driver it names in your lock file.

UDP, interactive SSH sessions and listening for incoming connections aren’t available. Use an HTTPS API where the service offers one.