# README
Firestore rollback helper for golang
This has been built to handle the rollback a firestore event from the new value to and old value. The reason this now exists is because of the horrendous design feeling of data you retrieve from firestore.
See more about values: https://cloud.google.com/firestore/docs/reference/rest/v1/Value An example is the following:
// This struct:
type Tmp struct {
Id string `json:"id"`
}
// would become
type Tmp struct {
Id struct {
StringValue string `json:"stringValue"`
} `json:"stringValue"`
}
// TBD: Add all fields (bool etc). Arrays and maps are solved.
// Don't even get me started on arrays and maps...
Setup and use
go get github.com/frikky/firestore-rollback-go
Define according to sample: https://github.com/GoogleCloudPlatform/golang-samples/blob/master/functions/firebase/upper/upper.go
import (
rollback "github.com/frikky/firestore-rollback-go"
)
// Data from cloud function comes here
type FirestoreEvent struct {
OldValue FirestoreValue `json:"oldValue"`
Value FirestoreValue `json:"value"`
UpdateMask struct {
FieldPaths []string `json:"fieldPaths"`
} `json:"updateMask"`
}
// FirestoreValue holds Firestore fields.
type FirestoreValue struct {
CreateTime time.Time `json:"createTime"`
UserId string `json:"userId"`
Name string `json:"name"`
UpdateTime time.Time `json:"updateTime"`
Fields MyData `json:"fields"`
}
// MyData represents a value from Firestore. The type definition depends on the
// format of your database.
// string = StringValue
// int/int64 = IntegerValue
// bool = BooleanValue
// .. etc
type MyData struct {
Original rollback.StringValue `json:"original"`
}
// Now that you got it parsed into MyData, you will have to fix the data to rollback
// This will give you back a map[string]interface
// Rolls back your data for you (with transforms)
writtenData, firestoreReturn, err := rollback.Rollback(
ctx,
firestoreClient,
firestoreValue.Name, // The path from the struct above
firestoreValue.Fields, // The parsed data you have
)
// To just get the interface decoded itself into a map[string]interface{}
data := fsf.GetInterface(oldValue.Fields)
// ret, err := client.Collection("THISCOLLECTION").Doc("THISDOC").Set(ctx, data)
// if err != nil {
// ...
// }
TBD: Make transformer for normal struct -> firestore struct
Firestore cloud functions
How cloud functions are/can be used:
- Frontend -> firestore -> cloud function -> firestore
- Frontend -> cloud function -> firestore
This project is made specifically for handling the first case.
# Structs
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
No description provided by the author
This is inside an array again.