简体   繁体   中英

creating a breeze metadata for multi-level JSON

I am new to breeze and am not quite sure how to create javascript metadata for this JSON data returned by server. I have to create create a custom metadata since the server does not provide the metadata I need. We use oData. Could someone please help me with creating a JavaScript metadata based on this JSON data?

 { id: "5091bed7-e035-45c6-b2c5-d1301e4170e6", name: "Mike", address: { city: "San Jose", state: "CA" }, orders: [ { id: "1889e425-e8ad-4b79-b797-fe9aa6b0e433", date: "2015/04/12 00:00:00", items: [ { id: "a71aa2eb-9f8d-4234-aa83-6dfc5cd8abcc", description: "chocolate", price: "5.00" }, { id: "5091bed7-e035-45c6-b2c5-d1301e414444", description: "cookie", price: "1.00" } ] }, { id: "5ed077b0-72c4-43d0-fe96-6bebce6e3c86", date: "2015/05/22 00:00:00", items: [ { id: "bc5ed003-1302-4686-8294-4a0186e550a2", description: "pencil", price: "0.50" } ] } ] } 

In Breeze, each of the objects would be handled as either an Entity (if it has an identifier) or a Complex Type (if it does not). In your case, I've made Entites for "Customer" (the top level object), "Order", and "OrderItem". I've made a ComplexType for "Address".

Note that each incoming object should have a "$type" property to tell Breeze what Entity Type or Complex Type it is. Without that, you'll need a custom [JsonResultsAdapter] to tell Breeze the type.

My metadata differs from your objects in that I've assumed that foreign keys are available to connect Customer to Order to OrderItem and vice-versa. That makes the entities easier to work with, but you should omit them from the metadata if your server cannot provide them.

{
  "structuralTypes": [
    {
      "shortName": "Address",
      "namespace": "MyApp",
      "isComplexType": true,
      "dataProperties": [
        {
          "name": "city",
          "dataType": "String",
          "maxLength": 15
        },
        {
          "name": "state",
          "dataType": "String",
          "maxLength": 15
        }
      ]
    },
    {
      "shortName": "Customer",
      "namespace": "MyApp",
      "autoGeneratedKeyType": "KeyGenerator",
      "defaultResourceName": "Customers",
      "dataProperties": [
        {
          "name": "id",
          "dataType": "Guid",
          "isNullable": false,
          "isPartOfKey": true
        },
        {
          "name": "name",
          "dataType": "String",
          "maxLength": 30
        },
        {
          "name": "location",
          "complexTypeName": "Address:#MyApp"
        }
      ],
      "navigationProperties": [
        {
          "name": "orders",
          "entityTypeName": "Order:#MyApp",
          "isScalar": false,
          "associationName": "AN_Customer_Order"
        }
      ]
    },
    {
      "shortName": "Order",
      "namespace": "MyApp",
      "autoGeneratedKeyType": "Identity",
      "defaultResourceName": "Orders",
      "dataProperties": [
        {
          "name": "id",
          "dataType": "Guid",
          "isNullable": false,
          "isPartOfKey": true
        },
        {
          "name": "customerID",
          "dataType": "Guid"
        },
        {
          "name": "date",
          "dataType": "DateTime"
        }
      ],
      "navigationProperties": [
        {
          "name": "customer",
          "entityTypeName": "Customer:#MyApp",
          "isScalar": true,
          "associationName": "AN_Customer_Order",
          "foreignKeyNames": [
            "customerID"
          ]
        },
        {
          "name": "items",
          "entityTypeName": "OrderItem:#MyApp",
          "isScalar": false,
          "associationName": "AN_Order_OrderItem"
        }
      ]
    },
    {
      "shortName": "OrderItem",
      "namespace": "MyApp",
      "autoGeneratedKeyType": "None",
      "defaultResourceName": "OrderItems",
      "dataProperties": [
        {
          "name": "id",
          "dataType": "Guid",
          "isNullable": false,
          "isPartOfKey": true
        },
        {
          "name": "orderID",
          "dataType": "Guid",
          "isNullable": false
        },
        {
          "name": "description",
          "dataType": "String",
          "isNullable": false
        },
        {
          "name": "price",
          "dataType": "Decimal",
          "isNullable": false
        }
      ],
      "navigationProperties": [
        {
          "name": "order",
          "entityTypeName": "Order:#MyApp",
          "isScalar": true,
          "associationName": "AN_Order_OrderItem",
          "foreignKeyNames": [
            "orderID"
          ]
        }
      ]
    }
  ],
  "resourceEntityTypeMap": {
    "Customers": "Customer:#MyApp",
    "Orders": "Order:#MyApp",
    "OrderItems": "OrderItem:#MyApp"
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM