pub struct Collection<'a> { /* private fields */ }Expand description
Represents a specific collection in a PocketBase database.
The Collection struct provides an interface for interacting with a specific collection
within a PocketBase instance. Instances of this struct are created using the
PocketBase::collection method. All operations on the target collection, such as retrieving,
creating, updating, or deleting records, are accessible through methods implemented on
this struct.
§Fields
client: A mutable reference to thePocketBaseclient instance. This allows theCollectionto send requests toPocketBase.name: The name of the collection being interacted with.
Implementations§
Source§impl Collection<'_>
impl Collection<'_>
Sourcepub async fn auth_refresh(&mut self) -> Result<AuthStore, RequestError>
pub async fn auth_refresh(&mut self) -> Result<AuthStore, RequestError>
Returns a new auth response (token and record data) for an already authenticated record.
This method is usually called by users on page/screen reload to ensure that the previously stored data in pb.auth_store() is still valid and up-to-date.
§Example
let auth_data = pb.collection("users")
.auth_refresh()
.await?;
println!("New token: {}", auth_data.token);Source§impl<'a> Collection<'a>
impl<'a> Collection<'a>
Sourcepub async fn auth_refresh_for_user(
&mut self,
user_token: &'a str,
) -> Result<AuthStore, RequestError>
pub async fn auth_refresh_for_user( &mut self, user_token: &'a str, ) -> Result<AuthStore, RequestError>
Source§impl Collection<'_>
impl Collection<'_>
Sourcepub async fn auth_with_password(
&mut self,
identity: &str,
password: &str,
) -> Result<AuthStore, AuthenticationError>
pub async fn auth_with_password( &mut self, identity: &str, password: &str, ) -> Result<AuthStore, AuthenticationError>
Authenticate with combination of email/username and password.
On success, the auth token is automatically stored and used for subsequent requests.
§Example
let auth_data = pb.collection("users")
.auth_with_password("YOUR_EMAIL_OR_USERNAME", "YOUR_PASSWORD")
.await?;
println!("Token: {}", auth_data.token);Source§impl<'a> Collection<'a>
impl<'a> Collection<'a>
Sourcepub const fn impersonate(
self,
user_id: &'a str,
) -> CollectionImpersonateBuilder<'a>
pub const fn impersonate( self, user_id: &'a str, ) -> CollectionImpersonateBuilder<'a>
Authenticate as a different user by generating a non-refreshable auth token.
Only superusers can perform this action. Returns a new PocketBase client
with the impersonated user’s auth token.
§Example
let impersonate_client = pb
.collection("users")
.impersonate("USER_RECORD_ID")
.duration(3600)
.call()
.await?;
println!("Token: {}", impersonate_client.auth_store().unwrap().token);Source§impl<'a> Collection<'a>
impl<'a> Collection<'a>
Sourcepub async fn request_verification(
&self,
email: &'a str,
) -> Result<(), RequestError>
pub async fn request_verification( &self, email: &'a str, ) -> Result<(), RequestError>
Source§impl Collection<'_>
impl Collection<'_>
Sourcepub async fn create<T: Default + Serialize + Clone + Send>(
self,
record: T,
) -> Result<CreateResponse, CreateError>
pub async fn create<T: Default + Serialize + Clone + Send>( self, record: T, ) -> Result<CreateResponse, CreateError>
Create a new record.
For file uploads, use Collection::create_multipart().
§Example
#[derive(Default, Serialize, Clone, Debug)]
struct Article {
name: String,
content: String,
}
let article = pb
.collection("articles")
.create::<Article>(Article {
name: "test".to_string(),
content: "an interesting article content.".to_string(),
})
.await?;Sourcepub async fn create_multipart(
self,
form: Form,
) -> Result<CreateResponse, CreateError>
pub async fn create_multipart( self, form: Form, ) -> Result<CreateResponse, CreateError>
Create a new record with multipart form data (e.g., for file uploads).
For simple JSON records without files, use Collection::create().
§Example
use std::fs;
use pocketbase_rs::{Form, Part};
let image = fs::read("./vulpes_vulpes.jpg")?;
let image_part = Part::bytes(image)
.file_name("vulpes_vulpes")
.mime_str("image/jpeg")?;
let form = Form::new()
.text("name", "Red Fox")
.part("illustration", image_part);
let record = pb
.collection("foxes")
.create_multipart(form)
.await?;Source§impl<'a> Collection<'a>
impl<'a> Collection<'a>
Source§impl<'a> Collection<'a>
impl<'a> Collection<'a>
Sourcepub const fn get_first_list_item<T: Default + DeserializeOwned + Clone + Send>(
self,
) -> CollectionGetFirstListItemBuilder<'a, T>
pub const fn get_first_list_item<T: Default + DeserializeOwned + Clone + Send>( self, ) -> CollectionGetFirstListItemBuilder<'a, T>
Source§impl<'a> Collection<'a>
impl<'a> Collection<'a>
Sourcepub const fn get_full_list<T: Default + DeserializeOwned + Clone + Send>(
self,
) -> CollectionGetFullListBuilder<'a, T>
pub const fn get_full_list<T: Default + DeserializeOwned + Clone + Send>( self, ) -> CollectionGetFullListBuilder<'a, T>
Fetch all records from the collection.
Automatically handles pagination by iterating through all pages.
For performance, skipTotal is automatically set to true.
§Example
#[derive(Default, Deserialize, Clone)]
struct Article {
id: String,
title: String,
content: String,
}
let all_articles = pb
.collection("articles")
.get_full_list::<Article>()
.sort("-created")
.call()
.await?;
println!("Total articles: {}", all_articles.len());Source§impl<'a> Collection<'a>
impl<'a> Collection<'a>
Sourcepub const fn get_list<T: Default + DeserializeOwned + Clone + Send>(
self,
) -> CollectionGetListBuilder<'a, T>
pub const fn get_list<T: Default + DeserializeOwned + Clone + Send>( self, ) -> CollectionGetListBuilder<'a, T>
Fetch a paginated records list from the given collection.
§Example
#[derive(Default, Deserialize, Clone)]
struct Article {
id: String,
title: String,
content: String,
}
let articles = pb
.collection("articles")
.get_list::<Article>()
.sort("-created,id")
.call()
.await?;
for article in articles.items {
println!("{article:?}");
}Source§impl<'a> Collection<'a>
impl<'a> Collection<'a>
Source§impl<'a> Collection<'a>
impl<'a> Collection<'a>
Sourcepub async fn update<T: Default + Serialize + Clone + Send>(
self,
record_id: &'a str,
record: T,
) -> Result<UpdateResponse, UpdateError>
pub async fn update<T: Default + Serialize + Clone + Send>( self, record_id: &'a str, record: T, ) -> Result<UpdateResponse, UpdateError>
Update a single record.
§Example
#[derive(Default, Serialize, Clone, Debug)]
struct Article {
name: String,
content: String,
}
let updated_article = Article {
name: String::from("Updated Article Title"),
content: String::from("Updated article content"),
};
let response = pb
.collection("articles")
.update::<Article>("record_id_123", updated_article)
.await?;