checkpoint-labs / checkpoint

Checkpoint is a library for indexing data of Starknet contracts.
https://checkpoint.box
MIT License
55 stars 23 forks source link

Issue to update a row using ID #309

Closed idirall22 closed 4 months ago

idirall22 commented 4 months ago

Description

I'm indexing a contract to store the user balance. The ID I use is a combination of the contract address and the user's address

const id = `${user_address}_${contract_address}`

The idea is to store the user balance one time, so before inserting a new row in the database I'm doing this check


const id = "AAA_BBB"
let userBalance = await UserBalance.loadEntity(id);
if (!userBalance ) {
    userBalance = new UserBalance(id);
}
...
await userBalance.save()

The issue I have in version 0.1.37, is that I can not update the row based on the ID it creates a new row each time with the same ID. Capture

Sekhmet commented 4 months ago

This is expected and needed for time travel queries (https://github.com/checkpoint-labs/checkpoint/pull/297).

With this you can look up state of the data at specific block:

{
  users (block: 65309) {
    id
    created_at
    created_at_block
}

This is also needed for upcoming reorg support so data can be rolled back.

Is there anything you try to do that doesn't work with this change? This change should be transparent from consumer point of view (in writers and when querying data you shouldn't need to worry about it). If you only want to see latest data in DB you can filter your results with upper_inf(block_range).

I'd imagine that in your case this might result in lots of entires if user balances changes happen frequently, we are planning on making time travel optional and just store data for last 500 blocks or so to still be able to support reorgs.

idirall22 commented 4 months ago

Hi @Sekhmet Thanks for the quick and clear answer! I'll add modifications on my side to include this new feature...