4D Blog

Home Product Multiple 4D data sources, interested?

Multiple 4D data sources, interested?

October 31, 2019

Product

We’ve got an exciting new feature to announce in 4D v18 and the title may have already given you a hint!

This feature opens new possibilities for client/server work. Rather than being limited to the current database and requiring a permanent network connection, an application in 4D v18 can get data from another, remote 4D database that’s exposed on a 4D server!

A lot of options become available thanks to this feature. For example, you could organize your applications to work offline and only synchronize local data when remote data is reachable. Or you could consider publishing your data on multiple servers and switching from one to the other as necessary. Another option could be splitting your data model across different databases (e.g. local data, international data). What about having your data distributed in different places yet still accessible through a single 4D client code (project methods and form objects)? All of these scenarios are now possible and this blog post tells you how!

HDI: How to work with a remote datastore

With 4D v18, your 4D database can be exposed as a REST server for other 4D client applications. This means that a 4D client can interact with the data you’ve exposed (create, read, update, delete). And even more good news: this interaction is based on ORDA concepts, so it’s completely object-oriented!

how to Expose your 4D database for remote access?

In the database Web settings:

  • On the Configuration tab, configure an HTTP port (for security reasons, be sure to use HTTPS for production mode!)

  • On the REST resource tab, select the “Expose as REST Server” option

blank

  • Start the Web server. In the example above, we chose to start the server at database startup.
  • Open your database on the 4D Server.
  • Congratulations! Your database is now reachable for remote access through your machine’s IP and the port you configured!

Note: Using the Web Server to access the REST Server does not require a Web Server license. Standard 4D Client licenses are used for connections.

Working with remote databases

Open datastore command

As we stated earlier, your database is reachable via ORDA concepts: a database is handled with a datastore object. Therefore, the first step is to get the datastore object corresponding to the remote database you want to access. No problem! Call the Open datastore command with the correct hostname and you’re off and running.

We associate the datastore object to the local ID, “students”. We can now start working with our remote database using ORDA (run queries, load/update entities, etc.)

C_OBJECT($connectTo;$schoolDS;$s)
C_TEXT($dataClass)

//The database contains a Students dataclass
$dataClass:="Students"

$connectTo:=New object("hostname";"school.acme.com")
$schoolDS:=Open datastore($connectTo;"students") //local id of this remote datastore is "students"

//Start working with ORDA means
ALERT("They are "+String($schoolDS[$dataClass].all().length)+" students")

//Run an ORDA query on the Students dataclass
$s:=$schoolDS[$dataClass].query("lastname=:1";"Smith").first()
If ($s#Null)
  ALERT("Student "+$s.lastname+" lives in "+$s.address.city)
End if

Check the Open datastore documentation to learn more about how you can access a remote datatabase with a secured connection (TLS).

using multiple remote databases

Here’s a short example showing how easy it is to work with multiple remote databases. We have two databases: one contains French students, the other contains English students.

We can choose to view either French or English students.

blank

blank

The form method:

Case of
: (FORM Event.code=On Load)
Form.frenchServer:="french.acme.com"
Form.englishServer:="english.acme.com"
End case

The object method behind the “View French students” radio button:

C_OBJECT($connectTo;$students)
$connectTo:=New object()
$connectTo.hostname:=Form.frenchServer
$students:=Open datastore($connectTo;"french") //datastore containing French students
Form.students:=$students.Students.all()

The object method behind the  “View English students” radio button:

C_OBJECT($connectTo;$students)
$connectTo:=New object()
$connectTo.hostname:=Form.englishServer
$students:=Open datastore($connectTo;"english") //datastore containing English students
Form.students:=$students.Students.all()

Note: The first time the Open datastore command is called, the datastore object is loaded in memory and a session is opened on the server. On subsequent calls it simply returns a reference on this datastore object.

control access to your remote database

To to keep the 4D database you’ve exposed safe, you can filter access to it. Let’s take another look at the Open datastore command. As shown below, it can be passed a user and a password.

C_OBJECT($connectTo;$myStudents)

ON ERR CALL("manageErrors")

$connectTo:=New object()
$connectTo.hostname:="students.acme.com"
$connectTo.user:="mary@4d.com"
$connectTo.password:=Form.password

//local id of this remote datastore is "students"
$myStudents:=Open datastore($connectTo;"students") 

ON ERR CALL("")

 4D provides two ways to restrict access to authorized users:

Use a 4D users group

Acces can be restricted using a 4D users group. When you expose the database, select the group allowed access on the Web settings page’s REST resource tab.

blank

If the user given in the Open datastore command belongs to the selected group, access is granted, otherwise an authentication error is generated.

Use the on rest authentication database method

You can also use the new On REST authentication database method to code your own access control to the database you’ve exposed. This method receives the user’s credentials in the Open datastore command. Simply return True if the user is allowed to work with your remote database.

Here’s an example:

C_TEXT($1;$name;$2;$password)
C_BOOLEAN($0;$result;$3;$digest)
C_OBJECT($user)

$name:=$1 // The user to provide in Open datastore command
$password:=$2 // The password to provide in Open datastore command
$digest:=$3 // True if password is hashed

$result:=False

//Search for the user in our Users dataclass

$user:=ds.Users.query("name=:1";$name).first()

If ($user#Null)
// Passwords are hashed in Users dataclass
  If ($digest & ($user.password=$password))
  $result:=True
  End if
End if


$0:=$result

Note: Theds and datastore.getInfo() commands have been updated, and three new methods were added: datastore.startTransaction(), datastore.cancelTransaction(), and datastore.validateTransaction().

Now it’s time to download the above HDI to learn more about this amazing feature!

Discuss

Tags 4D server, ORDA, Programming, Remote datastore, REST, REST server, v18

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 Listbox Logs Mail Microsoft 365 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 Listbox Logs Mail Microsoft 365 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