Get started
関係スキーマ
13 分
このガイドでは、back4appでリレーショナルスキーマを扱う方法、関連クラスの作成やparse serverを使用した効率的なクエリの実行について説明します。 pointers と relations を効果的に使用する方法と、実用的な例を学びます。 リレーショナルスキーマとは何ですか? リレーショナルスキーマは、データを互いに接続された異なるクラスに整理します。parse serverでは、これらの関係は次のように管理されます pointers 単一のオブジェクトを直接参照します。 relations 複数の接続(多対多の関係)を管理します。 これらのツールを使用すると、複雑なクエリを効率的かつ一貫して実行できます。 目標 このガイドの終わりまでに、あなたは次のことができるようになります pointers と relations を使用してクラス間の関係を作成します。 リレーショナルクエリを実行して接続されたデータを取得します。 パフォーマンスを向上させるためにスキーマを最適化します。 前提条件 back4appアプリケーション。 アプリ作成ガイド https //www back4app com/docs/get started/new parse app parse sdkがインストールされている。 インストールガイド https //www back4app com/docs/get started/parse sdk 1 関連クラスの作成 実用例 州と都市 都市が州に関連付けられたシステムをモデル化したいと想像してみてください state nameフィールドを持つstateクラス。 city nameフィールドを持つcityクラスと、 state へのポインタ。 クラスと関係の作成 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 関連データのクエリ データが関連付けられたので、クエリを実行してそれを取得できます。 例 1 特定の州の都市を取得する 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\\"}}" 例 2 関連する都市を持つクエリ状態 任意の都市に接続されている州を返すクエリを作成します 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" ベストプラクティス back4appのparse serverを使用してリレーショナルスキーマで効果的に作業するために、パフォーマンス、保守性、スケーラビリティを確保するためのベストプラクティスに従ってください。 適切なリレーションシップタイプを選択する ポインタを使用する 1対1のリレーションシップ に、ユーザーをそのプロフィールにリンクするなど。 リレーションを使用する 多対多のリレーションシップ に、プロジェクトを複数のタスクにリンクするなど。 効率的なクエリ 使用する include() を使って、関連データを同じクエリで読み込み、複数のリクエストの必要性を減らします。 結果を制限する には、 limit() と skip() を使用して、一度に大きなデータセットを取得しないようにします。 頻繁にクエリされるフィールドをインデックス化する ことで、検索を高速化します。 過度のネストを避ける クエリをフラットに保つ ことで、複雑さを減らし、パフォーマンスを向上させます。ネストされたクエリは必要な場合にのみ、控えめに使用してください。 複雑なクエリをクラウドコードにオフロードする 複数のリレーションを含む 複雑なクエリ や大規模なデータセットについては、これらを クラウドコード にオフロードして、クライアントを軽量で応答性の高いものに保ちます。 結論 このガイドでは、クラス間の関係を作成し、back4appで関連オブジェクトをクエリする方法を学びました。さらに深く掘り下げるために、 特定のsdkドキュメント https //docs parseplatform org を引き続き探索してください!