index.js
function createRow(collection, id, row, callback) {
	injectFKs(row, settings.knownFK[collection]);

	// build up list of all sub-document _ref's that we need to find in the newly saved document
	// this is to ensure we capture _id from inside nested array documents that do not exist at root level
	var refsMeta = [];
	traverse(row).forEach(function (value) {
		var path;
		if ('_ref' === this.key) {
			path = this.path.concat();
			path[path.length - 1] = '_id';
			refsMeta.push({
				ref: value,
				path: path
			});
		}
	});

	row = _.omit(row, settings.omitFields);

	settings.getCollection(collection).create(row, function(err, newItem) {
		if (err) return callback(err);

		var newItemAsObject = newItem.toObject();
		for(var i = 0; i < refsMeta.length; i++) {
			if (traverse(newItemAsObject).has(refsMeta[i].path)) {
				settings.refs[refsMeta[i].ref] = traverse(newItemAsObject).get(refsMeta[i].path).toString();
			}
		}

		if (id) { // This unit has its own reference - add it to the stack
			settings.refs[id] = newItem._id.toString();
		}

		if (!settings.progress.created[collection])
			settings.progress.created[collection] = 0;
		settings.progress.created[collection]++;
		callback(null, newItem._id);
	});
}
function createRow(collection, id, row, callback) {
	injectFKs(row, settings.knownFK[collection]);

	// build up list of all sub-document _ref's that we need to find in the newly saved document
	// this is to ensure we capture _id from inside nested array documents that do not exist at root level
	var refsMeta = [];
	traverse(row).forEach(function (value) {
		var path;
		if ('_ref' === this.key) {
			path = this.path.concat();
			path[path.length - 1] = '_id';
			refsMeta.push({
				ref: value,
				path: path
			});
		}
	});

	row = _.omit(row, settings.omitFields);

	settings.getCollection(collection).create(row, function(err, newItem) {
		if (err) return callback(err);

		var newItemAsObject = newItem.toObject();
		for(var i = 0; i < refsMeta.length; i++) {
			if (traverse(newItemAsObject).has(refsMeta[i].path)) {
				settings.refs[refsMeta[i].ref] = traverse(newItemAsObject).get(refsMeta[i].path).toString();
			}
		}

		if (id) { // This unit has its own reference - add it to the stack
			settings.refs[id] = newItem._id.toString();
		}

		if (!settings.progress.created[collection])
			settings.progress.created[collection] = 0;
		settings.progress.created[collection]++;
		callback(null, newItem._id);
	});
}
function createRow ( collection , id , row , callback ) { injectFKs ( row , settings . knownFK [ collection ] ) ; // build up list of all sub-document _ref's that we need to find in the newly saved document // this is to ensure we capture _id from inside nested array documents that do not exist at root level var refsMeta = [ ] ; traverse ( row ) . forEach ( function ( value ) { var path ; if ( '_ref' === this . key ) { path = this . path . concat ( ) ; path [ path . length - 1 ] = '_id' ; refsMeta . push ( { ref : value , path : path } ) ; } } ) ; row = _ . omit ( row , settings . omitFields ) ; settings . getCollection ( collection ) . create ( row , function ( err , newItem ) { if ( err ) return callback ( err ) ; var newItemAsObject = newItem . toObject ( ) ; for ( var i = 0 ; i < refsMeta . length ; i ++ ) { if ( traverse ( newItemAsObject ) . has ( refsMeta [ i ] . path ) ) { settings . refs [ refsMeta [ i ] . ref ] = traverse ( newItemAsObject ) . get ( refsMeta [ i ] . path ) . toString ( ) ; } } if ( id ) { // This unit has its own reference - add it to the stack settings . refs [ id ] = newItem . _id . toString ( ) ; } if ( ! settings . progress . created [ collection ] ) settings . progress . created [ collection ] = 0 ; settings . progress . created [ collection ] ++ ; callback ( null , newItem . _id ) ; } ) ; }
Create a single row in a collection
@param {string} collection The collection where to create the row
@param {string} id The ID of the row (if any)
@param {Object} row The (flattened) row contents to create
@param {function} callback(err) Callback to chainable async function
Create a single row in a collection