Skip to content

Commit

Permalink
fix: share a postgres instance across rust project
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstevens19 committed Sep 13, 2024
1 parent 0045134 commit d5bcc97
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 68 deletions.
12 changes: 12 additions & 0 deletions core/src/generator/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{
networks_bindings::generate_networks_code,
};
use crate::{
generator::database_bindings::generate_database_code,
helpers::{
camel_to_snake, create_mod_file, format_all_files_for_project, write_file,
CreateModFileError, WriteFileError,
Expand Down Expand Up @@ -65,6 +66,13 @@ fn write_global(
Ok(())
}

fn write_database(output: &Path) -> Result<(), WriteGlobalError> {
let database_code = generate_database_code();
write_file(&generate_file_location(output, "database"), database_code.as_str())?;

Ok(())
}

#[derive(thiserror::Error, Debug)]
pub enum WriteIndexerEvents {
#[error("Could not write events code: {0}")]
Expand Down Expand Up @@ -163,6 +171,10 @@ pub fn generate_rindexer_typings(
write_global(&output, global, &manifest.networks)?;
}

if manifest.storage.postgres_enabled() {
write_database(&output)?;
}

write_indexer_events(project_path, &output, manifest.to_indexer(), &manifest.storage)?;

create_mod_file(output.as_path(), true)?;
Expand Down
23 changes: 23 additions & 0 deletions core/src/generator/database_bindings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::types::code::Code;

pub fn generate_database_code() -> Code {
Code::new(
r#"
use std::sync::Arc;
use rindexer::PostgresClient;
use tokio::sync::OnceCell;
static POSTGRES_CLIENT: OnceCell<Arc<PostgresClient>> = OnceCell::const_new();
pub async fn get_or_init_postgres_client() -> Arc<PostgresClient> {
POSTGRES_CLIENT
.get_or_init(|| async {
Arc::new(PostgresClient::new().await.expect("Failed to connect to Postgres"))
})
.await
.clone()
}
"#
.to_string(),
)
}
10 changes: 8 additions & 2 deletions core/src/generator/events_bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ fn generate_event_callback_structs_code(
struct_result = info.struct_result(),
struct_data = info.struct_data(),
database = if databases_enabled {
r#"database: Arc::new(PostgresClient::new().await.expect("Failed to connect to Postgres")),"#
"database: get_or_init_postgres_client().await,"
} else {
""
},
Expand Down Expand Up @@ -498,10 +498,11 @@ fn generate_event_bindings_code(
contract::{{Contract, ContractDetails}},
yaml::read_manifest,
}},
provider::JsonRpcCachedProvider,
{postgres_client_import}
provider::JsonRpcCachedProvider
}};
use super::super::super::super::typings::networks::get_provider_cache_for_network;
{postgres_import}
{structs}
Expand Down Expand Up @@ -623,6 +624,11 @@ fn generate_event_bindings_code(
}}
}}
"#,
postgres_import = if storage.postgres_enabled() {
"use super::super::super::super::typings::database::get_or_init_postgres_client;"
} else {
""
},
postgres_client_import = if storage.postgres_enabled() { "PostgresClient," } else { "" },
csv_import = if storage.csv_enabled() { "AsyncCsvAppender," } else { "" },
abigen_mod_name = abigen_contract_mod_name(contract),
Expand Down
2 changes: 2 additions & 0 deletions core/src/generator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
pub mod build;

mod context_bindings;
mod database_bindings;
mod docker;
mod events_bindings;
mod networks_bindings;

pub use docker::generate_docker_file;
1 change: 1 addition & 0 deletions documentation/docs/pages/docs/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ fix: running rust project should only start indexer or graphql passed on args pa
fix: resolve issue of paths in generated typings
fix: when running rindexer codegen typings csv folder created
fix: underscores in events within a rust project maps it wrong in typings
fix: share a postgres instance across rust project

### Breaking changes
-------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion rindexer_rust_playground/rindexer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contracts:
- filter:
event_name: Transfer
network: ethereum
start_block: 56399431
#start_block: 56399431
abi: ./abis/erc20-abi.json
generate_csv: true
global:
Expand Down
111 changes: 57 additions & 54 deletions rindexer_rust_playground/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use std::env;
use std::{env, path::PathBuf, str::FromStr};

use rindexer::{start_rindexer, GraphqlOverrideSettings, IndexingDetails, StartDetails};
use rindexer::{
manifest::yaml::read_manifest, start_rindexer, GraphqlOverrideSettings, IndexingDetails,
StartDetails,
};

use self::rindexer_lib::indexers::all_handlers::register_all_handlers;

Expand Down Expand Up @@ -78,55 +81,55 @@ async fn main() {
}
}

