ReactJS
Data objects
Eseguire Join Relazionali in Parse con JavaScript
8 min
unire query usando parse introduzione in questa guida, eseguirai query relazionali in parse mimando il comportamento delle query sql join utilizzando il tuo database prerequisiti per completare questo tutorial, avrai bisogno di un'app creata su back4app obiettivo interrogare dati relazionali memorizzati su back4app in modo simile a una query sql join 1 comprendere la classe parse query qualsiasi operazione di query di parse utilizza il parse query parse query tipo di oggetto, che ti aiuterà a recuperare dati specifici dal tuo database in tutta la tua app è fondamentale sapere che un parse query parse query si risolverà solo dopo aver chiamato un metodo di recupero (come parse query find parse query find o parse query get parse query get ), quindi una query può essere impostata e diversi modificatori possono essere concatenati prima di essere effettivamente chiamati per creare un nuovo parse query parse query , devi passare come parametro la parse object parse object desiderata, che sarà quella che conterrà i risultati della tua query un esempio di query può essere visto qui sotto, in cui viene interrogata una profile profile fittizia 1 // this will create your query 2 let parsequery = new parse query("profile"); 3 // the query will resolve only after calling this method 4 let queryresult = await parsequery find(); puoi leggere di più sulla parse query parse query classe qui nella documentazione ufficiale https //parseplatform org/parse sdk js/api/master/parse query html 2 salva alcuni dati su back4app creiamo due classi di esempio, tablea tablea e tableb tableb , che saranno gli obiettivi delle nostre query in questa guida sulla console js di parse è possibile eseguire codice javascript direttamente, interrogando e aggiornando i contenuti del database della tua applicazione utilizzando i comandi del js sdk esegui il codice qui sotto dalla tua console js e inserisci i dati su back4app ecco come appare la console js nel tuo dashboard vai avanti e crea le classi con il seguente contenuto 1 // create tablea and its records 2 let tablearecord1 = new parse object('tablea'); 3 tablearecord1 set('fielda', 'value a 1'); 4 tablearecord1 = await tablearecord1 save(); 5	 6 let tablearecord2 = new parse object('tablea'); 7 tablearecord2 set('fielda', 'value a 2'); 8 tablearecord2 = await tablearecord2 save(); 9	 10 let tablearecord3 = new parse object('tablea'); 11 tablearecord3 set('fielda', 'value a 3'); 12 tablearecord3 = await tablearecord3 save(); 13	 14 // create tableb and its records, some of them linked to tablea 15 let tablebrecord1 = new parse object('tableb'); 16 tablebrecord1 set('fieldb', 'value b 1'); 17 tablebrecord1 set('link', tablearecord1); 18 tablebrecord1 = await tablebrecord1 save(); 19	 20 let tablebrecord2 = new parse object('tableb'); 21 tablebrecord2 set('fieldb', 'value b 2'); 22 tablebrecord2 set('link', tablearecord1); 23 tablebrecord2 = await tablebrecord2 save(); 24	 25 let tablebrecord3 = new parse object('tableb'); 26 tablebrecord3 set('fieldb', 'value b 3'); 27 tablebrecord3 set('link', tablearecord3); 28 tablebrecord3 = await tablebrecord3 save(); 29	 30 let tablebrecord4 = new parse object('tableb'); 31 tablebrecord4 set('fieldb', 'value b 4'); 32 tablebrecord4 = await tablebrecord4 save(); 33	 34 console log('success!'); 3 interrogare i dati ora che hai popolato le classi, possiamo ora eseguire le query relazionali in esse iniziamo eseguendo l' inner join inner join , introducendo la query relazionale di join che utilizzeremo in tutti i nostri esempi questa query rappresenta i risultati di due query combinate tra le tabelle a e b, restituendo tutti i record che sono correlati da una condizione specifica utilizzando il parse query matchesquery parse query matchesquery metodo 1 // join query, get all records in tablea that have matching records in tableb 2 let innerquerytablea = new parse query("tablea"); 3 // limit to 10 results only for example so we don't fetch too much data 4 innerquerytablea limit(10); 5 let joinquerytableb = new parse query("tableb"); 6 // match the tablea query by the "link" property 7 joinquerytableb matchesquery("link", innerquerytablea); 8 // include the "link" property so we have the content of tablea as well 9 joinquerytableb include("link"); 10 let joinqueryresults = await joinquerytableb find(); 11	 12 // inner join, get only the records in tablea that have matching records in tableb 13 console log("inner join"); 14 console log("table a id | field a | field b"); 15 for (let joinresult of joinqueryresults) { 16 console log( 17 `${joinresult get("link") id} | ${joinresult 18 get("link") 19 get("fielda")} | ${joinresult get("fieldb")}` 20 ); 21 } il inner join inner join comportamento della query sql è esattamente quello ottenuto nella nostra query relazionale di join generica, quindi dobbiamo stampare i suoi risultati nella console ricorda che con un parse object parse object puoi utilizzare il metodo get per recuperare i dati utilizzando il nome della colonna ora eseguiamo un left outer join left outer join consistente nel recuperare tutti i record su tablea tablea e mostrando i dati relazionali su tableb, quando disponibili 1 // join query, get all records in tablea that have matching records in tableb 2 let innerquerytablea = new parse query("tablea"); 3 // limit to 10 results only for example so we don't fetch too much data 4 innerquerytablea limit(10); 5 let joinquerytableb = new parse query("tableb"); 6 // match the tablea query by the "link" property 7 joinquerytableb matchesquery("link", innerquerytablea); 8 // include the "link" property so we have the content of tablea as well 9 joinquerytableb include("link"); 10 let joinqueryresults = await joinquerytableb find(); 11	 12 // left outer join, get records in tablea that have matching records in tableb and also every 13 // other tablea record 14 let querytablea = new parse query("tablea"); 15 querytablea limit(10); 16 let querytablearesults = await querytablea find(); 17 console log("left join"); 18 console log("table a id | field a | field b"); 19 for (let result of querytablearesults) { 20 // get all entries from join query that have a link to this tablea entry 21 let joinqueryresultsfiltered = joinqueryresults filter( 22 (joinqueryresult) => 23 joinqueryresult get("link") !== undefined && 24 joinqueryresult get("link") id == result id 25 ); 26 if (joinqueryresultsfiltered length > 0) { 27 for (let joinresult of joinqueryresultsfiltered) { 28 let fieldbvalue = joinresult get("fieldb"); 29 console log(`${result id} | ${result get("fielda")} | ${fieldbvalue}`); 30 } 31 } else { 32 console log(`${result id} | ${result get("fielda")} | `); 33 } 34 } il right outer join right outer join è l'opposto del sinistro, recuperando i record da tableb tableb 1 // join query, get all records in tablea that have matching records in tableb 2 let innerquerytablea = new parse query("tablea"); 3 // limit to 10 results only for example so we don't fetch too much data 4 innerquerytablea limit(10); 5 let joinquerytableb = new parse query("tableb"); 6 // match the tablea query by the "link" property 7 joinquerytableb matchesquery("link", innerquerytablea); 8 // include the "link" property so we have the content of tablea as well 9 joinquerytableb include("link"); 10 let joinqueryresults = await joinquerytableb find(); 11	 12 // right outer join, get records in tablea that have matching records in tableb and also every 13 // other tableb record 14 let querytableb = new parse query("tableb"); 15 querytableb limit(10); 16 let querytablebresults = await querytableb find(); 17 console log("right join"); 18 console log("table b id | field a | field b"); 19 for (let result of querytablebresults) { 20 // get all entries from join query that matches this tableb entry 21 let joinqueryresultsfiltered = joinqueryresults filter( 22 (joinqueryresult) => joinqueryresult id == result id 23 ); 24 if (joinqueryresultsfiltered length > 0) { 25 for (let joinresult of joinqueryresultsfiltered) { 26 let fieldavalue = ""; 27 if (joinresult get("link") !== undefined) { 28 fieldavalue = joinresult get("link") get("fielda"); 29 } 30 console log( 31 `${result id} | ${fieldavalue} | ${joinresult get("fieldb")}` 32 ); 33 } 34 } else { 35 console log(`${result id} | | ${result get("fieldb")}`); 36 } 37 } infine, abbiamo il full outer join full outer join che è la combinazione dei join interni sinistro e destro 1 // join query, get all records in tablea that have matching records in tableb 2 let innerquerytablea = new parse query("tablea"); 3 // limit to 10 results only for example so we don't fetch too much data 4 innerquerytablea limit(10); 5 let joinquerytableb = new parse query("tableb"); 6 // match the tablea query by the "link" property 7 joinquerytableb matchesquery("link", innerquerytablea); 8 // include the "link" property so we have the content of tablea as well 9 joinquerytableb include("link"); 10 let joinqueryresults = await joinquerytableb find(); 11	 12 // full outer join, combining left and right outer join results 13 console log("full join"); 14 console log("table id | field a | field b"); 15 // first print all inner join results 16 for (let joinresult of joinqueryresults) { 17 console log( 18 `${joinresult get("link") id} | ${joinresult 19 get("link") 20 get("fielda")} | ${joinresult get("fieldb")}` 21 ); 22 } 23 // print left join leftovers 24 let outerquerytablea = new parse query("tablea"); 25 outerquerytablea limit(10); 26 let outerquerytablearesults = await outerquerytablea find(); 27 // get all entries from query that doesn't match the join query results 28 let filteredouterquerytablearesults = outerquerytablearesults filter( 29 (outerquerytablearesult) => 30 joinqueryresults find( 31 (joinqueryresult) => 32 joinqueryresult get("link") !== undefined && 33 joinqueryresult get("link") id === outerquerytablearesult id 34 ) === undefined 35 ); 36 for (let result of filteredouterquerytablearesults) { 37 console log(`${result id} | ${result get("fielda")} | `); 38 } 39 // print right join leftovers 40 let outerquerytableb = new parse query("tableb"); 41 outerquerytableb limit(10); 42 let outerquerytablebresults = await outerquerytableb find(); 43 // get all entries from query that doesn't match the join query results 44 let filteredouterquerytablebresults = outerquerytablebresults filter( 45 (outerquerytablebresult) => 46 joinqueryresults find( 47 (joinqueryresult) => joinqueryresult id === outerquerytablebresult id 48 ) === undefined 49 ); 50 for (let result of filteredouterquerytablebresults) { 51 console log(`${result id} | | ${result get("fieldb")}`); 52 } conclusione alla fine di questa guida, hai imparato come eseguire query relazionali su back4app utilizzando parse ed emulando le query sql più comuni join join in un database nosql