Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/waterline/adapter/aggregateQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;
module.exports = {

// If an optimized createEach exists, use it, otherwise use an asynchronous loop with create()
createEach: function(valuesList, cb) {
createEach: function(valuesList, cb, metaContainer) {
var self = this;
var connName,
adapter;
Expand All @@ -27,7 +27,7 @@ module.exports = {
adapter = this.connections[connName]._adapter;

if (hasOwnProperty(adapter, 'createEach')) {
return adapter.createEach(connName, this.collection, valuesList, cb);
return adapter.createEach(connName, this.collection, valuesList, cb, metaContainer);
}
}

Expand All @@ -48,15 +48,15 @@ module.exports = {
if (err) return cb(err);
results.push(row);
cb();
});
}, metaContainer);
}, function(err) {
if (err) return cb(err);
cb(null, results);
});
},

// If an optimized findOrCreateEach exists, use it, otherwise use an asynchronous loop with create()
findOrCreateEach: function(attributesToCheck, valuesList, cb) {
findOrCreateEach: function(attributesToCheck, valuesList, cb, metaContainer) {
var self = this;
var connName;
var adapter;
Expand Down Expand Up @@ -84,7 +84,7 @@ module.exports = {
adapter = this.connections[connName]._adapter;

if (hasOwnProperty(adapter, 'findOrCreateEach')) {
return adapter.findOrCreateEach(connName, this.collection, valuesList, cb);
return adapter.findOrCreateEach(connName, this.collection, valuesList, cb, metaContainer);
}
}

Expand Down Expand Up @@ -122,7 +122,7 @@ module.exports = {
if (model) models.push(model);

cb(null, model);
});
}, metaContainer);
}, function(err) {
if (err) return cb(err);
cb(null, models);
Expand Down
8 changes: 4 additions & 4 deletions lib/waterline/adapter/compoundQueries.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;

module.exports = {

findOrCreate: function(criteria, values, cb) {
findOrCreate: function(criteria, values, cb, metaContainer) {
var self = this;
var connName,
adapter;
Expand All @@ -29,7 +29,7 @@ module.exports = {
adapter = this.connections[connName]._adapter;

if (hasOwnProperty(adapter, 'findOrCreate')) {
return adapter.findOrCreate(connName, this.collection, values, cb);
return adapter.findOrCreate(connName, this.collection, values, cb, metaContainer);
}
}

Expand All @@ -39,8 +39,8 @@ module.exports = {
if (err) return cb(err);
if (result) return cb(null, result[0]);

self.create(values, cb);
});
self.create(values, cb, metaContainer);
}, metaContainer);
}

};
37 changes: 19 additions & 18 deletions lib/waterline/adapter/dql.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ module.exports = {
* @param {[type]} criteria
* @param {Function} cb
*/
join: function(criteria, cb) {
join: function(criteria, cb, metaContainer) {

// Normalize Arguments
criteria = normalize.criteria(criteria);
Expand All @@ -49,7 +49,7 @@ module.exports = {
// This is done here so that everywhere else in the codebase can use the collection identity.
criteria = schema.serializeJoins(criteria, this.query.waterline.schema);

adapter.join(connName, this.collection, criteria, cb);
adapter.join(connName, this.collection, criteria, cb, metaContainer);
},


Expand All @@ -62,14 +62,16 @@ module.exports = {
* @param {Function} cb [description]
* @return {[type]} [description]
*/
create: function(values, cb) {
create: function(values, cb, metaContainer) {

var globalId = this.query.globalId;

// Normalize Arguments
cb = normalize.callback(cb);

if (Array.isArray(values)) return this.createEach.call(this, values, cb);
if (Array.isArray(values)) {
return this.createEach.call(this, values, cb, metaContainer);
}

// Build Default Error Message
var err = 'No create() method defined in adapter!';
Expand All @@ -87,7 +89,7 @@ module.exports = {
return cb(err);
}
else return cb(null, createdRecord);
}));
}), metaContainer);
},


