Skip to content

Feature Request: <Before|After><Insert|Update|Delete>Hook #5586

Description

@tamaro-skaljic

Instead of

  • telling all my colleagues to always remember to call a custom insert, update or delete helper function which uses ctx.db.<table> .<action>() or .<index>().<action>() and does something before and/or afterwards (in server module code) rather than using ctx.db directly (because the logic executed before/after it in the helper function is crucial for e.g. referential integrity, authorization, etc.),

I want to

  • hook into SpacetimeDBs `<insert|update|upsert|delete> functionality so that SpacetimeDB makes sure that it's happening.

Proposed API:

// Position table struct omitted…

/// Before Insert Hooks allow validating or changing rows before insertion
#[spacetimedb::hook(
    moment = before,
    action = insert,
    table = position,
)]
pub fn before_position_insert(
    ctx: &TxContext,
    new_position: Position,
) -> Result<Position, impl Display>;

/// After Insert Hooks allow to react on row insertions
#[spacetimedb::hook(
    moment = after,
    action = insert,
    table = position,
)]
pub fn after_position_insert(
    ctx: &TxContext,
    new_position: &Position,
) -> Result<(), impl Display>;

/// Before Update Hooks allow validating or changing rows before modification
#[spacetimedb::hook(
    moment = before,
    action = update,
    table = position,
)]
pub fn before_position_update(
    ctx: &TxContext,
    old_position: &Position,
    new_position: Position,
) -> Result<Position, impl Display>;

/// After Update Hooks allow to react on row modifications
#[spacetimedb::hook(
    moment = after,
    action = update,
    table = position,
)]
pub fn after_position_update(
    ctx: &TxContext,
    old_position: &Position,
    new_position: &Position,
) -> Result<(), impl Display>;

/// Before Delete Hooks allow validating before deletion
#[spacetimedb::hook(
    moment = before,
    action = delete,
    table = position,
)]
pub fn before_position_delete(
    ctx: &TxContext,
    old_positions: &Vec<Position>,
) -> Result<(), impl Display>;

/// After Delete Hooks allow to react on row deletions
#[spacetimedb::hook(
    moment = after,
    action = delete,
    table = position,
)]
pub fn after_position_delete(
    ctx: &TxContext,
    old_positions: Vec<Position>,
) -> Result<(), impl Display>;

Currently implemented in https://github.com/tamaro-skaljic/SpacetimeDSL/blob/main/docs/DOCUMENTATION.md#hooks-system

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions