-
Notifications
You must be signed in to change notification settings - Fork 1.1k
feat(tools/firestore-add-documents): Add firestore-add-documents tool #1107
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
185c29e
feat: Add firestore-add-documents tool
trehanshakuntG 4801263
add prebuilt tools to root.go
trehanshakuntG 4593f9e
Update root_test.go file
trehanshakuntG ffedbe1
Update converter code for referenceValue
trehanshakuntG e5a4851
converter tests fixed
trehanshakuntG 0288491
Update integration tests
trehanshakuntG d06377d
Merge branch 'main' of https://github.com/googleapis/genai-toolbox in…
trehanshakuntG 0ec910c
Update firestore test teardown
trehanshakuntG 32d478c
refactor converter_test
trehanshakuntG 93d0a9b
Update lint changes
trehanshakuntG ca32496
Update documentation
trehanshakuntG 42e09bf
Merge branch 'main' of https://github.com/googleapis/genai-toolbox in…
trehanshakuntG 3522a72
Merge branch 'main' into trehanshakunt-firestore-add-doc
trehanshakuntG 9d9adaa
Address requested changes
trehanshakuntG a3110d1
Merge branch 'main' of https://github.com/googleapis/genai-toolbox in…
trehanshakuntG 5bcf376
Add strconv function for str to int conversion
trehanshakuntG f90fd14
Merge branch 'main' into trehanshakunt-firestore-add-doc
averikitsch 32d2a78
Merge branch 'main' of https://github.com/googleapis/genai-toolbox in…
trehanshakuntG 01926c7
Merge branch 'trehanshakunt-firestore-add-doc' of https://github.com/…
trehanshakuntG File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
274 changes: 274 additions & 0 deletions
274
docs/en/resources/tools/firestore/firestore-add-documents.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,274 @@ | ||
| --- | ||
| title: "firestore-add-documents" | ||
| type: docs | ||
| weight: 1 | ||
| description: > | ||
| A "firestore-add-documents" tool adds document to a given collection path. | ||
| aliases: | ||
| - /resources/tools/firestore-add-documents | ||
| --- | ||
| ## Description | ||
|
|
||
| The `firestore-add-documents` tool allows you to add new documents to a Firestore collection. It supports all Firestore data types using Firestore's native JSON format. The tool automatically generates a unique document ID for each new document. | ||
trehanshakuntG marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| ## Parameters | ||
|
|
||
| | Parameter | Type | Required | Description | | ||
| |-----------|------|----------|-------------| | ||
| | `collectionPath` | string | Yes | The path of the collection where the document will be added | | ||
| | `documentData` | map | Yes | The data to be added as a document to the given collection. Must use [Firestore's native JSON format](https://cloud.google.com/firestore/docs/reference/rest/Shared.Types/ArrayValue#Value) with typed values | | ||
| | `returnData` | boolean | No | If set to true, the output will include the data of the created document. Defaults to false to help avoid overloading the context | | ||
|
|
||
| ## Output | ||
|
|
||
| The tool returns a map containing: | ||
|
|
||
| | Field | Type | Description | | ||
| |-------|------|-------------| | ||
| | `documentPath` | string | The full resource name of the created document (e.g., `projects/{projectId}/databases/{databaseId}/documents/{document_path}`) | | ||
| | `createTime` | string | The timestamp when the document was created | | ||
| | `documentData` | map | The data that was added (only included when `returnData` is true) | | ||
|
|
||
| ## Data Type Format | ||
|
|
||
| The tool requires Firestore's native JSON format for document data. Each field must be wrapped with its type indicator: | ||
|
|
||
| ### Basic Types | ||
| - **String**: `{"stringValue": "your string"}` | ||
| - **Integer**: `{"integerValue": "123"}` or `{"integerValue": 123}` | ||
| - **Double**: `{"doubleValue": 123.45}` | ||
| - **Boolean**: `{"booleanValue": true}` | ||
| - **Null**: `{"nullValue": null}` | ||
| - **Bytes**: `{"bytesValue": "base64EncodedString"}` | ||
| - **Timestamp**: `{"timestampValue": "2025-01-07T10:00:00Z"}` (RFC3339 format) | ||
|
|
||
| ### Complex Types | ||
| - **GeoPoint**: `{"geoPointValue": {"latitude": 34.052235, "longitude": -118.243683}}` | ||
| - **Array**: `{"arrayValue": {"values": [{"stringValue": "item1"}, {"integerValue": "2"}]}}` | ||
| - **Map**: `{"mapValue": {"fields": {"key1": {"stringValue": "value1"}, "key2": {"booleanValue": true}}}}` | ||
| - **Reference**: `{"referenceValue": "collection/document"}` | ||
|
|
||
| ## Examples | ||
|
|
||
| ### Basic Document Creation | ||
|
|
||
| ```yaml | ||
| tools: | ||
| add-company-doc: | ||
| kind: firestore-add-documents | ||
| source: my-firestore | ||
| description: Add a new company document | ||
| ``` | ||
|
|
||
| Usage: | ||
| ```json | ||
| { | ||
| "collectionPath": "companies", | ||
| "documentData": { | ||
| "name": { | ||
| "stringValue": "Acme Corporation" | ||
| }, | ||
| "establishmentDate": { | ||
| "timestampValue": "2000-01-15T10:30:00Z" | ||
| }, | ||
| "location": { | ||
| "geoPointValue": { | ||
| "latitude": 34.052235, | ||
| "longitude": -118.243683 | ||
| } | ||
| }, | ||
| "active": { | ||
| "booleanValue": true | ||
| }, | ||
| "employeeCount": { | ||
| "integerValue": "1500" | ||
| }, | ||
| "annualRevenue": { | ||
| "doubleValue": 1234567.89 | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ### With Nested Maps and Arrays | ||
|
|
||
| ```json | ||
| { | ||
| "collectionPath": "companies", | ||
| "documentData": { | ||
| "name": { | ||
| "stringValue": "Tech Innovations Inc" | ||
| }, | ||
| "contactInfo": { | ||
| "mapValue": { | ||
| "fields": { | ||
| "email": { | ||
| "stringValue": "info@techinnovations.com" | ||
| }, | ||
| "phone": { | ||
| "stringValue": "+1-555-123-4567" | ||
| }, | ||
| "address": { | ||
| "mapValue": { | ||
| "fields": { | ||
| "street": { | ||
| "stringValue": "123 Innovation Drive" | ||
| }, | ||
| "city": { | ||
| "stringValue": "San Francisco" | ||
| }, | ||
| "state": { | ||
| "stringValue": "CA" | ||
| }, | ||
| "zipCode": { | ||
| "stringValue": "94105" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| }, | ||
| "products": { | ||
| "arrayValue": { | ||
| "values": [ | ||
| { | ||
| "stringValue": "Product A" | ||
| }, | ||
| { | ||
| "stringValue": "Product B" | ||
| }, | ||
| { | ||
| "mapValue": { | ||
| "fields": { | ||
| "productName": { | ||
| "stringValue": "Product C Premium" | ||
| }, | ||
| "version": { | ||
| "integerValue": "3" | ||
| }, | ||
| "features": { | ||
| "arrayValue": { | ||
| "values": [ | ||
| { | ||
| "stringValue": "Advanced Analytics" | ||
| }, | ||
| { | ||
| "stringValue": "Real-time Sync" | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }, | ||
| "returnData": true | ||
| } | ||
| ``` | ||
|
|
||
| ### Complete Example with All Data Types | ||
|
|
||
| ```json | ||
| { | ||
| "collectionPath": "test-documents", | ||
| "documentData": { | ||
| "stringField": { | ||
| "stringValue": "Hello World" | ||
| }, | ||
| "integerField": { | ||
| "integerValue": "42" | ||
| }, | ||
| "doubleField": { | ||
| "doubleValue": 3.14159 | ||
| }, | ||
| "booleanField": { | ||
| "booleanValue": true | ||
| }, | ||
| "nullField": { | ||
| "nullValue": null | ||
| }, | ||
| "timestampField": { | ||
| "timestampValue": "2025-01-07T15:30:00Z" | ||
| }, | ||
| "geoPointField": { | ||
| "geoPointValue": { | ||
| "latitude": 37.7749, | ||
| "longitude": -122.4194 | ||
| } | ||
| }, | ||
| "bytesField": { | ||
| "bytesValue": "SGVsbG8gV29ybGQh" | ||
| }, | ||
| "arrayField": { | ||
| "arrayValue": { | ||
| "values": [ | ||
| { | ||
| "stringValue": "item1" | ||
| }, | ||
| { | ||
| "integerValue": "2" | ||
| }, | ||
| { | ||
| "booleanValue": false | ||
| } | ||
| ] | ||
| } | ||
| }, | ||
| "mapField": { | ||
| "mapValue": { | ||
| "fields": { | ||
| "nestedString": { | ||
| "stringValue": "nested value" | ||
| }, | ||
| "nestedNumber": { | ||
| "doubleValue": 99.99 | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| ## Authentication | ||
|
|
||
| The tool can be configured to require authentication: | ||
|
|
||
| ```yaml | ||
| tools: | ||
| secure-add-docs: | ||
| kind: firestore-add-documents | ||
| source: prod-firestore | ||
| description: Add documents with authentication required | ||
| authRequired: | ||
| - google-oauth | ||
| - api-key | ||
| ``` | ||
|
|
||
| ## Error Handling | ||
|
|
||
| Common errors include: | ||
| - Invalid collection path | ||
| - Missing or invalid document data | ||
| - Permission denied (if Firestore security rules block the operation) | ||
| - Invalid data type conversions | ||
|
|
||
| ## Best Practices | ||
|
|
||
| 1. **Always use typed values**: Every field must be wrapped with its appropriate type indicator (e.g., `{"stringValue": "text"}`) | ||
| 2. **Integer values can be strings**: The tool accepts integer values as strings (e.g., `{"integerValue": "1500"}`) | ||
| 3. **Use returnData sparingly**: Only set to true when you need to verify the exact data that was written | ||
| 4. **Validate data before sending**: Ensure your data matches Firestore's native JSON format | ||
| 5. **Handle timestamps properly**: Use RFC3339 format for timestamp strings | ||
| 6. **Base64 encode binary data**: Binary data must be base64 encoded in the `bytesValue` field | ||
| 7. **Consider security rules**: Ensure your Firestore security rules allow document creation in the target collection | ||
|
|
||
| ## Related Tools | ||
|
|
||
| - [`firestore-get-documents`](firestore-get-documents.md) - Retrieve documents by their paths | ||
| - [`firestore-query-collection`](firestore-query-collection.md) - Query documents in a collection | ||
| - [`firestore-delete-documents`](firestore-delete-documents.md) - Delete documents from Firestore | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.