-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
165 lines (137 loc) · 3.83 KB
/
index.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
var _ = require('underscore'),
// console = require('../console.js'),
$fh = require('fh-mbaas-api'),
async = require('async');
// ERRORS = require('../errorCodes').dbErrors;
// DITCH_REQ_LIMIT = require('../constants').DITCH_REQ_LIMIT;
function noop() {}
// function taskRunner(func, qCallback) {
// func.call(undefined, qCallback);
// }
// var queues = {
// 'CREATE': async.queue(taskRunner, DITCH_REQ_LIMIT),
// 'READ': async.queue(taskRunner, DITCH_REQ_LIMIT),
// 'UPDATE': async.queue(taskRunner, DITCH_REQ_LIMIT),
// 'LIST': async.queue(taskRunner, DITCH_REQ_LIMIT),
// 'DELETE': async.queue(taskRunner, DITCH_REQ_LIMIT),
// 'DELETEALL': async.queue(taskRunner, DITCH_REQ_LIMIT)
// }
// Generic wrapper for all database callbacks
// Ensures logging occurs and a client friendly error is propogated up
function dbCb(cb) {
return function (err, res) {
if (err) {
// console.error(ERRORS.DB_ERROR.DEV_MSG, err);
// return cb(ERRORS.DB_ERROR.CLIENT_MSG, null);
}
return cb(null, res);
};
}
exports.create = function (col, fields, cb) {
// Note create time of items
if (_.isArray(fields)) {
fields.forEach( function (field) {
field._createDateTime = Date.now();
field._createDateTimeHuman = new Date();
});
} else {
fields._createDateTime = Date.now();
fields._createDateTimeHuman = new Date();
}
$fh.db({
'act': 'create',
'type': col,
'fields': fields
}, dbCb(cb));
// var func = function (callback) {
// $fh.db({
// 'act': 'create',
// 'type': col,
// 'fields': fields
// }, dbCb(callback));
// }
// // Queue //TODO:Brid - replicate for all above
// queues['CREATE'].push(func, cb);
};
exports.read = function (col, guid, cb) {
$fh.db({
'act': 'read',
'type': col,
'guid': guid
}, dbCb(cb));
};
exports.update = function (col, guid, fields, cb) {
// Track when items are updated
fields._lastModified = Date.now();
$fh.db({
'act': 'update',
'type': col,
'fields': fields,
'guid': guid
}, dbCb(cb));
};
exports.list = function (col, restrictions, cb) {
var params = {
'act': 'list',
'type': col
};
if (restrictions && typeof restrictions === 'function') {
cb = restrictions;
restrictions = null;
}
else if (restrictions) {
params = _.extend(params, restrictions);
}
$fh.db(params, dbCb(cb));
};
exports.listWithFields = function (col, restrictions, fields, cb) {
var params = {
'act': 'list',
'type': col,
'fields': fields
};
if (restrictions) {
params = _.extend(params, restrictions);
}
$fh.db(params, dbCb(cb));
};
var remove = exports.remove = function (col, guid, cb) {
$fh.db({
'act': 'delete',
'type': col,
guid: guid
}, dbCb(cb));
};
var removeAll = exports.removeAll = function (col, cb) {
console.log('Deleting collection: %s', col);
$fh.db({
'act': 'deleteall',
'type': col
}, dbCb(cb));
};
exports.removeAllCollections = function (collections, callback) {
// callback = callback || noop;
async.eachSeries(collections, function (col, cb) {
removeAll(col, cb);
}, function (err) {
callback(err);
});
};
/**
* Remove collections based on GUIDs
*/
exports.removeByIds = function (col, ids, cb) {
var funcs = [];
cb = cb || noop;
_.each(ids, function (id) {
funcs.push(function (callback) {
remove(col, id, callback);
});
});
async.parallel(funcs, function (err, data) {
cb(err, data);
});
};
exports.genericQuery = function (query, cb) {
$fh.db(query, cb);
};