clockworklabs / clockworklabs/SpacetimeDB

`ctx.timestamp` for views - to query for `last_updated`.

Open
#4,533 2 comments 1 reaction 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

I'm trying to create a view which would list me the entires updated in the last 24h.

SELECT *
FROM your_table
WHERE last_updated >= NOW() - INTERVAL '24 HOURS';

Here's the rust code I tried:

use std::time::Duration;

use spacetimedb::{view, AnonymousViewContext, ViewContext, TimeDuration, Query};
use super::tables::{YourTable, your_table__view};
use crate::features::location::tables::your_table__query;


const DURATION_1H: u64 = 3_600;
const DURATION_24H: u64 = 24 * DURATION_1H;

/// Public view of the recently records in your_table.
///
/// Clients can subscribe directly to this view to receive updates on shared your_table entries.
#[view(accessor = recently_changed, public)]
pub fn recently_updated(ctx: &ViewContext) -> impl Query<YourTable> {
    const duration24h: TimeDuration = TimeDuration::from(Duration::from_secs(DURATION_24H));
    let last24h: u64 = ctx.timestamp - duration24h;
    ctx.from.your_table().r#where(|row| row.updated_at.gte(last24h))
}

However, ctx.timestamp is not defined. This is only available in a reducer.
But I don't want to modify any values, I just want to apply a filter.

Is the field ctx.timestamp missing a bug, or is this intended?
If so, how would this be best written in spacetimedb instead?

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 with crates/bindings/src/lib.rs around the reducer timestamp implementation, then compare the available ViewContext API with the reported recently_updated view. Determine whether view timestamps are intended to be supported and identify the supported filtering approach; done means the behavior and any required implementation or documentation are clearly established.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
database
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.