Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/table row support composite keys #6

Merged
merged 3 commits into from
Oct 31, 2023

Conversation

sduchesneau
Copy link
Contributor

@sduchesneau sduchesneau commented Oct 31, 2023

example usage for composite primary key in BAYC substreams:

    events.approvals.into_iter().for_each(|evt| {
        tables
            .create_row("approval", [("evt_tx_hash", evt.evt_tx_hash),("evt_index", evt.evt_index.to_string())].into())
            .set("evt_block_time", evt.evt_block_time.unwrap())
            .set("evt_block_number", evt.evt_block_number)
            .set("approved", Hex(&evt.approved).to_string())
            .set("owner", Hex(&evt.owner).to_string())
            .set("token_id", BigDecimal::from_str(&evt.token_id).unwrap());
    });

note that we don't need the evt_tx_hash and evt_index fields to be repeated in the set(...) commands, as they are part of the primary key.

The primary could also simply be format("{}-{}", evt.evt_tx_hash, evt.evt_index).into() for a Single key.

@sduchesneau sduchesneau requested a review from maoueh October 31, 2023 18:16
CHANGELOG.md Outdated
Comment on lines 11 to 13
* New enum in this crate: `tables::PrimaryKey`
* Single(string): `let sng_pk: PrimaryKey = "hello world".into()`
* Composite(BTreeMap<String, String>: `let cmp_pk: PrimaryKey = [("evt_tx_hash","hello".to_string()),("evt_index","world".to_string())].into()`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* New enum in this crate: `tables::PrimaryKey`
* Single(string): `let sng_pk: PrimaryKey = "hello world".into()`
* Composite(BTreeMap<String, String>: `let cmp_pk: PrimaryKey = [("evt_tx_hash","hello".to_string()),("evt_index","world".to_string())].into()`
* New enum in this crate: `tables::PrimaryKey`
* `Single(String)`: `let single: PrimaryKey = "hello world".into()`
* `Composite(BTreeMap<String, String>)`: `let composite: PrimaryKey = [("evt_tx_hash","hello".to_string()),("evt_index","world".to_string())].into()`

src/tables.rs Outdated
.pks
.entry(key.as_ref().to_string())
.or_insert(Row::new());
let key_debug = format!("{:?}", key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be on the panic directly. In the sense, inline this code at line 35 directly.

}
}

impl From<String> for PrimaryKey {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You got a problem with AsRef<str>?

@sduchesneau sduchesneau merged commit cc48c9f into develop Oct 31, 2023
3 checks passed
@sduchesneau sduchesneau deleted the feature/table-row-support-composite-keys branch October 31, 2023 19:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants