Repository navigation
Expand file tree
/
Copy pathData.js
More file actions
162 lines (141 loc) · 5.24 KB
/
Copy pathData.js
File metadata and controls
162 lines (141 loc) · 5.24 KB
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
'use strict';
/**
* /api/services/Data.js
*
* Generic data service which contains helper functions for data fetching.
*/
var actionUtil = require('sails/lib/hooks/blueprints/actionUtil');
/**
* Helper data fetch function which will return a collection of specified property of
* specified model data.
*
* @param {string} modelName Name of the model
* @param {string} propertyName Property to get
* @param {{}} criteria Find criteria object
* @param {Function} next Callback function
*/
exports.getCollectionProperty = function(modelName, propertyName, criteria, next) {
sails.models[modelName]
.find()
.where(criteria)
.exec(function(error, results) {
if (error) {
sails.log.error(__filename + ':' + __line + ' [Failed to fetch model \'' + modelName + '\' property collection]');
sails.log.error(error);
return next(error, null);
} else if (!results) {
return next(null, []);
} else {
return next(null, _.pluck(results, propertyName));
}
});
};
/**
* Helper function to fetch generic data from database with sails.js. Note that
* this will not use .sort(), .limit() nor .skip() methods of waterline.
*
* Usually this function is used to fetch grouped data from database and that
* is the reason why we cannot use those previously mentioned waterline ORM functions.
*
* @param {Request} request Request object
* @param {Object} [extraCriteria] Extra criteria object
* @param {Function} next Callback function
*/
exports.getCollection = function(request, extraCriteria, next) {
// Determine model name and used default criteria object
var model = actionUtil.parseModel(request);
var criteria = actionUtil.parseCriteria(request);
// Merge extra criteria to main criteria object
if (extraCriteria) {
criteria = _.merge(criteria, extraCriteria);
}
// Fetch data from database
model
.find(criteria)
.exec(function found(error, items) {
next(error, items);
});
};
/**
* Helper function to sort and paginate array of items. Sort and pagination parameters
* are parsed from current Request object.
*
* This function is needed because waterline doesn't support sorting and pagination with
* groupBy criteria.
*
* @param {Array} items Array of items to sort and paginate
* @param {Request} request Request object
*
* @returns {Array}
*/
exports.sortAndPaginate = function(items, request) {
var limit = actionUtil.parseLimit(request);
var skip = actionUtil.parseSkip(request);
var sort = !_.isUndefined(actionUtil.parseSort(request)) ? actionUtil.parseSort(request).split(' ') : ['id'];
items = _.sortBy(items, sort[0]);
if (sort[1] === 'DESC') {
items.reverse();
}
return items.slice(skip, (skip + limit));
};
/**
* Helper service method to remove objects from array where specified key is null.
*
* @param {{}[]} items Array of objects
* @param {String} nullKey Object key to check if null or not
*
* @returns {Array} Array of objects that hasn't null on specified key
*/
exports.rejectNullItems = function(items, nullKey) {
items = _.reject(items, function(item) {
return _.isNull(item[nullKey]);
});
return items;
};
/**
* Service method to fetch single user data from database.
*
* @param {Number|Object} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getUser = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
sails.models['user']
.findOne(where)
.populate('passports')
.exec(function(error, /** sails.model.user */ user) {
if (error) {
sails.log.error(__filename + ':' + __line + ' [Failed to fetch user data]');
sails.log.error(error);
} else if (!user && !noExistsCheck) {
error = new Error();
error.message = 'User not found.';
error.status = 404;
}
next(error, user);
});
};
/**
* Service method to fetch single passport data from database.
*
* @param {Number|Object} where Used query conditions
* @param {Function} next Callback function to call after query
* @param {Boolean} [noExistsCheck] If data is not found, skip error
*/
exports.getPassport = function(where, next, noExistsCheck) {
noExistsCheck = noExistsCheck || false;
sails.models['passport']
.findOne(where)
.exec(function(error, /** sails.model.passport */ passport) {
if (error) {
sails.log.error(__filename + ':' + __line + ' [Failed to fetch passport data]');
sails.log.error(error);
} else if (!passport && !noExistsCheck) {
error = new Error();
error.message = 'Passport not found.';
error.status = 404;
}
next(error, passport);
});
};