Request and Response Model - nodejayes/molly GitHub Wiki

Each CRUD request is always a POST request. The URL always determines what is done and with which model it is done. The body of the request is a JSON object, whatever is equipped with a params property. The params property contains either the object to be created or a filter that filters the results of Read. params can also contain an Id + Update Set to modify the object or just the Id to delete it.

Lets say we want to CRUD a User...

Create

It is only possible to create one object per request at a time. To create several objects, please use the Route transaction. The object to be created is always transferred as params without the ID. After successful creation, the same object comes back with the created ID. Before the object is persisted, it is validated.

Url: /create/User

Request Body

{
    "params": {
        "name": "John",
        "password": "d88807c3c0af47d4b2379262a513b441",
        "age": 25
    }
}

Response Body

{
    "data":[{
        "_id":"5ba749ff4d8ca93738ad8522",
        "name":"John",
        "password":"d88807c3c0af47d4b2379262a513b441",
        "age":25
   }],
   "errors":null
}

Read

For reading objects a valid MongoDb filter must be used as params. There is a special feature, the property RESTRICTIONS. This property is used to influence the result of the query e.g. with a limit. The result is always a list of objects. A validation is also carried out here.

URL: /read/User

Request Body

{
    "params": {
        "name": "John",
        "RESTRICTIONS": {
            "limit": 1
        }
    }
}

Response Body

{
    "data":[{
        "_id":"5ba749ff4d8ca93738ad8522",
        "name":"John",
        "password":"d88807c3c0af47d4b2379262a513b441",
        "age":25
   }],
   "errors":null
}

RESTRICTIONS

The RESTRICTIONS Property is to set some Options for the Filter. The follow Options are available:

Option Description Example
limit limit the Result {"limit": 5}
skip skip the first Datasets of the Reuslt {"skip": 5}
sort sort the Result by Properties {"sort": {_id: 1, name: -1}}

Update

The same applies for updating as for creating the data, only one data record can be updated at a time. The update model consists of the ID of the object to be updated and an update set containing the updates.

Url: /update/User

Request Body

{
    "params": {
        "id": "5ba749ff4d8ca93738ad8522",
        "updateSet": {
            "name": "David"
        }
    }
}

Response Body

{
    "data":true,
    "errors":null
}

Delete

Url: /delete/User

When deleting a record, the same applies as when creating or updating, that you can only delete one record per request. For this only the ID of the record has to be passed. The result is an error or the value true.

Request Body

{
    "params": {
        "id": "5ba749ff4d8ca93738ad8522"
    }
}

Response Body

{
    "data":true,
    "errors":null
}

Errors

When we do something that ends in a Error, for Example delete a User with an invalid ObjectId, we get the Error in the Response. In this Case the data in the Response is always null.

URL: /delete/User

Request Body

{
    "params": {
        "id": "xyz"
    }
}

Response Body

{
    "data":null,
    "errors":"child \"id\" fails because [\"id\" length must be at least 24 characters long]"
}

Back to Index