Get started
Schema Relazionale
14 min
questa guida spiega come lavorare con schemi relazionali su back4app, inclusa la creazione di classi correlate e l'esecuzione di query efficienti utilizzando parse server imparerai a utilizzare pointers e relations in modo efficace, insieme a esempi pratici che cos'è uno schema relazionale? uno schema relazionale organizza i dati in diverse classi collegate tra loro in parse server, queste relazioni sono gestite tramite pointers riferisci direttamente a un singolo oggetto relations gestisci più connessioni (relazioni molti a molti) questi strumenti ti consentono di eseguire query complesse in modo efficiente e coerente obiettivi entro la fine di questa guida, sarai in grado di creare relazioni tra classi utilizzando pointers e relations eseguire query relazionali per recuperare dati connessi ottimizzare il tuo schema per migliori prestazioni prerequisiti un'app back4app guida per creare un'app https //www back4app com/docs/get started/new parse app parse sdk installato guida all'installazione https //www back4app com/docs/get started/parse sdk 1 creazione di classi correlate esempio pratico stati e città immagina di voler modellare un sistema in cui le città sono associate agli stati classe stato con il campo state name classe città con il campo city name e un puntatore a stato creazione di classi e relazioni javascript async function createstateandcity() { try { // create a state const state = parse object extend('state'); const california = new state(); california set('state name', 'california'); const savedstate = await california save(); console log(`state created with objectid ${savedstate id}`); // create a city with a pointer to state const city = parse object extend('city'); const losangeles = new city(); losangeles set('city name', 'los angeles'); losangeles set('state', savedstate); const savedcity = await losangeles save(); console log(`city created with objectid ${savedcity id}`); } catch (error) { console error('error creating state and city ', error message); } } createstateandcity(); flutter future\<void> createclasses() async { await parse() initialize('your app id', 'https //parseapi back4app com/', clientkey 'your client key', autosendsessionid true); // create a state final state = parseobject('state') set('state name', 'california'); final stateresult = await state save(); if (stateresult success) { // create a city with a pointer to state final city = parseobject('city') set('city name', 'los angeles') set('state', state); final cityresult = await city save(); if (cityresult success) { print('state and city created successfully '); } } } android public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super oncreate(savedinstancestate); setcontentview(r layout activity main); // initialize parse parse initialize(new parse configuration builder(this) applicationid("your app id") clientkey("your client key") server("https //parseapi back4app com/") build() ); // create state and city createclasses(); } private void createclasses() { // create a state parseobject state = new parseobject("state"); state put("state name", "california"); state saveinbackground(e > { if (e == null) { log d("parse", "state created successfully with objectid " + state getobjectid()); // save city with a pointer to state parseobject city = new parseobject("city"); city put("city name", "los angeles"); city put("state", state); city saveinbackground(ex > { if (ex == null) { log d("parse", "city created successfully with objectid " + city getobjectid()); } else { log e("parse", "failed to create city " + ex getmessage()); } }); } else { log e("parse", "failed to create state " + e getmessage()); } }); } } ios struct state parseobject { var objectid string? var state name string? var createdat date? var updatedat date? } struct city parseobject { var objectid string? var city name string? var state state? var createdat date? var updatedat date? } func createclasses() async throws { try await parseswift initialize(applicationid "your app id", clientkey "your client key", serverurl url(string "https //parseapi back4app com")!) let state = state(state name "california") let savedstate = try await state save() var city = city(city name "los angeles", state savedstate) city = try await city save() print("state and city created successfully ") } php require 'vendor/autoload php'; use parse\parseclient; use parse\parseobject; parseclient initialize('your app id', 'your client key', 'https //parseapi back4app com/'); try { // create a state $state = new parseobject("state"); $state >set("state name", "california"); $state >save(); // create a city with a pointer to state $city = new parseobject("city"); $city >set("city name", "los angeles"); $city >set("state", $state); $city >save(); echo "state and city created successfully "; } catch (exception $ex) { echo "error " $ex >getmessage(); } net namespace parseexampleapp { class program { static async task main(string\[] args) { // initialize parse parseclient initialize(new parseclient configuration { applicationid = "your app id", server = "https //parseapi back4app com/", key = "your client key" }); // call the method to create state and city await createclassesasync(); } static async task createclassesasync() { try { // create a state var state = new parseobject("state"); state\["state name"] = "california"; await state saveasync(); console writeline($"state created with objectid {state objectid}"); // create a city with a pointer to the state var city = new parseobject("city"); city\["city name"] = "los angeles"; city\["state"] = state; // set the pointer to the state await city saveasync(); console writeline($"city created with objectid {city objectid}"); } catch (exception ex) { console writeline($"error {ex message}"); } } } } rest api # create state curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"state name" "california"}' \\ https //parseapi back4app com/classes/state \# use the objectid of the created state to create a city curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"city name" "los angeles", "state" {" type" "pointer", "classname" "state", "objectid" "state object id"}}' \\ https //parseapi back4app com/classes/city 2 interrogazione dei dati correlati ora che i dati sono correlati, puoi eseguire query per recuperarli esempio 1 recupera città in uno stato specifico javascript const statequery = new parse query("state"); statequery equalto("state name", "california"); statequery first() then(state => { const cityquery = new parse query("city"); cityquery equalto("state", state); return cityquery find(); }) then(cities => { cities foreach(city => { console log(`city ${city get("city name")}`); }); }) catch(error => { console error("error fetching cities ", error message); }); flutter final statequery = querybuilder(parseobject('state')) whereequalto('state name', 'california'); final stateresult = await statequery query(); if (stateresult success && stateresult results != null) { final state = stateresult results! first; final cityquery = querybuilder(parseobject('city')) whereequalto('state', state); final cityresult = await cityquery query(); if (cityresult success && cityresult results != null) { for (var city in cityresult results!) { print('city ${city get\<string>('city name')}'); } } } android parsequery\<parseobject> statequery = parsequery getquery("state"); statequery whereequalto("state name", "california"); statequery getfirstinbackground((state, e) > { if (e == null) { parsequery\<parseobject> cityquery = parsequery getquery("city"); cityquery whereequalto("state", state); cityquery findinbackground((cities, ex) > { if (ex == null) { for (parseobject city cities) { log d("parse", "city " + city getstring("city name")); } } else { log e("parse", "error fetching cities " + ex getmessage()); } }); } else { log e("parse", "error fetching state " + e getmessage()); } }); ios let statequery = state query("state name" == "california") statequery first { result in switch result { case success(let state) let cityquery = city query("state" == state) cityquery find { cityresult in switch cityresult { case success(let cities) cities foreach { city in print("city \\(city city name ?? "unknown")") } case failure(let error) print("error fetching cities \\(error localizeddescription)") } } case failure(let error) print("error fetching state \\(error localizeddescription)") } } php use parse\parsequery; // query state $statequery = new parsequery("state"); $statequery >equalto("state name", "california"); $state = $statequery >first(); if ($state) { // query cities $cityquery = new parsequery("city"); $cityquery >equalto("state", $state); $cities = $cityquery >find(); foreach ($cities as $city) { echo "city " $city >get("city name") "\n"; } } net var statequery = new parsequery\<parseobject>("state") whereequalto("state name", "california"); var state = await statequery firstasync(); if (state != null) { var cityquery = new parsequery\<parseobject>("city") whereequalto("state", state); var cities = await cityquery findasync(); foreach (var city in cities) { console writeline($"city {city\["city name"]}"); } } rest api # query state curl x get \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ "https //parseapi back4app com/classes/state?where={\\"state name\\" \\"california\\"}" \# query cities \# replace state object id with the objectid from the state query curl x get \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ "https //parseapi back4app com/classes/city?where={\\"state\\" {\\" type\\" \\"pointer\\",\\"classname\\" \\"state\\",\\"objectid\\" \\"state object id\\"}}" esempio 2 interrogare gli stati con città correlate crea una query che restituisca gli stati collegati a qualsiasi città javascript const statequery = new parse query("state"); const cityquery = new parse query("city"); cityquery matchesquery("state", statequery); cityquery include("state"); cityquery find() then(cities => { cities foreach(city => { const state = city get("state"); console log(`city ${city get("city name")} belongs to state ${state get("state name")}`); }); }) catch(error => { console error("error fetching data ", error message); }); flutter final statequery = querybuilder(parseobject('state')); final cityquery = querybuilder(parseobject('city')) wherematchesquery('state', statequery) includeobject(\['state']); final cityresult = await cityquery query(); if (cityresult success && cityresult results != null) { for (var city in cityresult results!) { final state = city get\<parseobject>('state'); print('city ${city get\<string>('city name')} belongs to state ${state? get\<string>('state name')}'); } } android parsequery\<parseobject> statequery = parsequery getquery("state"); parsequery\<parseobject> cityquery = parsequery getquery("city"); cityquery wherematchesquery("state", statequery); cityquery include("state"); cityquery findinbackground((cities, e) > { if (e == null) { for (parseobject city cities) { parseobject state = city getparseobject("state"); log d("parse", "city " + city getstring("city name") + " belongs to state " + state getstring("state name")); } } else { log e("parse", "error " + e getmessage()); } }); ios let statequery = state query() let cityquery = city query(matchesquery(key "state", query statequery)) cityquery include("state") cityquery find { result in switch result { case success(let cities) cities foreach { city in if let state = city state { print("city \\(city city name ?? "unknown") belongs to state \\(state state name ?? "unknown")") } } case failure(let error) print("error \\(error localizeddescription)") } } php use parse\parsequery; $statequery = new parsequery("state"); $cityquery = new parsequery("city"); $cityquery >matchesquery("state", $statequery); $cityquery >includekey("state"); $cities = $cityquery >find(); foreach ($cities as $city) { $state = $city >get("state"); echo "city " $city >get("city name") " belongs to state " $state >get("state name") "\n"; } net var statequery = new parsequery\<parseobject>("state"); var cityquery = new parsequery\<parseobject>("city") wherematchesquery("state", statequery) include("state"); var cities = await cityquery findasync(); foreach (var city in cities) { var state = city get\<parseobject>("state"); console writeline($"city {city\["city name"]} belongs to state {state\["state name"]}"); } rest api curl x get \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ "https //parseapi back4app com/classes/city?where={\\"state\\" {\\"$inquery\\" {\\"classname\\" \\"state\\"}}}\&include=state" migliori pratiche per lavorare efficacemente con schemi relazionali utilizzando parse server in back4app, segui queste migliori pratiche per garantire prestazioni, manutenibilità e scalabilità scegli il tipo di relazione giusto usa punteri per relazioni uno a uno , come collegare un utente al proprio profilo usa relazioni per relazioni molti a molti , come collegare un progetto a più attività query efficiente usa include() per caricare dati correlati nella stessa query, riducendo la necessità di più richieste limita i risultati usando limit() e skip() per evitare di recuperare grandi set di dati in una sola volta indicizza i campi frequentemente interrogati per velocizzare le ricerche evita annidamenti eccessivi mantieni le query piatte per ridurre la complessità e migliorare le prestazioni usa le query annidate con parsimonia e solo quando necessario scarica query complesse su cloud code per query complesse che coinvolgono più relazioni o grandi set di dati, scarica queste su cloud code per mantenere il client leggero e reattivo conclusione in questa guida, hai imparato come creare relazioni tra classi e interrogare oggetti correlati su back4app continua a esplorare la documentazione specifica dell'sdk https //docs parseplatform org per approfondire ulteriormente!