all files / lib/waterline/adapter/ compoundQueries.js

72.73% Statements 16/22
41.67% Branches 5/12
100% Functions 2/2
78.95% Lines 15/19
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47              15× 15×       15×     15× 15×     15×     15×                     15× 15× 15×   13×          
/**
 * Compound Queries Adapter Normalization
 */
 
var _ = require('lodash');
var normalize = require('../utils/normalize');
var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;
 
module.exports = {
 
  findOrCreate: function(criteria, values, cb, metaContainer) {
    var self = this;
    var connName,
        adapter;
 
    // If no values were specified, use criteria
    Iif (!values) values = criteria.where ? criteria.where : criteria;
 
    // Normalize Arguments
    criteria = normalize.criteria(criteria);
    cb = normalize.callback(cb);
 
    // Build Default Error Message
    var err = 'No find() or create() method defined in adapter!';
 
    // Custom user adapter behavior
    Iif (hasOwnProperty(this.dictionary, 'findOrCreate')) {
      connName = this.dictionary.findOrCreate;
      adapter = this.connections[connName]._adapter;
 
      if (hasOwnProperty(adapter, 'findOrCreate')) {
        return adapter.findOrCreate(connName, this.collection, values, cb, metaContainer);
      }
    }
 
    // Default behavior
    // WARNING: Not transactional!  (unless your data adapter is)
    this.findOne(criteria, function(err, result) {
      Iif (err) return cb(err);
      if (result) return cb(null, result[0]);
 
      self.create(values, cb, metaContainer);
    }, metaContainer);
  }
 
};