lib/predefine.js
function persist(client, next) {
    var single = isModel(item)
      , data;

    switch (method) {
      case 'create':
        data = single ? [ item.clone() ] : item.clone().models;
        client.insert(pluck(data), config, function inserted(error, results) {
          if (error) return next(error);

          //
          // Set the _stored property for each model to true.
          //
          results.forEach(function each(model) {
            if (single) return item.stored = model._id;
            item.id(model._id).stored = model._id;
          });

          next(null, results);
        });
      break;

      case 'read':
        //
        // Models will be read by Model _id if no query was passed or if
        // the Model already exists in the database. This would otherwise
        // result in data ambiguity.
        //
        query = query || {};
        if (single && item.id) query._id = item.id;

        //
        // No query and Model has no ObjectId reference, return early.
        //
        if (single && !Object.keys(query).length) return next(null, null);

        //
        // Read the documents from the MongoDB database and process
        // the results, depending on the item being a Model or Collection.
        //
        client.find(query, config).toArray(function found(error, results) {
          if (error) return next(error);
          if (single) results = results[0];

          //
          // If the item is a model, parse first. After merge the results
          // of the read operation in the current item.
          //
          if (single && options.parse) results = item.parse(results, options);
          item.set(results);

          next(null, results);
        });
      break;

      case 'update':
      case 'patch':
        data = single ? [ item ] : item.models;
        async.reduce(data, 0, function loopModels(memo, model, fn) {
          model = model.clone();
          var update = { $set: pluck(model) };
          if (config.upsert) update = pluck(model);

          client.update({ _id: model.id }, update, config, function updated(error, results) {
            if (error) return next(error);

            model.stored = model.id;
            fn(null, memo + results);
          });
        }, next);
      break;

      case 'delete':
        data = single ? [ item ] : item.models;
        async.reduce(data, 0, function loopModels(memo, model, fn) {
          client.remove({ _id: model.id }, config, function updated(error, results) {
            if (error) return fn(error);

            //
            // Update the stored state properties to false.
            //
            model.stored = false;
            fn(null, memo + results);
          });
        }, next);
      break;
    }
  }
function persist(client, next) {
    var single = isModel(item)
      , data;

    switch (method) {
      case 'create':
        data = single ? [ item.clone() ] : item.clone().models;
        client.insert(pluck(data), config, function inserted(error, results) {
          if (error) return next(error);

          //
          // Set the _stored property for each model to true.
          //
          results.forEach(function each(model) {
            if (single) return item.stored = model._id;
            item.id(model._id).stored = model._id;
          });

          next(null, results);
        });
      break;

      case 'read':
        //
        // Models will be read by Model _id if no query was passed or if
        // the Model already exists in the database. This would otherwise
        // result in data ambiguity.
        //
        query = query || {};
        if (single && item.id) query._id = item.id;

        //
        // No query and Model has no ObjectId reference, return early.
        //
        if (single && !Object.keys(query).length) return next(null, null);

        //
        // Read the documents from the MongoDB database and process
        // the results, depending on the item being a Model or Collection.
        //
        client.find(query, config).toArray(function found(error, results) {
          if (error) return next(error);
          if (single) results = results[0];

          //
          // If the item is a model, parse first. After merge the results
          // of the read operation in the current item.
          //
          if (single && options.parse) results = item.parse(results, options);
          item.set(results);

          next(null, results);
        });
      break;

      case 'update':
      case 'patch':
        data = single ? [ item ] : item.models;
        async.reduce(data, 0, function loopModels(memo, model, fn) {
          model = model.clone();
          var update = { $set: pluck(model) };
          if (config.upsert) update = pluck(model);

          client.update({ _id: model.id }, update, config, function updated(error, results) {
            if (error) return next(error);

            model.stored = model.id;
            fn(null, memo + results);
          });
        }, next);
      break;

      case 'delete':
        data = single ? [ item ] : item.models;
        async.reduce(data, 0, function loopModels(memo, model, fn) {
          client.remove({ _id: model.id }, config, function updated(error, results) {
            if (error) return fn(error);

            //
            // Update the stored state properties to false.
            //
            model.stored = false;
            fn(null, memo + results);
          });
        }, next);
      break;
    }
  }
function persist ( client , next ) { var single = isModel ( item ) , data ; switch ( method ) { case 'create' : data = single ? [ item . clone ( ) ] : item . clone ( ) . models ; client . insert ( pluck ( data ) , config , function inserted ( error , results ) { if ( error ) return next ( error ) ; // // Set the _stored property for each model to true. // results . forEach ( function each ( model ) { if ( single ) return item . stored = model . _id ; item . id ( model . _id ) . stored = model . _id ; } ) ; next ( null , results ) ; } ) ; break ; case 'read' : // // Models will be read by Model _id if no query was passed or if // the Model already exists in the database. This would otherwise // result in data ambiguity. // query = query || { } ; if ( single && item . id ) query . _id = item . id ; // // No query and Model has no ObjectId reference, return early. // if ( single && ! Object . keys ( query ) . length ) return next ( null , null ) ; // // Read the documents from the MongoDB database and process // the results, depending on the item being a Model or Collection. // client . find ( query , config ) . toArray ( function found ( error , results ) { if ( error ) return next ( error ) ; if ( single ) results = results [ 0 ] ; // // If the item is a model, parse first. After merge the results // of the read operation in the current item. // if ( single && options . parse ) results = item . parse ( results , options ) ; item . set ( results ) ; next ( null , results ) ; } ) ; break ; case 'update' : case 'patch' : data = single ? [ item ] : item . models ; async . reduce ( data , 0 , function loopModels ( memo , model , fn ) { model = model . clone ( ) ; var update = { $set : pluck ( model ) } ; if ( config . upsert ) update = pluck ( model ) ; client . update ( { _id : model . id } , update , config , function updated ( error , results ) { if ( error ) return next ( error ) ; model . stored = model . id ; fn ( null , memo + results ) ; } ) ; } , next ) ; break ; case 'delete' : data = single ? [ item ] : item . models ; async . reduce ( data , 0 , function loopModels ( memo , model , fn ) { client . remove ( { _id : model . id } , config , function updated ( error , results ) { if ( error ) return fn ( error ) ; // // Update the stored state properties to false. // model . stored = false ; fn ( null , memo + results ) ; } ) ; } , next ) ; break ; } }
Persist item data to MongoDB.

@param {Object} client Fossa MongoDB client
@param {Function} next callback
@api private
Persist item data to MongoDB .