Search code examples
javascriptnode.jsnosqlgoogle-cloud-datastoredatastore

How to retrieve entities by passing array of IDs as input in Google datastore?


I am trying to implement the following SQL logic in the datastore,

SELECT * from table where id in [1,2,3,4,5]

Implementing this in datastore, I want to retrieve all the corresponding entities with these IDs as an array.

let employees = []
try {
  for (let id of idArray) {
    const employee = await employeeRepo.getOneById(workspace, id)
    employees.push(employee)
  }
} catch (e) {
  throw e;
 }

This is the naive logic of the function, and I am trying to reduce it to a single query.


Solution

  • Are you using the Node.js library referenced here: https://cloud.google.com/datastore/docs/reference/libraries

    There is a function get where you can pass in an array of keys and it will return the array of entities.

    https://googleapis.dev/nodejs/datastore/latest/Datastore.html#get