0.5.1
This commit is contained in:
195
kb_lib/src/db/queries/known_ws_endpoint.rs
Normal file
195
kb_lib/src/db/queries/known_ws_endpoint.rs
Normal file
@@ -0,0 +1,195 @@
|
||||
// file: kb_lib/src/db/queries/known_ws_endpoint.rs
|
||||
|
||||
//! Queries for `kb_known_ws_endpoints`.
|
||||
|
||||
/// Inserts or updates one known WS endpoint row.
|
||||
pub async fn upsert_known_ws_endpoint(
|
||||
database: &crate::KbDatabase,
|
||||
dto: &crate::KbKnownWsEndpointDto,
|
||||
) -> Result<(), crate::KbError> {
|
||||
let entity_result = crate::KbKnownWsEndpointEntity::try_from(dto.clone());
|
||||
let entity = match entity_result {
|
||||
Ok(entity) => entity,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query(
|
||||
r#"
|
||||
INSERT INTO kb_known_ws_endpoints (
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
enabled,
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?)
|
||||
ON CONFLICT(name) DO UPDATE SET
|
||||
provider = excluded.provider,
|
||||
url = excluded.url,
|
||||
enabled = excluded.enabled,
|
||||
roles_json = excluded.roles_json,
|
||||
last_seen_at = excluded.last_seen_at,
|
||||
updated_at = excluded.updated_at
|
||||
"#,
|
||||
)
|
||||
.bind(entity.name)
|
||||
.bind(entity.provider)
|
||||
.bind(entity.url)
|
||||
.bind(entity.enabled)
|
||||
.bind(entity.roles_json)
|
||||
.bind(entity.last_seen_at)
|
||||
.bind(entity.updated_at)
|
||||
.execute(pool)
|
||||
.await;
|
||||
match query_result {
|
||||
Ok(_) => Ok(()),
|
||||
Err(error) => Err(crate::KbError::Db(format!(
|
||||
"cannot upsert kb_known_ws_endpoints on sqlite: {}",
|
||||
error
|
||||
))),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Reads one known WS endpoint by name.
|
||||
pub async fn get_known_ws_endpoint(
|
||||
database: &crate::KbDatabase,
|
||||
name: &str,
|
||||
) -> Result<std::option::Option<crate::KbKnownWsEndpointDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbKnownWsEndpointEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
enabled,
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
FROM kb_known_ws_endpoints
|
||||
WHERE name = ?
|
||||
LIMIT 1
|
||||
"#,
|
||||
)
|
||||
.bind(name)
|
||||
.fetch_optional(pool)
|
||||
.await;
|
||||
let entity_option = match query_result {
|
||||
Ok(entity_option) => entity_option,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot read known ws endpoint '{}' on sqlite: {}",
|
||||
name, error
|
||||
)));
|
||||
}
|
||||
};
|
||||
match entity_option {
|
||||
Some(entity) => {
|
||||
let dto_result = crate::KbKnownWsEndpointDto::try_from(entity);
|
||||
match dto_result {
|
||||
Ok(dto) => Ok(Some(dto)),
|
||||
Err(error) => Err(error),
|
||||
}
|
||||
}
|
||||
None => Ok(None),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Lists all known WS endpoints.
|
||||
pub async fn list_known_ws_endpoints(
|
||||
database: &crate::KbDatabase,
|
||||
) -> Result<std::vec::Vec<crate::KbKnownWsEndpointDto>, crate::KbError> {
|
||||
match database.connection() {
|
||||
crate::KbDatabaseConnection::Sqlite(pool) => {
|
||||
let query_result = sqlx::query_as::<sqlx::Sqlite, crate::KbKnownWsEndpointEntity>(
|
||||
r#"
|
||||
SELECT
|
||||
name,
|
||||
provider,
|
||||
url,
|
||||
enabled,
|
||||
roles_json,
|
||||
last_seen_at,
|
||||
updated_at
|
||||
FROM kb_known_ws_endpoints
|
||||
ORDER BY name ASC
|
||||
"#,
|
||||
)
|
||||
.fetch_all(pool)
|
||||
.await;
|
||||
let entities = match query_result {
|
||||
Ok(entities) => entities,
|
||||
Err(error) => {
|
||||
return Err(crate::KbError::Db(format!(
|
||||
"cannot list known ws endpoints on sqlite: {}",
|
||||
error
|
||||
)));
|
||||
}
|
||||
};
|
||||
let mut dtos = std::vec::Vec::new();
|
||||
for entity in entities {
|
||||
let dto_result = crate::KbKnownWsEndpointDto::try_from(entity);
|
||||
let dto = match dto_result {
|
||||
Ok(dto) => dto,
|
||||
Err(error) => return Err(error),
|
||||
};
|
||||
dtos.push(dto);
|
||||
}
|
||||
Ok(dtos)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
#[tokio::test]
|
||||
async fn known_ws_endpoint_roundtrip_works() {
|
||||
let tempdir = tempfile::tempdir().expect("tempdir must succeed");
|
||||
let database_path = tempdir.path().join("known_ws_endpoint.sqlite3");
|
||||
let config = crate::KbDatabaseConfig {
|
||||
enabled: true,
|
||||
backend: crate::KbDatabaseBackend::Sqlite,
|
||||
sqlite: crate::KbSqliteDatabaseConfig {
|
||||
path: database_path.to_string_lossy().to_string(),
|
||||
create_if_missing: true,
|
||||
busy_timeout_ms: 5000,
|
||||
max_connections: 1,
|
||||
auto_initialize_schema: true,
|
||||
use_wal: true,
|
||||
},
|
||||
};
|
||||
let database = crate::KbDatabase::connect_and_initialize(&config)
|
||||
.await
|
||||
.expect("database init must succeed");
|
||||
let dto = crate::KbKnownWsEndpointDto::new(
|
||||
"mainnet_public_ws_slots".to_string(),
|
||||
"solana".to_string(),
|
||||
"wss://api.mainnet.solana.com".to_string(),
|
||||
true,
|
||||
vec!["ws_slots".to_string(), "ws_subscriptions".to_string()],
|
||||
);
|
||||
crate::upsert_known_ws_endpoint(&database, &dto)
|
||||
.await
|
||||
.expect("upsert must succeed");
|
||||
let fetched = crate::get_known_ws_endpoint(&database, "mainnet_public_ws_slots")
|
||||
.await
|
||||
.expect("fetch must succeed");
|
||||
assert!(fetched.is_some());
|
||||
let fetched = fetched.expect("endpoint must exist");
|
||||
assert_eq!(fetched.provider, "solana");
|
||||
assert_eq!(fetched.roles.len(), 2);
|
||||
let listed = crate::list_known_ws_endpoints(&database)
|
||||
.await
|
||||
.expect("list must succeed");
|
||||
assert_eq!(listed.len(), 1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user