// #[allow(dead_code)]
// fn generate() {
// let path = PathBuf::from_str(
// "/Users/joshstevens/code/rindexer/rindexer_rust_playground/rindexer.yaml",
// )
// .expect("Invalid path");
// let manifest = read_manifest(&path).expect("Failed to read manifest");
// rindexer::generator::build::generate_rindexer_typings(&manifest, &path, true)
// .expect("Failed to generate typings");
// }
//
// #[allow(dead_code)]
// fn generate_code_test() {
// let path = PathBuf::from_str(
// "/Users/joshstevens/code/rindexer/rindexer_rust_playground/rindexer.yaml",
// )
// .expect("Invalid path");
// let manifest = read_manifest(&path).expect("Failed to read manifest");
//
// rindexer::generator::build::generate_rindexer_handlers(manifest, &path, true)
// .expect("Failed to generate handlers");
// }
//
// #[allow(dead_code)]
// fn generate_all() {
// let path = PathBuf::from_str(
// "/Users/joshstevens/code/rindexer/rindexer_rust_playground/rindexer.yaml",
// )
// .expect("Invalid path");
// rindexer::generator::build::generate_rindexer_typings_and_handlers(&path)
// .expect("Failed to generate typings and handlers");
// }
//
// #[cfg(test)]
// mod tests {
// use super::*;
//
// #[test]
// fn test_generate() {
// generate();
// }
//
// #[test]
// fn test_code_generate() {
// generate_code_test();
// }
//
// #[test]
// fn test_generate_all() {
// generate_all();
// }
// }
#[allow(dead_code)]
fn generate() {
let path = PathBuf::from_str(
"/Users/joshstevens/code/rindexer/rindexer_rust_playground/rindexer.yaml",
)
.expect("Invalid path");
let manifest = read_manifest(&path).expect("Failed to read manifest");
rindexer::generator::build::generate_rindexer_typings(&manifest, &path, true)
.expect("Failed to generate typings");
}

#[allow(dead_code)]
fn generate_code_test() {
let path = PathBuf::from_str(
"/Users/joshstevens/code/rindexer/rindexer_rust_playground/rindexer.yaml",
)
.expect("Invalid path");
let manifest = read_manifest(&path).expect("Failed to read manifest");

rindexer::generator::build::generate_rindexer_handlers(manifest, &path, true)
.expect("Failed to generate handlers");
}

#[allow(dead_code)]
fn generate_all() {
let path = PathBuf::from_str(
"/Users/joshstevens/code/rindexer/rindexer_rust_playground/rindexer.yaml",
)
.expect("Invalid path");
rindexer::generator::build::generate_rindexer_typings_and_handlers(&path)
.expect("Failed to generate typings and handlers");
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_generate() {
generate();
}

#[test]
fn test_code_generate() {
generate_code_test();
}

#[test]
fn test_generate_all() {
generate_all();
}
}
15 changes: 15 additions & 0 deletions rindexer_rust_playground/src/rindexer_lib/typings/database.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use std::sync::Arc;

use rindexer::PostgresClient;
use tokio::sync::OnceCell;

static POSTGRES_CLIENT: OnceCell<Arc<PostgresClient>> = OnceCell::const_new();

pub async fn get_or_init_postgres_client() -> Arc<PostgresClient> {
POSTGRES_CLIENT
.get_or_init(|| async {
Arc::new(PostgresClient::new().await.expect("Failed to connect to Postgres"))
})
.await
.clone()
}
1 change: 1 addition & 0 deletions rindexer_rust_playground/src/rindexer_lib/typings/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#![allow(dead_code, unused)]
pub mod database;
pub mod global_contracts;
pub mod networks;
pub mod rindexer_playground;
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use rindexer::{
AsyncCsvAppender, FutureExt, PostgresClient,
};

use super::super::super::super::typings::networks::get_provider_cache_for_network;
use super::super::super::super::typings::{
database::get_or_init_postgres_client, networks::get_provider_cache_for_network,
};
/// THIS IS A GENERATED FILE. DO NOT MODIFY MANUALLY.
///
/// This file was auto generated by rindexer - https://github.com/joshstevens19/rindexer.
Expand Down Expand Up @@ -138,9 +140,7 @@ where
Self {
callback: transfer_handler(closure),
context: Arc::new(EventContext {
database: Arc::new(
PostgresClient::new().await.expect("Failed to connect to Postgres"),
),
database: get_or_init_postgres_client().await,
csv: Arc::new(csv),
extensions: Arc::new(extensions),
}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ use rindexer::{
AsyncCsvAppender, FutureExt, PostgresClient,
};

use super::super::super::super::typings::networks::get_provider_cache_for_network;
use super::super::super::super::typings::{
database::get_or_init_postgres_client, networks::get_provider_cache_for_network,
};
/// THIS IS A GENERATED FILE. DO NOT MODIFY MANUALLY.
///
/// This file was auto generated by rindexer - https://github.com/joshstevens19/rindexer.
Expand Down Expand Up @@ -140,9 +142,7 @@ where
Self {
callback: approval_handler(closure),
context: Arc::new(EventContext {
database: Arc::new(
PostgresClient::new().await.expect("Failed to connect to Postgres"),
),
database: get_or_init_postgres_client().await,
csv: Arc::new(csv),
extensions: Arc::new(extensions),
}),
Expand Down Expand Up @@ -243,9 +243,7 @@ where
Self {
callback: transfer_handler(closure),
context: Arc::new(EventContext {
database: Arc::new(
PostgresClient::new().await.expect("Failed to connect to Postgres"),
),
database: get_or_init_postgres_client().await,
csv: Arc::new(csv),
extensions: Arc::new(extensions),
}),
Expand Down

0 comments on commit d5bcc97

Please sign in to comment.