vettloffah / odoo-await

Odoo API client featuring async await.
74 stars 29 forks source link

Add readOne method #5

Closed richrd closed 3 years ago

richrd commented 3 years ago

I'm proposing to add a readOne method that's similar to read but only accepts a single ID and returns a single result or undefined. It'd be neater to use when just getting a single record without having to then do something like record = records[0];

Any thoughts?

vettloffah commented 3 years ago

Hi sorry I didn't see this before. A couple of notes here:

  1. In version 2.0, you can supply a single ID or an array of ID's in the read method. (but it still returns an array)
  2. It would be pretty easy to dd a feature to return a single record if a single ID is supplied, since it's already checking for an array in the read method. If an array is found, could return an array, otherwise return the first element of the array.
  3. There is a fairly elegant work-around now to get the single record now, and it looks like this:
const record = (await odoo.read('res.partner', 384, ['name','email']))[0]

I will add 2 from above to the next release.

Thank you

richrd commented 3 years ago

Thanks! That workaround is indeed pretty much what I want. I had forgotten that accessing non-existing array indices in JavaScript just returns undefined anyway. So no need for an if statement. That said I think a readOne method is probably unnecessary.