index.js
function injectFKs(row, fks) {
	_.forEach(fks, function(fk, id) {
		if (!_.has(row, id)) return; // Skip omitted FK refs

		var lookupKey = _.get(row, id);
		switch (fk.type) {
			case FK_OBJECTID: // 1:1 relationship
				if (!settings.refs[lookupKey])
					throw new Error('Attempting to inject non-existant reference "' + row[lookupKey] + '" (from lookup ref "' + lookupKey + '") into field path "' + id + '" before its been created!');
				_.set(row, id,  settings.refs[lookupKey]);
				break;
			case FK_OBJECTID_ARRAY: // 1:M array based relationship
				_.set(row, id, _.map(lookupKey, function(fieldValueArr) { // Resolve each item in the array
					if (!settings.refs[fieldValueArr])
						throw new Error('Attempting to use reference "' + fieldValueArr + '" in 1:M field path "' + id + '" before its been created!');
					return settings.refs[fieldValueArr];
				}));
				break;
			case FK_SUBDOC: // Mongo subdocument
				_.forEach(_.get(row, id), function(subdocItem, subdocIndex) {
					injectFKs(subdocItem, fks[id].fks);
				});
				break;
		}
	});
}
function injectFKs(row, fks) {
	_.forEach(fks, function(fk, id) {
		if (!_.has(row, id)) return; // Skip omitted FK refs

		var lookupKey = _.get(row, id);
		switch (fk.type) {
			case FK_OBJECTID: // 1:1 relationship
				if (!settings.refs[lookupKey])
					throw new Error('Attempting to inject non-existant reference "' + row[lookupKey] + '" (from lookup ref "' + lookupKey + '") into field path "' + id + '" before its been created!');
				_.set(row, id,  settings.refs[lookupKey]);
				break;
			case FK_OBJECTID_ARRAY: // 1:M array based relationship
				_.set(row, id, _.map(lookupKey, function(fieldValueArr) { // Resolve each item in the array
					if (!settings.refs[fieldValueArr])
						throw new Error('Attempting to use reference "' + fieldValueArr + '" in 1:M field path "' + id + '" before its been created!');
					return settings.refs[fieldValueArr];
				}));
				break;
			case FK_SUBDOC: // Mongo subdocument
				_.forEach(_.get(row, id), function(subdocItem, subdocIndex) {
					injectFKs(subdocItem, fks[id].fks);
				});
				break;
		}
	});
}
function injectFKs ( row , fks ) { _ . forEach ( fks , function ( fk , id ) { if ( ! _ . has ( row , id ) ) return ; // Skip omitted FK refs var lookupKey = _ . get ( row , id ) ; switch ( fk . type ) { case FK_OBJECTID : // 1:1 relationship if ( ! settings . refs [ lookupKey ] ) throw new Error ( 'Attempting to inject non-existant reference "' + row [ lookupKey ] + '" (from lookup ref "' + lookupKey + '") into field path "' + id + '" before its been created!' ) ; _ . set ( row , id , settings . refs [ lookupKey ] ) ; break ; case FK_OBJECTID_ARRAY : // 1:M array based relationship _ . set ( row , id , _ . map ( lookupKey , function ( fieldValueArr ) { // Resolve each item in the array if ( ! settings . refs [ fieldValueArr ] ) throw new Error ( 'Attempting to use reference "' + fieldValueArr + '" in 1:M field path "' + id + '" before its been created!' ) ; return settings . refs [ fieldValueArr ] ; } ) ) ; break ; case FK_SUBDOC : // Mongo subdocument _ . forEach ( _ . get ( row , id ) , function ( subdocItem , subdocIndex ) { injectFKs ( subdocItem , fks [ id ] . fks ) ; } ) ; break ; } } ) ; }
Inject foreign keys into a row before it gets passed to Mongo for insert
@param {Object} row The row that will be inserted - values will be replaced inline
@param {Object} fks The foreign keys for the given row (extacted via extractFKs)
@see extractFKs()
Inject foreign keys into a row before it gets passed to Mongo for insert