Employee Raise Example
In addition to using create, read, update, delete (CRUD) services to alter data, it is sometimes desirable to create a more explicit API. These are sometimes referred to as an RPC-style API. So, instead of (or in addition to) updating the employee row directly, you can create APIs oriented to specific transactions.
In addition to using create, read, update, delete (CRUD) services to alter data, it is sometimes desirable to create a more explicit API. These are sometimes referred to as an RPC-style API. So, instead of (or in addition to) updating the
employeerow directly, you can create APIs oriented to specific transactions.
This Business to Business (B2B) example illustrates how you can create a function for the
employeestable and the explicitly defined
EmployeeWithRaisesresource that gives an employee a raise.
In this article:
You can associate functions with tables or resources that you explicitly define in API Creator.
Layer7 Live API Creatorpasses a (table) row object to your function code. If you alter it,
Layer7 Live API Creatorruns the reactive logic and (if no validations are violated) saves the row to disk. The following image shows the giveRaise function in the B2B Northwind API:
This example shows that you can use parameters.
For more information about how to associate functions with tables or resources that you explicitly define in API Creator, see Manage Functions.
Use Functions from Data Explorer
- Start Data Explorer.For more information about how to start Data Explorer and call functions from Data Explorer, see Data Explorer.
- On the toolbar, selectEntitiesas the endpoint type from the endpoint type drop-down and then select the Employees table from the endpoints drop-down.
- In the Main Grid, select the row for which you want to call the function.
- In the Form, select thegiveRaisefunction from the functions drop-down.The functions dialog appears.
- Enter a Raise Percent, for example 10, and then clickSubmit.The response displays in the dialog.
The giveRaise function is called on the selected row in the Employees table. If the function contains logic that updates the row, the table row is updated. The Form is updated.
Use Event Sourcing
The event sourcing pattern seeks to retain persistent data for each transaction, for audibility. So, in addition to an Employee row, you might have a series of Employee Salary child rows. You give raises by inserting into the Employee Salary rows.
For more information about this pattern, see Martin Fowler's website.
You can implement this pattern with the following simple
Update salary from Posted dataevent rule that is in the B2B Northwind API:
For more information about how to create event rules, see Event Rule Types.
This example also illustrates the use of loadable libraries, and meta data APIs, as illustrated in the following image:
You can run this example in Data Explorer. Insert a row into the
EmployeeRaisestable with the new salary.
For more information about how to manage your libraries, see Logic Libraries.