SpacetimeDB icon indicating copy to clipboard operation
SpacetimeDB copied to clipboard

Table Insert Problems

Open KMUS1997 opened this issue 9 months ago • 1 comments

I wish to do a cached message for client, to avoid all message sent to a new client. but we dont have a way to do that, then I manage to maintain two tables to do that. on for keep all ,one for keep the newst 10 messages.

but both dont works.

SQL can not support the ORDER and when Insert to a table, it can not insert in order also。

so how can I do, or can you make some method to do that,

not everything need to sync。。

PS /home/ubuntu> spacetime sql combat "SELECT * FROM world_message" WARNING: This command is UNSTABLE and subject to breaking changes.

sender | name | sent | text -------------------------------------------------------------------------------+----------------+------------------+------ 87749735942141927258676195533202707267511107059813012757897815701149281837291 | "我是头号玩家" | 1741868642733860 | "16" 87749735942141927258676195533202707267511107059813012757897815701149281837291 | "我是头号玩家" | 1741868642733624 | "15" 87749735942141927258676195533202707267511107059813012757897815701149281837291 | "我是头号玩家" | 1741868642733379 | "14" 87749735942141927258676195533202707267511107059813012757897815701149281837291 | "我是头号玩家" | 1741868642733152 | "13" 87749735942141927258676195533202707267511107059813012757897815701149281837291 | "我是头号玩家" | 1741868642732828 | "12" 87749735942141927258676195533202707267511107059813012757897815701149281837291 | "我是头号玩家" | 1741868642732522 | "11"

the code is show bellow

    ctx.db.world_message_all().insert(new_message.clone());
    ctx.db.world_message().insert(new_message)
    // more than 10 ,clear all of the table
    if ctx.db.world_message().count() >= 10 {
        // delete
        for msg in ctx.db.world_message().iter() {
            ctx.db.world_message().delete(msg);
        }
    }

    Ok(())
}

KMUS1997 avatar Mar 13 '25 12:03 KMUS1997

Applogies to necro an old issue here but yes you would need to manually do this through code using 2 tables, one table which contains all messages and then another table which contains the "recent" messages. I would probably recommend using the timestamp in the reducer context to associate each message with a time and then when adding a new message into the "new messages" table you just remove the oldest message until there are only 10 messages remaining in that table.

If you have more questions on this let us know, again sorry for the very very long turn around time on this one.

jdetter avatar Oct 31 '25 15:10 jdetter