Guest User

Untitled

a guest
Jan 23rd, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. const options = { discriminatorKey: 'kind' };
  2. const Event = mongoose.model('Event, new Schema({
  3. name: { type: String }
  4. }, options);
  5.  
  6. const ClickEvent = Event.discriminator('ClickEvent', new Schema({
  7. url: { type: String }
  8. }, options);
  9.  
  10. // on another file
  11. const ClickEvent = mongoose.model('ClickEvent');
  12. const clickEvent = new ClickEvent({
  13. name: 'sir',
  14. url: 'http://somewhere.com/hello'
  15. });
  16. console.log(clickEvent); // { name: 'sir', url: 'http://somewhere.com/hello', kind: 'ClickEvent' }
  17. // look carefully kind is set to ClickEvent
  18.  
  19. const ClickEvent = Event.discriminator('ClickEvent', new Schema({
  20. url: { type: String }
  21. }, { ...options, discriminatorValue: 'click' });
Add Comment
Please, Sign In to add comment