Skip to content

Commit 46a2208

Browse files
committed
feat: alias
1 parent 57a1f6e commit 46a2208

File tree

4 files changed

+130
-79
lines changed

4 files changed

+130
-79
lines changed

typesense/src/client/alias.rs

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//! Provides access to the collection alias-related API endpoints.
2+
//!
3+
//! An `Alias` instance is created via the main `client.alias()` method.
4+
5+
use super::{Client, Error};
6+
use std::sync::Arc;
7+
use typesense_codegen::{
8+
apis::{collections_api, configuration},
9+
models,
10+
};
11+
12+
/// Provides methods for interacting with a specific Typesense collection alias.
13+
///
14+
/// This struct is created by calling `client.alias()`.
15+
pub struct Alias<'a> {
16+
pub(super) client: &'a Client,
17+
pub(super) name: &'a str,
18+
}
19+
20+
impl<'a> Alias<'a> {
21+
/// Creates a new `Alias` instance.
22+
pub(super) fn new(client: &'a Client, name: &'a str) -> Self {
23+
Self { client, name }
24+
}
25+
26+
/// Retrieves the details of a collection alias, including the collection it points to.
27+
pub async fn retrieve(
28+
&self,
29+
) -> Result<models::CollectionAlias, Error<collections_api::GetAliasError>> {
30+
let params = collections_api::GetAliasParams {
31+
alias_name: self.name.to_string(),
32+
};
33+
34+
self.client
35+
.execute(|config: Arc<configuration::Configuration>| {
36+
let params_for_move = params.clone();
37+
async move { collections_api::get_alias(&config, params_for_move).await }
38+
})
39+
.await
40+
}
41+
42+
/// Deletes a collection alias.
43+
pub async fn delete(
44+
&self,
45+
) -> Result<models::CollectionAlias, Error<collections_api::DeleteAliasError>> {
46+
let params = collections_api::DeleteAliasParams {
47+
alias_name: self.name.to_string(),
48+
};
49+
self.client
50+
.execute(|config: Arc<configuration::Configuration>| {
51+
let params_for_move = params.clone();
52+
async move { collections_api::delete_alias(&config, params_for_move).await }
53+
})
54+
.await
55+
}
56+
}

typesense/src/client/aliases.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//! Provides access to the collection aliases-related API endpoints.
2+
//!
3+
//! An `Aliases` instance is created via the main `client.aliases()` method.
4+
5+
use super::{Client, Error};
6+
use std::sync::Arc;
7+
use typesense_codegen::{
8+
apis::{collections_api, configuration},
9+
models,
10+
};
11+
12+
/// Provides methods for interacting with Typesense collection aliases.
13+
///
14+
/// This struct is created by calling `client.aliases()`.
15+
pub struct Aliases<'a> {
16+
pub(super) client: &'a Client,
17+
}
18+
19+
impl<'a> Aliases<'a> {
20+
/// Creates a new `Aliases` instance.
21+
pub(super) fn new(client: &'a Client) -> Self {
22+
Self { client }
23+
}
24+
25+
/// Creates or updates a collection alias.
26+
///
27+
/// An alias is a virtual collection name that points to a real collection.
28+
/// Aliases are useful when you want to re-index your data in the background
29+
/// on a new collection and then switch your application to it without any
30+
/// changes to your code.
31+
///
32+
/// # Arguments
33+
/// * `schema` - A `CollectionAliasSchema` pointing to the target collection.
34+
pub async fn upsert(
35+
&self,
36+
alias_name: &str,
37+
schema: models::CollectionAliasSchema,
38+
) -> Result<models::CollectionAlias, Error<collections_api::UpsertAliasError>> {
39+
let params = collections_api::UpsertAliasParams {
40+
alias_name: alias_name.to_string(),
41+
collection_alias_schema: Some(schema),
42+
};
43+
self.client
44+
.execute(|config: Arc<configuration::Configuration>| {
45+
let params_for_move = params.clone();
46+
async move { collections_api::upsert_alias(&config, params_for_move).await }
47+
})
48+
.await
49+
}
50+
51+
/// Lists all aliases and the corresponding collections that they map to.
52+
pub async fn retrieve(
53+
&self,
54+
) -> Result<models::CollectionAliasesResponse, Error<collections_api::GetAliasesError>> {
55+
self.client
56+
.execute(|config: Arc<configuration::Configuration>| async move {
57+
collections_api::get_aliases(&config).await
58+
})
59+
.await
60+
}
61+
}