Expand All @@ -100,8 +102,7 @@ module.exports = {
* @param {Function} cb [description]
* @return {[type]} [description]
*/
find: function(criteria, cb) {

find: function(criteria, cb, metaContainer) {
// Normalize Arguments
criteria = normalize.criteria(criteria);
cb = normalize.callback(cb);
Expand All @@ -116,7 +117,7 @@ module.exports = {
var adapter = this.connections[connName]._adapter;

if (!adapter.find) return cb(new Error(err));
adapter.find(connName, this.collection, criteria, cb);
adapter.find(connName, this.collection, criteria, cb, metaContainer);
},


Expand All @@ -129,7 +130,7 @@ module.exports = {
* @param {Function} cb [description]
* @return {[type]} [description]
*/
findOne: function(criteria, cb) {
findOne: function(criteria, cb, metaContainer) {

// make shallow copy of criteria so original does not get modified
criteria = _.clone(criteria);
Expand All @@ -151,7 +152,7 @@ module.exports = {
if (adapter.findOne) {
// Normalize Arguments
criteria = normalize.criteria(criteria);
return adapter.findOne(connName, this.collection, criteria, cb);
return adapter.findOne(connName, this.collection, criteria, cb, metaContainer);
}
}

Expand All @@ -164,7 +165,7 @@ module.exports = {
if (models.length < 1) return cb(err);

cb(null, models);
});
}, metaContainer);
},

/**
Expand All @@ -173,7 +174,7 @@ module.exports = {
* @param {Function} cb [description]
* @return {[type]} [description]
*/
count: function(criteria, cb) {
count: function(criteria, cb, metaContainer) {
var connName;

// Normalize Arguments
Expand All @@ -196,13 +197,13 @@ module.exports = {
if (!connName) connName = this.dictionary.count;
var adapter = this.connections[connName]._adapter;

if (hasOwnProperty(adapter, 'count')) return adapter.count(connName, this.collection, criteria, cb);
if (hasOwnProperty(adapter, 'count')) return adapter.count(connName, this.collection, criteria, cb, metaContainer);

this.find(criteria, function(err, models) {
if (err) return cb(err);
var count = models && models.length || 0;
cb(err, count);
});
}, metaContainer);
},


Expand All @@ -213,7 +214,7 @@ module.exports = {
* @param {Function} cb [description]
* @return {[type]} [description]
*/
update: function(criteria, values, cb) {
update: function(criteria, values, cb, metaContainer) {
var globalId = this.query.globalId;


Expand Down Expand Up @@ -242,7 +243,7 @@ module.exports = {
return cb(err);
}
return cb(null, updatedRecords);
}));
}), metaContainer);
},


