mornindew
mornindew

Reputation: 2221

Best Practices For Ignoring Nil Values in Firestore

I am building custom webservices using Golang (Go-Swagger for API Definition) and Firestore as my document store. Everything is working fine except it gets "awkward" on Updates to the database. I have a solution but I am not convinced it is the most elegant one. Has anyone else run into this, and how did they solve it....

Problem Statement: Nil values are overwriting existing values in firebase when I write to the database

Proposed Solution - MY API has json:"omitempty" on optional parameters and nil values will be passed in (as opposed to getting the default golang values). I am planning on taking the Struct passed into the API and one attr at a time populating it into my Struct that firestore is using. I have performance concerns but don't want to couple my API Struct with my DB Struct. I am also going to set the firestore:"omitempty"` string literal on my firestore struct much like the API Struct. I hope this will tell firestore to not write any values that are nil.

Is this the best way? Are their other alternatives? The default values of golang seems to have pluses and minuses.

UPDATE:

I ended up in a similar place as my proposed solution but also followed the suggestion that Dimitry suggested. I created an []firestore.Update object to contain only the values that I want to update. When I called firestore it will only update those values.

Here is the function that I am using to call firestore.

func updateProfileUsingUpdateMethod(documentName string, values []firestore.Update) error {
    ctx := context.Background()
    app := firestorehelper.GetFirestoreApp()

    client, err := app.Firestore(ctx)
    if err != nil {
        return err
    }
    defer client.Close()

    //Set the updated date

    wr, error := client.Doc(collectionName+"/"+documentName).Update(ctx, values)
    if error != nil {
        return error
    }
    fmt.Println(wr.UpdateTime)
    //Assume success
    return nil
} 

I also create a firestore.Update object and will append that to my []firestore.Update slice.

firestore.Update{Path: "PATH_TO_ATTR", Value: VALUE_TO_PASS_IN}

Upvotes: 1

Views: 3096

Answers (1)

Dmitry Harnitski
Dmitry Harnitski

Reputation: 6008

You have two options:

  1. Use map[string]interface{}
  2. Use custom structure with firestore attribute -firestore:"FieldName,omitempty".

    _, err := client.Collection("cities").Doc("DC").Set(ctx, map[string]interface{}{ "capital": true, }, firestore.MergeAll) if err != nil { // Handle any errors in an appropriate way, such as returning them. log.Printf("An error has occurred: %s", err) }

Do not confuse json omitempty with firestore.

You also must to provide field name as this syntax will use omitempty as name.

firestore:"omitempty" - not correct

https://firebase.google.com/docs/firestore/manage-data/add-data

code

Upvotes: 2

Related Questions