Version:

Shorthand reference

A shorthand is a simple way to write a route handler for common API scenarios. Here’s a reference of the shorthands, and the code they represent.

Shorthands use default status codes, based on the HTTP verb:

  • GET is 200
  • PUT is 204
  • POST is 201
  • DEL is 204

PUT and POST change to 200 if there is a response body.

GET shorthands

Single collection

Shorthand
this.get('/contacts');          // finds type by singularizing url
this.get('/contacts', 'users'); // optionally specify the collection as second param
Expanded
this.get('/contacts', function(db, request) {
  return {
    contacts: db.contacts // db.users in the second case
  };
});

Multiple collections

Shorthand
this.get('/', ['photos', 'articles']);
Expanded
this.get('/', function(db, request) {
  return {
    photos: db.photos,
    articles: db.articles
  };
});

Single record

Shorthand
this.get('/contacts/:id');         // finds type by singularizing url
this.get('/contacts/:id', 'user'); // optionally specify the type as second param
Expanded
this.get('/contacts/:id', function(db, request) {
  var id = request.params.id;

  return {
    contact: db.contacts.find(id)
  };
});

Single record with related records

Shorthand
this.get('/contacts/:id', ['contact', 'addresses']); // put the owning (singular) model first
Expanded
this.get('/contacts/:id', function(db, request) {
  var id = request.params.id;

  return {
    contact: db.contacts.find(id),
    addresses: db.addresses.where({ contact_id: id })
  };
});

Array of specific records

For example, GET /contacts?ids=1,3

Shorthand
this.get('/contacts', { coalesce: true });
this.get('/contacts', 'users', { coalesce: true });
Expanded
this.get('/contacts', function(db, request) {
  var ids = request.queryParams.ids;

  return {
    contacts: db.contacts.find(ids) // db.users in the second case
  };
});

POST shorthands

Create a resource

Shorthand
this.post('/contacts');          // finds type by singularizing url
this.post('/contacts', 'user');  // optionally specify the type as second param
Expanded
this.post('/contacts', function(db, request) {
  var attrs = JSON.parse(request.requestBody);
  var record = db.contacts.insert(attrs);
  
  return {
    contact: record
  };
});

PUT shorthands

Update a resource

Shorthand
this.put('/contacts');          // finds type by singularizing url
this.put('/contacts', 'user');  // optionally specify the type as second param
Expanded
this.put('/contacts/:id', function(db, request) {
  var id = request.params.id;
  var attrs = JSON.parse(request.requestBody)['contact'];
  var record = db.contacts.update(id, attrs);

  return {
    contact: record
  };
});

DELETE shorthands

Remove a resource

Shorthand
this.del('/contacts/:id');          // finds type by singularizing url
this.del('/contacts/:id', 'user');  // optionally specify the type as second param
Expanded
this.put('/contacts/:id', function(db, request) {
  var id = request.params.id;
  db.contacts.remove(id);

  return {};
});

Remove a resource and related models

Shorthand
this.del('/contacts/:id', ['contact', 'addresses']);
Expanded
this.put('/contacts/:id', function(db, request) {
  var id = request.params.id;
  db.contacts.remove(id);

  var addresses = db.addresses.where({ contact_id: id });
  addresses.forEach(function(address) {
    db.addresses.remove(address.id);
  });

  return {};
});