Mercury Documentation
  • Get Started with Mercury
    • Pricing
    • Endpoints
    • Authentication
  • The Definitive Guide to Understanding The Mercury's Stack Vision
  • Retroshades
    • Introduction to Retroshades
    • Get Started
      • Importing the Retroshades SDK
      • Writing and Emitting Retroshades
      • Deploying to Mercury Retroshades
      • Querying Retroshades
  • Zephyr: Full Customization
    • Introduction
    • General concepts
      • Zephyr Programs
      • Accessing the Ledger: Contract Entries
      • Accessing the Ledger Meta: Contract Events
      • Database Interactions
      • Create Custom Callable APIs: Serverless Functions
      • Catchups
      • Using Soroban inside Zephyr
    • Quickstart
    • Learn
      • Introduction to Mercury's Cloud and the Zephyr Stack
      • Get Started: Set Up and Manage the Project
        • Create the Project
        • Writing the Program
        • Local Testing
        • Deploy
        • Data catchups/backfill
        • Monitor Execution
        • Querying
      • Database Interactions
        • Zephyr.toml: Define the Tables Structure
        • Understanding Database Interactions
        • Database Operations
      • Accessing the Ledger
      • Accessing Ledger Transition: Soroban Events
      • Working with Contract Custom Types
      • Working with Soroban SDK Types
      • Web Requests, Automation and Alerts.
      • Zephyr.toml Extensions: Indexes And Dashboard
      • Reading From Indexes/External Tables
      • Custom APIs - Serverless Functions
        • General Concepts
        • Custom RPC-alike Endpoints
        • Querying APIs for Composable Data
      • Data Catchups/Backfill
      • Custom Dashboards
        • Creating the Dashboard
        • Plotting: Simple
        • Complex Plotting
    • Support
  • Mercury "Classic"
    • Subscriptions
      • API Definition
    • Queries
      • Contract Events
      • Contract Data Entry Updates
      • Stellar Operations, Balances, and Account Objects
  • TUTORIALS
    • Zephyr
      • Self-hosting Zephyr
      • No-RPC Dapp
      • Indexing a DeFi liquidity pool (Blend)
      • Building a Secure DeFi Real-Time Bot Through Smart Accounts
      • Monitoring Large Deposits with Zephyr and Sending Web Alerts
    • Mercury Classic
      • Index and query contract events
Powered by GitBook
On this page
  • Write
  • Read
  • Update
  1. Zephyr: Full Customization
  2. Learn
  3. Database Interactions

Database Operations

Learn about Zephyr's database operations.

Zephyr currently supports the following types of database interactions:

  • Write.

  • Read.

  • Update.

On the guest level, there is no auth to handle so this is pretty straightforward as you don't have to grant any special permissions.

Write

In the Zephyr VM, or better in the Mercury abstraction of the VM implementation, a write operation translates to a SQL insert statement and it is performed by providing a constructed DatabaseDerive object to the EnvClient::put() function:

#[derive(DatabaseDerive, Clone)]
#[with_name("test")]
struct TestTable {
    hello: ScVal,
}

#[no_mangle]
pub extern "C" fn on_close() {
    let env = EnvClient::new();
    // define the message
    let table = TestTable {
        hello: message.clone(),
    };
    
    table.put(&env);
}

In this case, "table" is a DatabaseDerive object and represents a new row in the table.

Read

Reading means returning an iterator (a vec currently) over DatabaseDerive elements, as many as the rows present in the requested table. It is done by simply calling EnvClient::read() while inferring the requested table type (which will automatically translate to reading from the corresponding table):

let rows = env.read::<TestTable>();
for row in rows {
    // ...
}

Update

Updating lets you update a selection of the table by changing some rows with a new given one. You have to specify the conditions under which to change the rows, and the new row as a DatabaseDerive element:

let table = TestTable {
    hello: message1.clone(),
};

// note: message1 is an ScVal
env.update().column_equal_to_xdr("hello", &message).execute(&table)

In this snippet, all rows where the hello column equals to the message ScVal will be updated with the row defined by table.

PreviousUnderstanding Database InteractionsNextAccessing the Ledger

Last updated 10 months ago