2
votes

I'm trying to build some sort of repositories with arangodb rest api and spring-cloud-feign.

When I perform a Get, everything is fine, I receive the Entity as it should and I can even map _key to my property.

My issue is when I try to perform a Create / Update (Post / Patch), if I add a query param for returnNew I receive the new Object, but inside new.

Eg: http://localhost:8529/_db/testDB/_api/document/orderCollection?returnNew=true

{
  "_id": "orderCollection/ERGDEF34",
  "_key": "ERGDEF34",
  "_rev": "_UqhLPC----",
  "new": {
    "_key": "ERGDEF34",
    "_id": "orderCollection/ERGDEF34",
    "_rev": "_UqhLPC----",
    "description": "descriptionxpto",
    "amount": "5000000000000",
    "operation": {
      "id": "1",
      "description": "operation description",
      "status": "Completed"
    },
    "creationDate": [
      2017,
      3,
      13,
      15,
      23,
      1,
      546000000
    ]   
  }
}

Is there any way to send the new object outside the new property?

2

2 Answers

1
votes

Yes, using the create API or the update API return fresh created documents in the new property. This behaviour is the way the API is documented, and its intendet to be that way. All existing client drivers were implemented on top of this specification, so there is no easy way to change this (even if we wanted to).

The main reason for the new property is that you can find out whether the document is new or not.

However, ArangoDB offers the Foxx Microservices so you can easily create your own APIs that work the way you prefer it.

In a general note - we rather manage feature requests via Github issues.

0
votes

**Edit: Just noticed you are using the Rest API. If you are using Java (as tagged), why not just use the Java drivers? Regardless, you can still create an abstraction to handle the use case.

You should handle this inside your Data Access Layer (You have abstracted that, right?)

This is how I am currently doing this:

Interface:

public interface DataAccess {

    public <T extends BaseEntity> T update(T entity, Class<T> c) throws DataException;

}

Implementation:

public class DataAccessImpl implements DataAccess {

    private ArangoDB arangoDB; 

    public DataAccessImpl(String database) {
        this.database = database;
        arangoDB = ArangoBuilderService.getInstance().getArangoDB();
    }

    public <T extends BaseEntity> T update(T entity, Class<T> c) throws DataException {
        try {
            String key = ((BaseEntity)entity).getKey();
            DocumentUpdateEntity<T> value = arangoDB.db(database).collection(c.getSimpleName().toLowerCase()).updateDocument(key, entity);
            return (T) value.getNew(); // TODO: better error handling
        } catch(ArangoDBException e){
            throw new DataException(e.getMessage(), e);
        }
    }

}

Usage:

DataAccess db = new DataAccessImpl(tenant);
User user = db.getByKey("userkey", User.class);
db.update(user, User.class);

This way, you abstract away all small details and just use POJOs.