Search code examples
androidreact-nativerealmrealm-list

Use React Native Realm to Query through multiple List Objects


First day using React Native and Realm, and I'm having a hard time figuring how to perform a query through two Realm List Objects.

Tasks have Reservations, which have Renters, which have first_name and last_name fields. I want my users to be able to search for tasks by the Renter's first and last names.

Essentially, "Give me all the tasks whose renter's first or last name begins with "xyz""

const TaskSchema = {
  name:'Task',
  properties: {
    reservations:{ type: LIST, objectType: ReservationSchema.name },
  }
},

const ReservationSchema = {
  name:'Reservation',
  properties: {
    renters:{ type: LIST, objectType: RenterSchema.name },
  },
}

const RenterSchema = {
  name:'Renter',
  properties: {
    first_name:{ type:STRING, optional:true },
    last_name:{ type:STRING, optional:true },
  },
}

I just can't figure out how to set up my query and predicates to accomplish this.


Solution

  • You can filter through nested objects.

    let tasks = realm.objects('Dog');
    let xyzTasks = task.filtered('reservations.renters.first_name BEGINSWITH "xyz" OR reservations.renters.last_name BEGINSWITH "xyz"');
    

    Reference: https://realm.io/docs/react-native/latest/#filtering