Manage Non-Persistent Attributes
Manage Non-Persistent Attributes
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.
- For more information about the B2B sample, see B2B API 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 Creatorcan 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 Creatorcan eliminate the cost of rework. As you add or remove NPAs,
CA Live API Creatoradjusts 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 Creatorre-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 Creatormaintains aggregates using adjustment logic, see Performance.
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
- With your API open, in the Create section, clickData Sources.A list of the data sources for your API display.
- From the list of data sources, select the data source to which you want to add the NPA.
- Click theNon-persistent attributestab.A list of your NPAs for the selected data source display.
- Above the NPA list, clickAdd.An NPA is added to your data source.
- 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.
- With your API open, in the Create section, clickResources.A list of resources display.
- Click theAttributestab.
- Complete the following fields, and then save your changes:ColumnsClear the checkbox for the base entity columns you do not want the resource to return.Default:Selected.Attribute nameSelect which attributes your resource returns (subject to security) and override the default name. Each resource attribute is identified with an (alias) name, and includes acolumn_nameandformat.Optional:YesIf 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.