Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 728
djibouti33 Avatar asked Sep 21 '26 05:09

djibouti33


1 Answers

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

like image 87
Malitta N Avatar answered Sep 22 '26 20:09

Malitta N



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!