Skip to content

Commit 8ad46af

Browse files
Merge pull request #1325 from balderdashy/meta-argument
[Proposal] Pass additional argument to adapters
2 parents f3e18d5 + 8e69cf2 commit 8ad46af

15 files changed

Lines changed: 134 additions & 77 deletions

File tree

lib/waterline/adapter/aggregateQueries.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;
1010
module.exports = {
1111

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

2929
if (hasOwnProperty(adapter, 'createEach')) {
30-
return adapter.createEach(connName, this.collection, valuesList, cb);
30+
return adapter.createEach(connName, this.collection, valuesList, cb, metaContainer);
3131
}
3232
}
3333

@@ -48,15 +48,15 @@ module.exports = {
4848
if (err) return cb(err);
4949
results.push(row);
5050
cb();
51-
});
51+
}, metaContainer);
5252
}, function(err) {
5353
if (err) return cb(err);
5454
cb(null, results);
5555
});
5656
},
5757

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

8686
if (hasOwnProperty(adapter, 'findOrCreateEach')) {
87-
return adapter.findOrCreateEach(connName, this.collection, valuesList, cb);
87+
return adapter.findOrCreateEach(connName, this.collection, valuesList, cb, metaContainer);
8888
}
8989
}
9090

@@ -122,7 +122,7 @@ module.exports = {
122122
if (model) models.push(model);
123123

124124
cb(null, model);
125-
});
125+
}, metaContainer);
126126
}, function(err) {
127127
if (err) return cb(err);
128128
cb(null, models);

lib/waterline/adapter/compoundQueries.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var hasOwnProperty = require('../utils/helpers').object.hasOwnProperty;
88

99
module.exports = {
1010

11-
findOrCreate: function(criteria, values, cb) {
11+
findOrCreate: function(criteria, values, cb, metaContainer) {
1212
var self = this;
1313
var connName,
1414
adapter;
@@ -29,7 +29,7 @@ module.exports = {
2929
adapter = this.connections[connName]._adapter;
3030

3131
if (hasOwnProperty(adapter, 'findOrCreate')) {
32-
return adapter.findOrCreate(connName, this.collection, values, cb);
32+
return adapter.findOrCreate(connName, this.collection, values, cb, metaContainer);
3333
}
3434
}
3535

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

42-
self.create(values, cb);
43-
});
42+
self.create(values, cb, metaContainer);
43+
}, metaContainer);
4444
}
4545

4646
};

lib/waterline/adapter/dql.js

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ module.exports = {
2828
* @param {[type]} criteria
2929
* @param {Function} cb
3030
*/
31-
join: function(criteria, cb) {
31+
join: function(criteria, cb, metaContainer) {
3232

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

52-
adapter.join(connName, this.collection, criteria, cb);
52+
adapter.join(connName, this.collection, criteria, cb, metaContainer);
5353
},
5454

5555

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

6767
var globalId = this.query.globalId;
6868

6969
// Normalize Arguments
7070
cb = normalize.callback(cb);
7171

72-
if (Array.isArray(values)) return this.createEach.call(this, values, cb);
72+
if (Array.isArray(values)) {
73+
return this.createEach.call(this, values, cb, metaContainer);
74+
}
7375

7476
// Build Default Error Message
7577
var err = 'No create() method defined in adapter!';
@@ -87,7 +89,7 @@ module.exports = {
8789
return cb(err);
8890
}
8991
else return cb(null, createdRecord);
90-
}));
92+
}), metaContainer);
9193
},
9294

9395

@@ -100,8 +102,7 @@ module.exports = {
100102
* @param {Function} cb [description]
101103
* @return {[type]} [description]
102104
*/
103-
find: function(criteria, cb) {
104-
105+
find: function(criteria, cb, metaContainer) {
105106
// Normalize Arguments
106107
criteria = normalize.criteria(criteria);
107108
cb = normalize.callback(cb);
@@ -116,7 +117,7 @@ module.exports = {
116117
var adapter = this.connections[connName]._adapter;
117118

118119
if (!adapter.find) return cb(new Error(err));
119-
adapter.find(connName, this.collection, criteria, cb);
120+
adapter.find(connName, this.collection, criteria, cb, metaContainer);
120121
},
121122

122123

@@ -129,7 +130,7 @@ module.exports = {
129130
* @param {Function} cb [description]
130131
* @return {[type]} [description]
131132
*/
132-
findOne: function(criteria, cb) {
133+
findOne: function(criteria, cb, metaContainer) {
133134

134135
// make shallow copy of criteria so original does not get modified
135136
criteria = _.clone(criteria);
@@ -151,7 +152,7 @@ module.exports = {
151152
if (adapter.findOne) {
152153
// Normalize Arguments
153154
criteria = normalize.criteria(criteria);
154-
return adapter.findOne(connName, this.collection, criteria, cb);
155+
return adapter.findOne(connName, this.collection, criteria, cb, metaContainer);
155156
}
156157
}
157158

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

166167
cb(null, models);
167-
});
168+
}, metaContainer);
168169
},
169170

170171
/**
@@ -173,7 +174,7 @@ module.exports = {
173174
* @param {Function} cb [description]
174175
* @return {[type]} [description]
175176
*/
176-
count: function(criteria, cb) {
177+
count: function(criteria, cb, metaContainer) {
177178
var connName;
178179

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

199-
if (hasOwnProperty(adapter, 'count')) return adapter.count(connName, this.collection, criteria, cb);
200+
if (hasOwnProperty(adapter, 'count')) return adapter.count(connName, this.collection, criteria, cb, metaContainer);
200201

201202
this.find(criteria, function(err, models) {
202203
if (err) return cb(err);
203204
var count = models && models.length || 0;
204205
cb(err, count);
205-
});
206+
}, metaContainer);
206207
},
207208

