展開
展開
API Server はOData $expand 機能をサポートしています。これにより、単一リクエストでレコードおよび関連エンティティからのデータを提供できます。この機能を有効にするには、エンティティ間のリレーションシップをAPI Server リソースで定義する必要があります。
リレーションシップ
OData $expand 機能を使用する前に、API Server でリソース間のリレーションシップを定義する必要があります。エンティティ間のリレーションシップを定義する方法はいくつかあります。以下の例では、Product/Category リレーションシップを使用します。
ユーザーインターフェースの使用
[Edit Resource]ウィザードを使用して、API Server にインポートしたリソース間のリレーションシップを設定できます。有効なリレーションシップを指定するには、関連テーブルに対応するリソースがAPI Server にインポートされている必要があります。
スキーマエディタの使用
リレーションシップを定義するカラムに’relationships’ XML 属性を追加することによって、スキーマエディタでリレーションイップを手動で定義できます。属性は次の形式にする必要があります: relationshipName(ResourceName.ColumnName)。複数のリレーションシップは、| 記号で区切ることができます。リレーションシップ定義の書式設定は、カーディナリティによって異なります。
一対多リレーションシップ
親オブジェクトのスキーマ内の主キーに対応するスキーマエントリに一対多のリレーションシップを定義できます。リレーションシップの名前は、カーディナリティを示す* で始まることに注意してください。
<attr name="CategoryID" key="true" type="int" columnsize="11"
isNullable="false" readonly="false" relationships="*Products(Product.CategoryID)"
desc="" />
多対一リレーションシップ
子オブジェクトのスキーマ内の外部キーに対応する行に多対一のリレーションシップを定義できます。
<attr name="CategoryID" key="false" type="int" columnsize="11"
isNullable="true" readonly="false" relationships="ProductsCategory(Category.CategoryID)"
desc="" />
OData 展開構文
関連エンティティ間のリレーションシップが定義されると、$expand キーワードを使用して、単一の要求で関連オブジェクトのセットだけでなく、レコードプロパティも選択できます。$expand クエリ構文の詳細については、OData ドキュメントを参照してください。
一対多リレーションシップ
各基底レコードに対して、すべての関連オブジェクトを取得します。
http://MyServer:MyPort/api.rsc/Categories(1)?$expand=Products
Response:
{
"@odata.context": "http://localhost:8153/api.rsc/$metadata#Categories",
"value": [
{
"CategoryID": "1",
"CategoryName": "Books",
"Products": [
{
"ProductID": "101",
"Price": "45",
"Name": "Astronomy 101",
"Supplierid": "1",
"CompanyId": "1",
"CategoryId": "1"
},
{
"ProductID": "102",
"Price": "50",
"Name": "Physics 101",
"Supplierid": "2",
"CompanyId": "1",
"CategoryId": "1"
},
{
"ProductID": "103",
"Price": "65",
"Name": "Chemistry 101",
"Supplierid": "3",
"CompanyId": "1",
"CategoryId": "1"
}
]
}
]
}
多対一リレーションシップ
各レコードに対して、関連する親レコードを取得します。
http://MyServer:MyPort/api.rsc/Products?$expand=ProductsCategory&$filter=price lt 60&$orderby=price desc
Response:
{
"@odata.context": "http://localhost:8153/api.rsc/$metadata#Products",
"value": [
{
"ProductId": 102,
"Name": "Physics 101",
"Price": "50",
"CategoryId": "1",
"ProductsCategory": [
{
"CategoryName": "Books",
"CategoryId": "1"
}
]
},
{
"ProductId": 101,
"Name": "Astronomy 101",
"Price": "45",
"CategoryId": "1",
"ProductsCategory": [
{
"CategoryName": "Books",
"CategoryId": "1"
}
]
}
]
}
複数の展開
$expand パラメータは、多対一と一対多リレーションシップを組み合わせたカンマ区切りリストの値をとることができます。
http://MyServer:MyPort/api.rsc/Products(102)?$expand=Supplier,ProductReviews
Response:
{
"@odata.context": "http://localhost:8153/api.rsc/$metadata#Products",
"value": [
{
"ProductId": 102,
"Name": "Physics 101",
"Price": "50",
"ProductReviews": [
{
"ProductReviewID": "1001",
"ProductId": "102",
"Rating": "4.5",
"Description": "Easy to read, good illustrations."
},
{
"ProductReviewID": "1002",
"ProductId": "102",
"Rating": "1.5",
"Description": "Boring!"
}
],
"Supplier": [
{
"SupplierId": "1",
"SupplierName": "New Science Publishing"
}
]
}
]
}
展開オプション
OData API calls can use expand options for a more granular definitions of the related entities to return. Expand options are specified as a semicolon-separated list, in parenthesis appended to the related entity.
Select
The $select option defines which columns of the related entity will be returned. If not specified, all available values will be returned. e.g.:
http://MyServer:MyPort/api.rsc/Products(102)?$expand=ProductReviews($select=Description),Supplier($select=SupplierName)
Filter
The $filter option can be used to define a criteria to apply on related results. Related entities which do not conform by this criteria will be ommitted from the response. e.g.:
http://MyServer:MyPort/api.rsc/Products(102)?$expand=ProductReviews($select=Rating;$filter=Rating ge 4)