ORDA – Improve your API with function scope

Automatically translated from English

4D v18 R4 and ORDA made it possible for you to create high-level class functions above the data model. This allows you to write business-oriented code to hide complexity, reduce errors, and speed up the development process.

With 4D v18 R5, we’re bringing even more features to help you optimize and organize your code. For example, you might need to run a function locally on the client to improve performance when working in client/server mode. It’s now possible! Or if you expose your database as a REST server, you might want some functions to be available on the server-side but hidden for your REST clients. This is also possible!

HDI: Functions keywords in ORDA data model classes

 

Two new keywords for functions in ORDA data model classes are available:

working in C/S with the LOCAL keyword

Functions are executed on the server by default in C/S, but you can now choose to execute some of them on the client. This can greatly improve performance.

Example

In this example, we have a Students dataclass. Let’s write a function that returns the student’s ages.

 

Here is our age() function in the cs.StudentsEntity entity class.

Class extends Entity

// This is an estimate of the age
local Function age() : Variant
If (This.birthDate#!00-00-00!)
 $0:=Year of(Current date)-Year of(This.birthDate)
Else
 $0:=Null
End if

And this is a list box displaying all the students with the age column mapped with the expression This.age():

 

Thanks to the local keyword, the age() function is executed locally on the client with the birthdate loaded while displaying the list box. This is important to be in tune with the 4D v17 R5 ORDA requests optimization.

Without the local keyword, the age() function would trigger a request to the server for each line!

exposed keyword

As promised with 4D v18 R4, we’ve enhanced exposing functions for the REST server. Now with 4D v18 R5, you can choose which functions to publish (or not).

You can use the exposed keyword to have a function published as an API for REST clients.

In the example below, the registerNewStudent() function in the cs.Students dataclass class  is callable from a REST client while computeStudentId() is not.

Class extends DataClass

exposed Function registerNewStudent($student : Object) : Object
var $entity : cs.StudentsEntity
var $status : Object

$entity:=ds.Students.new()
$entity.fromObject($student)
$entity.studentId:=This.computeStudentId()
$status:=$entity.save()

If ($status.success)
  $0:=$entity
Else
  $0:=$status
End if

// This function not callable from a REST client
Function computeStudentId() : Integer
//compute a new student Id
Use (Storage.
infos)
  Storage.
infos.studentId:=Storage.infos.studentId+1
$0:=Storage.
infos.studentId
End use

Here is the calling code on the REST client:

var $connect;$ds;$student;$result : Object
var $studentId : Integer

$connect:= New object("hostname";"127.0.0.1:8044")
$ds:=Open datastore($connect;"demo")

$student:=New object("firstname"; "Mary"; "lastname"; "Smith")
// Execution is OK, no error is raised
$result:=$ds.Students.registerNewStudent($student)

//An error is raised
$studentId:=$ds.Students.computeStudentId()

Note: In 4D v18 R4, all functions are exposed on the REST server. In 4D v18 R5, functions are NOT exposed by default. Don’t forget to mark the functions you want to expose.

Check out the documentation and the HDI above to learn more about the new features!

 

Avatar

• Product Owner •

Marie-Sophie Landrieu-Yvert has joined the 4D Product team as a Product Owner in 2017. As a Product Owner, she is in charge of writing the user stories then translating it to functional specifications. Her role is also to make sure that the feature implementation delivered is meeting the customer need.

Marie-Sophie graduated from the ESIGELEC Engineering School and began her career as an engineer at IBM in 1995. She participated on various projects (maintenance or build projects) and worked as a Cobol developer. Then she worked as an UML designer and Java developer. Lately her main roles were analyzing and writing functional requirements, coordinate business and development teams.