all files / lib/offshore/adapter/ddl/ index.js

69.01% Statements 49/71
54% Branches 27/50
83.33% Functions 5/6
71.88% Lines 46/64
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                        320×     320×     320×     320×                       320× 320× 320×     320× 1477×       320× 320× 320×     320× 320×     320×   320× 119×             630×       630× 630× 630×     630× 630×     630×   630× 228×         215× 108× 108×     215×                 215×     215×     215× 215× 215×     215× 215×     215×   215× 215×                                                        
/**
 * Module dependencies
 */
 
var _ = require('lodash');
var normalize = require('../../utils/normalize');
var getRelations = require('../../utils/getRelations');
var hasOwnProperty = require('../../utils/helpers').object.hasOwnProperty;
 
 
/**
 * DDL Adapter Normalization
 */
 
module.exports = {
 
  define: function(cb) {
    var self = this;
 
    // Normalize Arguments
    cb = normalize.callback(cb);
 
    // Grab attributes from definition
    var schema = _.clone(this.query._schema.schema) || {};
 
    // Find any junctionTables that reference this collection
    var relations = getRelations({
      schema: self.query.offshore.schema,
      parentCollection: self.collection
    });
 
    //
    // TODO: if junction tables don't exist, define them
    // console.log(relations);
    //
 
    // Verify that collection doesn't already exist
    // and then define it and trigger callback
    this.describe(function(err, existingAttributes) {
      Iif (err) return cb(err);
      Iif (existingAttributes) return cb(new Error('Trying to define a collection (' + self.collection + ') which already exists.'));
 
      // Remove hasMany association keys before sending down to adapter
      Object.keys(schema).forEach(function(key) {
        if (schema[key].type) return;
        delete schema[key];
      });
 
      // Find the connection to run this on
      var query = self._query || {};
      var connName = self.connection;
      Iif (connName === 'default' && query.defaultConnection) {
        connName = query.defaultConnection;
      }
      var connection = self.query.offshore.connections[connName];
      Iif (!connection) {
        return cb();
      }
      var adapter = connection._adapter;
 
      if (!hasOwnProperty(adapter, 'define')) return cb();
      adapter.define(connName, self.collection, schema, cb);
    });
  },
 
  describe: function(cb) {
 
    // Normalize Arguments
    cb = normalize.callback(cb);
 
    // Find the connection to run this on
    // NOTE: if `describe` doesn't exist, an error is not being returned.
    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 cb();
    }
    var adapter = connection._adapter;
  
    if (!hasOwnProperty(adapter, 'describe')) return cb();
    adapter.describe(connName, this.collection, cb);
  },
 
  drop: function(relations, cb) {
    // Allow relations to be optional
    if (typeof relations === 'function') {
      cb = relations;
      relations = [];
    }
 
    relations = [];
 
    //
    // TODO:
    // Use a more normalized strategy to get relations so we can omit the extra argument above.
    // e.g. getRelations({ schema: self.query.offshore.schema, parentCollection: self.collection });
    //
 
    // Normalize Arguments
    cb = normalize.callback(cb);
 
    // Build Default Error Message
    var err = 'No drop() method defined in adapter!';
 
    // Find the connection to run this on
    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 cb();
    }
    var adapter = connection._adapter;
 
    Iif (!hasOwnProperty(adapter, 'drop')) return cb(new Error(err));
    adapter.drop(connName, this.collection, relations, cb);
  },
 
  alter: function(cb) {
 
    // Normalize arguments
    cb = normalize.callback(cb);
 
    // Build Default Error Message
    var err = 'No alter() method defined in adapter!';
 
    // Find the connection to run this on
    var query = this._query || {};
    var connName = this.connection;
    if (connName === 'default' && query.defaultConnection) {
      connName = query.defaultConnection;
    }
    var connection = this.query.offshore.connections[connName];
    if (!connection) {
      return cb();
    }
    var adapter = connection._adapter;
 
    if (!hasOwnProperty(adapter, 'alter')) return cb(new Error(err));
    adapter.alter(connName, this.collection, cb);
  }
 
};