Skip to content

Commit

Permalink
Revising parameters of join() to support two WHERE clause descrip…
Browse files Browse the repository at this point in the history
…tors, adding a test
  • Loading branch information
avoidwork committed Oct 24, 2015
1 parent f5370b2 commit 93959e2
Show file tree
Hide file tree
Showing 9 changed files with 142 additions and 141 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -489,11 +489,12 @@ var store = haro();
store.get('keyValue');
```
**join(other, on[, type="inner"])**
**join(other, on[, type="inner", where=[]])**
_Array_
Joins `this` instance of `Haro` with another, on a field/property. Supports "inner", "left", & "right" joins. Resulting
composite records implement a `storeId_field` convention for fields/properties.
Joins `this` instance of `Haro` with another, on a field/property. Supports "inner", "left", & "right" JOINs. Resulting
composite records implement a `storeId_field` convention for fields/properties. The optional forth parameter is an Array
which can be used for WHERE clauses, similar to `find()`, `[store1, store2]`.
```javascript
var store1 = haro([{id: "abc", name: "jason", age: 35}, {id: "def", name: "jen", age: 31}], {id: 'users', key: 'id', index: ['name', 'age']});
Expand Down
2 changes: 1 addition & 1 deletion bower.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "haro",
"version": "1.8.0",
"version": "1.9.2",
"homepage": "http://haro.rocks",
"authors": [
"Jason Mulligan <jason.mulligan@avoidwork.com>"
Expand Down
46 changes: 20 additions & 26 deletions lib/haro.es6.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* @copyright 2015
* @license BSD-3-Clause
* @link http://haro.rocks
* @version 1.9.1
* @version 1.9.2
*/
"use strict";

Expand Down Expand Up @@ -607,34 +607,28 @@ class Haro {
return this.data.has(key);
}

join (other, on = this.key, type = "inner", where = {}) {
let defer = deferred();
join (other, on = this.key, type = "inner", where = []) {
let defer = deferred(),
promise;

if (other.total > 0) {
if (Object.keys(where).length > 0) {
Promise.all([
this.find(where, true),
other.find(where, true)
]).then(data => {
return data[0].length > 0 && data[1].length > 1 ? this.offload([[this.id, other.id], data[0], data[1], this.key, on, type], "join") : [];
}, function (e) {
throw e;
}).then(function (result) {
if (typeof result === "string") {
defer.reject(new Error(result));
} else {
defer.resolve(result);
}
}, defer.reject);
if (where.length > 0) {
if (!where[1]) {
promise = this.offload([[this.id, other.id], this.find(where[0], true), other.toArray(null, true), this.key, on, type], "join");
} else {
promise = this.offload([[this.id, other.id], this.find(where, true), other.find(where, true), this.key, on, type], "join");
}
} else {
this.offload([[this.id, other.id], this.toArray(null, true), other.toArray(null, true), this.key, on, type], "join").then(function (result) {
if (typeof result === "string") {
defer.reject(new Error(result));
} else {
defer.resolve(result);
}
}, defer.reject);
promise = this.offload([[this.id, other.id], this.toArray(null, true), other.toArray(null, true), this.key, on, type], "join");
}

promise.then(function (result) {
if (typeof result === "string") {
defer.reject(new Error(result));
} else {
defer.resolve(result);
}
}, defer.reject);
} else {
defer.resolve([]);
}
Expand Down Expand Up @@ -1272,7 +1266,7 @@ function factory (data = null, config = {}, indexes = []) {
}

factory.transform = cast;
factory.version = "1.9.1";
factory.version = "1.9.2";

// Node, AMD & window supported
if (typeof exports !== "undefined") {
Expand Down
Loading

0 comments on commit 93959e2

Please sign in to comment.