all files / lib/offshore/adapter/ dql.js

76.47% Statements 117/153
60.74% Branches 82/135
100% Functions 12/12
79.58% Lines 113/142
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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343                      525× 525× 525×     525× 525×     525×                               295× 295×   295× 295×   295×     295×   295×     295×   295×       295×       295×   295×                           1513×     1513×   1513×         1513×   1513× 1513×   1513×     1513×   1513×     1513×   1513×     1513× 1513× 1513× 11× 11×   1502×                               1355× 1355×   1355× 1355×   1355×     1355×   1355×     1355×   1355×       1355× 1355×                             428×     428×     428×     428×   428× 428×   428×     428×   428×     428×   428×                       428×   428× 428× 418×   305×                           14× 14×   14× 14×   14×     14×   14×     14×   14×           11×       11× 11× 11× 11×                         317×     317× 317×   317×   317×       317× 317×   317×     317×   317×     317×   317×       317×   317× 317× 12× 12×   305×                           76× 76×   76× 76×   76×     76×   76×     76×   76×     76× 76×        
/**
 * Module Dependencies
 */
 
var normalize = require('../utils/normalize');
var schema = require('../utils/schema');
var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;
var _ = require('lodash');
 
 
/**
 * DQL Adapter Normalization
 */
module.exports = {
 
  hasJoin: function() {
    var query = this._query || {};
    var connName = this.connection;
    Iif (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    Iif (!connection) {
      return false;
    }
    return hasOwnProperty(connection._adapter, 'join');
  },
 
 
  /**
   * join()
   *
   * If `join` is defined in the adapter, Offshore will use it to optimize
   * the `.populate()` implementation when joining collections within the same
   * database connection.
   *
   * @param  {[type]}   criteria
   * @param  {Function} cb
   */
  join: function(criteria, cb, metaContainer) {
    // Normalize Arguments
    criteria = normalize.criteria(criteria);
    cb = normalize.callback(cb);
 
    var query = this._query || {};
    var connName = this.connection;
    // check default connection
    Iif (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    // check connection
    Iif (!connection) {
      return cb(new Error('No valid connection specified'));
    }
    var adapter = connection._adapter;
    // check transaction
    Iif (query.transaction && query.transaction[connName]) {
      connName = query.transaction[connName];
    }
 
    Iif (!hasOwnProperty(adapter, 'join')) return cb(new Error('No join() method defined in adapter!'));
 
    // Parse Join Criteria and set references to any collection tableName properties.
    // This is done here so that everywhere else in the codebase can use the collection identity.
    criteria = schema.serializeJoins(criteria, this.query.offshore.schema);
 
    adapter.join(connName, this.collection, criteria, cb, metaContainer);
  },
 
 
  /**
   * create()
   *
   * Create one or more models.
   *
   * @param  {[type]}   values [description]
   * @param  {Function} cb     [description]
   * @return {[type]}          [description]
   */
  create: function(values, cb, metaContainer) {
    var globalId = this.query.globalId;
 
    // Normalize Arguments
    cb = normalize.callback(cb);
 
    Iif (Array.isArray(values)) {
      return this.createEach.call(this, values, cb, metaContainer);
    }
 
    // Build Default Error Message
    var err = 'No create() method defined in adapter!';
 
    var query = this._query || {};
    var connName = this.connection;
    // check default connection
    Iif (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    // check connection
    Iif (!connection) {
      return cb(new Error('No valid connection specified'));
    }
    var adapter = connection._adapter;
    // check transaction
    if (query.transaction && query.transaction[connName]) {
      connName = query.transaction[connName];
    }
 
    Iif (!hasOwnProperty(adapter, 'create')) return cb(new Error(err));
    adapter.create(connName, this.collection, values, normalize.callback(function afterwards(err, createdRecord) {
      if (err) {
        Eif (typeof err === 'object') err.model = globalId;
        return cb(err);
      }
      else return cb(null, createdRecord);
    }), metaContainer);
  },
 
  /**
   * find()
   *
   * Find a set of models.
   *
   * @param  {[type]}   criteria [description]
   * @param  {Function} cb       [description]
   * @return {[type]}            [description]
   */
  find: function(criteria, cb, metaContainer) {
 
    // Normalize Arguments
    criteria = normalize.criteria(criteria);
    cb = normalize.callback(cb);
 
    var query = this._query || {};
    var connName = this.connection;
    // check default connection
    Iif (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    // check connection
    Iif (!connection) {
      return cb(new Error('No valid connection specified'));
    }
    var adapter = connection._adapter;
    // check transaction
    Iif (query.transaction && query.transaction[connName]) {
      connName = query.transaction[connName];
    }
 
    Iif (!adapter.find) return cb(new Error('No find() method defined in adapter!'));
    adapter.find(connName, this.collection, criteria, cb, metaContainer);
  },
 
  /**
   * findOne()
   *
   * Find exactly one model.
   *
   * @param  {[type]}   criteria [description]
   * @param  {Function} cb       [description]
   * @return {[type]}            [description]
   */
  findOne: function(criteria, cb, metaContainer) {
 
    // make shallow copy of criteria so original does not get modified
    criteria = _.clone(criteria);
 
    // Normalize Arguments
    cb = normalize.callback(cb);
 
    // Build Default Error Message
    var err = '.findOne() requires a criteria. If you want the first record try .find().limit(1)';
 
    // If no criteria is specified or where is empty return an error
    Iif (!criteria || criteria.where === null) return cb(new Error(err));
 
    var query = this._query || {};
    var connName = this.connection;
    // check default connection
    Iif (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    // check connection
    Iif (!connection) {
      return cb(new Error('No valid connection specified'));
    }
    var adapter = connection._adapter;
 
    Iif (hasOwnProperty(adapter, 'findOne')) {
      // check transaction
      if (query.transaction && query.transaction[connName]) {
        connName = query.transaction[connName];
      }
      // Normalize Arguments
      criteria = normalize.criteria(criteria);
      return adapter.findOne(connName, this.collection, criteria, cb, metaContainer);      
    }
 
    // Fallback to use `find()` to simulate a `findOne()`
    // Enforce limit to 1
    criteria.limit = 1;
 
    this.find(criteria, function(err, models) {
      if (!models) return cb(err);
      if (models.length < 1) return cb(err);
 
      cb(null, models);
    }, metaContainer);
  },
 
  /**
   * [count description]
   * @param  {[type]}   criteria [description]
   * @param  {Function} cb       [description]
   * @return {[type]}            [description]
   */
  count: function(criteria, cb, metaContainer) {
 
 
    // Normalize Arguments
    cb = normalize.callback(cb);
    criteria = normalize.criteria(criteria);
 
    var query = this._query || {};
    var connName = this.connection;
    // check default connection
    Iif (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    // check connection
    Iif (!connection) {
      return cb(new Error('No valid connection specified'));
    }
    var adapter = connection._adapter;
 
    if (hasOwnProperty(adapter, 'count')) {
      // check transaction
      Iif (query.transaction && query.transaction[connName]) {
        connName = query.transaction[connName];
      }
      return adapter.count(connName, this.collection, criteria, cb, metaContainer);
    }
 
    Iif (!hasOwnProperty(adapter, 'find')) {
      return cb(new Error('.count() requires the adapter define either a count method or a find method'));
    }
 
    this.find(criteria, function(err, models) {
      Iif (err) return cb(err);
      var count = models && models.length || 0;
      cb(err, count);
    }, metaContainer);
  },
 
 
  /**
   * [update description]
   * @param  {[type]}   criteria [description]
   * @param  {[type]}   values   [description]
   * @param  {Function} cb       [description]
   * @return {[type]}            [description]
   */
  update: function(criteria, values, cb, metaContainer) {
    var globalId = this.query.globalId;
 
    // Normalize Arguments
    cb = normalize.callback(cb);
    criteria = normalize.criteria(criteria);
 
    Iif (criteria === false) {
      return cb(null, []);
    } else Iif (!criteria) {
      return cb(new Error('No criteria or id specified!'));
    }
 
    var query = this._query || {};
    var connName = this.connection;
    // check default connection
    Iif (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    // check connection
    Iif (!connection) {
      return cb(new Error('No valid connection specified'));
    }
    var adapter = connection._adapter;
    // check transaction
    Iif (query.transaction && query.transaction[connName]) {
      connName = query.transaction[connName];
    }
 
    Iif (!hasOwnProperty(adapter, 'update')) return cb(new Error('No update() method defined in adapter!'));
 
    adapter.update(connName, this.collection, criteria, values, normalize.callback(function afterwards(err, updatedRecords) {
      if (err) {
        Eif (typeof err === 'object') err.model = globalId;
        return cb(err);
      }
      return cb(null, updatedRecords);
    }), metaContainer);
  },
 
 
  /**
   * [destroy description]
   * @param  {[type]}   criteria [description]
   * @param  {Function} cb       [description]
   * @return {[type]}            [description]
   */
  destroy: function(criteria, cb, metaContainer) {
 
    // Normalize Arguments
    cb = normalize.callback(cb);
    criteria = normalize.criteria(criteria);
 
    var query = this._query || {};
    var connName = this.connection;
    // check default connection
    Iif (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    // check connection
    Iif (!connection) {
      return cb(new Error('No valid connection specified'));
    }
    var adapter = connection._adapter;
    // check transaction
    Iif (query.transaction && query.transaction[connName]) {
      connName = query.transaction[connName];
    }
    Iif (!hasOwnProperty(adapter, 'destroy')) return cb(new Error('No destroy() method defined in adapter!'));
    adapter.destroy(connName, this.collection, criteria, cb, metaContainer);
  }
 
};