Created
November 20, 2019 05:35
-
-
Save frikky/faf3df28fb0ddc9e9064d7c74b735fd1 to your computer and use it in GitHub Desktop.
A Firestore user edit that should roll back your data
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
package user | |
import ( | |
"context" | |
"errors" | |
"log" | |
"time" | |
"cloud.google.com/go/firestore" | |
) | |
var client *firestore.Client | |
type FirestoreEvent struct { | |
OldValue FirestoreValue `json:"oldValue"` | |
Value FirestoreValue `json:"value"` | |
UpdateMask struct { | |
FieldPaths []string `json:"fieldPaths"` | |
} `json:"updateMask"` | |
} | |
type FirestoreValue struct { | |
CreateTime time.Time `json:"createTime"` | |
Name string `json:"name"` | |
UpdateTime time.Time `json:"updateTime"` | |
Fields User `json:"fields"` | |
} | |
// This is our self-defined fields. | |
// FirestoreEvent.Value.Fields = User | |
type User struct { | |
Username StringValue `json:"userId"` | |
Email StringValue `json:"email"` | |
DateEdited IntegerValue `json:"date_edited"` | |
} | |
type IntegerValue struct { | |
IntegerValue string `json:"integerValue"` | |
} | |
type StringValue struct { | |
StringValue string `json:"stringValue"` | |
} | |
// Simple init to have a firestore client available | |
func init() { | |
ctx := context.Background() | |
var err error | |
// FIXME - add your username in here | |
client, err = firestore.NewClient(ctx, "medium-77273") | |
if err != nil { | |
log.Fatalf("Firestore: %v", err) | |
} | |
} | |
// Handles the rollback to a previous document | |
func handleRollback(ctx context.Context, e FirestoreEvent) error { | |
return errors.New("Should have rolled back to a previous version") | |
} | |
// The function that runs with the cloud function itself | |
func HandleUserChange(ctx context.Context, e FirestoreEvent) error { | |
// This is the data that's in the database itself | |
newFields := e.Value.Fields | |
oldFields := e.OldValue.Fields | |
// As our goal is simply to check if the username has changed | |
if newFields.Username.StringValue == oldFields.Username.StringValue { | |
log.Printf("Bad username: %s - %s", newFields.Username.StringValue, oldFields.Username.StringValue) | |
return handleRollback(ctx, e) | |
} | |
// Check if the email is the same as previously | |
if newFields.Email.StringValue != oldFields.Email.StringValue { | |
log.Printf("Bad email: %s - %s", newFields.Email.StringValue, oldFields.Email.StringValue) | |
return handleRollback(ctx, e) | |
} | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment