Search code examples
javascriptgoogle-app-enginegoogle-cloud-datastore

Google Cloud Datastore: Storing arrays and objects


I'm just coming from learning IndexedDB, so the concept of Google Cloud Datastore is confusing me.

How exactly do you store arrays and objects?

Consider this scenario:

{ category: 'football', keywords: ['foo', 'bar'] }
{ dinosaurs: { trex: { teeth: 'large' } } }

How exactly should I structure the database?

In IndexedDB, you have multiEntry which allows for all the keywords in an array to be stored as separate entries.

For the array: If I want to search by keyword, should I build a new entity with "kind" keywords and store each keyword as a separate entity? How would I link it to it's relevant category?

For the object: Should I store it as a JSON string and then stringify / decode it in the script?


Solution

  • You can save a list of Strings in a single property. This property can be either indexed, or unindexed.

    For an object, you create an entity:

    Entity entity = new Entity("dinosaurs");
    entity.setProperty("type", "trex");
    entity.setUnindexedProperty("teeth", "large");
    

    This way you can indicate which properties you want to index (at extra cost), and which properties you want unindexed.

    The example above is using Java Datastore API, but you can also use a framework like Objectify to manage your objects and entities.

    There is a reasonably good documentation on Datastore, and many examples/tutorials available.