nodejs-datastore icon indicating copy to clipboard operation
nodejs-datastore copied to clipboard

transaction.get() is undocumented and buggy

Open kaansoral opened this issue 4 years ago • 2 comments

  1. I can't see .get() documented here: https://googleapis.dev/nodejs/datastore/latest/Transaction.html
  2. Like all annoying Node.js methods, even if you provide a single key to it, it returns an array with that entity
  3. It seems to work if you provide an array of keys to it, but instead of returning an array of entities, it returns an array of an array that have the first entity in it's 0th slot (This is a bug, it shouldn't work at all)

Some bonus questions to whoever designed this entire API:

  1. Why datastore.key(['Kind',key]) instead of datastore.key('Kind',key)
  2. For the regular datastore.get (and this one too), providing a single key returns an array including that single element, can you sleep at night?

Coming from a Python NDB background, I really don't understand these decisions, even from a JS standpoint, they don't make sense

kaansoral avatar Jan 22 '21 16:01 kaansoral

I mean, since transaction.get() doesn't accept an array of keys like datastore.get() - it should've at least returned a single bare entity, but it also returns a 1 Length array with the entity inside - I mean it just doesn't make any sense ...

kaansoral avatar Jan 22 '21 16:01 kaansoral

I can't see .get() documented here: https://googleapis.dev/nodejs/datastore/latest/Transaction.html

@AVaksman do you know how to get that method in there?

if you provide a single key to [transaction.get()], it returns an array with that entity providing a single key returns an array including that single element it should've at least returned a single bare entity, but it also returns a 1 Length array with the entity inside It seems to work if you provide an array of keys to it, but instead of returning an array of entities, it returns an array of an array

@AVaksman Is promisifyAll forcing the array / is it possible to work around it?

transaction.get() doesn't accept an array of keys like datastore.get()

It seemed to accept two keys and return both matching entities in my testing:

const key = datastore.key(['Company', 'Google']);
const key2 = datastore.key(['Company', 'Google2']);

datastore.save({key, data: {url: 'www.google.com'}});
datastore.save({key: key2, data: {url: 'www.google.com'}});

const transaction = datastore.transaction();
await transaction.run();

const [entities] = await transaction.get([key, key2]);
console.log(entities[0]); // { url: 'www.google.com', [Symbol(KEY)]: [Key] }
console.log(entities[1]); // { url: 'www.google.com', [Symbol(KEY)]: [Key] }

Why datastore.key(['Kind',key]) instead of datastore.key('Kind',key)

We could probably add support for that style of usage. Are you interested in sending a PR?

stephenplusplus avatar Jan 22 '21 19:01 stephenplusplus