Skip to main content

Table Store

Table<V> is the SDK abstraction for the table model - a named collection of typed entries, each identified by a key. This page explains each operation in detail.

Declare

A table is declared as a handle bound to a name and a value type. Declare it as a module-level constant:

use myrmic_sdk::db::table::Table;
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Device {
// ...
}

const DEVICES: Table<Device> = Table::new("devices");

This binds to the default scope - private to the cell. To bind to a custom scope, pass it explicitly using new_in:

const DEVICES: Table<Device> = Table::new_in("devices", Scope::public("my-app"));

Insert

The table handle exposes an insert method. It takes a reference to the value and stores it under a runtime-assigned UUID:

DEVICES.insert(&device)?;

To assign an explicit key, use insert_with:

DEVICES.insert_with("sensor-01", &device)?;

Get by ID

The table handle exposes a get method. It returns the entry stored under that key, or None if nothing is there:

match DEVICES.get("sensor-01")? {
Some(device) => { /* use device */ }
None => { /* not found */ }
}

Delete

The table handle exposes a delete method. It removes the entry under that key:

DEVICES.delete("sensor-01")?;

Count

The table handle exposes a count method. It returns the number of entries in the table:

let count = DEVICES.count()?;

Iterate

The table offers three ways to iterate over its entries:

  1. for_each - applies a closure to each entry in ascending key order:
DEVICES.for_each(|device| {
// handle device
})?;

Entries that fail to decode are skipped.

Use for_each_rev to iterate in descending order.

  1. iter - returns a lazy iterator over (key, value) pairs, surfacing decode errors:
for entry in DEVICES.iter() {
let (key, device) = entry?;
// handle key and device
}

Use iter_rev to iterate in descending order.

  1. list - collects all table entries into memory at once:
let devices = DEVICES.list()?;

for device in devices {
// handle device
}

Use list_rev to collect in descending order.

  1. to_map - collects the full table into a BTreeMap<K, V>:
let map = DEVICES.to_map()?;

for (key, device) in &map {
// handle key and device
}

Prefer for_each or iter for large tables - they process entries one at a time and keep memory use low.

Keys

The table handle offers two ways to access its keys:

  1. keys - returns a lazy iterator over keys in ascending order, surfacing errors:
for key in DEVICES.keys() {
let key = key?;
// handle key
}

Use keys_rev to iterate in descending order.

  1. ids - collects all keys into memory at once:
let ids = DEVICES.ids()?;

for id in ids {
// handle id
}

Prefer keys for large tables - ids loads all keys into memory at once and causes higher memory use.

See also

Cookie Policy