Adding a computed or "lookup" field to an Aurelius Entity

I have a set of Aurelius entities in a datamodel.

This is working well.

I would like to extend the Aurelius entities with additional properties which are not fields in the database, but which can be retrieved when a call for the entity occurs.

Is there an attribute to add to the property to make it behave as a "readonly field" Which is pulled back with the database fields when .Find is called?

How do you expect such additional properties to be "pulled back with the database fields" if they "are not fields in the database"?

Sorry if I am not expressing myself well Wagner.

Here is what I mean:

When I call a URL:

http://localhost:2001/xdata/MyEntity(MyPrimaryKey)

JSON comes back:
{
"$id": 1,
"PassbookNum": "",
"DateJoined": "1899-12-30",
"DateCreated": "2014-07-12T15:43:58.188",
"Current": true,
...

These fields are all returned because they are properties of the Aurelius Entity. AND they are database fields.

Is it possible to extend this JSON to include other properties which are generated dynamically by Aurelius, but which are not fields in the database. These would be ReadOnly fields, so they would not form any part of the update process of the record. I am assuming it would be possible to add properties and give them attributes to include them in the entities.

There are many ways to do this implementing your own xdata services, but I think you want to benefit from the automatic Aurelius CRUD endpoints & filtering facilities.

So you may use a [Transient] field linked to an [XDataProperty] property, or just the property managed by a getter/setter.

Example:

[Transient]
FHashTags : string;
.
.
[XDataProperty]
property HashTags: string read FHashTags write FHashTags;

or :

function GetHashTags : string;
procedure SetHashTags(const Value: string);
.
.
[XDataProperty]
property HashTags: string read GetHashTags write SetHashTags;

It's Ok if your client uses pure JSON. If your client will also use Aurelius entities definitions, you'll need to implement some IFDEFs to detect if you're "in" the client or in the xdata server application, so you could bypass the getter/setter when compiling the client.

1 Like

Reply added from email communication:

I have added the following code to my Aurelius entity:

[Transient]
FTitle: String;

[XDataProperty]
property Title: string read GetTitle;

function TPeople.GetTitle: string;
begin
FTitle:= 'Mr';
Result:= FTitle;
end;

When I call

http://localhost:2001/xdata/People(16)

The JSON that is returned does not include the "Title" field, which is the effect I am trying to achieve.

Is there any way to achieve this, to add a read only computed or lookup property to the values returned in the Aurelius Entity so its value is returned in a call to return the entity?

--
Wagner Replied:

Have you added unit XData.Model.Attributes to your unit, so the attribute is effectively used?
It would be better if you please continue the discussion in the Support Center topic.

--

AH: I had not known I needed to add that unit.
Adding XData.Model.Attributes to my uses clause has returned the values!

Thank you.

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.