0

here i want to create model relations on StrongLoop. I have this kind of scenario:

  1. Category:
    • id:1
    • name:history
  2. Book
    • id:1
    • id_category:1
    • title:american-hustle
    • id_publisher:1
    • --------------------------
    • id:2
    • id_category:1
    • title:american-hustle2
    • id_publisher:2
  3. Publisher
    • id:1
    • name:Publisher-1
    • --------------------------
    • id:2
    • name:Publisher-2

how can i create model relations like that? Thanks.

Gery
  • 573
  • 4
  • 18

1 Answers1

0

You should be able to find what you need at:

http://strongloop.com/strongblog/defining-and-mapping-data-relations-with-loopback-connected-models/

Basically,

// 1-many
Category.hasMany(Book, {as: 'books', foreignKey: 'id_category'});
Publisher.hasMany(Book, {as: 'books', foreignKey: 'id_publisher'});

// belongsTo
Book.belongsTo(Category, {as: 'category', foreignKey: 'id_category'});
Book.belongsTo(Publisher, {as: 'publisher', foreignKey: 'id_publisher'}); 

Depending on how you want to navigate the model relations, you can define one direction or both.

Raymond Feng
  • 1,516
  • 9
  • 5