Skip to content

Add primary key support for procedural views to rust and ts modules#5111

Open
joshua-spacetime wants to merge 5 commits into
masterfrom
joshua/proc-view-pks
Open

Add primary key support for procedural views to rust and ts modules#5111
joshua-spacetime wants to merge 5 commits into
masterfrom
joshua/proc-view-pks

Conversation

@joshua-spacetime
Copy link
Copy Markdown
Contributor

@joshua-spacetime joshua-spacetime commented May 23, 2026

Description of Changes

Adds support for primary keys to procedural views in rust and typescript modules. Now clients can receive update events when subscribed to such views.

#[spacetimedb::view(accessor = my_players, public, primary_key = id)]
pub fn my_players(ctx: &spacetimedb::ViewContext) -> Vec<Player> {
    ctx.db.players().owner().filter(ctx.sender()).collect()
}
const Player = t.row('Player', {
  id: t.u64().primaryKey(),
  owner: t.identity().index('btree'),
  name: t.string(),
});

export const my_players = spacetimedb.view(
  { public: true },
  t.array(players.rowType),
  ctx => Array.from(ctx.db.players.owner.filter(ctx.sender))
);

In rust, a view's primary key is declared within the #[view] macro, whereas in typescript it is declared at the column/row level as it is for tables. I could have made it a view level attribute for typescript as well, but since primary keys are already row-level attributes in typescript, I just kept that as is.

Note, care must be taken to ensure that the view never returns duplicate primary keys, or else it will fail which will currently result in the transaction that triggered the view refresh to be rolled back. Better error handling for this exact scenario will be added in a separate follow up patch.

Alternative Considered: #[primary_key] attribute on SpacetimeTypes in Rust

This would be equivalent to what we do in typescript.

API and ABI breaking changes

None, although adding a primary key to an existing view will require a client update.

Expected complexity level and risk

3

Testing

Compile-time failure scenarios:

  • Primary key on non-existent column
  • Primary key doesn't reference custom accessor name (rust)
  • Multiple primary key columns specified (typescript)
  • Primary keys must be FilterableValues

SDK tests (rust sdk, rust and ts modules):

  • OnUpdate
  • OnUpdate is sender-scoped
  • Can join on primary key columns

Smoketests:

  • Modifying primary key columns breaks clients (auto-migrations)

@joshua-spacetime joshua-spacetime linked an issue May 23, 2026 that may be closed by this pull request
@joshua-spacetime joshua-spacetime self-assigned this May 23, 2026
@joshua-spacetime joshua-spacetime force-pushed the joshua/proc-view-pks branch 7 times, most recently from a9320f9 to c4bf7b1 Compare May 27, 2026 00:24
@joshua-spacetime joshua-spacetime marked this pull request as ready for review May 27, 2026 00:25
@joshua-spacetime joshua-spacetime requested a review from gefjon May 27, 2026 17:37
Comment thread crates/schema/src/auto_migrate.rs
Copy link
Copy Markdown
Contributor

@gefjon gefjon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have not carefully reviewed the tests or the typescript code, but the Rust stuff looks good to me. The tests are at least at a glance reasonable. I don't love the difference between the Rust and TypeScript designs, but I can live with it, I suppose. Do we have a ticket for the C# and C++ implementations? If not, could you make one?

Copy link
Copy Markdown
Contributor

@JasonAtClockwork JasonAtClockwork left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks good but it does seem there is a missing check on the TypeScript side. For Rust you have the requirement that primary keys for views must be a FilterableValue but on TypeScript that isn't checked.

For example, this will fail in Rust:

#[derive(SpacetimeType)]
pub struct BadViewRow {
    pub identity: TimeDuration,
    pub name: String,
}

#[view(accessor = bad_time_duration_pk, public, primary_key = identity)]
pub fn bad_time_duration_pk(_: &ViewContext) -> Vec<BadViewRow> {
    vec![]
}

However in TypeScript this will compile:

const BadViewRow = t.row('BadViewRow', {
  identity: t.timeDuration().primaryKey(),
  name: t.string(),
});

const spacetimedb = schema({});
export default spacetimedb;

export const bad_time_duration_pk = spacetimedb.view(
  { public: true },
  t.array(BadViewRow),
  () => []
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Procedural view primary keys

4 participants