Guest User

Untitled

a guest
Jan 27th, 2016
14
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // PARTITION MODEL (With Partition AND Field schemas)
  2. // src/models/partition.js
  3. 'use strict'
  4.  
  5. module.exports = Mongoose => {
  6.     // Get all the models
  7.     const { Asset, Group, Role, Account } = Mongoose.models
  8.  
  9.     const Schema = Mongoose.Schema
  10.  
  11.     const fieldSchema = new Schema({
  12.         name: {
  13.             type: Schema.Types.String,
  14.             required: true,
  15.             minlength: 3,
  16.             unique: true,
  17.             maxlength: 25,
  18.             trim: true,
  19.             select: true
  20.         }
  21.     })
  22.  
  23.     const partitionSchema = new Schema({
  24.         name: {
  25.             type: Schema.Types.String
  26.         },
  27.         fields: [ fieldSchema ]
  28.     })
  29.  
  30.     return Mongoose.model( 'Partition', partitionSchema )
  31. }
  32.  
  33.  
  34. // ASSET MODEL
  35. // src/models/asset.js
  36. 'use strict'
  37.  
  38. module.exports = Mongoose => {
  39.     // Get all the models
  40.     const { Group, Role, Account, Partition } = Mongoose.models
  41.  
  42.     const Schema = Mongoose.Schema
  43.  
  44.     const modelSchema = new Schema({
  45.         attributes: [{
  46.             _field: {
  47.                 type: Schema.Types.ObjectId,
  48.                 ref: 'Partition.field' // <-- THIS LINE
  49.             },
  50.             value: {
  51.                 type: Schema.Types.Mixed,
  52.                 required: true
  53.             }
  54.         }]
  55.     })
  56.  
  57.     return Mongoose.model( 'Asset', modelSchema )
  58. }
Add Comment
Please, Sign In to add comment