clockworklabs / clockworklabs/SpacetimeDB

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

Open
#5,586 1 comment 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Rust
Stars
25.2k
Forks
1.1k
Avg merge
2d 7h
Merged PRs (30d)
46

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the proposed hook signatures in the issue and the referenced SpacetimeDSL docs/DOCUMENTATION.md hooks-system section. Compare the before and after APIs for insert, update, upsert, and delete, including their row arguments and error handling. Done means the requested hook behavior and API are defined and implemented consistently across those actions.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
database
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.