Search code examples
gogoogle-cloud-platformgoogle-cloud-datastore

Write entity only if the key isn't present in Google Datastore


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:

  1. keep the database up-to-date, i.e. containing all the entities that have ever been found
  2. be able to tell when a new entity is found. Each entity contains a timestamp, so if I can make sure entities aren't over-written, this shouldn't be too hard (I'll just query the database for the entities that were found today and these should be the new ones), so this is what I'm trying to do.

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.


Solution

  • 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
    }