Skip to content

Commit

Permalink
with_options_service_account_key_file support added (#107)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdolence authored Aug 7, 2023
1 parent faf33e5 commit 1220d61
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 6 deletions.
23 changes: 17 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,40 @@ To run an example with environment variables:
PROJECT_ID=<your-google-project-id> cargo run --example crud
```

## Firestore database client instance lifecycle
## Firestore database client instance and lifecycle

To create a new instance of Firestore client you need to provide at least a GCP project ID.
It is not recommended creating a new client for each request, so it is recommended to create a client once and reuse it whenever possible.
Cloning instances is much cheaper than creating a new one.

The client is created using the `Firestore::new` method:
```rust
use firestore::*;

// Create an instance
let db = FirestoreDb::new(&config_env_var("PROJECT_ID")?).await?;
```
This is the recommended way to create a new instance of the client, since it
automatically detects the environment and uses credentials, service accounts, Workload Identity on GCP, etc.
Look at the section below [Google authentication](#google-authentication) for more details.

It is not recommended creating a new client for each request, so it is recommended to create a client once and reuse it whenever possible.
Cloning instances is much cheaper than creating a new one.
In cases if you need to create a new instance explicitly specifying a key file, you can use:

```rust
FirestoreDb::with_options_service_account_key_file(
FirestoreDbOptions::new(config_env_var("PROJECT_ID")?.to_string()),
"/tmp/key.json".into()
).await?
```

In case if it is needed you can also create a new client instance using preconfigured token source.
or if you need even more flexibility you can use a preconfigured token source and scopes with:

For example:
```rust
FirestoreDb::with_options_token_source(
FirestoreDbOptions::new(config_env_var("PROJECT_ID")?.to_string()),
gcloud_sdk::GCP_DEFAULT_SCOPES.clone(),
gcloud_sdk::TokenSourceType::File("/tmp/key.json".into())
).await?;
).await?
```

## Fluent API
Expand Down
12 changes: 12 additions & 0 deletions src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ impl FirestoreDb {
.await
}

pub async fn with_options_service_account_key_file(
options: FirestoreDbOptions,
service_account_key_path: std::path::PathBuf,
) -> FirestoreResult<Self> {
Self::with_options_token_source(
options,
gcloud_sdk::GCP_DEFAULT_SCOPES.clone(),
gcloud_sdk::TokenSourceType::File(service_account_key_path),
)
.await
}

pub async fn with_options_token_source(
options: FirestoreDbOptions,
token_scopes: Vec<String>,
Expand Down

0 comments on commit 1220d61

Please sign in to comment.