Working with server-api
Creating an endpoint
- To create an endpoint add a file with the endpoint name in src/api/definitions. Lets create a simple test endpoint.
- Creating this file automatically creates the endpoint in the server.
- Next step is to create the database schema for the test endpoint.
Creating database schema
- First step is to create a file under src/api/definitions/db/sql with the same name as the endpoint created.
- The basic information related to the attributes of the schema are mentioned in the model.
- After creating the database schema we need to sync it with the database. To do this set the value of SYNC_DB in the .env file to true and run
npm start
- Once the sync is complete set the value of SYNC_DB to false. Now we have created the "test" table in the database.
Adding logic to the endpoint
- Each endpoint has 5 methods by default i.e., create, update, remove, find, get.
- We can add validation for the fields for each of these methods using the following syntax.
- Saving of data in the database is automatic.
- In addition to the given fields, createdAt, createdBy, updatedAt, updatedBy and key are generated automatically at the time of creation of the record in the database.
onBefore:
- As the name implies, this method is invoked before the data is sent to the database.
- This method can be defined for any path of an endpoint to add extra validation or processing before storing the information in the database.
- It contains 3 arguments. First argument is the input given to the endpoint (here name and age), second is the req object and third is the res object corresponding to the request and response objects in a api request.
Data of logged in user can be accessed using the req.user
object
onAfter:
- As the name implies, this method is invoked after the data is saved in the database.
- This method can be defined for any path of an endpoint to format the data before sending it to the client.
- The arguments of this function are similar to onBefore.
method:
- The storing of data in the database is automatic, however we can overwrite this behavior using method.
- Once method is defined for any path of an endpoint the data will no longer be saved automatically.
Security
- The security type can be defined for either each path or the whole endpoint.
- There are three types of security:
- security: false (accessible without login)
- security: true (accessible only to logged in users)
- security: { role: "admin" } (accessible only to admins)
- Security is false by default if not mentioned.
- In this example only the create endpoint has the security level of admin.
Additional Paths
- Apart from the default paths we can also define our custom paths using the additional paths.
- One major difference here is that the data base storage is not automatic like in other generic paths.
Service
- Services can be used to make requests to other endpoints internally.
- Services can be used to call create, update, get or remove operations (or any other additionally defined paths) on another endpoint.
- The syntax is simply
this.service("service-name").methodName(data)
No Comments