Expand All @@ -252,7 +253,7 @@ module.exports = {
* @param {Function} cb [description]
* @return {[type]} [description]
*/
destroy: function(criteria, cb) {
destroy: function(criteria, cb, metaContainer) {

// Normalize Arguments
cb = normalize.callback(cb);
Expand All @@ -267,7 +268,7 @@ module.exports = {
var connName = this.dictionary.destroy;
var adapter = this.connections[connName]._adapter;

adapter.destroy(connName, this.collection, criteria, cb);
adapter.destroy(connName, this.collection, criteria, cb, metaContainer);
}

};
4 changes: 2 additions & 2 deletions lib/waterline/adapter/stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ module.exports = {

// stream.write() is used to send data
// Must call stream.end() to complete stream
stream: function(criteria, stream) {
stream: function(criteria, stream, metaContainer) {

// Normalize Arguments
criteria = normalize.criteria(criteria);
Expand All @@ -28,7 +28,7 @@ module.exports = {
var adapter = this.connections[connName]._adapter;

if (!hasOwnProperty(adapter, 'stream')) return stream.end(new Error(err));
adapter.stream(connName, this.collection, criteria, stream);
adapter.stream(connName, this.collection, criteria, stream, metaContainer);
}

};
17 changes: 9 additions & 8 deletions lib/waterline/query/aggregate.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,13 @@ module.exports = {
* @return Deferred object if no callback
*/

createEach: function(valuesList, cb) {
createEach: function(valuesList, cb, metaContainer) {
var self = this;

// Handle Deferred where it passes criteria first
if (arguments.length === 3) {
var args = Array.prototype.slice.call(arguments);
cb = args.pop();
valuesList = args.pop();
if(_.isPlainObject(arguments[0]) && _.isArray(arguments[1])) {
valuesList = arguments[1];
cb = arguments[2];
}

// Return Deferred or pass to adapter
Expand All @@ -52,7 +51,9 @@ module.exports = {
});

// Create will take care of cloning values so original isn't mutated
async.map(filteredValues, self.create.bind(self), cb);
async.map(filteredValues, function(data, next) {
self.create(data, next, metaContainer);
}, cb);
},

/**
Expand All @@ -65,7 +66,7 @@ module.exports = {
* @return Deferred object if no callback
*/

findOrCreateEach: function(criteria, valuesList, cb) {
findOrCreateEach: function(criteria, valuesList, cb, metaContainer) {
var self = this;

if (typeof valuesList === 'function') {
Expand Down Expand Up @@ -160,7 +161,7 @@ module.exports = {

cb(null, models);
});
});
}, metaContainer);
});
}
};
Expand Down
18 changes: 15 additions & 3 deletions lib/waterline/query/composite.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
* @return Deferred object if no callback
*/

findOrCreate: function(criteria, values, cb) {
findOrCreate: function(criteria, values, cb, metaContainer) {
var self = this;

if (typeof values === 'function') {
Expand Down Expand Up @@ -53,7 +53,13 @@ module.exports = {
if (typeof cb !== 'function') return usageError('Invalid callback specified!', usage, cb);

// Try a find first.
this.find(criteria).exec(function(err, results) {
var q = this.find(criteria);

if(metaContainer) {
q.meta(metaContainer);
}

q.exec(function(err, results) {
if (err) return cb(err);

if (results && results.length !== 0) {
Expand All @@ -67,7 +73,13 @@ module.exports = {
}

// Create a new record if nothing is found.
self.create(values).exec(function(err, result) {
var q2 = self.create(values);

if(metaContainer) {
q2.meta(metaContainer);
}

q2.exec(function(err, result) {
if (err) return cb(err);
return cb(null, result);
});
Expand Down
14 changes: 14 additions & 0 deletions lib/waterline/query/deferred.js
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,15 @@ Deferred.prototype.set = function(values) {
return this;
};

/**
* Pass metadata down to the adapter that won't be processed or touched by Waterline
*/

Deferred.prototype.meta = function(data) {
this._meta = data;
return this;
};

/**
* Execute a Query using the method passed into the
* constuctor.
Expand All @@ -523,6 +532,11 @@ Deferred.prototype.exec = function(cb) {
var args = [this._criteria, cb];
if (this._values) args.splice(1, 0, this._values);

// If there is a meta value, throw it on the very end
if(this._meta) {
args.push(this._meta);
}

// Pass control to the adapter with the appropriate arguments.
this._method.apply(this._context, args);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/waterline/query/dql/count.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ var Deferred = require('../deferred');
* @return Deferred object if no callback
*/

module.exports = function(criteria, options, cb) {
module.exports = function(criteria, options, cb, metaContainer) {
var usage = utils.capitalize(this.identity) + '.count([criteria],[options],callback)';

if (typeof criteria === 'function') {
Expand Down Expand Up @@ -56,5 +56,5 @@ module.exports = function(criteria, options, cb) {
// Transform Search Criteria
criteria = this._transformer.serialize(criteria);

this.adapter.count(criteria, cb);
this.adapter.count(criteria, cb, metaContainer);
};
Loading