Manage Resource Attributes

You can pick the resource attributes that your client application requires and change the way  reflects the underlying entity (table or view) in JSON results. For example, the table has 50 columns but your client application requires only seven of them. By default, resources use the entity columns (the Use Table/View Schema checkbox is selected on the Resources page) and return all base entity columns. You shape your resource and override this default by adding the resource attributes that you want the resource to return for the resource.
lac52
You can pick the resource attributes that your client application requires and change the way 
CA Live API Creator
 reflects the underlying entity (table or view) in JSON results. For example, the table has 50 columns but your client application requires only seven of them. By default, resources use the entity columns (the 
Use Table/View Schema
 checkbox is selected on the Resources page) and return all base entity columns. You shape your resource and override this default by adding the resource attributes that you want the resource to return for the resource.
In this article:
 
 
Resource Attribute Types
You can add the following types of attributes to your table-based resource:
Use the following table to guide you on which attribute type to define for your resource:
Task
Entity
Parent
Virtual
Are defined on resource rows
No
No
 
Yes
 
Support GET, PUT, and POST
 
Yes
 
 
Yes
 
No
Reflected in the OpenAPI (Swagger) documentation for your API
 
Yes
 
 
Yes
 
 
Yes
 
Can be aliased
 
Yes
 
 
Yes
 
 
Yes
 
Can be hidden from JSON response
No
No
 
Yes
 
Can reference non-persistent attributes (NPAs)
 
Yes
 
No
No
Define Aliases for Resource Attributes
By default, the names of the resource attributes that you add are the same name as the corresponding entity column. You can override the default names, for example, to make their name less obscure or to hide the column name in your API. You override the name by entering another name, or alias, in the 
Name/Alias
 field for the resource attribute, and then by saving your changes.
The aliased resource attribute names that you define are in the context of the resource that you have selected.
The 
B2B Northwind
 API sample includes the 
PartnerOrder
 resource with the 
CustomerID
 and the 
OrderID
 columns added as resource attributes. The names of the attributes are aliased.
For more information about the Business to Business (B2B) API sample, see B2B API Sample.
Define Resource Attributes as Primary Keys for the Resource
(Advanced) 
CA Live API Creator
 always returns the primary key column. You can define a resource attribute as an effective primary key for the resource by selecting the 
Key
 checkbox for the resource attribute. You can select one or more resource attributes as primary keys for the resource.
If your JSON includes the 
MERGE_INSERT
 and 
LOOKUP
 metadata action tags, defining a resource attribute as a primary key defines it as the primary key only for the resource and not for the underlying entity.
For more information about metadata action tags, see Complex Transaction Processing.
Entity Resource Attributes
Entity resource attributes are the attributes from the underlying entity that you want the resource to return in JSON results. You can reference NPAs in entity resource attributes and then inject the computed values by using the attributes in rules.
The 
B2B Northwind
 API sample includes the 
EmployeeWithRaises
 resource with the 
FullNameNPA
 NPA added as an entity resource attribute.
Add an Entity Resource Attribute to a Table-Based Resource
  1. With your API open, and the table-based resource to which you want to add an entity resource attribute is selected in the list, clear the 
    Use Table/View Schema
     checkbox, and click the 
    Add an attribute (+)
     icon.
    The 
    Add Resource Attributes
     window opens. By default, the 
    Entity
     resource attribute type is selected. The base entity columns display.
  2. Select the checkbox for the column or NPA that you want to use for this entity resource attribute and then click 
    Add
    .
    Only columns that are not already associated to another entity resource attribute that has been added to this resource display in the list.
     You can add all resource attributes to the resource by clicking the topmost checkbox, next to 
    Attribute
    , and then clicking 
    Add
    .
  3. Click 
    Save
    .
The entity resource attribute is added to the resource.
Parent Resource Attributes
Parent resource attributes are attributes from the resource's parent that 
CA Live API Creator
 combines and includes in the containing root resource. 
CA Live API Creator
 merges the attributes into the JSON of the containing resource. Parent resource attributes look up to the parent resource using the to-parent relationships that are defined for the resource.
 
CA Live API Creator
 can GET, PUT, or POST data to populate foreign keys or virtual relationships by way of a GET or PUT/POST event that looks up to a parent resource.
For more information about how to create GET and PUT/POST events, see Manage Resource Events.
The 
PartnerOrderUsingLookups
 resource in the 
B2B Northwind
 API sample includes the 
Shipper
 parent resource attribute.
Add a Parent Resource Attribute to a Table-Based Resource
Prerequisite:
 The entity for the table-based resource includes a virtual or physical to-parent relationship to the entity that is used for the parent resource. For more information about database relationships, see Database Relationships.
