With this package included, you can define joins between collections. Collection.find
and Collection.findOne
will return data expanded with docs from joined collections. You can also create "generic join" - join one collection with multiple others using the same foreign key.
This package is used by Meteor Kitchen - code generator for Meteor.
We have two collections: Companies & Employees
var Companies = new Mongo.Collection("companies");
var Employees = new Mongo.Collection("employees");
Example company document:
{
_id: "CQKDzmqmQXGhsC6PG",
name: "Acme"
}
Example employee document:
{
_id: "dySSKA25pCtKjo5uA",
name: "Jimi Hendrix",
companyId: "CQKDzmqmQXGhsC6PG"
}
Let's define join (in both server & client scope)
Employees.join(Companies, "companyId", "company", ["name"]);
Or you can pass collection name:
Employees.join("Companies", "companyId", "company", ["name"]);
And at server in publication, instead simply returning cursor, return with Collection.publishJoinedCursors method:
Meteor.publish("employees", function() {
var cursor = Employees.find(); // do what you normally do here
return Employees.publishJoinedCursors(cursor); // instead of simply returning resulting cursor
});
Now, if you do:
Employees.find();
You'l get:
{
_id: "dySSKA25pCtKjo5uA",
name: "Jimi Hendrix",
companyId: "CQKDzmqmQXGhsC6PG",
company: {
name: "Acme"
}
}
Join will be reactive if you pass reactive: true
as option to publishJoinedCursors and publication context as last argument:
Meteor.publish("employees", function() {
var cursor = Employees.find();
return Employees.publishJoinedCursors(cursor, { reactive: true }, this);
});
Let's say we have four collections:
var Companies = new Mongo.Collection("companies");
var Employees = new Mongo.Collection("employees");
var Tags = new Mongo.Collection("tags");
var TaggedDocuments = new Mongo.Collection("tagged_documents");
in "Tags" collection we have list of possible tags:
{
_id: "wrWrXDqWwPrXCWsgu",
name: "Awesome!"
}
We can tag documents from both "Companies" and "Employees". When document is tagged we are storing three values into "TaggedDocuments" collection:
{
tagId: "wrWrXDqWwPrXCWsgu",
collectionName: "Employees",
docId: "dySSKA25pCtKjo5uA"
},
{
tagId: "wrWrXDqWwPrXCWsgu",
collectionName: "Companies",
docId: "CQKDzmqmQXGhsC6PG"
}
tagId
stores tag id from "Tags" collectioncollectionName
stores name of collection where tagged document belongs todocId
stores id of tagged documentcollectionName can be any existing collection.
Let's define generic join:
TaggedDocuments.genericJoin("collectionName", "docId", "document");
Now, if you do:
TaggedDocuments.find({ tagId: "wrWrXDqWwPrXCWsgu" });
You'l get something like this:
{
tagId: "wrWrXDqWwPrXCWsgu",
collectionName: "Employees",
docId: "dySSKA25pCtKjo5uA",
document: {
name: "Jimi Hendrix",
companyId: "CQKDzmqmQXGhsC6PG"
}
},
{
tagId: "wrWrXDqWwPrXCWsgu",
collectionName: "Companies",
docId: "CQKDzmqmQXGhsC6PG",
document: {
name: "Acme"
}
}
Also, you can define simple join to "Tags" collection too:
TaggedDocuments.join(Tags, "tagId", "tag", []);
TaggedDocuments.genericJoin("collectionName", "docId", "document");
And now if you do:
TaggedDocuments.find({ tagId: "wrWrXDqWwPrXCWsgu" });
You'l get:
{
tagId: "wrWrXDqWwPrXCWsgu",
tag: {
name: "Awesome!"
},
collectionName: "Employees",
docId: "dySSKA25pCtKjo5uA",
document: {
name: "Jimi Hendrix",
companyId: "CQKDzmqmQXGhsC6PG"
}
},
{
tagId: "wrWrXDqWwPrXCWsgu",
tag: {
name: "Awesome!"
},
collectionName: "Companies",
docId: "CQKDzmqmQXGhsC6PG",
document: {
name: "Acme"
}
}
voilà - we have generic N:M join!
Collection.join(collection, foreignKey, containerField, fieldList)
collection
Mongo.Collection object (or collection name) to joinforeignKey
field name where foreign document _id is stored (in our example: "companyId"
)containerField
field name where to store foreign document (in our example: "company"
)fieldList
array of field names we want to get from foreign collection (in our example array with one field ["name"]
)Use this function in scope visible both to client and server.
Collection.genericJoin(collectionNameField, foreignKey, containerField)
collectionNameField
field name (from this collection) in which foreign collection name is storedforeignKey
field name where foreign document _id is storedcontainerField
field name where to store joined foreign documentFor use server side in publications: instead of simply returning result from collection, we want to return cursors with data from joined collections too. This function will query joined collections and will return array of cursors.
Collection.publishJoinedCursors(cursor, options, publicationContext)
cursor
cursor that you normally return from publish functionoptions
options object, currently only one option exists: { reactive: true }
publicationContext
publish's this
(only if you want it reactive)Example publish function:
Meteor.publish("employees", function() {
var cursor = Employees.find(); // do what you normally do here
return Employees.publishJoinedCursors(cursor); // instead of simply returning resulting cursor
});
With queried employees, cursor with companies filtered by employee.companyId will be returned too.
If you want it reactive:
Meteor.publish("employees", function() {
var cursor = Employees.find(); // do what you normally do here
return Employees.publishJoinedCursors(cursor, { reactive: true }, this);
});
reactive: true
and auditargument-checks
packageYour publication will be called with one extra argument (internally used by package). That's OK unless you are using audit-argument-checks
which forces you to check()
all arguments passed to publication. In that case, you need to check that extra argument:
Meteor.publish("publicationName", function(arg1, extraArgument) {
check(arg1, ...); // check your arguments as you normally do ...
check(extraArgument, Match.Any); // ... but don't forget to check extraArgument
});
Issue related to audit-argument-checks
package is added to README.md
Fixed bug with Random
If joined doc is not found, set container to empty object instead null
Improved reactive joins
Fixed bug with reactive joins and removed super-dirty trick
Updated README.md
Fixed bugs related to reactive joins
Fixed bugs related to reactive joins
Updated README.md
Join is now reactive (using super ugly & dirty tricks)
Unsuccessfully tried to add reactivity (update details when master document changes)
a.b.c
Now you can pass collection name as first argument to join
function.
Added generic joins.
Thanks to Robert Moggach.
That's it :)