Removing or preventing meta data from coldbox Active Entities.

We have been using Loopback for API’s lately, but thanks to the wonderfull work of the guys at Ortus with Commandbox and Coldboxes REST skeleton, we have decided to do our next one with CF and Coldbox… which is great!

We are using ActiveEntity, and then just passing any entities retrieved straight out through prc.response.setData()… which is great!

However the entities hold a whole lot of meta data such as $mixed, entityName, queryCacheRegion etc, which then get passed through to the client. This can get quite lengthy when returning associated entities as well. I have attached an example of a JSON response the API is returning.

Is there a setting, or way of having the entities just return the entity data, and not the mixed in meta data?

Thanks!

I’m not sure if this is the best solution, but it has worked for me:

I have a base class for all of my entities called “entity.cfc” which all of my actual entities extend. In that base class I have a method called “getDto()” (DTO = Data Transformation Object).

getDto() basically looks for all public properties in the entity, puts them into a ColdFusion struct, and returns it. I’ve seen other CF developers use a similar function called “getMemento()”.

I would be happy to share my GetDto() method if it would be helpful - or perhaps you or the community could improve upon it.

I always make getDto() a public method by the way, so I can use it when I need to just get plain old data from my entities for persistence, REST API responses, etc…

HI David,

That sounds similar to what I was starting to think of doing. So to confirm… your entity.cfc extends coldbox’s ActiveEntity?

If you could share would be great… if on git let me know and happy to fork … I’ve found using Coldbox REST API skeleton and ORM a great way to build out an API, the only issue when comparing to the node framework we use being all the meta data in the JSON output… so would be great if we could get a plugin that cleaned that out.

Thanks!!

Jason

Sorry for the delayed reply. I must have missed this.

I’m actually not using Coldbox’s ActiveEntity. I’m using my own entity base class which has a set of commonly used component utilities like “getDto()”. I’m actually in the middle of refactoring the component utilities now so it can handle more complex entities. As soon as I get it to a place that I’m content with, I’ll upload it to Github so you can check it out.

Dave

I know this post is long overdue. I have finally taken the time to put together an example for you on how you can extract public properties from your entities.
Here’s the Github Repo: https://github.com/homestar9/component-utilities

The only thing I would change if you were using this in a Coldbox app would be to have Wirebox inject ComponentUtilities automatically for you instead of instancing it manually in the base class.