typesense/src/client/collections.rs

Lines changed: 0 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -59,83 +59,4 @@ impl<'a> Collections<'a> {
5959
})
6060
.await
6161
}
62-
63-
// --- Alias-Specific Methods ---
64-
65-
/// Creates or updates a collection alias.
66-
///
67-
/// An alias is a virtual collection name that points to a real collection.
68-
/// Aliases are useful when you want to re-index your data in the background
69-
/// on a new collection and then switch your application to it without any
70-
/// changes to your code.
71-
///
72-
/// # Arguments
73-
/// * `name` - The name of the alias to create or update.
74-
/// * `schema` - A `CollectionAliasSchema` pointing to the target collection.
75-
pub async fn upsert_alias(
76-
&self,
77-
name: &str,
78-
schema: models::CollectionAliasSchema,
79-
) -> Result<models::CollectionAlias, Error<collections_api::UpsertAliasError>> {
80-
let params = collections_api::UpsertAliasParams {
81-
alias_name: name.to_string(),
82-
collection_alias_schema: Some(schema),
83-
};
84-
self.client
85-
.execute(|config: Arc<configuration::Configuration>| {
86-
let params_for_move = params.clone();
87-
async move { collections_api::upsert_alias(&config, params_for_move).await }
88-
})
89-
.await
90-
}
91-
92-
/// Retrieves the details of a collection alias, including the collection it points to.
93-
///
94-
/// # Arguments
95-
/// * `name` - The name of the alias to retrieve.
96-
pub async fn get_alias(
97-
&self,
98-
name: &str,
99-
) -> Result<models::CollectionAlias, Error<collections_api::GetAliasError>> {
100-
let params = collections_api::GetAliasParams {
101-
alias_name: name.to_string(),
102-
};
103-
104-
self.client
105-
.execute(|config: Arc<configuration::Configuration>| {
106-
let params_for_move = params.clone();
107-
async move { collections_api::get_alias(&config, params_for_move).await }
108-
})
109-
.await
110-
}
111-
112-
/// Lists all aliases and the corresponding collections that they map to.
113-
pub async fn list_aliases(
114-
&self,
115-
) -> Result<models::CollectionAliasesResponse, Error<collections_api::GetAliasesError>> {
116-
self.client
117-
.execute(|config: Arc<configuration::Configuration>| async move {
118-
collections_api::get_aliases(&config).await
119-
})
120-
.await
121-
}
122-
123-
/// Deletes a collection alias.
124-
///
125-
/// # Arguments
126-
/// * `name` - The name of the alias to delete.
127-
pub async fn delete_alias(
128-
&self,
129-
name: &str,
130-
) -> Result<models::CollectionAlias, Error<collections_api::DeleteAliasError>> {
131-
let params = collections_api::DeleteAliasParams {
132-
alias_name: name.to_string(),
133-
};
134-
self.client
135-
.execute(|config: Arc<configuration::Configuration>| {
136-
let params_for_move = params.clone();
137-
async move { collections_api::delete_alias(&config, params_for_move).await }
138-
})
139-
.await
140-
}
14162
}

typesense/src/client/mod.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
//! }
5151
//! ```
5252
53+
pub mod alias;
54+
pub mod aliases;
5355
pub mod analytics;
5456
pub mod collection;
5557
pub mod collections;
@@ -64,6 +66,8 @@ pub mod stemming;
6466
pub mod stopword;
6567
pub mod stopwords;
6668

69+
pub use alias::Alias;
70+
pub use aliases::Aliases;
6771
pub use analytics::Analytics;
6872
pub use collection::Collection;
6973
pub use collections::Collections;
@@ -311,6 +315,15 @@ impl Client {
311315
))))
312316
}
313317

318+
/// Provides access to the collection aliases-related API endpoints.
319+
pub fn aliases(&self) -> Aliases<'_> {
320+
Aliases::new(self)
321+
}
322+
323+
/// Provides access to a specific collection alias's-related API endpoints.
324+
pub fn alias<'a>(&'a self, name: &'a str) -> Alias<'a> {
325+
Alias::new(self, name)
326+
}
314327
/// Provides access to API endpoints for managing collections like `create()` and `retrieve()`.
315328
pub fn collections(&self) -> collections::Collections<'_> {
316329
collections::Collections::new(self)

0 commit comments

Comments
 (0)