Working with dynamic types on Google Cloud Datastore

Viewed 697

Our application is currently working on MongoDb. Our plan is to make it work with Datastore. In our application, there are some API calls and we don't know how its response is structured. So, we work with dynamic types. We get response and put it to Mongodb as is.

As far as we examined, Google.Cloud.Datastore.V1 .Net SDK pushes developers to work with structured type. We have to create an entity which has certain properties with predefined key names.

As shown below:

        MapField<string, Value> fields = new MapField<string, Value>();
        fields.Add("d1", "string value");
        fields.Add("d2", 30);
        fields.Add("d3", 55);

        var entity = new Entity
        {
            Key = key,
            Properties = { fields }
        };

What I've already tried is to set API response to a field. Unfortunately it ended up saving response as a string.

Is there anyway to achive it?

2 Answers

I believe the problem is simplified if you take int account that you don't need to use the Properties entity key.

I did the following, basing this in your examples. Note that i'm not a C# expert as you will see, but this did the trick:

string projectId = "MY-PROJECT";
string kind = "Task";
string name = "samplekey1";

DatastoreDb db = DatastoreDb.Create(projectId);

KeyFactory keyFactory = db.CreateKeyFactory(kind);

Key key = keyFactory.CreateKey(name);

var request = JObject.Parse("{number:1000, str:'string', array: [1,2,3,4,5,6]}");

var entity = new Entity()
{
    Key = key,
};

foreach(JProperty property in request.Properties())
{
    entity[property.Name] = property.Value.ToString();
}


using (DatastoreTransaction transaction = db.BeginTransaction())
{
    transaction.Insert(entity);
    transaction.Commit();
};

Note that this will not save the type of the values for each key (all of them will be Strings). This is not an issue if you don't need the Type (i.e. Int, String...) for each value to be used later.

However this seems to be the easiest solution, and it gets the job done to create each property with each value.

Take a look at this page in the documentation Properties and Value Types, specially the bit named Embedded entity which will allow you to store a JSON entity, and then query its subproperties as long as it is indexed.

Related