pub struct Filemaker { /* private fields */ }
Expand description
Represents a connection to a Filemaker database with authentication and query capabilities.
This struct manages the connection details and authentication token needed to interact with a Filemaker database through its Data API.
Implementations§
Source§impl Filemaker
impl Filemaker
Sourcepub async fn new(
username: &str,
password: &str,
database: &str,
table: &str,
) -> Result<Self>
pub async fn new( username: &str, password: &str, database: &str, table: &str, ) -> Result<Self>
Creates a new Filemaker
instance.
Initializes a connection to a FileMaker database with the provided credentials. This function performs authentication and sets up the HTTP client with appropriate configuration.
§Arguments
username
- The username for FileMaker authenticationpassword
- The password for FileMaker authenticationdatabase
- The name of the FileMaker database to connect totable
- The name of the table/layout to operate on
§Returns
Result<Self>
- A new Filemaker instance or an error
Examples found in repository?
5async fn main() -> Result<()> {
6 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
7 let username = "your_username";
8 let password = "your_password";
9 let database = "your_database";
10 let table = "your_table";
11
12 // Create a Filemaker instance
13 let filemaker = Filemaker::new(username, password, database, table).await?;
14 println!("Filemaker instance created successfully.");
15
16 Ok(())
17}
More examples
5async fn main() -> Result<()> {
6 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
7 let username = "your_username";
8 let password = "your_password";
9 let database = "your_database";
10 let table = "your_table";
11 let filemaker = Filemaker::new(username, password, database, table).await?;
12
13 // Get the total number of records
14 let total_records = filemaker.get_number_of_records().await?;
15 println!("Total Records: {}", total_records);
16
17 Ok(())
18}
5async fn main() -> Result<()> {
6 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
7 let username = "your_username";
8 let password = "your_password";
9 let database = "your_database";
10 let table = "your_table";
11 let filemaker = Filemaker::new(username, password, database, table).await?;
12
13 // Fetch the first 10 records
14 let records = filemaker.get_records(1, 10).await?;
15 println!("Fetched Records:");
16 for record in records {
17 println!("{:?}", record);
18 }
19
20 Ok(())
21}
5async fn main() -> Result<()> {
6 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
7 let username = "your_username";
8 let password = "your_password";
9 let database = "your_database";
10 let table = "your_table";
11 let filemaker = Filemaker::new(username, password, database, table).await?;
12
13 // Record ID to delete
14 let record_id = 123; // Replace with the actual record ID
15
16 // Delete the record
17 let result = filemaker.delete_record(record_id).await?;
18 println!("Record deleted successfully: {:?}", result);
19
20 Ok(())
21}
6async fn main() -> Result<(), anyhow::Error> {
7 // Initialize the Filemaker instance
8 let filemaker = Filemaker::new("username", "password", "database_name", "table_name").await?;
9
10 // Create the field data for a single record
11 let mut single_record_data = HashMap::new();
12 single_record_data.insert("field_name1".to_string(), Value::String("Value 1".to_string()));
13 single_record_data.insert("field_name2".to_string(), Value::String("Value 2".to_string()));
14
15 // Add the single record
16 let result = filemaker.add_record(single_record_data).await?;
17
18 // Print the result
19 println!("Single record added: {:?}", result);
20
21 Ok(())
22}
7async fn main() -> Result<()> {
8 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
9 let username = "your_username";
10 let password = "your_password";
11 let database = "your_database";
12 let table = "your_table";
13 let filemaker = Filemaker::new(username, password, database, table).await?;
14
15 // Record ID to update
16 let record_id = 123; // Replace with the actual record ID
17
18 // Data to update
19 let mut field_data = HashMap::new();
20 field_data.insert("fieldName".to_string(), Value::String("new_value".to_string())); // Replace "fieldName" and "new_value"
21
22 // Update the record
23 let result = filemaker.update_record(record_id, field_data).await?;
24 println!("Update Result: {:?}", result);
25
26 Ok(())
27}
Sourcepub async fn get_records<T>(&self, start: T, limit: T) -> Result<Vec<Value>>
pub async fn get_records<T>(&self, start: T, limit: T) -> Result<Vec<Value>>
Retrieves a specified range of records from the database.
§Arguments
start
- The starting position (offset) for record retrievallimit
- The maximum number of records to retrieve
§Returns
Result<Vec<Value>>
- A vector of record objects on success, or an error
Examples found in repository?
5async fn main() -> Result<()> {
6 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
7 let username = "your_username";
8 let password = "your_password";
9 let database = "your_database";
10 let table = "your_table";
11 let filemaker = Filemaker::new(username, password, database, table).await?;
12
13 // Fetch the first 10 records
14 let records = filemaker.get_records(1, 10).await?;
15 println!("Fetched Records:");
16 for record in records {
17 println!("{:?}", record);
18 }
19
20 Ok(())
21}
Sourcepub async fn get_all_records(&self) -> Result<Vec<Value>>
pub async fn get_all_records(&self) -> Result<Vec<Value>>
Retrieves all records from the database in a single query.
This method first determines the total record count and then fetches all records in a single request.
§Returns
Result<Vec<Value>>
- A vector containing all records on success, or an error
Sourcepub async fn get_number_of_records(&self) -> Result<u64>
pub async fn get_number_of_records(&self) -> Result<u64>
Retrieves the total number of records in the database table.
§Returns
Result<u64>
- The total record count on success, or an error
Examples found in repository?
5async fn main() -> Result<()> {
6 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
7 let username = "your_username";
8 let password = "your_password";
9 let database = "your_database";
10 let table = "your_table";
11 let filemaker = Filemaker::new(username, password, database, table).await?;
12
13 // Get the total number of records
14 let total_records = filemaker.get_number_of_records().await?;
15 println!("Total Records: {}", total_records);
16
17 Ok(())
18}
Sourcepub async fn search<T>(
&self,
query: Vec<HashMap<String, String>>,
sort: Vec<String>,
ascending: bool,
limit: Option<u64>,
) -> Result<FindResult<T>>where
T: DeserializeOwned + Default,
pub async fn search<T>(
&self,
query: Vec<HashMap<String, String>>,
sort: Vec<String>,
ascending: bool,
limit: Option<u64>,
) -> Result<FindResult<T>>where
T: DeserializeOwned + Default,
Searches the database for records matching specified criteria.
§Arguments
query
- Vector of field-value pairs to search forsort
- Vector of field names to sort byascending
- Whether to sort in ascending (true) or descending (false) orderlimit
- If None, all results will be returned; otherwise, the specified limit will be applied
§Returns
Result<Vec<T>>
- A vector of matching records as the specified type on success, or an error
Examples found in repository?
7async fn main() -> Result<()> {
8 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
9 let username = "your_username";
10 let password = "your_password";
11 let database = "your_database";
12 let table = "your_table";
13 let filemaker = Filemaker::new(username, password, database, table).await?;
14
15 // Construct search query
16 let mut query = HashMap::new();
17 query.insert("fieldName".to_string(), "example_value".to_string()); // Replace "fieldName" and "example_value"
18
19 // Sorting criteria
20 let sort_fields = vec!["fieldName".to_string()]; // Replace with the field name to sort
21 let ascending = true;
22
23 let records = filemaker
24 .search::<Value>(vec![query], sort_fields, ascending, Some(10))
25 .await?;
26 println!("Search Results:");
27 for record in records.response.data {
28 println!("{:?}", record);
29 }
30
31 Ok(())
32}
Sourcepub async fn add_record(
&self,
field_data: HashMap<String, Value>,
) -> Result<HashMap<String, Value>>
pub async fn add_record( &self, field_data: HashMap<String, Value>, ) -> Result<HashMap<String, Value>>
Adds a record to the database.
§Parameters
field_data
: AHashMap
representing the field data for the new record.
§Returns
A Result
containing the added record as a Value
on success, or an error.
Examples found in repository?
6async fn main() -> Result<(), anyhow::Error> {
7 // Initialize the Filemaker instance
8 let filemaker = Filemaker::new("username", "password", "database_name", "table_name").await?;
9
10 // Create the field data for a single record
11 let mut single_record_data = HashMap::new();
12 single_record_data.insert("field_name1".to_string(), Value::String("Value 1".to_string()));
13 single_record_data.insert("field_name2".to_string(), Value::String("Value 2".to_string()));
14
15 // Add the single record
16 let result = filemaker.add_record(single_record_data).await?;
17
18 // Print the result
19 println!("Single record added: {:?}", result);
20
21 Ok(())
22}
More examples
6async fn main() -> Result<(), anyhow::Error> {
7 // Initialize the Filemaker instance
8 let filemaker = Filemaker::new("username", "password", "database_name", "table_name").await?;
9
10 // Prepare data for multiple records
11 let records = vec![
12 {
13 let mut record = HashMap::new();
14 record.insert("field_name1".to_string(), Value::String("First Record - Value 1".to_string()));
15 record.insert("field_name2".to_string(), Value::String("First Record - Value 2".to_string()));
16 record
17 },
18 {
19 let mut record = HashMap::new();
20 record.insert("field_name1".to_string(), Value::String("Second Record - Value 1".to_string()));
21 record.insert("field_name2".to_string(), Value::String("Second Record - Value 2".to_string()));
22 record
23 },
24 ];
25
26 // Add each record to the database
27 for (i, record_data) in records.into_iter().enumerate() {
28 match filemaker.add_record(record_data).await {
29 Ok(result) => println!("Record {} added successfully: {:?}", i + 1, result),
30 Err(e) => eprintln!("Failed to add record {}: {}", i + 1, e),
31 }
32 }
33
34 Ok(())
35}
Sourcepub async fn update_record<T>(
&self,
id: T,
field_data: HashMap<String, Value>,
) -> Result<Value>
pub async fn update_record<T>( &self, id: T, field_data: HashMap<String, Value>, ) -> Result<Value>
Updates a record in the database using the FileMaker Data API.
§Arguments
id
- The unique identifier of the record to updatefield_data
- A hashmap containing the field names and their new values
§Returns
Result<Value>
- The server response as a JSON value or an error
§Type Parameters
T
- A type that can be used as a record identifier and meets various trait requirements
Examples found in repository?
7async fn main() -> Result<()> {
8 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
9 let username = "your_username";
10 let password = "your_password";
11 let database = "your_database";
12 let table = "your_table";
13 let filemaker = Filemaker::new(username, password, database, table).await?;
14
15 // Record ID to update
16 let record_id = 123; // Replace with the actual record ID
17
18 // Data to update
19 let mut field_data = HashMap::new();
20 field_data.insert("fieldName".to_string(), Value::String("new_value".to_string())); // Replace "fieldName" and "new_value"
21
22 // Update the record
23 let result = filemaker.update_record(record_id, field_data).await?;
24 println!("Update Result: {:?}", result);
25
26 Ok(())
27}
Sourcepub async fn get_databases(
username: &str,
password: &str,
) -> Result<Vec<String>>
pub async fn get_databases( username: &str, password: &str, ) -> Result<Vec<String>>
Retrieves the list of databases accessible to the specified user.
§Arguments
username
- The FileMaker username for authenticationpassword
- The FileMaker password for authentication
§Returns
Result<Vec<String>>
- A list of accessible database names or an error
Examples found in repository?
More examples
5async fn main() -> Result<()> {
6 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
7 let username = "your_username";
8 let password = "your_password";
9
10 // Fetch the list of databases
11 let databases = Filemaker::get_databases(username, password).await?;
12 println!("Databases:");
13 for db in databases {
14 println!("{}", db);
15 }
16
17 Ok(())
18}
Sourcepub async fn get_layouts(
username: &str,
password: &str,
database: &str,
) -> Result<Vec<String>>
pub async fn get_layouts( username: &str, password: &str, database: &str, ) -> Result<Vec<String>>
Retrieves the list of layouts for the specified database using the provided credentials.
§Arguments
username
- The FileMaker username for authenticationpassword
- The FileMaker password for authenticationdatabase
- The name of the database to get layouts from
§Returns
Result<Vec<String>>
- A list of layout names or an error
Examples found in repository?
5async fn main() -> Result<()> {
6 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
7 let username = "your_username";
8 let password = "your_password";
9 let database = "your_database";
10
11 // Fetch layouts
12 let layouts = Filemaker::get_layouts(username, password, database).await?;
13 println!("Available Layouts:");
14 for layout in layouts {
15 println!("{}", layout);
16 }
17
18 Ok(())
19}
Sourcepub async fn get_record_by_id<T>(&self, id: T) -> Result<Value>
pub async fn get_record_by_id<T>(&self, id: T) -> Result<Value>
Sourcepub async fn delete_record<T>(&self, id: T) -> Result<Value>
pub async fn delete_record<T>(&self, id: T) -> Result<Value>
Deletes a record from the database by its ID.
§Arguments
id
- The ID of the record to delete.
§Returns
A result indicating the deletion was successful or an error message.
Examples found in repository?
5async fn main() -> Result<()> {
6 std::env::set_var("FM_URL", "https://fm.example.com/fmi/data/vLatest"); // Replace with actual filemaker server url
7 let username = "your_username";
8 let password = "your_password";
9 let database = "your_database";
10 let table = "your_table";
11 let filemaker = Filemaker::new(username, password, database, table).await?;
12
13 // Record ID to delete
14 let record_id = 123; // Replace with the actual record ID
15
16 // Delete the record
17 let result = filemaker.delete_record(record_id).await?;
18 println!("Record deleted successfully: {:?}", result);
19
20 Ok(())
21}
Sourcepub async fn delete_database(
database: &str,
username: &str,
password: &str,
) -> Result<()>
pub async fn delete_database( database: &str, username: &str, password: &str, ) -> Result<()>
Deletes the specified database.
§Arguments
database
- The name of the database to delete.username
- The username for authentication.password
- The password for authentication.
Sourcepub async fn clear_database(&self) -> Result<()>
pub async fn clear_database(&self) -> Result<()>
Deletes all records from the current database.
This function retrieves and systematically removes all records from the database. It first checks if there are any records to delete, then proceeds with deletion if records exist.
§Returns
Result<()>
- Ok(()) if all records were successfully deleted, or an error
§Errors
- Returns error if unable to retrieve records
- Returns error if record ID parsing fails
- Returns error if record deletion fails
Sourcepub fn get_row_names_by_example(record: &Value) -> Vec<String>
pub fn get_row_names_by_example(record: &Value) -> Vec<String>
Sourcepub async fn get_row_names(&self) -> Result<Vec<String>>
pub async fn get_row_names(&self) -> Result<Vec<String>>
Gets the field names for the first record in the database.
This function retrieves a single record from the database and extracts field names from it. If no records exist, an empty vector is returned.
§Returns
Result<Vec<String>>
- A vector of field names on success, or an error