From 87a5ae67c749933606fa3b13b6cd67c2249526e9 Mon Sep 17 00:00:00 2001 From: Lee Smet Date: Thu, 31 Jul 2025 13:21:12 +0200 Subject: [PATCH] Add indexed_fields method on Model For postgres indexes we want to know the indexed field names upfront without an actual instance of Model Signed-off-by: Lee Smet --- heromodels-derive/src/lib.rs | 11 +++++++++++ heromodels_core/src/lib.rs | 5 +++++ 2 files changed, 16 insertions(+) diff --git a/heromodels-derive/src/lib.rs b/heromodels-derive/src/lib.rs index d2021a2..d51c635 100644 --- a/heromodels-derive/src/lib.rs +++ b/heromodels-derive/src/lib.rs @@ -123,6 +123,11 @@ pub fn model(_attr: TokenStream, item: TokenStream) -> TokenStream { } }; + let indexed_field_names = indexed_fields + .iter() + .map(|f| f.0.to_string()) + .collect::>(); + let model_impl = quote! { impl heromodels_core::Model for #struct_name { fn db_prefix() -> &'static str { @@ -137,6 +142,12 @@ pub fn model(_attr: TokenStream, item: TokenStream) -> TokenStream { &mut self.base_data } + fn indexed_fields() -> Vec<&'static str> { + vec![ + #(#indexed_field_names),* + ] + } + #db_keys_impl } }; diff --git a/heromodels_core/src/lib.rs b/heromodels_core/src/lib.rs index ca7cb2a..a593008 100644 --- a/heromodels_core/src/lib.rs +++ b/heromodels_core/src/lib.rs @@ -58,6 +58,11 @@ pub trait Model: Vec::new() } + /// Return a list of field names which have an index applied. + fn indexed_fields() -> Vec<&'static str> { + Vec::new() + } + /// Get the unique ID for this model fn get_id(&self) -> u32;