Advertisement
randomCodes

populate

Oct 16th, 2016
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //pessoas
  2. 'use strict'
  3. const mongoose = require('mongoose');
  4. const Schema   = mongoose.Schema;
  5. let ObjectID = Schema.ObjectId;
  6.  
  7. const schemaStructure  = {
  8.     person : {
  9.         first_name       : { type: String }
  10.         ,last_name       : { type: String }
  11.         ,birthdate       : { type: Date }
  12.         ,document_number : { type: String }
  13.         ,add_company     : { type: Boolean, default: false }
  14.         ,company_code    : { type: ObjectID, ref: 'company'}
  15.         ,is_dealer       : { type: Boolean, default: false }
  16.         ,is_provider     : { type: Boolean, default: false }
  17.     }
  18.     ,contact : {
  19.         phone   : { type: String }
  20.         ,mobile : { type: String }
  21.         ,email  : { type: String }
  22.     }
  23.     ,address : {
  24.         street     : { type: String }
  25.         ,number    : { type: Number }
  26.         ,district  : { type: String }
  27.         ,city      : { type: String }
  28.         ,state     : { type: String }
  29.         ,zip_code  : { type: String }
  30.         ,reference : { type: String }
  31.     }
  32.     ,created_at : { type: Date, default: Date.now }
  33.     ,updated_at : { type: Date }
  34. };
  35. const personSchema = new Schema(schemaStructure);
  36. module.exports = mongoose.model('person', personSchema, 'people');
  37.  
  38.  
  39. //empresas
  40. 'use strict'
  41. const mongoose = require('mongoose');
  42. const Schema   = mongoose.Schema;
  43.  
  44. const schemaStructure  = {
  45.     company : {
  46.         name               : { type: String }
  47.         ,document_number   : { type: Number }
  48.         ,is_provider       : { type: Boolean, default: false }
  49.     }
  50.     ,contact : {
  51.         phone   : { type: String }
  52.         ,email  : { type: String }
  53.     }
  54.     ,address : {
  55.         city       : { type: String }
  56.         ,state     : { type: String }
  57.         ,zip_code  : { type: Number }
  58.     }
  59.     ,created_at : { type: Date, default: Date.now }
  60.     ,updated_at : { type: Date }
  61. };
  62. const company = new Schema(schemaStructure);
  63.  
  64. module.exports = mongoose.model('company', company, 'companies');
  65.  
  66. //rota
  67. router.get('/', (req, res) => {
  68.         Person.find()
  69.             .populate('person.company_code')
  70.             .exec((err, data) => {
  71.                 if(err) return res.json({'Erro': 'GET/'})
  72.                     return res.json(data)
  73.             })
  74.     })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement