Grid Express API

You can use Grid Express API to link the client UI and Grid server model.

Grid Express API helps to perform the next requests:

Method URL Description
GET / Get all records
GET /:recordId Get particular record
PUT / Update multiple records
POST / Create a record
POST /validation Validate particular record

Initialization

  const gridApiBuilder = UIKernel.gridExpressApi();    //initializes builder

Builder methods

model

  gridApiBuilder.model(gridModel);
  gridApiBuilder.model(getModel);

Specify a Grid model which must implement Grid Model Interface and can be an instance or a constructor so that the Grid Express Api will be able to use the provided data model to perform client requests.

Parameters:

Type Name Description
GridModel gridModel Required. The instance of a Grid Model

Or:

Type Name Description
Function getModel Required. Function to be called with Express middleware’s req and res
    parameters returning a Grid model instance

result

  gridApiBuilder.result(callback);

Specify a function to be called when the response is ready

Parameters:

Type Name Description
Function callback Optional. Function to be called with the result.

callbacks arguments:

Type Name Description
Any err Error caught from model methods
Any data Result returned by model methods

getRouter

  const router = gridApiBuilder.getRouter();

Creates an Express router object with middlewares performing requests specified above.


Usage

Pass a model instance to Express API:

  import advertisersModel from 'somewhere';
  const router = UIKernel.gridExpressApi()
    .model(advertisersModel)
    .getRouter();

Or use a constructor for that:

  import AdvertisersModel from 'somewhere';
  const router = UIKernel.gridExpressApi()
    .model((req, res) => {
      return new AdvertisersModel(req.params.param1, res.locals.param2)
    })
    .getRouter();

To interact with this API at front-end use Grid Xhr Model.

You can also customize your API with some additional methods, or perform a similar behaviour using other frameworks of course.

Example of customizing provided API with your own methods

  import model from 'somewhere';
  const router = UIKernel.gridExpressApi()
    .model(model)
    .getRouter()
    //here you can add performing of some other API requests:
    .delete('/:recordId', (req, res, next) => {
      model.delete(req.params.recordId)
        .then(() => res.sendStatus(200))
        .catch(err => next(err));
    });