/meteor-publish-with-relations

Meteor.js SmartPackage to publish associated collections at once.

Primary LanguageCoffeeScript

BREAKING CHANGES: This package does not automatically call the @ready() function anymore, that makes it easier to set up more than one relationships method.

This package is an updated version of tmeasday:publish-with-relations the key difference is support for arrays, nested arrays, a friendlier interface, and some bug fixes

API

this.relations(ops) (SERVER SIDE)

Used inside a Meteor.publish() function to define db relationships.

  • ops.collection: (REQUIRED)
    The anchor collection from which relations will be made.

  • ops.filter: (OPTIONAL)
    The object that filters the collection. This is the equivalent to filter in collection.find(filter).

  • ops.options: (OPTIONAL)
    The object that sorts and limits the collection. This is the equivalent to options in collection.find(filter,options).

  • ops.mappings: (OPTIONAL)
    An array of objects that maps relationships between collections using foreign_key and key

  • ops.mappings[].collection: (REQUIRED)
    Defines the collection that will be associated.

  • _ops.mappings[].foreign_key: (DEFAULT:"id")
    Defines the key to associate with at the parent collection.

  • _ops.mappings[].key: (DEFAULT:"id")
    Defines the key to associate with at the current collection.

  • ops.mappings[].filter: (OPTIONAL)
    The object that filters the collection. This is the equivalent to filter in collection.find(filter).

  • ops.mappings[].options: (OPTIONAL)
    The object that sorts and limits the collection. This is the equivalent to options in collection.find(filter,options).

Sample

Meteor.publish "things", ->
	if @userId #only publish when there's a user
		@relations
			collection:Things
			mappings:[
				{
					foreign_key:"sub_things.deep_things.deep_thing"
					collection:DeepThings
				}
				{
					foreign_key:"sub_things.sub_thing"
					collection:SubThings
				}
				{
					foreign_key:"other_thing"
					collection:OtherThings
				}
				{
					key:"thing"
					collection:ReverseThings
				}
			]

	# always call ready
	@ready()

This will publish all Things and their respective DeepThings, SubThings, OtherThings, and ReverseThings

IMPORTANT: When an association is broken, the package will stop all subscriptions to all broken associations but will not remove the association from the client. This means updates to the object with the broken association will not be recorded BUT the object will persist on the client. New associations will be published as expected. (This should not have an impact unless you are doing something with total published counts).