rollback

package module
v0.2.6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 17, 2019 License: MIT Imports: 9 Imported by: 0

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:

  1. Frontend -> firestore -> cloud function -> firestore
  2. Frontend -> cloud function -> firestore

This project is made specifically for handling the first case.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetInterface

func GetInterface(subValue interface{}) map[string]interface{}

passedField takes arrayValue

func Rollback

func Rollback(ctx context.Context, client *firestore.Client, firestoreLocation string, subValue interface{}) (map[string]interface{}, *firestore.WriteResult, error)

Types

type ArrayValue

type ArrayValue struct {
	Values []Value `json:"values"`
}

type BooleanValue

type BooleanValue struct {
	BooleanValue string `json:"booleanValue"`
}

type BytesValue

type BytesValue struct {
	BytesValue string `json:"bytesValue"`
}

type DoubleValue

type DoubleValue struct {
	DoubleValue string `json:"doubleValue"`
}

type IntegerValue

type IntegerValue struct {
	IntegerValue string `json:"integerValue,omitempty"`
}

type MapValue

type MapValue struct {
	Fields interface{} `json:"fields"`
}

type NullValue

type NullValue struct {
	NulLValue string `json:"nullValue"`
}

type ReferenceValue

type ReferenceValue struct {
	ReferenceValue string `json:"referenceValue"`
}

type StringValue

type StringValue struct {
	StringValue string `json:"stringValue"`
}

type TimestampValue

type TimestampValue struct {
	TimestampValue string `json:"timestampValue"`
}

type Value

type Value struct {
	MapValue     MapValue     `json:"mapValue,omitempty"`
	StringValue  StringValue  `json:"stringValue,omitempty"`
	IntegerValue IntegerValue `json:"integerValue,omitempty"`
	ArrayValue   ArrayValue   `json:"arrayValue,omitempty"`
	BooleanValue BooleanValue `json:"booleanValue,omitempty"`
	DoubleValue  DoubleValue  `json:"doubleValue,omitempty"`
}

This is inside an array again. Always confuse.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL