Skip to content

clients.postgresql_client #

PostgreSQL Client

The PostgreSQL client provides a simple interface to interact with PostgreSQL databases through HeroScript.

Configuration

The PostgreSQL client can be configured using HeroScript. Configuration settings are stored on the filesystem for future use.

Basic Configuration Example

#!/usr/bin/env -S v -n -w -gc none  -cc tcc -d use_openssl -enable-globals run

import freeflowuniverse.herolib.core
import freeflowuniverse.herolib.clients.postgresql_client


// Configure PostgreSQL client
heroscript := "
!!postgresql_client.configure 
    name:'test'
    user: 'postgres'
    port: 5432
    host: 'localhost'
    password: '1234'
    dbname: 'postgres'
"

// Process the heroscript configuration
postgresql_client.play(heroscript: heroscript)!

// Get the configured client
mut db_client := postgresql_client.get(name: "test")!

// Check if test database exists, create if not
if !db_client.db_exists('test')! {
    println('Creating database test...')
    db_client.db_create('test')!
}

// Switch to test database
db_client.dbname = 'test'

// Create table if not exists
create_table_sql := "CREATE TABLE IF NOT EXISTS users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(255) UNIQUE NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)"

println('Creating table users if not exists...')
db_client.exec(create_table_sql)!

println('Database and table setup completed successfully!')


Configuration Parameters

ParameterDescriptionDefault Value
nameUnique identifier for this configuration'default'
userPostgreSQL user'root'
portPostgreSQL server port5432
hostPostgreSQL server host'localhost'
passwordPostgreSQL user password''
dbnameDefault database name'postgres'

Database Operations

Check Connection

// Check if connection is working
db_client.check()!

Database Management

// Check if database exists
exists := db_client.db_exists('mydb')!

// Create database
db_client.db_create('mydb')!

// Delete database
db_client.db_delete('mydb')!

// List all databases
db_names := db_client.db_names()!

Query Execution

// Execute a query
rows := db_client.exec('SELECT * FROM mytable;')!

// Query without semicolon is automatically appended
rows := db_client.exec('SELECT * FROM mytable')!

Backup Functionality

The client provides functionality to backup databases:

// Backup a specific database
db_client.backup(dbname: 'mydb', dest: '/path/to/backup/dir')!

// Backup all databases
db_client.backup(dest: '/path/to/backup/dir')!

Backups are created in custom PostgreSQL format (.bak files) which can be restored using pg_restore.

Constants #

const version = '0.0.0'

fn exists #

fn exists(args_ ArgsGet) !bool

check we find the config on the filesystem

fn get #

fn get(args_ ArgsGet) !&PostgresClient

fn heroscript_default #

fn heroscript_default() !string

fn load #

fn load(args_ ArgsGet) !

load the config error if it doesn't exist

fn play #

fn play(args_ PlayArgs) !

fn save #

fn save(o PostgresClient) !

save the config to the filesystem in the context

fn set #

fn set(o PostgresClient) !

set the model in mem and the config on the filesystem

struct ArgsGet #

@[params]
struct ArgsGet {
pub mut:
	name string
}

///////FACTORY

struct BackupParams #

@[params]
struct BackupParams {
pub mut:
	dbname string
	dest   string
}

struct PlayArgs #

@[params]
struct PlayArgs {
pub mut:
	heroscript string // if filled in then plbook will be made out of it
	plbook     ?playbook.PlayBook
	reset      bool
}

struct PostgresClient #

@[heap]
struct PostgresClient {
mut:
	db_ ?pg.DB
pub mut:
	name     string = 'default'
	user     string = 'root'
	port     int    = 5432
	host     string = 'localhost'
	password string
	dbname   string = 'postgres'
}

THIS THE THE SOURCE OF THE INFORMATION OF THIS FILE, HERE WE HAVE THE CONFIG OBJECT CONFIGURED AND MODELLED

fn (PostgresClient) backup #

fn (mut self PostgresClient) backup(args BackupParams) !

fn (PostgresClient) check #

fn (mut self PostgresClient) check() !

fn (PostgresClient) db #

fn (mut self PostgresClient) db() !pg.DB

fn (PostgresClient) db_create #

fn (mut self PostgresClient) db_create(name_ string) !

fn (PostgresClient) db_delete #

fn (mut self PostgresClient) db_delete(name_ string) !

fn (PostgresClient) db_exists #

fn (mut self PostgresClient) db_exists(name_ string) !bool

fn (PostgresClient) db_names #

fn (mut self PostgresClient) db_names() ![]string

fn (PostgresClient) exec #

fn (mut self PostgresClient) exec(c_ string) ![]pg.Row