Skip to content

ctx.timestamp for views - to query for last_updated. #4533

@luckydonald

Description

@luckydonald

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions