MCPcopy Index your code
hub / github.com/dresende/node-orm2 / addChainMethod

Function addChainMethod

lib/ChainFind.js:294–331  ·  view source on GitHub ↗
(chain, association, opts)

Source from the content-addressed store, hash-verified

292}
293
294function addChainMethod(chain, association, opts) {
295 chain[association.hasAccessor] = function (value) {
296 if (!opts.exists) {
297 opts.exists = [];
298 }
299 var conditions = {};
300
301 var assocIds = Object.keys(association.mergeAssocId);
302 var ids = association.model.id;
303 function mergeConditions(source) {
304 for (var i = 0; i < assocIds.length; i++) {
305 if (typeof conditions[assocIds[i]] === "undefined") {
306 conditions[assocIds[i]] = source[ids[i]];
307 } else if (Array.isArray(conditions[assocIds[i]])) {
308 conditions[assocIds[i]].push(source[ids[i]]);
309 } else {
310 conditions[assocIds[i]] = [ conditions[assocIds[i]], source[ids[i]] ];
311 }
312 }
313 }
314
315 if (Array.isArray(value)) {
316 for (var i = 0; i < value.length; i++) {
317 mergeConditions(value[i]);
318 }
319 } else {
320 mergeConditions(value);
321 }
322
323 opts.exists.push({
324 table : association.mergeTable,
325 link : [ Object.keys(association.mergeId), association.model.id ],
326 conditions : conditions
327 });
328
329 return chain;
330 };
331}

Callers 1

ChainFindFunction · 0.85

Calls 1

mergeConditionsFunction · 0.85

Tested by

no test coverage detected