SHARE
TWEET

Untitled

a guest Feb 17th, 2017 61 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.   "name": "Foo",
  3.   "id": 1,
  4.   ...
  5.   "team": {
  6.     "name": "MGMT",
  7.     "id": 1,
  8.     "department": 1
  9.     ...
  10.   },
  11.   "department": {
  12.     "name": "Top",
  13.     "id": 1,
  14.     "office": 1
  15.     ...
  16.   }
  17. }
  18.    
  19. // A person that belongs to a team
  20. module.exports = {
  21.  
  22.   attributes: {
  23.     name: {
  24.       type: 'string',
  25.       required: true
  26.     },
  27.  
  28.     //Assosiations    
  29.     team: {
  30.       model: 'team'
  31.     },
  32.     department: {
  33.       model: 'department',
  34.       via: 'team.department'
  35.     },
  36.   }
  37. };
  38.    
  39. // A team with many persons and belongs to one department
  40. module.exports = {
  41.  
  42.   attributes: {
  43.     name: {
  44.       type: 'string',
  45.       required: true
  46.     },
  47.  
  48.     //Associations
  49.     department: {
  50.       model: 'department'
  51.     },
  52.  
  53.     members: {
  54.       collection: 'person',
  55.       via: 'team'
  56.     }
  57.   }
  58. };
  59.    
  60. // A department that has many teams
  61. module.exports = {
  62.  
  63.   attributes: {
  64.     name: {
  65.       type: 'string',
  66.       required: true
  67.     },
  68.  
  69.     teams: {
  70.       collection: 'team',
  71.       via: 'department'
  72.     }
  73.   }
  74. };
  75.    
  76. function (req, res) {
  77.  
  78.         Person.findById(1).exec(function (err, people) {
  79.             Team.findById(people[0].team).exec(function (err, teams) {
  80.                 Department.findById(teams[0].department).exec(function (err, departments) {
  81.                     Office.findById(departments[0].office).exec(function (err, offices) {
  82.                         Company.findById(offices[0].company).exec(function (err, companies) {
  83.  
  84.                             var composeRecord = Object.assign(
  85.                                 people[0], {
  86.                                     team: teams[0],
  87.                                     department: departments[0],
  88.                                     office: offices[0],
  89.                                     company: companies[0],
  90.                                 });
  91.  
  92.                             res.send(composeRecord);
  93.  
  94.                         })
  95.                     })
  96.                 })
  97.             })
  98.         })  
  99.     }
RAW Paste Data
Top