2

I'm using SailsJS / Waterline ORM with a Mongo DB. It's a realtime application so the sails sockets.io pub/sub is used heavily.

I have a few instances in my application which require many-to-many and many-to-one associations.

I'm fairly sure I can achieve many-to-* relationships with embedded collections, however waterline associations seems a little cleaner (eg. Access to publishAdd() for example).

Question is: what's best practice (for my current stack) for connecting/linking data (embedded or associated)? Is there a performance hit with associated records due to the additional queries?

Travis Webb
  • 14,688
  • 7
  • 55
  • 109
mcnamee
  • 508
  • 1
  • 5
  • 11

1 Answers1

1

Ask yourself these questions:

1. Am I going to just Create/Read these Associated or Embedded collections?

OR

2. Do I require a lot of Update/Delete operations on these collections?

  1. If your answer is more inclined to Q1, then Embedded Collections can be used. They make access faster by querying only one collection at a time. Its also simpler to implement.

BUT

If your answer is more inclined to Q2, then Associations are what you need. There are many benefits to this approach:

  1. Update/Delete operations do not require updating/deleting the embedded document in all collections its embedded in.

  2. Since you are using sockets heavily, Associations will help with notifying changes to the model through sockets in a more organised manner.

  3. Each Embedded Collection will have its own Model, and hence its own structure, which makes code more organised, more easily maintained and more understandable.

Feel free to add your own pointers if you find any more pros and cons in the course of implementation. Good luck!

myusuf
  • 11,810
  • 11
  • 35
  • 50