-
Notifications
You must be signed in to change notification settings - Fork 362
/
Copy pathdao.js
1327 lines (1193 loc) · 36.8 KB
/
dao.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*!
* Module exports class Model
*/
module.exports = DataAccessObject;
/*!
* Module dependencies
*/
var jutil = require('./jutil');
var ValidationError = require('./validations').ValidationError;
var Relation = require('./relations.js');
var Inclusion = require('./include.js');
var List = require('./list.js');
var geo = require('./geo');
var Memory = require('./connectors/memory').Memory;
var utils = require('./utils');
var fieldsToArray = utils.fieldsToArray;
var removeUndefined = utils.removeUndefined;
var setScopeValuesFromWhere = utils.setScopeValuesFromWhere;
var mergeQuery = utils.mergeQuery;
var util = require('util');
var assert = require('assert');
/**
* Base class for all persistent objects.
* Provides a common API to access any database connector.
* This class describes only abstract behavior. Refer to the specific connector for additional details.
*
* `DataAccessObject` mixes `Inclusion` classes methods.
* @class DataAccessObject
*/
function DataAccessObject() {
if (DataAccessObject._mixins) {
var self = this;
var args = arguments;
DataAccessObject._mixins.forEach(function (m) {
m.call(self, args);
});
}
}
function idName(m) {
return m.definition.idName() || 'id';
}
function getIdValue(m, data) {
return data && data[idName(m)];
}
function setIdValue(m, data, value) {
if (data) {
data[idName(m)] = value;
}
}
function byIdQuery(m, id) {
var pk = idName(m);
var query = { where: {} };
query.where[pk] = id;
m.applyScope(query);
return query;
}
DataAccessObject._forDB = function (data) {
if (!(this.getDataSource().isRelational && this.getDataSource().isRelational())) {
return data;
}
var res = {};
for (var propName in data) {
var type = this.getPropertyType(propName);
if (type === 'JSON' || type === 'Any' || type === 'Object' || data[propName] instanceof Array) {
res[propName] = JSON.stringify(data[propName]);
} else {
res[propName] = data[propName];
}
}
return res;
};
DataAccessObject.defaultScope = function(target, inst) {
var scope = this.definition.settings.scope;
if (typeof scope === 'function') {
scope = this.definition.settings.scope.call(this, target, inst);
}
return scope;
};
DataAccessObject.applyScope = function(query, inst) {
var scope = this.defaultScope(query, inst) || {};
if (typeof scope === 'object') {
mergeQuery(query, scope || {}, this.definition.settings.scoping);
}
};
DataAccessObject.applyProperties = function(data, inst) {
var properties = this.definition.settings.properties;
properties = properties || this.definition.settings.attributes;
if (typeof properties === 'object') {
util._extend(data, properties);
} else if (typeof properties === 'function') {
util._extend(data, properties.call(this, data, inst) || {});
} else if (properties !== false) {
var scope = this.defaultScope(data, inst) || {};
if (typeof scope.where === 'object') {
setScopeValuesFromWhere(data, scope.where, this);
}
}
};
DataAccessObject.lookupModel = function(data) {
return this;
};
/**
* Create an instance of Model with given data and save to the attached data source. Callback is optional.
* Example:
*```js
* User.create({first: 'Joe', last: 'Bob'}, function(err, user) {
* console.log(user instanceof User); // true
* });
* ```
* Note: You must include a callback and use the created model provided in the callback if your code depends on your model being
* saved or having an ID.
*
* @param {Object} data Optional data object
* @param {Function} callback Callback function called with these arguments:
* - err (null or Error)
* - instance (null or Model)
*/
DataAccessObject.create = function (data, callback) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
var self = this;
if (typeof data === 'function') {
callback = data;
data = {};
}
if (typeof callback !== 'function') {
callback = function () {
};
}
if (!data) {
data = {};
}
if (Array.isArray(data)) {
var instances = [];
var errors = Array(data.length);
var gotError = false;
var wait = data.length;
if (wait === 0) {
callback(null, []);
}
for (var i = 0; i < data.length; i += 1) {
(function (d, i) {
Model = self.lookupModel(d); // data-specific
instances.push(Model.create(d, function (err, inst) {
if (err) {
errors[i] = err;
gotError = true;
}
modelCreated();
}));
})(data[i], i);
}
return instances;
function modelCreated() {
if (--wait === 0) {
callback(gotError ? errors : null, instances);
if(!gotError) {
instances.forEach(function(inst) {
inst.constructor.emit('changed');
});
}
}
}
}
var enforced = {};
var obj;
var idValue = getIdValue(this, data);
// if we come from save
if (data instanceof Model && !idValue) {
obj = data;
} else {
obj = new Model(data);
}
this.applyProperties(enforced, obj);
obj.setAttributes(enforced);
Model = this.lookupModel(data); // data-specific
if (Model !== obj.constructor) obj = new Model(data);
data = obj.toObject(true);
// validation required
obj.isValid(function (valid) {
if (valid) {
create();
} else {
callback(new ValidationError(obj), obj);
}
}, data);
function create() {
obj.trigger('create', function (createDone) {
obj.trigger('save', function (saveDone) {
var _idName = idName(Model);
var modelName = Model.modelName;
this._adapter().create(modelName, this.constructor._forDB(obj.toObject(true)), function (err, id, rev) {
if (id) {
obj.__data[_idName] = id;
defineReadonlyProp(obj, _idName, id);
}
if (rev) {
obj._rev = rev;
}
if (err) {
return callback(err, obj);
}
obj.__persisted = true;
saveDone.call(obj, function () {
createDone.call(obj, function () {
callback(err, obj);
if(!err) Model.emit('changed', obj);
});
});
}, obj);
}, obj, callback);
}, obj, callback);
}
// for chaining
return obj;
};
function stillConnecting(dataSource, obj, args) {
return dataSource.ready(obj, args);
}
/**
* Update or insert a model instance: update exiting record if one is found, such that parameter `data.id` matches `id` of model instance;
* otherwise, insert a new record.
*
* NOTE: No setters, validations, or hooks are applied when using upsert.
* `updateOrCreate` is an alias
* @param {Object} data The model instance data
* @param {Function} callback The callback function (optional).
*/
// [FIXME] rfeng: This is a hack to set up 'upsert' first so that
// 'upsert' will be used as the name for strong-remoting to keep it backward
// compatible for angular SDK
DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data, callback) {
if (stillConnecting(this.getDataSource(), this, arguments)) {
return;
}
var self = this;
var Model = this;
if (!getIdValue(this, data)) {
return this.create(data, callback);
}
if (this.getDataSource().connector.updateOrCreate) {
var update = data;
var inst = data;
if(!(data instanceof Model)) {
inst = new Model(data);
}
update = inst.toObject(false);
this.applyProperties(update, inst);
update = removeUndefined(update);
Model = this.lookupModel(update);
this.getDataSource().connector.updateOrCreate(Model.modelName, update, function (err, data) {
var obj;
if (data && !(data instanceof Model)) {
inst._initProperties(data);
obj = inst;
} else {
obj = data;
}
callback(err, obj);
if(!err) {
Model.emit('changed', inst);
}
});
} else {
this.findById(getIdValue(this, data), function (err, inst) {
if (err) {
return callback(err);
}
if (inst) {
inst.updateAttributes(data, callback);
} else {
Model = self.lookupModel(data);
var obj = new Model(data);
obj.save(data, callback);
}
});
}
};
/**
* Find one record that matches specified query criteria. Same as `find`, but limited to one record, and this function returns an
* object, not a collection.
* If the specified instance is not found, then create it using data provided as second argument.
*
* @param {Object} query Search conditions. See [find](#dataaccessobjectfindquery-callback) for query format.
* For example: `{where: {test: 'me'}}`.
* @param {Object} data Object to create.
* @param {Function} cb Callback called with (err, instance)
*/
DataAccessObject.findOrCreate = function findOrCreate(query, data, callback) {
if (query === undefined) {
query = {where: {}};
}
if (typeof data === 'function' || typeof data === 'undefined') {
callback = data;
data = query && query.where;
}
if (typeof callback === 'undefined') {
callback = function () {
};
}
var t = this;
this.findOne(query, function (err, record) {
if (err) return callback(err);
if (record) return callback(null, record);
t.create(data, callback);
});
};
/**
* Check whether a model instance exists in database
*
* @param {id} id Identifier of object (primary key value)
* @param {Function} cb Callback function called with (err, exists: Bool)
*/
DataAccessObject.exists = function exists(id, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
if (id !== undefined && id !== null && id !== '') {
this.count(byIdQuery(this, id).where, function(err, count) {
cb(err, err ? false : count === 1);
});
} else {
cb(new Error('Model::exists requires the id argument'));
}
};
/**
* Find model instance by ID.
*
* Example:
* ```js
* User.findById(23, function(err, user) {
* console.info(user.id); // 23
* });
* ```
*
* @param {*} id Primary key value
* @param {Function} cb Callback called with (err, instance)
*/
DataAccessObject.findById = function find(id, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
this.findOne(byIdQuery(this, id), cb);
};
DataAccessObject.findByIds = function(ids, cond, cb) {
if (typeof cond === 'function') {
cb = cond;
cond = {};
}
var pk = idName(this);
if (ids.length === 0) {
process.nextTick(function() { cb(null, []); });
return;
}
var filter = { where: {} };
filter.where[pk] = { inq: [].concat(ids) };
mergeQuery(filter, cond || {});
this.find(filter, function(err, results) {
cb(err, err ? results : utils.sortObjectsByIds(pk, ids, results));
}.bind(this));
};
function convertNullToNotFoundError(ctx, cb) {
if (ctx.result !== null) return cb();
var modelName = ctx.method.sharedClass.name;
var id = ctx.getArgByName('id');
var msg = 'Unknown "' + modelName + '" id "' + id + '".';
var error = new Error(msg);
error.statusCode = error.status = 404;
cb(error);
}
// alias function for backwards compat.
DataAccessObject.all = function () {
DataAccessObject.find.apply(this, arguments);
};
var operators = {
gt: '>',
gte: '>=',
lt: '<',
lte: '<=',
between: 'BETWEEN',
inq: 'IN',
nin: 'NOT IN',
neq: '!=',
like: 'LIKE',
nlike: 'NOT LIKE'
};
/*
* Normalize the filter object and throw errors if invalid values are detected
* @param {Object} filter The query filter object
* @returns {Object} The normalized filter object
* @private
*/
DataAccessObject._normalize = function (filter) {
if (!filter) {
return undefined;
}
var err = null;
if ((typeof filter !== 'object') || Array.isArray(filter)) {
err = new Error(util.format('The query filter %j is not an object', filter));
err.statusCode = 400;
throw err;
}
if (filter.limit || filter.skip || filter.offset) {
var limit = Number(filter.limit || 100);
var offset = Number(filter.skip || filter.offset || 0);
if (isNaN(limit) || limit <= 0 || Math.ceil(limit) !== limit) {
err = new Error(util.format('The limit parameter %j is not valid',
filter.limit));
err.statusCode = 400;
throw err;
}
if (isNaN(offset) || offset < 0 || Math.ceil(offset) !== offset) {
err = new Error(util.format('The offset/skip parameter %j is not valid',
filter.skip || filter.offset));
err.statusCode = 400;
throw err;
}
filter.limit = limit;
filter.offset = offset;
filter.skip = offset;
}
if (filter.order) {
var order = filter.order;
if (!Array.isArray(order)) {
order = [order];
}
var fields = [];
for (var i = 0, m = order.length; i < m; i++) {
if (typeof order[i] === 'string') {
// Normalize 'f1 ASC, f2 DESC, f3' to ['f1 ASC', 'f2 DESC', 'f3']
var tokens = order[i].split(/(?:\s*,\s*)+/);
for (var t = 0, n = tokens.length; t < n; t++) {
var token = tokens[t];
if (token.length === 0) {
// Skip empty token
continue;
}
var parts = token.split(/\s+/);
if (parts.length >= 2) {
var dir = parts[1].toUpperCase();
if (dir === 'ASC' || dir === 'DESC') {
token = parts[0] + ' ' + dir;
} else {
err = new Error(util.format('The order %j has invalid direction', token));
err.statusCode = 400;
throw err;
}
}
fields.push(token);
}
} else {
err = new Error(util.format('The order %j is not valid', order[i]));
err.statusCode = 400;
throw err;
}
}
if (fields.length === 1 && typeof filter.order === 'string') {
filter.order = fields[0];
} else {
filter.order = fields;
}
}
// normalize fields as array of included property names
if (filter.fields) {
filter.fields = fieldsToArray(filter.fields,
Object.keys(this.definition.properties));
}
filter = removeUndefined(filter);
this._coerce(filter.where);
return filter;
};
function DateType(arg) {
return new Date(arg);
}
function BooleanType(val) {
if (val === 'true') {
return true;
} else if (val === 'false') {
return false;
} else {
return Boolean(val);
}
}
function NumberType(val) {
var num = Number(val);
return !isNaN(num) ? num : val;
}
/*
* Coerce values based the property types
* @param {Object} where The where clause
* @returns {Object} The coerced where clause
* @private
*/
DataAccessObject._coerce = function (where) {
var self = this;
if (!where) {
return where;
}
var err;
if (typeof where !== 'object' || Array.isArray(where)) {
err = new Error(util.format('The where clause %j is not an object', where));
err.statusCode = 400;
throw err;
}
var props = self.definition.properties;
for (var p in where) {
// Handle logical operators
if (p === 'and' || p === 'or' || p === 'nor') {
var clauses = where[p];
if (Array.isArray(clauses)) {
for (var k = 0; k < clauses.length; k++) {
self._coerce(clauses[k]);
}
} else {
err = new Error(util.format('The %s operator has invalid clauses %j', p, clauses));
err.statusCode = 400;
throw err;
}
return where;
}
var DataType = props[p] && props[p].type;
if (!DataType) {
continue;
}
if (Array.isArray(DataType) || DataType === Array) {
DataType = DataType[0];
}
if (DataType === Date) {
DataType = DateType;
} else if (DataType === Boolean) {
DataType = BooleanType;
} else if (DataType === Number) {
// This fixes a regression in mongodb connector
// For numbers, only convert it produces a valid number
// LoopBack by default injects a number id. We should fix it based
// on the connector's input, for example, MongoDB should use string
// while RDBs typically use number
DataType = NumberType;
}
if (!DataType) {
continue;
}
if (DataType === geo.GeoPoint) {
// Skip the GeoPoint as the near operator breaks the assumption that
// an operation has only one property
// We should probably fix it based on
// http://docs.mongodb.org/manual/reference/operator/query/near/
// The other option is to make operators start with $
continue;
}
var val = where[p];
if (val === null || val === undefined) {
continue;
}
// Check there is an operator
var operator = null;
if ('object' === typeof val) {
if (Object.keys(val).length !== 1) {
// Skip if there are not only one properties
// as the assumption for operators is not true here
continue;
}
for (var op in operators) {
if (op in val) {
val = val[op];
operator = op;
switch(operator) {
case 'inq':
case 'nin':
if (!Array.isArray(val)) {
err = new Error(util.format('The %s property has invalid clause %j', p, where[p]));
err.statusCode = 400;
throw err;
}
break;
case 'between':
if (!Array.isArray(val) || val.length !== 2) {
err = new Error(util.format('The %s property has invalid clause %j', p, where[p]));
err.statusCode = 400;
throw err;
}
break;
case 'like':
case 'nlike':
if (!(typeof val === 'string' || val instanceof RegExp)) {
err = new Error(util.format('The %s property has invalid clause %j', p, where[p]));
err.statusCode = 400;
throw err;
}
break;
}
break;
}
}
}
// Coerce the array items
if (Array.isArray(val)) {
for (var i = 0; i < val.length; i++) {
if (val[i] !== null && val[i] !== undefined) {
val[i] = DataType(val[i]);
}
}
} else {
if (val !== null && val !== undefined) {
val = DataType(val);
}
}
// Rebuild {property: {operator: value}}
if (operator) {
var value = {};
value[operator] = val;
val = value;
}
where[p] = val;
}
return where;
};
/**
* Find all instances of Model that match the specified query.
* Fields used for filter and sort should be declared with `{index: true}` in model definition.
* See [Querying models](http://docs.strongloop.com/display/DOC/Querying+models) for more information.
*
* For example, find the second page of ten users over age 21 in descending order exluding the password property.
*
* ```js
* User.find({
* where: {
* age: {gt: 21}},
* order: 'age DESC',
* limit: 10,
* skip: 10,
* fields: {password: false}
* },
* console.log
* );
* ```
*
* @options {Object} [query] Optional JSON object that specifies query criteria and parameters.
* @property {Object} where Search criteria in JSON format `{ key: val, key2: {gt: 'val2'}}`.
* Operations:
* - gt: >
* - gte: >=
* - lt: <
* - lte: <=
* - between
* - inq: IN
* - nin: NOT IN
* - neq: !=
* - like: LIKE
* - nlike: NOT LIKE
*
* You can also use `and` and `or` operations. See [Querying models](http://docs.strongloop.com/display/DOC/Querying+models) for more information.
* @property {String|Object|Array} include Allows you to load relations of several objects and optimize numbers of requests.
* Format examples;
* - `'posts'`: Load posts
* - `['posts', 'passports']`: Load posts and passports
* - `{'owner': 'posts'}`: Load owner and owner's posts
* - `{'owner': ['posts', 'passports']}`: Load owner, owner's posts, and owner's passports
* - `{'owner': [{posts: 'images'}, 'passports']}`: Load owner, owner's posts, owner's posts' images, and owner's passports
* See `DataAccessObject.include()`.
* @property {String} order Sort order. Format: `'key1 ASC, key2 DESC'`
* @property {Number} limit Maximum number of instances to return.
* @property {Number} skip Number of instances to skip.
* @property {Number} offset Alias for `skip`.
* @property {Object|Array|String} fields Included/excluded fields.
* - `['foo']` or `'foo'` - include only the foo property
* - `['foo', 'bar']` - include the foo and bar properties. Format:
* - `{foo: true}` - include only foo
* - `{bat: false}` - include all properties, exclude bat
*
* @param {Function} callback Required callback function. Call this function with two arguments: `err` (null or Error) and an array of instances.
*/
DataAccessObject.find = function find(query, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
if (arguments.length === 1) {
cb = query;
query = null;
}
var self = this;
query = query || {};
try {
this._normalize(query);
} catch (err) {
return process.nextTick(function () {
cb && cb(err);
});
}
this.applyScope(query);
var near = query && geo.nearFilter(query.where);
var supportsGeo = !!this.getDataSource().connector.buildNearFilter;
if (near) {
if (supportsGeo) {
// convert it
this.getDataSource().connector.buildNearFilter(query, near);
} else if (query.where) {
// do in memory query
// using all documents
// TODO [fabien] use default scope here?
this.getDataSource().connector.all(this.modelName, {}, function (err, data) {
var memory = new Memory();
var modelName = self.modelName;
if (err) {
cb(err);
} else if (Array.isArray(data)) {
memory.define({
properties: self.dataSource.definitions[self.modelName].properties,
settings: self.dataSource.definitions[self.modelName].settings,
model: self
});
data.forEach(function (obj) {
memory.create(modelName, obj, function () {
// noop
});
});
memory.all(modelName, query, cb);
} else {
cb(null, []);
}
}.bind(this));
// already handled
return;
}
}
this.getDataSource().connector.all(this.modelName, query, function (err, data) {
if (data && data.forEach) {
data.forEach(function (d, i) {
var Model = self.lookupModel(d);
var obj = new Model(d, {fields: query.fields, applySetters: false, persisted: true});
if (query && query.include) {
if (query.collect) {
// The collect property indicates that the query is to return the
// standlone items for a related model, not as child of the parent object
// For example, article.tags
obj = obj.__cachedRelations[query.collect];
} else {
// This handles the case to return parent items including the related
// models. For example, Article.find({include: 'tags'}, ...);
// Try to normalize the include
var includes = Inclusion.normalizeInclude(query.include || []);
includes.forEach(function (inc) {
var relationName = inc;
if (utils.isPlainObject(inc)) {
relationName = Object.keys(inc)[0];
}
// Promote the included model as a direct property
var data = obj.__cachedRelations[relationName];
if(Array.isArray(data)) {
data = new List(data, null, obj);
}
if (data) obj.__data[relationName] = data;
});
delete obj.__data.__cachedRelations;
}
}
data[i] = obj;
});
if (data && data.countBeforeLimit) {
data.countBeforeLimit = data.countBeforeLimit;
}
if (!supportsGeo && near) {
data = geo.filter(data, near);
}
cb(err, data);
}
else
cb(err, []);
});
};
/**
* Find one record, same as `find`, but limited to one result. This function returns an object, not a collection.
*
* @param {Object} query Sarch conditions. See [find](#dataaccessobjectfindquery-callback) for query format.
* For example: `{where: {test: 'me'}}`.
* @param {Function} cb Callback function called with (err, instance)
*/
DataAccessObject.findOne = function findOne(query, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
if (typeof query === 'function') {
cb = query;
query = {};
}
query = query || {};
query.limit = 1;
this.find(query, function (err, collection) {
if (err || !collection || !collection.length > 0) return cb(err, null);
cb(err, collection[0]);
});
};
/**
* Destroy all matching records.
* Delete all model instances from data source. Note: destroyAll method does not destroy hooks.
* Example:
*````js
* Product.destroyAll({price: {gt: 99}}, function(err) {
// removed matching products
* });
* ````
*
* @param {Object} [where] Optional object that defines the criteria. This is a "where" object. Do NOT pass a filter object.
* @param {Function} [cb] Callback called with (err)
*/
DataAccessObject.remove = DataAccessObject.deleteAll = DataAccessObject.destroyAll = function destroyAll(where, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
if (!cb && 'function' === typeof where) {
cb = where;
where = undefined;
}
var query = { where: where };
this.applyScope(query);
where = query.where;
if (!where || (typeof where === 'object' && Object.keys(where).length === 0)) {
this.getDataSource().connector.destroyAll(this.modelName, function (err, data) {
cb && cb(err, data);
if(!err) Model.emit('deletedAll');
}.bind(this));
} else {
try {
// Support an optional where object
where = removeUndefined(where);
where = this._coerce(where);
} catch (err) {
return process.nextTick(function() {
cb && cb(err);
});
}
this.getDataSource().connector.destroyAll(this.modelName, where, function (err, data) {
cb && cb(err, data);
if(!err) Model.emit('deletedAll', where);
}.bind(this));
}
};
/**
* Delete the record with the specified ID.
* Aliases are `destroyById` and `deleteById`.
* @param {*} id The id value
* @param {Function} cb Callback called with (err)
*/
// [FIXME] rfeng: This is a hack to set up 'deleteById' first so that
// 'deleteById' will be used as the name for strong-remoting to keep it backward
// compatible for angular SDK
DataAccessObject.removeById = DataAccessObject.destroyById = DataAccessObject.deleteById = function deleteById(id, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this;
this.remove(byIdQuery(this, id).where, function(err) {
if ('function' === typeof cb) {
cb(err);
}
if(!err) Model.emit('deleted', id);
});
};
/**
* Return count of matched records. Optional query parameter allows you to count filtered set of model instances.
* Example:
*
*```js
* User.count({approved: true}, function(err, count) {
* console.log(count); // 2081
* });
* ```
*
* @param {Object} [where] Search conditions (optional)
* @param {Function} cb Callback, called with (err, count)
*/
DataAccessObject.count = function (where, cb) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
if (typeof where === 'function') {
cb = where;
where = null;
}
var query = { where: where };
this.applyScope(query);
where = query.where;
try {
where = removeUndefined(where);
where = this._coerce(where);
} catch (err) {
return process.nextTick(function () {
cb && cb(err);
});
}
this.getDataSource().connector.count(this.modelName, cb, where);
};
/**
* Save instance. If the instance does not have an ID, call `create` instead.
* Triggers: validate, save, update or create.
* @options {Object} options Optional options to use.
* @property {Boolean} validate Default is true.
* @property {Boolean} throws Default is false.
* @param {Function} callback Callback function with err and object arguments
*/
DataAccessObject.prototype.save = function (options, callback) {
if (stillConnecting(this.getDataSource(), this, arguments)) return;
var Model = this.constructor;
if (typeof options == 'function') {
callback = options;
options = {};
}
callback = callback || function () {
};
options = options || {};
if (!('validate' in options)) {
options.validate = true;
}
if (!('throws' in options)) {
options.throws = false;
}
var inst = this;
var data = inst.toObject(true);
var modelName = Model.modelName;