Manage Non-Persistent Attributes

Manage Non-Persistent Attributes
You can extend your data model to include non-persistent attributes (NPAs). NPAs are pseudo-columns, or attributes, that are not in the schema. You can add and use them in your API without altering the database schema. Add NPAs when you cannot change the database schemas. For example, another application that does not change the value uses the NPA.
You can minimize data-maintenance tasks and speed development using NPAs.
NPAs are analogous to SQL optimizer hints. You can use them to tune API Server. 
Layer7 Live API Creator
 can compute rules as SQL expressions when the rules follow certain restrictions. It computes rules that compute values, such as formula rules, for NPAs faster than other rules. NPAs eliminate the SQL that 
Layer7 Live API Creator
 requires to synchronize your data with your rules.
Layer7 Live API Creator
 reflects NPAs in the OpenAPI (Swagger) documentation for your API.
NPAs can incur substantial performance overhead as compared to columns that are stored in your database. If you have tested your logic and then you discover performance issues, delete the NPAs and alter the schema to define the persisted columns. 
Layer7 Live API Creator
 re-optimizes your logic.
For more information about performance, see the "NPA Best Practices" section.
In this article:
NPA Best Practices
The decision about whether you want  
Layer7 Live API Creator
 to persist derived data can result in a trade-off:
  • Performance.
     Non-persistent data can perform orders-of-magnitude slower than persistent data. This most often occurs with aggregate rules, when there are many children per parent, or when aggregate rules chain. For example, it can be expensive to compute a balance by adding up the items for the orders of a customer, or to compute a department budget by recursively summing the sub departments.
  • Data consistency.
     Applications must update the derivation logic properly to avoid consistency errors.
It is not always clear how data distributions will turn out. Switching strategies by recoding the accessing logic 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, 
Layer7 Live API Creator
 can eliminate the cost of rework. As you manage your NPAs, 
Layer7 Live API Creator
 adjusts your logic accordingly. It maintains aggregate rules using adjustment logic.
For more information about how 
Layer7 Live API Creator
 maintains aggregates using adjustment logic, see Performance.
Consider the following when establishing NPA best practices for your API:
  • If you are using an existing system and you cannot change the database schema, add the NPA to your data source, and then reference it from table-based resources and from rules.
  • If you cannot change the database schema by defining persistent attributes, do one of the following:
    • For update (PUT/POST) logic, add NPAs to your data source, and then have 
      Layer7 Live API Creator
       compute values or roll up sum/count aggregates by referencing the NPA in your rules, such as formula rules.
    • For retrieval (GET) logic. For example, you store first and last names in your database but you want to create APIs that return the full name. You can set virtual resource attributes with lookup values using "fuzzy" foreign key in GET events.
      For more information about how to set virtual resource attributes with lookup values, see Define Table-Based Resource Types.
For a list of all best practices, see Best Practices.
Verify the Prerequisites
Before add an NPA, ensure that you have:
  • Defined entities (tables or views).
  • Verified your data source.
Add a 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
    A list of your NPAs for the selected data source display.
  4. Above the NPA list, click 
    An NPA is added to your data source.
  5. Define the following fields, and then save your changes:
    The name for the NPA. Use a simple name, without spaces and special characters.
    The entity (table or view) for which the NPA is defined.
    Define the type of data (data type) the attribute can contain.
    • Boolean:
       true or false
    • Character:
       A single character
    • Date:
       A SimpleDate (Year-Month-Day)
    • Decimal:
       An arbitrary precision signed decimal number (such as money or percent)
    • Integer:
       An integer number between -2,147,483,648 to 2,147,483,647
    • Real:
       An IEEE 754 floating point number
    • String:
       An arbitrary list of characters
    • Timestamp:
       A SimpleTimestamp (Year-Month-Day-Hour-Minute-Second)
The NPA is added to your data source.
Use Non-Persistent Attributes
You can reference NPAs in entity resource attributes added to table-based resources and in rules, such as derivation and validation rules. The NPAs that you reference in entity resource attributes can return values. You can reference NPAs in entity resource attributes and then inject the computed values by using the attributes in rules.
B2B Northwind
 API sample includes the 
 resource with the 
 NPA added as a 
 entity resource attribute.
For more information about this API sample, see B2B API Sample.
For more information about how to reference NPAs in resources, see Manage Resource Attributes.
View Non-Persistent Attributes
You can view the NPAs that you have added to your data source in the following locations:
Non-Persistent Attribute Limitations
The following are limitations to adding and using NPAs:
  • You cannot add parent resource attributes that are NPAs to a resource.
  • 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. This data source uses the extensible data source framework.