A Pithy Post on Power Joins with ku4collections

It’s a fact, you can join collections with ku4collections! If you were unaware of this, you can read more about it in a past article. For those of you that did know this, did you know that there is more than one way to join collections? Well, there is. There is the more familiar way, where you join two collections on keys where the value of said keys are equal across both collections. There is also another way, where we join based upon the return value of a passed function that takes two collection item arguments and returns a boolean value representing the resultant veracity as to whether the items should be joined and included in the result set. Let’s take a look at both of these now.

First thing’s first, we need some data. Let’s use a common example, employees and departments.

var departments = $.ku4collection("departments").init([
 {
  id: 1,
  name: "Marketing"
 },
 {
  id: 2,
  name: "Finance"
 },
 {
  id: 3,
  name: "IT"
 },
 {
  id: 4,
  name: "Sales"
 }
]).save();
var employees = $.ku4collection("employees").init([
 {
  id: 1,
  firstName: "John",
  lastName: "Erickson"
 },
 {
  id: 2,
  department: 1,
  firstName: "Tammy",
  lastName: "Jean",
  title: "Manager"
 },
 {
  id: 3,
  department: 2,
  firstName: "Pearl",
  lastName: "Umber",
  title: "Accountant"
 },
 {
  id: 4,
  department: 3,
  firstName: "Matt",
  lastName: "McMasterson",
  title: "ku4Collection Guru"
 },
 {
  id: 5,
  department: 4,
  firstName: "Umi",
  lastName: "Yoniki",
  title: "Lead"
 },
 {
  id: 6,
  department: 4,
  firstName: "Earl",
  lastName: "Mills",
  title: "Account Manager"
 }
]);

Now that we have our data, lets’s take a quick look at the two different joins.

First, the familiar join by passing two keys whose values should be equal in order for them to be included in the results set.

var employeesByDepartment = employees.join(departments, "department", "id");
employeesByDepartment.find();

collectionblog1

As you can see, with this join we are missing an employee, and rightfully so. This is because one of the employees, John, does not have a department, and, therefore, the corresponding record cannot be included in the result set. This type of join is similar to an inner join in SQL.

Second, we have the join on a function. This opens the possibility for all kinds of crazy joins, but for this example, and the sake of brevity, we will just look at the ku4collection equivalent to a cross join in SQL.

var employeesWithDepartment = employees.join(departments, function(a, b) { return true; });
employeesWithDepartment.find();

collectionblog2

By returning true for every combination of records, we are joining and including every possible combination of records in our result set, and, therefore, we are getting the Cartesian product of our collections.

So, there you go. Quick and easy, to the point post. Now go start joining collections!

Please, comment on and share this post with others if you enjoy it; follow @kodmunki on Twitter for regular updates on new developments, deployments, articles, and tools; and check out kodmunki™ on github for cool, managed, and tested kodmunki™ projects.

Thank you for reading, and as always, happy coding :{)}

Advertisements

Software development leaders at the forefront of the latest in technology. Whether implementing updates or integrating with existing technology; developing products that push the bleeding edge of the latest in tech; or developing open-source products, paving the way of future tech, kodmunki™ inspires innovation, elevates quality, and drives value to production. kodmunki™ are experts in web, mobile, and hybrid solutions development; local and distributed team management and collaboration; and fast, quality, successful product delivery, offering R&D, training, consulting, and development services. Contact us at info@kodmunki.com. Let's discuss your vision.

Tagged with: , , , , , , , ,
Posted in JavaScript, ku4js-data
One comment on “A Pithy Post on Power Joins with ku4collections
  1. […] A Pithy Post on Power Joins with ku4collections […]

Enjoy the read? -- Let us know.

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

kodmunki™ Tweets

Enter your email address and receive notifications of new kodmunki™ posts by email.

%d bloggers like this: