Search code examples
baqend

Deep querying of data


Is it possible to do a "deep query" of data stored in Baqend?

Something like this:

db.Shifts.find()
   .where({
      "applicants.applicants": { "$in": ["db/User/12"] },
   })

applicants.applicants is a reference to a Set that is saved in a different data class.

With no filter, the data is shaped like this:

Array [
  Object {
    "id": "/db/Shifts/123",
    "applicants": Object {
      "applicants": Array [
        "/db/User/12",
        "/db/User/13",
      ],
  },
  Object {
    "id": "/db/Shifts/456",
    "applicants": Object {
      "applicants": Array [
        "/db/User/12",
        "/db/User/14",
      ],
  },  
  Object {
    "id": "/db/Shifts/789",
    "applicants": Object {
      "applicants": Array [
        "/db/User/13",
        "/db/User/14",
      ],
  },
]

So in this case I'm trying make a query that will return only shift 123 and 456, because they both have User 12. Is this possible or do we need to use Ramda to filter it after we get the data back on the client side?


Solution

  • deep-queries are only possible if the applicants are stored inside the Shifts objects. Here is actually an example of this deep query in our guide where we filter by activities.start.

    Your query should look something like this:

    db.Shifts.find()
      .in('applicants.applicants', "/db/User/12")
      .resultList()
      .then(shifts => ...)
    

    You can also search for multiple users at once if you fancy it. Works simply like this:

    db.Shifts.find()
      .in('applicants.applicants', "/db/User/12", "/db/User/13", "/db/User/14")
      .resultList()
      .then(shifts => ...)
    

    This will return all Shifts that where one of the users 12, 13 or 14 is an applicant.

    If your application stores the applicants in a different class (say the Applicant class) and only references them inside the Shifts class, you need to first query the Applicants objects like this:

    db.Applicants.find()
      .in('applicants', "/db/User/12")
      .resultList()
      .then(applicantObjects => ...)
    

    With these you can then query the Shifts like this

    db.Shifts.find()
      .in('applicants', applicantObjects.map(it => it.id))
      .resultList()
      .then(shifts => ...)
    

    Hope this helps ;)