Comparing version 0.6.3 to 0.6.4
@@ -160,2 +160,3 @@ 'use strict'; | ||
var joinObj = pipeObj.value; | ||
var options = pipeObj.args[0] || {}; | ||
var isObj = !_checkTypes2.default.array(docs); | ||
@@ -193,3 +194,3 @@ docs = !isObj ? docs : [docs]; | ||
docsById[d._id].isArray = !singleJoin; | ||
d[joinKey] = null; | ||
d[joinKey] = singleJoin ? null : []; | ||
@@ -203,3 +204,4 @@ (0, _forEach2.default)(joinIds, function (joinId) { | ||
return model.find({ _id: { $in: allIds } }).observe(function (res) { | ||
var execFnName = options.observe ? 'observe' : 'then'; | ||
return model.find({ _id: { $in: allIds } })[execFnName](function (res) { | ||
(0, _forEach2.default)(res, function (objToJoin) { | ||
@@ -210,9 +212,7 @@ var docIdsForJoin = childToRootMap[objToJoin._id]; | ||
if (doc) { | ||
var joinedVal = doc.d[joinKey] || (doc.isArray ? [] : null); | ||
if (doc.isArray) { | ||
joinedVal.push(objToJoin); | ||
doc.d[joinKey].push(objToJoin); | ||
} else { | ||
joinedVal = objToJoin; | ||
doc.d[joinKey] = objToJoin; | ||
} | ||
doc.d[joinKey] = joinedVal; | ||
} | ||
@@ -349,5 +349,7 @@ }); | ||
value: function join(joinFn) { | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
(0, _invariant2.default)(typeof joinFn === 'function' || _checkTypes2.default.object(joinFn), 'join(...): argument must be a function'); | ||
this._addPipeline(PIPELINE_TYPE.Join, joinFn); | ||
this._addPipeline(PIPELINE_TYPE.Join, joinFn, options); | ||
return this; | ||
@@ -374,5 +376,7 @@ } | ||
value: function joinObj(obj) { | ||
var options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1]; | ||
(0, _invariant2.default)(_checkTypes2.default.object(obj), 'joinObj(...): argument must be an object'); | ||
this._addPipeline(PIPELINE_TYPE.JoinObj, obj); | ||
this._addPipeline(PIPELINE_TYPE.JoinObj, obj, options); | ||
return this; | ||
@@ -379,0 +383,0 @@ } |
@@ -18,2 +18,6 @@ 'use strict'; | ||
var _forEach = require('fast.js/forEach'); | ||
var _forEach2 = _interopRequireDefault(_forEach); | ||
var _filter2 = require('fast.js/array/filter'); | ||
@@ -27,4 +31,2 @@ | ||
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } } | ||
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } | ||
@@ -120,3 +122,11 @@ | ||
var uniqIds = [].concat(_toConsumableArray(new Set(ids))); | ||
var usedIds = {}; | ||
var uniqIds = []; | ||
(0, _forEach2.default)(ids, function (id) { | ||
if (!usedIds[id]) { | ||
usedIds[id] = true; | ||
uniqIds.push(id); | ||
} | ||
}); | ||
var retrPromises = (0, _map3.default)(uniqIds, function (id) { | ||
@@ -123,0 +133,0 @@ return _this2.retriveOne(id); |
@@ -96,2 +96,3 @@ import _bind from 'fast.js/function/bind'; | ||
const joinObj = pipeObj.value; | ||
const options = pipeObj.args[0] || {}; | ||
const isObj = !_check.array(docs); | ||
@@ -124,3 +125,3 @@ docs = !isObj ? docs : [docs]; | ||
docsById[d._id].isArray = !singleJoin; | ||
d[joinKey] = null; | ||
d[joinKey] = singleJoin ? null : []; | ||
@@ -134,3 +135,4 @@ _each(joinIds, joinId => { | ||
return model.find({_id: {$in: allIds}}).observe(res => { | ||
const execFnName = options.observe ? 'observe' : 'then'; | ||
return model.find({_id: {$in: allIds}})[execFnName](res => { | ||
_each(res, objToJoin => { | ||
@@ -141,9 +143,7 @@ const docIdsForJoin = childToRootMap[objToJoin._id]; | ||
if (doc) { | ||
let joinedVal = doc.d[joinKey] || (doc.isArray ? [] : null); | ||
if (doc.isArray) { | ||
joinedVal.push(objToJoin); | ||
doc.d[joinKey].push(objToJoin); | ||
} else { | ||
joinedVal = objToJoin; | ||
doc.d[joinKey] = objToJoin; | ||
} | ||
doc.d[joinKey] = joinedVal; | ||
} | ||
@@ -286,3 +286,3 @@ }); | ||
join(joinFn) { | ||
join(joinFn, options = {}) { | ||
invariant( | ||
@@ -293,3 +293,3 @@ typeof joinFn === 'function' || _check.object(joinFn), | ||
this._addPipeline(PIPELINE_TYPE.Join, joinFn); | ||
this._addPipeline(PIPELINE_TYPE.Join, joinFn, options); | ||
return this; | ||
@@ -318,3 +318,3 @@ } | ||
joinObj(obj) { | ||
joinObj(obj, options = {}) { | ||
invariant( | ||
@@ -325,3 +325,3 @@ _check.object(obj), | ||
this._addPipeline(PIPELINE_TYPE.JoinObj, obj); | ||
this._addPipeline(PIPELINE_TYPE.JoinObj, obj, options); | ||
return this; | ||
@@ -328,0 +328,0 @@ } |
import _check from 'check-types'; | ||
import _map from 'fast.js/map'; | ||
import _each from 'fast.js/forEach'; | ||
import _filter from 'fast.js/array/filter'; | ||
@@ -83,3 +84,11 @@ import {selectorIsId, selectorIsIdPerhapsAsObject} from './Document'; | ||
retriveIds(ids) { | ||
const uniqIds = [...(new Set(ids))]; | ||
const usedIds = {}; | ||
const uniqIds = []; | ||
_each(ids, id => { | ||
if (!usedIds[id]) { | ||
usedIds[id] = true; | ||
uniqIds.push(id); | ||
} | ||
}); | ||
const retrPromises = _map(uniqIds, id => this.retriveOne(id)); | ||
@@ -86,0 +95,0 @@ return Promise.all(retrPromises).then((docs) => ( |
{ | ||
"name": "marsdb", | ||
"version": "0.6.3", | ||
"version": "0.6.4", | ||
"author": { | ||
@@ -5,0 +5,0 @@ "name": "Artem Artemev", |
@@ -461,3 +461,3 @@ import Collection from '../../lib/Collection'; | ||
res[5].j[1]._id.should.be.equal('7'); | ||
expect(res[6].j).to.be.null; | ||
expect(res[6].j).to.be.an('array'); | ||
}); | ||
@@ -491,2 +491,32 @@ }); | ||
}); | ||
it('should observe joined objects', function (done) { | ||
return db.findOne('1').joinObj({ j: db }, { observe: true }).then(res => { | ||
res.j._id.should.be.equal('2'); | ||
res.j.b.should.be.equal(2); | ||
return db.update('2', {$set: {b: 22}}).then(() => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
res.j.b.should.be.equal(22); | ||
done(); | ||
}, 100); | ||
}) | ||
}); | ||
}) | ||
}); | ||
it('should NOT observe joins by default', function (done) { | ||
return db.findOne('1').joinObj({ j: db }).then(res => { | ||
res.j._id.should.be.equal('2'); | ||
res.j.b.should.be.equal(2); | ||
return db.update('2', {$set: {b: 22}}).then(() => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
res.j.b.should.be.equal(2); | ||
done(); | ||
}, 100); | ||
}) | ||
}); | ||
}) | ||
}); | ||
}); | ||
@@ -493,0 +523,0 @@ |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
993352
22879