UNPKG

offshore

Version:
150 lines (119 loc) 4.25 kB
/** * 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) { if (err) return cb(err); if (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; if (connName === 'default' && query.defaultConnection) { connName = query.defaultConnection; } var connection = self.query.offshore.connections[connName]; if (!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; 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, '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; 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, '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); } };