Models - Predefined or Custom Endpoints

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

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

models/employee.js
var Arrow = require('arrow');

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 Arrow = require('arrow');

var employeeFindOne = Arrow.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;

Related Links