populate()which lets you reference other entities in your Schemas and easily fetch them and merge their data whenever needed. Population automatically replaces the specified paths in an entity with the data from other entities. We may populate a single entity, multiple entities, or all the entites returned from a query. We can even populate nested entities. Let's look at some examples.
123generated ID from the Datastore.
populate()calls to fetch multiple references
populate()withouth specifying any references. All the properties on your Schema that have their type set as
Schema.Types.Keywill be fetched.
populate()accepts a second argument to specify one or multiple properties to be returned from the reference entity.
populate()comes with queries as it allows you to very easily join entities data, similar to a "Left Join" in SQL. gstore uses Dataloader to fetch all the entities by keys in one call. Using Dataloader means that if multiple entities returned by the query have references pointing to the same entity Key, only 1 Key will be sent to
Datastore.get()and fetched from the Datastore. It also means that the entities fetched are memoized and returned from cache if the reference Key is the same at different "depth" of the tree traversal. This not only means faster retrieval time but only some nice savings on your Datastore billing at the end of the month!
['User', 1], ['User', 2], ['Collection', 1].
0keys fetched the second time that this query is executed, until a "User", "Post" or "Collection" is added/updated or removed.
populate(), this corresponds to the maximum number of distinct entity references at one "depth" of the entity data tree. Thanks to the Dataloader and cache, this limit should be hard to reach in most use cases. (View the Datastore limitations)
populate()chaining calls with the same arguments that we have seen in previous examples.
populate()method that you can call to quickly fetch all the entity references. Like any other populate() that we have seen above, it takes 2 arguments: the "references" to fetch and the "properties" to return from each entity reference.