lib/impl/dataSources/index.js
function create(connections, dataSource, callback) {
  async.waterfall([
    function validateParams(cb) {

      //If it is a deploy, the JSON can contain an _id param
      if (connections.deploy) {
        return cb(undefined, dataSource);
      }

      //Otherwise, check that it is not there.
      validate(dataSource).hasno(CONSTANTS.DATA_SOURCE_ID, function(err) {
        if (err) {
          return cb(buildErrorResponse({error: new Error("Data Source ID Should Not Be Included When Creating A Data Source"), code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS}));
        }

        cb(undefined, dataSource);
      });
    },
    function validateNameNotUsed(dataSource, cb) {
      //Duplicate Names Are Not Allowed.
      var DataSource = models.get(connections.mongooseConnection, models.MODELNAMES.DATA_SOURCE);

      DataSource.count({name: dataSource.name}, function(err, numDuplicateDSs) {
        if (numDuplicateDSs && numDuplicateDSs > 0) {
          return cb({
            userDetail: "Invalid Data To Create A Data Source" ,
            systemDetail: "A Data Source With The Name " + dataSource.name + " Already Exists",
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          });
        } else {
          return cb(err, dataSource);
        }
      });
    },
    function createDataSource(dataSourceJSON, cb) {
      dataSourceJSON = misc.sanitiseJSON(dataSourceJSON);

      var DataSource = models.get(connections.mongooseConnection, models.MODELNAMES.DATA_SOURCE);
      var newDataSource = new DataSource(dataSourceJSON);

      newDataSource.save(function(err) {
        if (err) {
          return cb(buildErrorResponse({
            error: err,
            userDetail: "Invalid Data Source Creation Data.",
            systemDetail: err.errors,
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          }));
        }

        newDataSource = processDataSourceResponse(newDataSource.toJSON());

        return cb(undefined, newDataSource);
      });

    }
  ], callback);
}
function create(connections, dataSource, callback) {
  async.waterfall([
    function validateParams(cb) {

      //If it is a deploy, the JSON can contain an _id param
      if (connections.deploy) {
        return cb(undefined, dataSource);
      }

      //Otherwise, check that it is not there.
      validate(dataSource).hasno(CONSTANTS.DATA_SOURCE_ID, function(err) {
        if (err) {
          return cb(buildErrorResponse({error: new Error("Data Source ID Should Not Be Included When Creating A Data Source"), code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS}));
        }

        cb(undefined, dataSource);
      });
    },
    function validateNameNotUsed(dataSource, cb) {
      //Duplicate Names Are Not Allowed.
      var DataSource = models.get(connections.mongooseConnection, models.MODELNAMES.DATA_SOURCE);

      DataSource.count({name: dataSource.name}, function(err, numDuplicateDSs) {
        if (numDuplicateDSs && numDuplicateDSs > 0) {
          return cb({
            userDetail: "Invalid Data To Create A Data Source" ,
            systemDetail: "A Data Source With The Name " + dataSource.name + " Already Exists",
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          });
        } else {
          return cb(err, dataSource);
        }
      });
    },
    function createDataSource(dataSourceJSON, cb) {
      dataSourceJSON = misc.sanitiseJSON(dataSourceJSON);

      var DataSource = models.get(connections.mongooseConnection, models.MODELNAMES.DATA_SOURCE);
      var newDataSource = new DataSource(dataSourceJSON);

      newDataSource.save(function(err) {
        if (err) {
          return cb(buildErrorResponse({
            error: err,
            userDetail: "Invalid Data Source Creation Data.",
            systemDetail: err.errors,
            code: ERROR_CODES.FH_FORMS_INVALID_PARAMETERS
          }));
        }

        newDataSource = processDataSourceResponse(newDataSource.toJSON());

        return cb(undefined, newDataSource);
      });

    }
  ], callback);
}
function create ( connections , dataSource , callback ) { async . waterfall ( [ function validateParams ( cb ) { //If it is a deploy, the JSON can contain an _id param if ( connections . deploy ) { return cb ( undefined , dataSource ) ; } //Otherwise, check that it is not there. validate ( dataSource ) . hasno ( CONSTANTS . DATA_SOURCE_ID , function ( err ) { if ( err ) { return cb ( buildErrorResponse ( { error : new Error ( "Data Source ID Should Not Be Included When Creating A Data Source" ) , code : ERROR_CODES . FH_FORMS_INVALID_PARAMETERS } ) ) ; } cb ( undefined , dataSource ) ; } ) ; } , function validateNameNotUsed ( dataSource , cb ) { //Duplicate Names Are Not Allowed. var DataSource = models . get ( connections . mongooseConnection , models . MODELNAMES . DATA_SOURCE ) ; DataSource . count ( { name : dataSource . name } , function ( err , numDuplicateDSs ) { if ( numDuplicateDSs && numDuplicateDSs > 0 ) { return cb ( { userDetail : "Invalid Data To Create A Data Source" , systemDetail : "A Data Source With The Name " + dataSource . name + " Already Exists" , code : ERROR_CODES . FH_FORMS_INVALID_PARAMETERS } ) ; } else { return cb ( err , dataSource ) ; } } ) ; } , function createDataSource ( dataSourceJSON , cb ) { dataSourceJSON = misc . sanitiseJSON ( dataSourceJSON ) ; var DataSource = models . get ( connections . mongooseConnection , models . MODELNAMES . DATA_SOURCE ) ; var newDataSource = new DataSource ( dataSourceJSON ) ; newDataSource . save ( function ( err ) { if ( err ) { return cb ( buildErrorResponse ( { error : err , userDetail : "Invalid Data Source Creation Data." , systemDetail : err . errors , code : ERROR_CODES . FH_FORMS_INVALID_PARAMETERS } ) ) ; } newDataSource = processDataSourceResponse ( newDataSource . toJSON ( ) ) ; return cb ( undefined , newDataSource ) ; } ) ; } ] , callback ) ; }
Creating A New Data Source
@param connections
@param dataSource
@param callback
Creating A New Data Source