Welcome to the new Golem Cloud Docs! 👋
Common Guides
Using Relational Databases

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:

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 result
  • query-stream executs a SQL statement and returns a streaming result
  • begin-transaction creates a transaction resource on which, in addition to the query and execute functions, there is also a commit and a rollback method.