index.js
function determineFKs(row, fks) {
	var refs = [];

	_.forEach(fks, function(fk, id) {
		if (row[id] === undefined) return; // Skip omitted FK refs

		switch (fk.type) {
			case FK_OBJECTID: // 1:1 relationship
				refs.push(row[id]);
				break;
			case FK_OBJECTID_ARRAY: // 1:M array based relationship
				_.forEach(row[id], function(v) {
					refs.push(v);
				});
				break;
			case FK_SUBDOC: // Mongo subdocument
				_.forEach(row[id], function(v) {
					determineFKs(v, fks[id].fks).forEach(function(dep) {
						refs.push(dep);
					});
				});
				break;
		}
	});

	return refs;
}
function determineFKs(row, fks) {
	var refs = [];

	_.forEach(fks, function(fk, id) {
		if (row[id] === undefined) return; // Skip omitted FK refs

		switch (fk.type) {
			case FK_OBJECTID: // 1:1 relationship
				refs.push(row[id]);
				break;
			case FK_OBJECTID_ARRAY: // 1:M array based relationship
				_.forEach(row[id], function(v) {
					refs.push(v);
				});
				break;
			case FK_SUBDOC: // Mongo subdocument
				_.forEach(row[id], function(v) {
					determineFKs(v, fks[id].fks).forEach(function(dep) {
						refs.push(dep);
					});
				});
				break;
		}
	});

	return refs;
}
function determineFKs ( row , fks ) { var refs = [ ] ; _ . forEach ( fks , function ( fk , id ) { if ( row [ id ] === undefined ) return ; // Skip omitted FK refs switch ( fk . type ) { case FK_OBJECTID : // 1:1 relationship refs . push ( row [ id ] ) ; break ; case FK_OBJECTID_ARRAY : // 1:M array based relationship _ . forEach ( row [ id ] , function ( v ) { refs . push ( v ) ; } ) ; break ; case FK_SUBDOC : // Mongo subdocument _ . forEach ( row [ id ] , function ( v ) { determineFKs ( v , fks [ id ] . fks ) . forEach ( function ( dep ) { refs . push ( dep ) ; } ) ; } ) ; break ; } } ) ; return refs ; }
Get an array of required foreign keys values so we can calculate the dependency tree
@param {Object} row The row that will be inserted
@param {Object} fks The foreign keys for the given row (extacted via extractFKs)
@see extractFKs()
@return {array} An array of required references
Get an array of required foreign keys values so we can calculate the dependency tree