Add primary key support for procedural views to rust and ts modules#5111
Open
joshua-spacetime wants to merge 5 commits into
Open
Add primary key support for procedural views to rust and ts modules#5111joshua-spacetime wants to merge 5 commits into
joshua-spacetime wants to merge 5 commits into
Conversation
a9320f9 to
c4bf7b1
Compare
c4bf7b1 to
2bcf3f4
Compare
2bcf3f4 to
d03addf
Compare
kistz
reviewed
May 29, 2026
gefjon
approved these changes
May 29, 2026
Contributor
gefjon
left a comment
There was a problem hiding this comment.
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?
Contributor
JasonAtClockwork
left a comment
There was a problem hiding this comment.
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),
() => []
);
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.
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 RustThis 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:
FilterableValuesSDK tests (rust sdk, rust and ts modules):
OnUpdateOnUpdateis sender-scopedSmoketests: