4D Blog

Home Product ORDA Data Model Classes and REST

ORDA Data Model Classes and REST

July 27, 2020

Product

In a previous blog post, you learned about ORDA classes and the possibilities they offer to make your coding experience smoother. In this blog post, you’ll see how to use them with the REST server.

Let’s see it in action!

HDI: Examples to use ORDA classes with REST server

We have a database named LearningSystem which contains the following tables:

This database is exposed as a REST server on localhost (port 8044). All of the defined functions in its classes can be called using REST requests in a POST operation.

Calling a function defined in the datastore class

The datastore object is retrieved using the /rest/$catalog prefix.

A getStatistics() function has been defined in the DataStore datastore class. The function returns a collection with information about the schools.

Class extends DataStoreImplementation

Function getStatistics

 C_COLLECTION($0;$result)
 C_OBJECT($obj;$school)

 $result:=New collection()


For each ($school;This.Schools.all())
 $obj:=New object()
 $obj.name:=$school.name
 $obj.city:=$school.city.name
 $obj
.numberOfStudents:=$school.students.length
 $result.push($obj)
End for each

$0:=$result

You can call it with the http://127.0.0.1:8044/rest/$catalog/getStatistics URL.

Here’s the response from the server:

{
 "result": [
    {
    "name": "Old school",
    "city": "Solebury",
    "numberOfStudents": 3
    },
    {
    "name": "History Institute",
    "city": "Solebury",
    "numberOfStudents": 3
    },
    {
    "name": "4D University",
    "city": "Hummelstown",
    "numberOfStudents": 3
    }
  ]
}

 

Calling a function defined in a dataclass class

A dataclass object is accessed via the /rest/dataClassName prefix.

A registerStudent() function has been defined in the Schools dataclass class. The function receives student’s data in an object and checks if the student’s English level meets the school’s required level. If their English level is OK, the function creates and saves the Students entity.

Class extends DataClass

Function registerStudent

 C_OBJECT($1;$data;$student;$school;$result;$0)

 $data:=$1

 $school:=ds.Schools.query("name = :1";$data.schoolName).first()
 $result:=New object("success";True)

If ($data.englishLevel < $school.minAcceptedEnglishLevel)
 $result.success:=False
 $result.statusText:="The english level is not enough"
 $0:=$result
End if

If ($result.success)
 $student:=ds.Students.new()
 $student.fromObject($data)
 $student.finalExam:="To take"
 $student.school:=$school
 $result:=$student.save()
 $0:=$student
End if

It’s accessible through the http://127.0.0.1:8044/rest/Schools/registerStudent URL. 

The parameters must be passed as a collection, in the body of the request:

[
 {
 "firstname": "Mary",
 "lastname": "Smith",
 "englishLevel": 2,
 "schoolName": "Math school"
 }
]

Here’s the response from the server:

{
  "__entityModel": "Students",
  "__DATACLASS": "Students",
  "__KEY": "9",
  "__TIMESTAMP": "2020-06-03T13:08:13.542Z",
  "__STAMP": 1,
  "ID": 9,
  "firstname": "Mary",
  "lastname": "Smith",
  "englishLevel": 2,
  "schoolID": 4,
  "finalExam": "To take",
  "school": {
    "__deferred": {
      "uri": "/rest/Schools(4)",
      "__KEY": "4"
     }
   }
}

Calling a function defined in an entity class

An entity object is accessed via the /rest/dataClassName(key) prefix, where key is the primary key of the entity.

A studyingInSameCity() function has been defined in the StudentsEntity entity class. It returns all other students studying in the same city as the current Student.

Class extends Entity

Function studyingInSameCity
 C_OBJECT($0;$city)

 $city:=This.school.city
 $0:=$city.schools.students.minus(This)

This example is for an entity with primary key = 7. It’s accessed via the http://127.0.0.1:8044/rest/Students(7)/studyingInSameCity/?$attributes=firstname, lastname URL.