Follow these steps:
 
  1. Add the parent resource attribute:
    1. With your API open, and the table-based resource to which you want to add a parent resource attribute is selected in the list, click the 
      Add an attribute (+)
       icon.
      The 
      Add Resource Attributes
       window opens. By default, the 
      Entity
       resource attribute type is selected.
    2. Click 
      Parent
      .
      The to-parent relationships display in the 
      Available Parents
       list.
    3. Select the relationship (the to-parent relationship name) of the parent resource that you want to look up, select the parent entity column (the column that holds the foreign key), and then click 
      Add
      .
      Only columns that are not already associated to another parent resource attribute that has been added to this resource display in the list. The relationship can be to other parent resources.
    4. Click 
      Save
      .
  2. (Optional) Create the GET event:
    1. Click the 
      Events
       tab.
      The 
      GET Event
       tab displays by default.
    2. Define the code for the event.
      Within the code, you can use the 
      logicRow
       variable to perform the lookup. This variable has full access to resource attributes, parents, and children of the resource row.
The parent resource attribute is added to the table-based resource.
Mark a Parent Resource Attribute to Look Up the Foreign Key
Prerequisite:
 You have added the parent resource attribute.
You can mark parent resource attributes as an attribute that looks up the foreign key by selecting the 
Lookup
 checkbox. PUT/POST calls use the value of the resource attribute to look up and set the foreign key of the parent resource attribute within the current row.
You can define only parent resource attributes as lookups. You can define one or more parent resource attributes as lookups for the resource.
Virtual Resource Attributes
Virtual resource attributes are attributes that you define on table-based resource and that 
CA Live API Creator
 calculates or derives by way of a GET event. For example, a resource attribute that is the value of the full name, computed from the first name (string) and the last name (string). Add virtual resource attributes to table-based resources when you want the response to include a JSON attribute.
Virtual attributes can be of the following JSON types:
  • any
  • string
  • number
  • boolean
  • object, such as a JSON object
    For example, you can wrap a container of data as a virtual resource attribute of type 
    object
    .
  • array
Add a Virtual Resource Attribute to a Table-Based Resource
The GET event computes the value for the virtual resource attribute. 
CA Live API Creator
 can read resource data by way of a GET event, and then compute and populate the value of a virtual attribute. 
Follow these steps:
 
  1. Add the virtual resource attribute of type 
    string
    :
    1. With your API open, and the table-based resource to which you want to add a virtual resource attribute is selected in the list, click the 
      Add an attribute (+)
       icon.
      The 
      Add Resource Attributes
       window opens. By default, the 
      Entity
       resource attribute type is selected.
    2. Click 
      Virtual
      .
      The JSON types display in the 
      Virtual Attribute JSON Type
       list. By default, the 
      any
       JSON type is selected.
    3. Select 
      string
       as the type, and then click 
      Add
      .
    4. Click 
      Save
      .
  2. Create a GET event:
    1. Click the 
      Events
       tab.
      The 
      GET Event
       tab appears by default.
    2. Define the code that sets the value of the virtual resource attribute.
      Within the code, you can add the 
      virtuals
       variable to set the value of virtual resource attributes. In the following example, the GET event computes the value for the 
      FullName
       virtual resource attribute using the values from the 
      FirstName
       and 
      LastName
       columns (the object names):
      virtuals.FullName = row.FirstName + " " + row.LastName;
The virtual resource attribute is added to the table-based resource.
Set a Virtual Resource Attribute with a Lookup Value using "Fuzzy" Foreign Key in a GET Event 
You can handle complex parent lookups that involve a "fuzzy" foreign key and can add a GET event that sets the value for the virtual resource attribute using a parent attribute. 
The 
B2B Northwind
 API sample includes the 
ShipperAPIDefHiddenVirtuals
 resource that illustrates the "fuzzy" foreign key lookup scenario. The GET event for this resource includes the 
SysUtility.findEntities()
 method to perform the lookup and then uses the 
virtuals
 variable in the GET event to set the value of the 
ShipperMemo
 virtual resource attribute.
Follow these steps:
 
  1. Add the virtual resource attribute:
    1. With your API open, and the table-based resource to which you want to add a virtual resource attribute is selected in the list, click the 
      Add an attribute (+)
       icon.
      The 
      Add Resource Attributes
       window opens. By default, the 
      Entity
       resource attribute type is selected.
    2. Click 
      Virtual
      .
      The JSON types display in the 
      Virtual Attribute JSON Type
       list. By default, the 
      any
       JSON type is selected.
    3. Select the JSON type that you want to use for this virtual resource attribute.
    4. Click 
      Add
      .
    5. Click 
      Save
      .
    The virtual resource attribute is added to the table-based resource.
  2. Create a GET event:
    1. Click the 
      Events
       tab.
      The 
      GET Events
       tab appears by default.
    2. Define the code that sets the value for the virtual resource attribute.
      Within the code, you can add the 
      SysUtility.findEntities()
       method to perform the lookup.
      For more information about how to use this method, see The SysUtility Object.
