Manage Non-Persistent Attributes

Manage Non-Persistent Attributes
lac42
You can extend your data model to include attributes that are not in the schema. Non-persistent attributes (NPAs) are pseudo-columns (attributes) that you can add and use in your API without affecting the database schema. They are useful when you cannot alter the database schemas. For example, another application that does not change the value is using the NPA. Use NPAs when you want to minimize data-maintenance tasks and speed development.
Use NPAs for retrieval and update logic. For example, you store a first and last name but create an API that returns the full name. For update logic, you can compute values and roll-up sum/count aggregates using derivation rules. This example is shown in the Business to Business (B2B) sample.
You can use NPAs in resources and rules, such as derivation and validation rules, and you can view them in Data Explorer. The NPAs that you use in resources can return values. You can use the NPAs that you use in derivation rules in other rules.
  • NPAs can incur substantial performance overhead as compared to columns that are stored in your database.
    For more information about performance, see the "NPA Best Practices" section.
  • You cannot add NPAs to APIs that are connected to data sources that have been created using a data source provider. For example, you cannot create NPAs if your API is connected to the MongoDB data source.
You can tune API Server using NPAs, analogous to SQL optimizer hints. 
CA Live API Creator
 can compute rules as SQL expressions if the rules follow certain restrictions. It computes rules that compute the value of NPAs faster than other rules.
 In this article:
NPA Best Practices
As with conventional systems, you must make design decisions about whether to persist derived data. Historically, the decision results in a trade-off:
  • Performance.
     Non-persisted data can perform orders-of-magnitude slower. This most often occurs with aggregates, when there are many children per parent, or when aggregations chain. For example, it can be expensive to compute a balance by adding up all the items for all the orders of a customer, or to compute a department budget by recursively summing all the sub departments.
  • Data consistency.
     All applications must update the derivations properly to avoid consistency errors.
It is not always clear how data distributions will turn out. Recoding all the accessing logic to switch strategies can be expensive. You often discover these challenges near the end of development when you load full test data volumes. These challenge can result in rework and delays.
While the dilemma remains, 
CA Live API Creator
 can eliminate the cost of rework. As you add or remove NPAs, 
CA Live API Creator
 adjusts your logic accordingly. It maintains aggregates using adjustment logic.
Consider the following when establishing NPA best practices for your API:
  • If you are using an existing system and you cannot change the schema, use NPAs.
  • If you cannot change the schema by defining persistent attributes, define the attributes as NPAs. This eliminates the SQL required to synchronize your data with your rules. If you have tested your logic and then you discover performance issues, delete the NPAs and alter the schema to define the persisted columns. 
    CA Live API Creator
     re-optimizes your logic.
    Logic processing interacts with the values that are stored in the database. If your schema persists attributes, you must synchronize your data with logic.
For more information about how 
CA Live API Creator
 maintains aggregates using adjustment logic, see Performance.
For a list of all best practices, see Best Practices.
Add and Use Non-Persistent Attributes in your API
Complete the following process to add and use an NPA in your API:
The B2B sample uses NPAs to provide a discount to customers who eat healthy.
Verify the Prerequisites
Before you can add an NPA, ensure that you have:
  • Defined tables or views.
  • Verified your data source.
Add the Non-Persistent Attribute to your Data Source
  1. With your API open, in the Create section, click
    Data Sources
    .
    A list of the data sources for your API display.
  2. From the list of data sources, select the data source to which you want to add the NPA.
  3. Click the 
    Non-persistent attributes
     tab.
    A list of your NPAs for the selected data source display.
  4. Above the NPA list, click
    Add
    .
    An NPA is added to your data source.
  5. Define the name, entity, and type for the NPA, and then save your changes.
The NPA is added to your data source.
Use the Non-Persistent Attribute in Rules
Create the derivation rule for the NPA value.
For more information about the list of derivation rule types you can create, see Rule Types.
Use the Non-Persistent Attribute in Resources
For more information about how to select the NPAs that your resource returns, see Customize your API.
  1. With your API open, in the Create section, click
    Resources
    .
    A list of resources display.
  2. Click the
    Attributes
    tab.
  3. Complete the following fields, and then save your changes:
    Columns
    Clear the checkbox for the base entity columns you do not want the resource to return.
    Default:
     Selected.
    Attribute name
    Select which attributes your resource returns (subject to security) and override the default name. Each resource attribute is identified with an (alias) name, and includes a 
    column_name
     and 
    format
    .
    Optional: 
    Yes
    If you do not select the response attributes, this resource returns all attributes in table.
    The attributes that you want your resource to return are selected and saved.