FuelLabs / fuel-indexer

🗃 The Fuel indexer is a standalone service that can be used to index various components of the Fuel blockchain.
https://docs.fuel.network/docs/indexer/
140 stars 66 forks source link

enhancement: implement find on entity #1446

Closed lostman closed 9 months ago

lostman commented 10 months ago

Description

Closes https://github.com/FuelLabs/fuel-indexer/issues/1437.

Example usage:

// Test searching for a string field
let f = FindEntity::find(
    FindEntity::string_value()
        .gt("f".to_string())
        .order_by_asc(FindEntity::value()),
)

This PR extends the codegen to generate field selectors for an @entity, e.g.:

1460   │     pub fn id() -> Field<ChainInfo, ID> {
1461   │         Field::new("id".to_string())
1462   │     }
1463   │     pub fn base_chain_height() -> Field<ChainInfo, U32> {
1464   │         Field::new("base_chain_height".to_string())
1465   │     }
1466   │     pub fn name() -> Field<ChainInfo, String> {
1467   │         Field::new("name".to_string())
1468   │     }
1469   │     pub fn peer_count() -> Field<ChainInfo, I32> {
1470   │         Field::new("peer_count".to_string())
1471   │     }
1472   │     pub fn latest_block() -> Field<ChainInfo, UID> {
1473   │         Field::new("latest_block".to_string())
1474   │     }
1475   │     pub fn consensus_parameters() -> Field<ChainInfo, UID> {
1476   │         Field::new("consensus_parameters".to_string())
1477   │     }

Field<T> structs implement methods such as eq, gt, lt, etc., which return a Filter<T>, which, in turn, can be combined using and and or to produce a mode complex Filter<T>. Finally, a Filter<T> can be ordered ascending or descending, turning it into a QueryFragment<T>.

The find function on Entity takes a Filter or QueryFragment and fetches the corresponding object from the database, if any exists.

Testing steps

CI testing. This PR includes an integration test for added functionality.

Changelog