Substreams Sink Database Changes
substreams-sink-database-changes
contains all the definitions for database changes which can be emitted by a substream.
To be consumed by
Install
# The Rust crate is named substreams-database-change for historical reasons
Quickstart
Cargo.toml
[]
= "0.6"
= "2.0"
src/lib.rs
use Error;
use Tables;
use DatabaseChanges;
Reference
Table Operations
-
Create a row
tables.create_row .set;
Creates a new row. Panics if the row is already scheduled for upsert, update, or delete.
-
Upsert a row
tables.upsert_row .set;
Schedules an insert or update (upsert) for the row. Panics if the row is already scheduled for create, update, or delete.
-
Update a row
tables.update_row .set;
Schedules an update for the row. Panics if the row is already scheduled for delete.
-
Delete a row
tables.delete_row;
Schedules a delete for the row. Clears any previously set fields.
All methods support both single and composite primary keys:
tables.create_row;
Automatic Type Transformations
The .set()
method automatically converts many Rust types to database-compatible strings, including:
- Integers:
i8
,i16
,i32
,i64
,u8
,u16
,u32
,u64
bool
String
,&str
BigInt
,BigDecimal
(fromsubstreams::scalar
)prost_types::Timestamp
Vec<u8>
,Hex<T>
(as hex strings)
Custom types can implement the ToDatabaseValue
trait for custom conversion.
For advanced use, .set_raw()
allows setting a field to a raw string value.
Re-generate Protobuf
Be sure to have buf
CLI installed (https://buf.build/docs/installation/) and run:
Release
Follow instructions the CLI is asking, the process is now automatic and version bump and Substreams package building is now all done automatically.