🐘 SQLDB Postgres Example
This folder contains a WebAssembly component that makes use of:
- The
wasmcloud:postgresWIT contract - The
sqldb-postgres-providerCapability Provider
📦 Dependencies
dockerfor easily running instances of [postgres]cargo(part of the Rust toolchain) for building this project- [
wash][wash] for building and running the components and wasmCloud hosts
👟 Quickstart
As with all other examples, you can get started quickly by using the [Wasmcloud SHell (wash)][wash].
Since wash supports declarative deployments (powered by Wasmcloud Application Deployment Manager (wadm)), you can get started quickly using the wadm.yaml manifest in this folder:
Start a local Postgres cluster
Before we can connect to a Postgres database cluster, we'll need to have one running. You can run one quickly with docker:
docker run \
--rm \
-e POSTGRES_PASSWORD=postgres \
--name pg -p 5432:5432\
postgres:16.2-alpineBuild this component
wash buildThis will create a folder called build which contains sqldb_postgres_query_s.wasm.
[!NOTE] If you're using a local build of the provider (using
file://...inwadm.yaml) this is a good time to ensure you've built the [provider archivepar.gz][par] for your provider.
Start a wasmCloud host
wash up[!NOTE]
wash upwill run as long as the host is running (you can cancel it withCtrl-C)
Set up configuration for the provider
Since configuration for Database clusters is usually sensitive information, we must pre-establish configuration for the provider using the [named configuration feature of wasmCloud][named-config].
Named configuration setup
Set up a named configuration for components that link to the sqldb-postgres provider which is used from wadm.yaml:
wash config put default-postgres \
POSTGRES_HOST=localhost \
POSTGRES_PORT=5432 \
POSTGRES_USERNAME=postgres \
POSTGRES_PASSWORD=postgres \
POSTGRES_DATABASE=postgres \
POSTGRES_TLS_REQUIRED=falseDeploy the example application with WADM
wash app deploy --replace wadm.yaml[!WARNING] If you simply want to stop the deployment, run
wash app delete <application name>.In this case,
wash app delete rust-sqldb-postgresshould work.
To ensure that the application is deployed you can use wadm app list:
wadm app listIf you want to see everything running in the lattice at once:
wash get inventoryInvoke the demo component
Once the component & provider are deployed, you can invoke the example component with wash call:
wash call rust_sqldb_postgres_query-querier wasmcloud:examples/invoke.callNote that the name of the component is prefixed with the WADM application, and the interface on it we call is defined in wit/provider.wit (the call function of the invoke interface).
⌨️ Code guide
With wasmCloud, you write only the important bits of your business logic, so the code for this component is short, with the important bits highlighted below:
impl Guest for QueryRunner {
fn call() -> String {
if let Err(e) = query(CREATE_TABLE_QUERY, &[]) {
return format!("ERROR - failed to create table: {e}");
};
match query(
INSERT_QUERY,
&[PgValue::Text(format!("inserted example row!"))],
) {
Ok(rows) => format!("SUCCESS: inserted new row:\n{rows:#?}"),
Err(e) => format!("ERROR: failed to insert row: {e}"),
}
}
}