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

73.68% Statements 42/57
57.69% Branches 15/26
83.33% Functions 5/6
84.78% Lines 39/46
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                        263×     263×     263×     263×     263×                       263× 263× 263×     263× 1168×       263×   65× 65×   65× 65×             528×     528×       528×   132× 132×   132× 132×         120× 55× 55×     120×                 120×     120×     120×   120× 120×   120× 120×                                            
/**
 * 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);
 
    // Build Default Error Message
    var errMsg = 'No define() method defined in adapter!';
 
    // Grab attributes from definition
    var schema = _.clone(this.query._schema.schema) || {};
 
    // Find any junctionTables that reference this collection
    var relations = getRelations({
      schema: self.query.waterline.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
      if (!hasOwnProperty(self.dictionary, 'define')) return cb();
 
      var connName = self.dictionary.define;
      var adapter = self.connections[connName]._adapter;
 
      Iif (!hasOwnProperty(adapter, 'define')) return cb(new Error(errMsg));
      adapter.define(connName, self.collection, schema, cb);
    });
  },
 
  describe: function(cb) {
 
    // Normalize Arguments
    cb = normalize.callback(cb);
 
    // Build Default Error Message
    var err = 'No describe() method defined in adapter!';
 
    // Find the connection to run this on
    // NOTE: if `describe` doesn't exist, an error is not being returned.
    if (!hasOwnProperty(this.dictionary, 'describe')) return cb();
 
    var connName = this.dictionary.describe;
    var adapter = this.connections[connName]._adapter;
 
    Iif (!hasOwnProperty(adapter, 'describe')) return cb(new Error(err));
    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.waterline.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
    Iif (!hasOwnProperty(this.dictionary, 'drop')) return cb(new Error(err));
 
    var connName = this.dictionary.drop;
    var adapter = this.connections[connName]._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
    if (!hasOwnProperty(this.dictionary, 'alter')) return cb(new Error(err));
 
    var connName = this.dictionary.alter;
    var adapter = this.connections[connName]._adapter;
 
    if (!hasOwnProperty(adapter, 'alter')) return cb(new Error(err));
    adapter.alter(connName, this.collection, cb);
  }
 
};