Page Contents

In addition to querying data directly through models using LoopBack APIs, nested models can be API endpoints. To do this, you must use nestRemoting functions.

The easiest way to understand nested queries is through an example.

Suppose an app has book, chapter, page, and image models.  And:

  • Each book can have many pages and chapters.
  • Each chapter and page can have many notes.
  • Each book has an image.

The following blocks of JSON show the model for Book and relations for Page and Chapter models.

For more information on the model relations used, see BelongsTo relations and HasMany relations.

common/models/book.json

{
  "name": "Book",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "name": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "pages": {
      "type": "hasMany",
      "model": "Page",
      "foreignKey": "",
      "through": ""
    },
    "chapters": {
      "type": "hasMany",
      "model": "Chapter",
      "foreignKey": "",
      "through": ""
    }
  },
  "acls": [],
  "methods": {}
}

common/models/chapter.json

{
  "name": "Chapter",
  ...
  "relations": {
    "notes": {
      "type": "hasMany",
      "model": "Note",
      "foreignKey": "",
      "through": ""
    }
  },
  ...
}

common/models/page.json

{
  "name": "Page",
  ...
  "relations": {
    "notes": {
      "type": "hasMany",
      "model": "Note",
      "foreignKey": "",
      "through": ""
    }
  },
  ...
}

common/models/image.json

{
  "name": "Image",
  ...
  "relations": {
    "book": {
      "type": "belongsTo",
      "model": "Book",
      "foreignKey": "",
      "required": true
    }
  },
  ...
}

You can query pages of a specific book via regular relationships,as illustrated with the following API endpoints:

Endpoint Output Description
/api/books/123/pages An array of pages data Queries pages of a specific book
/api/books/123/pages/456 An object of a page data Queries a page data of a specific page under a specific book

However, to query nested models more in depth and have them as API endpoints you need to use the model nestRemoting() function:

Book.nestRemoting('pages');
Book.nestRemoting('chapters');
Image.nestRemoting('book');

The above code enables the following nested queries:

Endpoint Output Description
/api/books/123/pages/456/notes An array of notes objects Queries all of the notes associated with a specific page under a specific book
/api/books/123/pages/456/notes/567 An object of a note data Queries a specific note associated with a specific page under a specific book

Alternatively, since an image belongs to book instance; you can query their pages through their images:

Endpoint Output Description
/api/images/345/book/pages An array of pages of a book Queries all of the pages of the book, whose associated image id is 345
/api/images/345/book/pages/456 An object of a page data Queries page with the id of 456 under the book, whose associated image id is 345