Code coverage report for bookshelf/lib/helpers.js

Statements: 90.63% (29 / 32)      Branches: 100% (22 / 22)      Functions: 57.14% (4 / 7)      Lines: 90% (27 / 30)      Ignored: none     

All files » bookshelf/lib/ » helpers.js
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86        1 1   1       63 63 21 21   63           51 99   51 3   48                     1094 911 911       1094   96   96       9 87       18 21 21           69   96                                 1
// Helpers
// ---------------
'use strict';
 
var _ = require('lodash');
var chalk = require('chalk');
 
var helpers = {
 
  // Sets the constraints necessary during a `model.save` call.
  saveConstraints: function saveConstraints(model, relatedData) {
    var data = {};
    if (relatedData && !relatedData.isThrough() && relatedData.type !== 'belongsToMany' && relatedData.type !== 'belongsTo') {
      data[relatedData.key('foreignKey')] = relatedData.parentFk || model.get(relatedData.key('foreignKey'));
      if (relatedData.isMorph()) data[relatedData.key('morphKey')] = relatedData.key('morphValue');
    }
    return model.set(model.parse(data));
  },
 
  // Finds the specific `morphTo` table we should be working with, or throws
  // an error if none is matched.
  morphCandidate: function morphCandidate(candidates, foreignTable) {
    var Target = _.find(candidates, function (Candidate) {
      return _.result(Candidate.prototype, 'tableName') === foreignTable;
    });
    if (!Target) {
      throw new Error('The target polymorphic model was not found');
    }
    return Target;
  },
 
  // If there are no arguments, return the current object's
  // query builder (or create and return a new one). If there are arguments,
  // call the query builder with the first argument, applying the rest.
  // If the first argument is an object, assume the keys are query builder
  // methods, and the values are the arguments for the query.
  query: function query(obj, args) {
 
    // Ensure the object has a query builder.
    if (!obj._knex) {
      var tableName = _.result(obj, 'tableName');
      obj._knex = obj._builder(tableName);
    }
 
    // If there are no arguments, return the query builder.
    if (args.length === 0) return obj._knex;
 
    var method = args[0];
 
    if (_.isFunction(method)) {
 
      // `method` is a query builder callback. Call it on the query builder
      // object.
      method.call(obj._knex, obj._knex);
    } else if (_.isObject(method)) {
 
      // `method` is an object. Use keys as methods and values as arguments to
      // the query builder.
      for (var key in method) {
        var target = _.isArray(method[key]) ? method[key] : [method[key]];
        obj._knex[key].apply(obj._knex, target);
      }
    } else {
 
      // Otherwise assume that the `method` is string name of a query builder
      // method, and use the remaining args as arguments to that method.
      obj._knex[method].apply(obj._knex, args.slice(1));
    }
    return obj;
  },
 
  error: function error(msg) {
    console.log(chalk.red(msg));
  },
 
  warn: function warn(msg) {
    console.log(chalk.yellow(msg));
  },
 
  deprecate: function deprecate(a, b) {
    helpers.warn(a + ' has been deprecated, please use ' + b + ' instead');
  }
 
};
 
module.exports = helpers;