title: How do I read all entities of a kind in a transaction with google cloud datastore nodejs
body: <p>When I try run a query to read all entities of a kind in a transaction with google datastore it gives me this error</p>
  
  <pre><code>{ Error: Only ancestor queries are allowed inside transactions.
      at /root/src/node_modules/grpc/src/client.js:554:15
    code: 3,
    metadata: Metadata { _internal_repr: {} },
  </code></pre>
  
  <p>So I need to use an ancestor query. How do I create an ancestor query? It appears to depend on how you structured the hierarchy in datastore. So my next question is, given every entity I have created in datastore has been saved like so (the identifier is unique to the entityData saved)</p>
  
  <pre><code>const entityKey = datastore.key({ namespace: ns, path: [kind, identifier] });
  { key: entityKey, method: 'upsert', data: entityData };
  </code></pre>
  
  <p>How do I read from the db within a transaction? I think I could do it if I knew the identifiers, but the identifiers are constructed from the entityData that I saved in the kind and I need to read the entities of the kind to figure out what I have in the db (chicken egg problem). I am hoping I am missing something.</p>
  
  <p><strong>More context</strong></p>
  
  <p>The domain of my problem involves sponsoring people. I have stored a kind <code>people</code> in datastore where each entity is a <code>person</code> consisting of a unique identifier, name and grade. I have another kind called <code>relationships</code> where each entity is a <code>relationship</code> containing two of the peoples identifiers, the <code>sponsor</code> &amp; <code>sponsee</code> (linking to people together). So I have structured it like an RDB. If I want to get a persons sponsor, I get all the relationships from the db, loop over them returning the relationships where the <code>person</code> is the <code>sponsee</code> then query the db for the <code>sponsor</code> of that <code>relationship</code>.</p>
  
  <p>How do I structure it the 'datastore' way, with entity groups/ancestors, given I have to model people and their links/relationships.</p>
  
  <p>Let's assume a RDB is out of the question.</p>
  
  <p><strong>Example scenario</strong></p>
  
  <p>Two people have to be deleted from the app/db (let's say they left the company on the same day). When I delete someone, I also want to remove their relationships. The two people I delete share a relationship (one is sponsoring the other). Assume the first transaction is successful i.e. I delete one person and their relationship. Next transaction, I delete one person, then search the relationships for relevant relationships and I find one that has already been deleted because eventually consistent. I try find the person for that relationship and they don't exist. Blows up.</p>
  
  <p>Note: each transaction wraps delete person &amp; their relationship. Multiple people equals multiple transactions.</p>
  
  <p><strong>Scalability is not a concern for my application</strong></p>