Get started
Esquema Relacional
14 min
esta guía explica cómo trabajar con esquemas relacionales en back4app, incluyendo la creación de clases relacionadas y la realización de consultas eficientes utilizando parse server aprenderás a usar pointers y relations de manera efectiva, junto con ejemplos prácticos ¿qué es un esquema relacional? un esquema relacional organiza los datos en diferentes clases conectadas entre sí en parse server, estas relaciones se gestionan a través de pointers referirse directamente a un solo objeto relations gestionar múltiples conexiones (relaciones de muchos a muchos) estas herramientas te permiten realizar consultas complejas de manera eficiente y consistente objetivos al final de esta guía, podrás crear relaciones entre clases utilizando pointers y relations realizar consultas relacionales para recuperar datos conectados optimizar tu esquema para un mejor rendimiento requisitos previos una aplicación de back4app guía para crear una aplicación https //www back4app com/docs/get started/new parse app sdk de parse instalado guía de instalación https //www back4app com/docs/get started/parse sdk 1 creando clases relacionadas ejemplo práctico estados y ciudades imagina que quieres modelar un sistema donde las ciudades están asociadas con estados clase estado con el campo state name clase ciudad con el campo city name y un pointer a estado creando clases y relaciones 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 consultando datos relacionados ahora que los datos están relacionados, puedes realizar consultas para recuperarlos ejemplo 1 obtener ciudades en un estado específico 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\\"}}" ejemplo 2 consultar estados con ciudades relacionadas crea una consulta que devuelva los estados conectados a cualquier ciudad 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" mejores prácticas para trabajar eficazmente con esquemas relacionales utilizando parse server en back4app, sigue estas mejores prácticas para asegurar rendimiento, mantenibilidad y escalabilidad elige el tipo de relación correcto usa punteros para relaciones uno a uno , como vincular un usuario a su perfil usa relaciones para relaciones muchos a muchos , como vincular un proyecto a múltiples tareas consulta eficiente usa include() para cargar datos relacionados en la misma consulta, reduciendo la necesidad de múltiples solicitudes limita los resultados usando limit() y skip() para evitar obtener grandes conjuntos de datos a la vez indexa campos consultados con frecuencia para acelerar las búsquedas evita la anidación excesiva mantén las consultas planas para reducir la complejidad y mejorar el rendimiento usa consultas anidadas con moderación y solo cuando sea necesario descarga consultas complejas a cloud code para consultas complejas que involucran múltiples relaciones o grandes conjuntos de datos, descarga estas a cloud code para mantener el cliente ligero y receptivo conclusión en esta guía, aprendiste cómo crear relaciones entre clases y consultar objetos relacionados en back4app ¡continúa explorando la documentación específica del sdk https //docs parseplatform org para profundizar aún más!