208209

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

219220

@@ -242,7 +243,7 @@ module.exports = {
242243
return cb(err);
243244
}
244245
return cb(null, updatedRecords);
245-
}));
246+
}), metaContainer);
246247
},
247248

248249

@@ -252,7 +253,7 @@ module.exports = {
252253
* @param {Function} cb [description]
253254
* @return {[type]} [description]
254255
*/
255-
destroy: function(criteria, cb) {
256+
destroy: function(criteria, cb, metaContainer) {
256257

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

270-
adapter.destroy(connName, this.collection, criteria, cb);
271+
adapter.destroy(connName, this.collection, criteria, cb, metaContainer);
271272
}
272273

273274
};

lib/waterline/adapter/stream.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ module.exports = {
1313

1414
// stream.write() is used to send data
1515
// Must call stream.end() to complete stream
16-
stream: function(criteria, stream) {
16+
stream: function(criteria, stream, metaContainer) {
1717

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

3030
if (!hasOwnProperty(adapter, 'stream')) return stream.end(new Error(err));
31-
adapter.stream(connName, this.collection, criteria, stream);
31+
adapter.stream(connName, this.collection, criteria, stream, metaContainer);
3232
}
3333

3434
};

lib/waterline/query/aggregate.js

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ module.exports = {
2121
* @return Deferred object if no callback
2222
*/
2323

24-
createEach: function(valuesList, cb) {
24+
createEach: function(valuesList, cb, metaContainer) {
2525
var self = this;
2626

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

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

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

5859
/**
@@ -65,7 +66,7 @@ module.exports = {
6566
* @return Deferred object if no callback
6667
*/
6768

68-
findOrCreateEach: function(criteria, valuesList, cb) {
69+
findOrCreateEach: function(criteria, valuesList, cb, metaContainer) {
6970
var self = this;
7071

7172
if (typeof valuesList === 'function') {
@@ -160,7 +161,7 @@ module.exports = {
160161

161162
cb(null, models);
162163
});
163-
});
164+
}, metaContainer);
164165
});
165166
}
166167
};

lib/waterline/query/composite.js

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ module.exports = {
2121
* @return Deferred object if no callback
2222
*/
2323

24-
findOrCreate: function(criteria, values, cb) {
24+
findOrCreate: function(criteria, values, cb, metaContainer) {
2525
var self = this;
2626

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

5555
// Try a find first.
56-
this.find(criteria).exec(function(err, results) {
56+
var q = this.find(criteria);
57+
58+
if(metaContainer) {
59+
q.meta(metaContainer);
60+
}
61+
62+
q.exec(function(err, results) {
5763
if (err) return cb(err);
5864

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

6975
// Create a new record if nothing is found.
70-
self.create(values).exec(function(err, result) {
76+
var q2 = self.create(values);
77+
78+
if(metaContainer) {
79+
q2.meta(metaContainer);
80+
}
81+
82+
q2.exec(function(err, result) {
7183
if (err) return cb(err);
7284
return cb(null, result);
7385
});

lib/waterline/query/deferred.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,15 @@ Deferred.prototype.set = function(values) {
502502
return this;
503503
};
504504

505+
/**
506+
* Pass metadata down to the adapter that won't be processed or touched by Waterline
507+
*/
508+
509+
Deferred.prototype.meta = function(data) {
510+
this._meta = data;
511+
return this;
512+
};
513+
505514
/**
506515
* Execute a Query using the method passed into the
507516
* constuctor.
@@ -523,6 +532,11 @@ Deferred.prototype.exec = function(cb) {
523532
var args = [this._criteria, cb];
524533
if (this._values) args.splice(1, 0, this._values);
525534

535+
// If there is a meta value, throw it on the very end
536+
if(this._meta) {
537+
args.push(this._meta);
538+
}
539+
526540
// Pass control to the adapter with the appropriate arguments.
527541
this._method.apply(this._context, args);
528542
};

lib/waterline/query/dql/count.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var Deferred = require('../deferred');
1717
* @return Deferred object if no callback
1818
*/
1919

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

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

59-
this.adapter.count(criteria, cb);
59+
this.adapter.count(criteria, cb, metaContainer);
6060
};

0 commit comments

Comments
 (0)