Skip to content

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

Merged
bfops merged 1 commit into
masterfrom
joshua/proc-view-pks
Jun 4, 2026
Merged

Add primary key support for procedural views to rust and ts modules#5111
bfops merged 1 commit 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),
  () => []
);

@joshua-spacetime
Copy link
Copy Markdown
Contributor Author

For Rust you have the requirement that primary keys for views must be a FilterableValue but on TypeScript that isn't checked.

Note, this is a pre-existing issue. It applies to tables as well. I'll update this in a followup patch since it's not a regression.

@joshua-spacetime joshua-spacetime added this pull request to the merge queue Jun 4, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 4, 2026
@bfops bfops added this pull request to the merge queue Jun 4, 2026
Merged via the queue into master with commit eef0e42 Jun 4, 2026
32 of 33 checks passed
@joshua-spacetime joshua-spacetime deleted the joshua/proc-view-pks branch June 4, 2026 20:42
bfops pushed a commit that referenced this pull request Jun 4, 2026
…t and ts modules (#5111)

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

```rust
pub fn my_players(ctx: &spacetimedb::ViewContext) -> Vec<Player> {
    ctx.db.players().owner().filter(ctx.sender()).collect()
}
```

```ts
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.

in Rust

This would be equivalent to what we do in typescript.

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

3

Compile-time failure scenarios:
- [x] Primary key on non-existent column
- [x] Primary key doesn't reference custom accessor name (rust)
- [x] Multiple primary key columns specified (typescript)
- [x] Primary keys must be `FilterableValue`s

SDK tests (rust sdk, rust and ts modules):
- [x] `OnUpdate`
- [x] `OnUpdate` is sender-scoped
- [X] Can join on primary key columns

Smoketests:
- [x] Modifying primary key columns breaks clients (auto-migrations)
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

5 participants