CloudKit JS and Many-To-Many relationships

Hi,

I'm having difficulties fetching many 2 many relationships. I have an Actor entity (which I can query in CloudKit JS) that has 2 relationships: inputComponents and outputComponents, both pointing towards a Component entity. How to query these relationships?

Thanks Tom

Assuming that your CloudKit schema has a record type named Actor, which has a CKReferenceList field pointing to an input component array (inputComponents), and you would like to query the actors that have a certain input component, you might try the following query:

var query = {
  recordType: 'Actor',
  filterBy: [{
    fieldName: 'inputComponents',
    comparator: 'CONTAINS_ANY_OF_TYPE', // This handles the to-many containment.
    fieldValue: {
      value: {
        recordName: 'theRecordName' // The name of target Component record.
      }
    }
  }]
};

Best,
——
Ziqiao Chen
 Worldwide Developer Relations.

I want to get a list of all component records which are inputComponents or outputComponents to the actor.

Below I first fetch all CD_Components belonging to a CD_Project. Next I fetch all CD_Actors belonging to that same CD_Project. And next I want to get the CD_Components which are inputComponents or outputComponents from a CD_Actor and I'm looking for the code on how to do this.

const componentQuery = {
  recordType: "CD_Component",
  filterBy: [{ 
    fieldName: 'CD_project', 
    comparator: 'EQUALS', 
    fieldValue: {value:  projectId}
  }],
};

try {
  const response = await database.performQuery(componentQuery);
  if (response.hasErrors) {
    console.error("Query Errors:", response.errors);
    setError("Error fetching records");
  } 
  else {
    components = response.records;
    console.log(response.records);
  }
} catch (err) {
  console.error("Error during query:", err);
}    

const actorQuery = {
  recordType: "CD_Actor",
  filterBy: [{ 
    fieldName: 'CD_project', 
    comparator: 'EQUALS', 
    fieldValue: {value:  projectId}
  }],
};

try {
  const response = await database.performQuery(actorQuery);
  if (response.hasErrors) {
    console.error("Query Errors:", response.errors);
    setError("Error fetching records");
  } 
  else {
    actors = response.records;
    console.log(response.records);
  }
} catch (err) {
  console.error("Error during query:", err);
}    
CloudKit JS and Many-To-Many relationships
 
 
Q