We apply the studyingInSameCity() function on this entity and filter the returned attributes to get only firstname and lastname (/?$attributes=firstname, lastname).

Here’s the response from the server:

{
 "__DATACLASS": "Students",
 "__entityModel": "Students",
 "__GlobalStamp": 0,
 "__COUNT": 3,
 "__FIRST": 0,
 "__ENTITIES": [
    {
     "__KEY": "5",
     "__TIMESTAMP": "2020-06-16T13:59:51.095Z",
     "__STAMP": 1,
     "firstname": "Ricky",
     "lastname": "Coyle"
    },
    {
     "__KEY": "6",
     "__TIMESTAMP": "2020-06-16T13:59:51.095Z",
     "__STAMP": 1,
     "firstname": "Alonzo",
     "lastname": "Zapata"
    }
  ],
 "__SENT": 3
}

Calling a function defined in an entity selection class

An entity selection object can be accessed via the filter syntax. For example, /?$filter=”finalExam=’To take'” to get all of the students with a value of “To take” for the finalExam attribute.

An entity selection can be accessed through other ways, too so be sure to check the documentation.

A setFinalExam() function has been defined in the StudentsSelection entity selection class. It updates the finalExam attribute of each student with the given value.

Class extends EntitySelection

Function setFinalExam

 C_TEXT($1;$value)
 C_OBJECT($student;$status;$0)

 $value:=$1


For each ($student;This) Until (Not($status.success))
 $student.finalExam:=$value
 $status:=$student.save()
End for each

$0:=$status

In this example, it’s accessible through the http://127.0.0.1:8044/rest/Students/setFinalExam/?$filter=”finalExam=’To take'” URL.

Here’s the body of the request:

[
"Passed"
]

Here’s the response from the server (all of the concerned students have been properly updated):

{
 "result": {
   "success": true
   }
}

Download the HDI to see all the above in action.

 

Note:
Be sure to check out this post about keywords. Since 4D v18 R5, functions are not exposed by default. Don’t forget to mark the functions you want to expose!

Discuss

Tags API, Class, Data model, Function, ORDA, REST, REST server, v18 R4, v19

Latest related posts

  • June 16, 2025

    Generate, share and use web sessions One-Time Passcodes (OTP)

  • May 20, 2025

    Automatic Refresh of OEM Build Licenses

  • May 16, 2025

    New class to handle incoming TCP connections

Avatar
Marie-Sophie Landrieu-Yvert
• 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.
  • Deutsch
  • Français
  • English
  • Português
  • Čeština
  • Español
  • Italiano
  • 日本語

Categories

Browse categories

  • 4D View Pro
  • 4D Write Pro
  • 4D for Mobile
  • Email
  • Development Mode
  • 4D Language
  • ORDA
  • User Interface / GUI
  • Qodly Studio
  • Server
  • Maintenance
  • Deployment
  • 4D Tutorials
  • Generic
  • 4D Summit sessions and other online videos

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor

Tags

4D-Analyzer 4D for Android 4D for iOS 4D NetKit 4D Qodly Pro 4D View Pro 4D Write Pro 20 R8 20 R9 Administration Authentication Build application CI/CD Class Client/Server Code editor Collections Compatibility settings Formula Google Listbox Logs Mail Network Objects ORDA PDF Pictures Preemptive Programming Qodly Studio REST Scalability Security Session Source control Speed Spreadsheet Tutorial UI User Experience v20 vscode Web Word processor
Subscribe to 4D Newsletter

© 2025 4D SAS - All rights reserved
Terms & Conditions | Legal Notices | Data Policy | Cookie Policy | Contact us | Write for us


Subscribe to 4D Newsletter

* Your privacy is very important to us. Please click here to view our Policy

Contact us

Got a question, suggestion or just want to get in touch with the 4D bloggers? Drop us a line!

* Your privacy is very important to us. Please click here to view our Policy