diff --git a/lib/adapter.js b/lib/adapter.js index 16d4993..b942557 100644 --- a/lib/adapter.js +++ b/lib/adapter.js @@ -40,7 +40,17 @@ module.exports = (function() { registerCollection: function(collection, cb) { var self = this, collectionName; - + console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') + console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') + console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') + console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') + console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') + console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') + console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') + console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') + console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$') + console.log('in registerCollection of sails-mongoose'); + console.log(collection); // If the configuration in this collection corresponds // with a known database, reuse it the connection(s) to that db collectionName = utils.capitalize(collection.identity); @@ -50,11 +60,14 @@ module.exports = (function() { // Otherwise initialize for the first time if (!dbs[ collectionName ]) { - dbs[ collectionName ] = mongoose.model( collectionName, new Schema({}) ); + // dbs[ collectionName ] = mongoose.model( collectionName, new Schema({}) ); + this.define(collectionName, collection.definition, cb); } // adding defaults to every mongoose model - dbs[ collectionName ].config = this.defaults; + // dbs[ collectionName ].config = this.defaults; + + // dbs[collectionName].schema = {}; return cb(); }, @@ -75,20 +88,50 @@ module.exports = (function() { }, define: function(collectionName, definition, cb) { - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); - console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); console.log('%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%'); console.log('******************** you are in define **************************************************************'); + + var index, tempSchema, + schemaObject = {} ; + + // Clone the definition + var def = _.clone(definition); + console.log(def); + // console.log('************** def *********************************'); + function processKey(key, cb) { + // Remove any autoIncement keys, Mongo won't support them without + // a hacky additional collection + + for(i in def){ + + if(def[key].autoIncrement) { + delete def[key].autoIncrement; + } + + schemaObject[ key ] = monUtils.convertToMongooseType( def[key]['type'] ); + + // Mongoose will create a _id element for each schema object + if (key === 'id') { + delete key; + }else if(typeof tempSchema !== 'undefined'){ + // console.log(' ======================== tempSchema is defined ==================== '); + // console.log( utils.capitalize(collectionName) ); + tempSchema.add( schemaObject ); + }else { + // console.log(' ======================== tempSchema is undefined ==================== '); + // console.log( utils.capitalize(collectionName) ); + tempSchema = new Schema( schemaObject ); + } + + modelName = utils.capitalize( collectionName ); + + dbs[modelName] = mongoose.model(modelName, tempSchema); + + return cb(); + } + } + /* spawnConnection(function __DEFINE__(connection, cb) { connection.createCollection(collectionName, function __DEFINE__(err, result) { // console.log('******************** you are in createCollection **************************************************************'); @@ -182,7 +225,8 @@ module.exports = (function() { }); }); }); - }, dbs[collectionName].config, cb); + }, dbs[collectionName].config, cb); */ + }, drop: function(collectionName, cb) { @@ -226,12 +270,12 @@ module.exports = (function() { create: function(collectionName, data, cb) { console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'); console.log('$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ create $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$'); - console.log( data ); + console.log( typeof data ); var modelName, modelInstance, tempObject; modelName = utils.capitalize( collectionName ); modelInstance = mongoose.models[ modelName ]; - tempObject = new modelInstance; + tempObject = new modelInstance( data ); for(i in data){ console.log('given %%%%%%%%%% = ' + data[i]); @@ -327,8 +371,6 @@ module.exports = (function() { modelName = utils.capitalize( collectionName ); modelInstance = mongoose.models[ modelName ]; - console.log(options); - if (options.where) { if (options.where.id != 'create') { options.query = monUtils.covertIdForMongoose( options.where ); @@ -340,9 +382,6 @@ module.exports = (function() { options.query = options.where; } - console.log('&&&&&&&&&&&&&&&&&& options after pre processing &&&&&&&&&&&&&&&&&&&&&&&&&&'); - console.log(options); - modelInstance.find(options.query, function(err, records) { if(err) throw err; return cb(err, utils.rewriteIds(records));