https://github.com/unitvectory-labs/firestoreproto2map
Helper library to convert Firestore Protocol Buffer from event to map that can be used by Firestore
https://github.com/unitvectory-labs/firestoreproto2map
gcp-firestore java-17
Last synced: 4 months ago
JSON representation
Helper library to convert Firestore Protocol Buffer from event to map that can be used by Firestore
- Host: GitHub
- URL: https://github.com/unitvectory-labs/firestoreproto2map
- Owner: UnitVectorY-Labs
- License: apache-2.0
- Created: 2024-04-14T00:41:22.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2024-09-14T12:36:37.000Z (10 months ago)
- Last Synced: 2024-09-14T22:45:02.697Z (10 months ago)
- Topics: gcp-firestore, java-17
- Language: Java
- Homepage:
- Size: 153 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://github.com/UnitVectorY-Labs/firestoreproto2map/releases/latest) [](https://opensource.org/licenses/Apache-2.0) [](https://guide.unitvectorylabs.com/bestpractices/status/#active) [](https://central.sonatype.com/artifact/com.unitvectory/firestoreproto2map) [](https://javadoc.io/doc/com.unitvectory/firestoreproto2map) [](https://codecov.io/gh/UnitVectorY-Labs/firestoreproto2map)
# firestoreproto2map
Java helper library to convert Firestore Protocol Buffer from event to map that can be used by Firestore
## Purpose
This library takes the Protocol Buffer sent from Firestore for a document and converts it to a Java Map Object. Firestore stores the underlying documents as Protocol Buffers and therefore that is what is sent to a Cloud Function when subscribed. If you want to take this and insert it back into Firestore it must be converted to a compatible object, that is what this library does.
## Getting Started
This library requires Java 17 and is available in the Maven Central Repository:
```xml
com.unitvectory
firestoreproto2map
0.0.4```
## Usage
```java
package functions;import com.google.cloud.functions.CloudEventsFunction;
import com.google.events.cloud.firestore.v1.DocumentEventData;
import com.google.protobuf.InvalidProtocolBufferException;
import com.unitvectory.firestoreproto2map.FirestoreProto2Map;
import io.cloudevents.CloudEvent;
import java.util.logging.Logger;public class FirebaseFirestore implements CloudEventsFunction {
private static final Logger logger = Logger.getLogger(FirebaseFirestore.class.getName());@Override
public void accept(CloudEvent event) throws InvalidProtocolBufferException {
DocumentEventData firestoreEventData = DocumentEventData.parseFrom(event.getData().toBytes());
// This will not handle DocumentReference attributes
FirestoreProto2Map firestoreProto2Map = new FirestoreProto2Map();if (firestoreEventData.hasOldValue()) {
Map oldValueMap = firestoreProto2Map.convert(firestoreEventData.getOldValue());// Map represents object that can be inserted into Firestore
}if (firestoreEventData.hasValue()) {
Map valueMap = firestoreProto2Map.convert(firestoreEventData.getValue());// Map represents object that can be inserted into Firestore
}
}
}
```## Reference Limitation
Firestore supports reference documents, these are handled different from the other field types and require a `DocumentReference` object which requires the Firestore SDK to get that object.
```java
Firestore firestore = FirestoreOptions.getDefaultInstance().getService();// This allows DocumentReference's to be handled
FirestoreProto2Map converter = new FirestoreProto2Map(new ValueToDocumentReferenceMapper() {
@Override
public DocumentReference convert(String referenceValue, String documentPath) {
return firestore.document(documentPath);
}
});
```