Models - predefined or custom Endpoints

2 minute read

By default, API Builder generates the following API endpoints for models:

  • GET /api/endpoints/<model_name> : Return all objects (the first 1000 records).
  • GET /api/endpoints/<model_name>/query : Return all objects that satisfy a query.
  • GET /api/endpoints/<model_name>/:id : Return a specific object by ID.
  • GET /api/endpoints/<model_name>/distinct : Find distinct objects.
  • GET /api/endpoints/<model_name>/count : Count objects.
  • PUT /api/endpoints/<model_name>/:id : Update a specific user by ID.
  • PUT /api/endpoints/<model_name>/findAndModify : Find and modify an object.
  • POST /api/endpoints/<model_name> : Create a new object.
  • POST /api/endpoints/<model_name>/upsert : Create or update an object.
  • DELETE /api/endpoints/<model_name>/:id : Delete a specific object by ID.
  • DELETE /api/endpoints/<model_name> : Delete all objects.

To disable API Builder from generating these endpoints, set the Model’s autogen property to false when defining the model. You will need to create API Builder APIs to access the model.

Example

The following model is disabled from generating pre-defined endpoints. An API endpoint needs to be defined to access the model data as shown below.

// modes/employee.js

var APIBuilder = require('@axway/api-builder-runtime');

var employee = createModel('employee', {
    fields: {
        first_name: { type: String, description: 'First name', required: true },
        last_name: { type: String, description:'Last name', required: true },
        email_address: { type: String, description: 'Email address', required: true }
    },
    connector: 'memory',
    autogen: false
});

module.exports = employee;

The example below implements the GET /api/<employee>/:id endpoint that would normally be generated by API Builder.

// apis/employeeFindOne.js

var APIBuilder = require('@axway/api-builder-runtime');

var employeeFindOne = APIBuilder.API.extend({
    group: 'employeeAPIs',
    path: '/api/employee/:id',
    method: 'GET',
    description: 'This API finds one employee record',
    model: 'employee',
    parameters: {
        id: { description: 'the employee id' }
    },
    action: function (req, resp, next) {
        resp.stream(req.model.find, req.params.id, next);
    }
});

module.exports = employeeFindOne;