I'm new to this nuget package and confused with the Key class.
Here is my code base on Google.Cloud.Datastore.V1 document:
public long InsertMessage<T>(T iEntity) where T : IEntity<T>
{
var keyFactory = _db.CreateKeyFactory(Kind);
var entity = iEntity.ToEntity();
entity.Key = keyFactory.CreateIncompleteKey();
using (var transaction = _db.BeginTransaction())
{
transaction.Insert(entity);
var commitResponse = transaction.Commit();
var insertedKey = commitResponse.MutationResults[0].Key;
Logger.Info($"Inserted key: {insertedKey}");
return insertedKey.Path[0].Id;
}
}
All I do is to create an entity and create an incomplete key, send to the server, then get the populated key back from server.
I think the key is served as unique identity to the entity.
If there's misunderstanding please correct me.
I can get entities by Query as below:
var query = new Query(Kind)
{
Filter = Filter.Equal("key", key),
Order = { { "created", PropertyOrder.Types.Direction.Ascending } }
};
foreach (var entity in _db.RunQueryLazily(query))
{
list.Add(entity);
}
But I don't know how to use the key I got when inserted to get the unique entity by Filter.Equal("key", key)
.
The example shows the Key's structure is:
{
"partitionId":
{
"projectId": "projectId",
"namespaceId": "namespaceId"
},
"path": [
{
"kind": "kind",
"id": "id"
}]
}
Here I conclude the question I have:
- What is Key class for?
- What is Key's path and why it's array?
- Is
id
the unique key to the entity? - How to
Query
byKey
andId
?
Thanks for reading and please don't mind my poor English.