Collection

Struct Collection 

Source
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 the PocketBase client instance. This allows the Collection to send requests to PocketBase.
  • name: The name of the collection being interacted with.

Implementations§

Source§

impl Collection<'_>

Source

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>

Source

pub async fn auth_refresh_for_user( &mut self, user_token: &'a str, ) -> Result<AuthStore, RequestError>

Refresh the authentication token for a specific user.

Useful when managing tokens for other users (e.g., as a superuser).

§Example
let auth_data = pb
    .collection("users")
    .auth_refresh_for_user("USER_TOKEN")
    .await?;

println!("New token: {}", auth_data.token);
Source§

impl Collection<'_>

Source

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>

Source

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>

Source

pub async fn request_verification( &self, email: &'a str, ) -> Result<(), RequestError>

Sends users account verification request.

§Example
pb.collection("users")
    .request_verification("test@example.com")
    .await?;
Source§

impl Collection<'_>

Source

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?;
Source

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>

Source

pub async fn delete(&self, record_id: &'a str) -> Result<(), DeleteError>

Delete a single record.

§Example
pb.collection("articles")
    .delete("RECORD_ID")
    .await?;
Source§

impl<'a> Collection<'a>

Source

pub const fn get_first_list_item<T: Default + DeserializeOwned + Clone + Send>( self, ) -> CollectionGetFirstListItemBuilder<'a, T>

Fetch the first record from the given collection.

§Example
#[derive(Default, Deserialize, Clone)]
struct Article {
    id: String,
    title: String,
    content: String,
}

let article = pb
    .collection("articles")
    .get_first_list_item::<Article>()
    .sort("-created,id")
    .filter("language='en'")
    .call()
    .await?;
Source§

impl<'a> Collection<'a>

Source

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>

Source

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>

Source

pub const fn get_one<T: Default + DeserializeOwned + Clone + Send>( self, record_id: &'a str, ) -> CollectionGetOneBuilder<'a, T>

Fetch a single record.

§Example
#[derive(Default, Deserialize, Clone)]
struct Article {
    id: String,
    title: String,
    content: String,
}

let article = pb
    .collection("articles")
    .get_one::<Article>("record_id_123")
    .call()
    .await?;
Source§

impl<'a> Collection<'a>

Source

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?;

Auto Trait Implementations§

§

impl<'a> Freeze for Collection<'a>

§

impl<'a> !RefUnwindSafe for Collection<'a>

§

impl<'a> Send for Collection<'a>

§

impl<'a> Sync for Collection<'a>

§

impl<'a> Unpin for Collection<'a>

§

impl<'a> !UnwindSafe for Collection<'a>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,