Using Relational Databases
Golem provides WIT packages to integrate with popular relational database systems from any of the supported languages. The currently supported databases are:
- PostgreSQL - see the WIT package (opens in a new tab)
- MySQL - see the WIT package (opens in a new tab)
Adding RDBMS support to a component
The WIT packages are already in the application's root wit/deps
if it was created with golem app new
; to use it, import it in the component's WIT:
import golem:rdbms/mysql@0.0.1;
or
import golem:rdbms/postgres@0.0.1;
Executing SQL statements
To execute an SQL statement with golem-rdbms
, first crete a db-connection
resource and call execute
on it:
use golem_rust::bindings::golem::rdbms::mysql::DbConnection;
let db = DbConnection::open("localhost:3006").expect("Failed to connect to the database");
db.execute(
r#"CREATE TABLE IF NOT EXISTS test_users
(
user_id varchar(25) NOT NULL,
name varchar(255) NOT NULL,
created_on timestamp NOT NULL DEFAULT NOW(),
PRIMARY KEY (user_id)
);"#,
&[],
).expect("CREATE TABLE failed");
Additionally you can:
query
executes a SQL statement and returns a resultquery-stream
executs a SQL statement and returns a streaming resultbegin-transaction
creates a transaction resource on which, in addition to thequery
andexecute
functions, there is also acommit
and arollback
method.