The virtual resource attribute is added to the table-based resource.
Add a Virtual Resource Attribute that Performs a "Fuzzy" Lookup during PUT/POST Event
You can perform "fuzzy" lookups to look up mismatched values, for example, where the value for the 
phone_number
 does not match the 
phone
 virtual resource attribute in the containing resource. The PUT/POST event can contain code that performs the "fuzzy" lookup using the virtual resource attribute.
The 
B2B Northwind
 API sample includes the 
PartnerOrderWithEvents
 resource that illustrates a "fuzzy" lookup. The PUT/POST event for this resource includes the 
SysUtility.findEntities()
 method to perform a "fuzzy" lookup and then uses the 
tableRow
 variable to set the value of the 
Shipper
 virtual resource attribute.
Follow these steps:
 
  1. Add the virtual resource attribute:
    1. With your API open, and the table-based resource to which you want to add a virtual resource attribute is selected in the list, click the 
      Add an attribute (+)
       icon.
      The 
      Add Resource Attributes
       window opens. By default, the 
      Entity
       resource attribute type is selected.
    2. Click 
      Virtual
      .
      The JSON types display in the 
      Virtual Attribute JSON Type
       list. By default, the 
      any
       JSON type is selected.
    3. Select the JSON type that you want to use for this virtual resource attribute.
    4. (Optional) Hide this virtual resource attribute from JSON responses by selecting the 
      Hide from JSON response
       checkbox. By default, virtual resource attributes are shown in JSON responses (the 
      Hide from JSON response
       checkbox is cleared).
    5. Click 
      Add
      .
    6. Click 
      Save
      .
    The virtual resource attribute is added to the table-based resource.
  2. Create a PUT/POST event:
    1. Click the 
      Events
       tab.
      The 
      GET Events
       tab appears by default.
    2. Click the 
      PUT/POST Event
       tab.
    3. Define the code for the event.
       Within the code, you can add the the 
      SysUtility.findEntities()
       method to perform the lookup.
The virtual resource attribute is added to the table-based resource.
Add a Virtual Resource Attribute that is Used in a Manual Entity Join of "Fuzzy" Children
You can join resources across multiple databases, including doing a "fuzzy" join of child records that do not include an exact match for the join by way of a manual join, using a virtual resource attribute.
The 
B2B Northwind
 API sample includes the 
ShipperAPIDef
 resource that illustrates a manual join across two databases. The 
ShipperMemos
 subresource uses the following join syntax to do a "fuzzy" join of child records:
"shipper_phone" = [[PromoPhone]]
The GET event sets the value for the 
PromoPhone
 
hidden
 virtual resource attribute in the 
ShipperAPIDef
 resource and then uses the virtual resource attribute to perform the manual join.
Follow these steps:
 
  1. Add the virtual resource attribute:
    1. With your API open, and the table-based resource to which you want to add a virtual resource attribute is selected in the list, click the 
      Add an attribute (+)
       icon.
      The 
      Add Resource Attributes
       window opens. By default, the 
      Entity
       resource attribute type is selected.
    2. Click 
      Virtual
      .
      The JSON types display in the 
      Virtual Attribute JSON Type
       list. By default, the 
      any
       JSON type is selected.
    3. Select the JSON type that you want to use for this virtual resource attribute.
    4. (Optional) Hide this virtual resource attribute from JSON responses by selecting the 
      Hide from JSON response
       checkbox. By default, virtual resource attributes are shown in JSON responses (the 
      Hide from JSON response
       checkbox is cleared).
    5. Click 
      Add
      .
    6. Click 
      Save
      .
    The virtual resource attribute is added to the table-based resource.
  2. Create a PUT/POST event:
    1. Click the 
      Events
       tab.
      The 
      GET Events
       tab appears by default.
    2. Define the code for the event.
      Within the code, you can add the the 
      SysUtility.findEntities()
       method to perform the lookup.
The virtual resource attribute is added to the table-based resource.
Resource Attribute Limitation
You can add virtual resource attributes and parent resource attributes to resources that are defined in APIs that are connected to data sources that do 
not
 use data source providers. Data source providers are sets of JavaScript code that implement the extensible data source framework.
For more information about data source providers, see Manage Data Source Providers.