The problem I'm trying to solve is: I have a service that generates entities every day. Some of those entities will be new, and some will have already been found in a previous run.
I need to:
The generated data looks like this
[
{"key": "a",
"foo": "bar",
"timestamp": "EXMAPLE_TIMESTAMP" },
{"key": "b",
"foo": "baz",
"timestamp": "EXMAPLE_TIMESTAMP" }
]
Note: The entities have unique datastore keys
Edit: I tried using Put()
, but it just overwrites the entities if their keys already exist, which leads to the timestamps being updated even if the actual content is the same.
Use an insert mutation to save the entity only if the key does not exist.
_, err = client.Mutate(ctx, datastore.NewInsert(key, value))
if merr, ok := err.(datastore.MultiError); ok && merr[0] == codes.AlreadyExists {
err = nil
}
if err != nil {
// handle error
}