Skip to content

Commit

Permalink
now changing the schema would call define function to add keys or any…
Browse files Browse the repository at this point in the history
…thing on hte go
  • Loading branch information
ktkaushik committed Aug 13, 2013
1 parent 7d987ee commit 15c10c6
Showing 1 changed file with 61 additions and 22 deletions.
83 changes: 61 additions & 22 deletions lib/adapter.js
Expand Up @@ -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);
Expand All @@ -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();
},
Expand All @@ -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 **************************************************************');
Expand Down Expand Up @@ -182,7 +225,8 @@ module.exports = (function() {
});
});
});
}, dbs[collectionName].config, cb);
}, dbs[collectionName].config, cb); */

},

drop: function(collectionName, cb) {
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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 );
Expand All @@ -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));
Expand Down

0 comments on commit 15c10c6

Please sign in to comment.