4d-databaseorda

How Can I Avoid Throwing Errors From Incorrect FieldNames in EntitySelection.toCollection($fieldNames)


I'm trying to convert code to ORDA. One thing that is giving me issues is Relations. Example setup:

Tables:

[CallLogs], [Employees]

Relation:

[CallLogs]EmployeeID (M)---->(O) [Employees]UniqueID

MtO Relation Name: "RelatedEmployee".

Using ORDA, I can do this:

ds["CallLogs"].query("......").toCollection("Date, Subject, RelatedEmployee.Name")

If I was to put in a fieldname into .toCollection() that wasn't real, 4D will throw an error. This is fine when I control the input. However, in a web API where the client can specify fields, I don't want to throw errors, but instead just drop any incorrect field names silently, or return an error message. Throwing a 4D error stops the process.

Previously, when I used Selection to JSON, I'd specify fields from linked tables as TableName_FieldName, and I could split it, then verified the table and field name against a table structure object. However, since ORDA uses Relation Name, instead of Table Name, I don't know what to do. I can't find anything that will take a relation name and give me back a table. Or, alternatively, I can't find a way to suppress errors in .toCollection(). Ideally, .toCollection() would just ignore field names that weren't real.

Any help would be appreciated on how to proceed.


Solution

  • You mention a web API. Are you using the 4D REST server? If so the burden is really on the caller to use the correct names. They could get the catalog first.

    If you are using the Web Server and managing your API calls through that you will want to parse the query strings yourself and verify the field spellings. If it's an open API I would want to do that anyway to prevent any malicious code injection attempts.

    since ORDA uses Relation Name, instead of Table Name

    True when you are referencing the table in the context of the related table. This is actually a great feature I find. For example, I would call the M->O relation simply "Employee" because it's more readable. And the O->M I'd call "Logs" or "CallLogs".

    OTOH some folks prefer "r_Employee" to explicitly identify the relation.

    And the dataStore ds is also an object as is ds.CallLogs. So you can easily get a complete list of the table fields with

    $o:=ds.CallLogs
    

    without having convert an entity selection in to a regular selection and then using Selection to JSON.

    I can't find a way to suppress errors

    Checkout ON ERR CALL You can create an error handler method that does nothing. Just create a method named Err_ignore. To use it:

    $err_method:=Method called on error  // get the current error method
    ON ERR CALL("Err_ignore")
     <  do some stuff >
    ON ERR CALL($err_method)  //  restore the original err handler
    

    This is a good idea for methods that run on the 4D server and the web server so you don't get blocked. I prefer to capture and log the errors but the example is all you need to ignore them.

    If you are providing the input on the web page you could use a selection list of some sort to control the input. But if you can't do that you have to manage it yourself.

    Hope this helps