-
Notifications
You must be signed in to change notification settings - Fork 0
/
findByIds.js
146 lines (104 loc) · 4.37 KB
/
findByIds.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
'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _cloneDeep = require('lodash/cloneDeep');
var _cloneDeep2 = _interopRequireDefault(_cloneDeep);
var _getType = require('../types/getType');
var _getType2 = _interopRequireDefault(_getType);
var _link = require('../types/link');
var _link2 = _interopRequireDefault(_link);
var _requestConnection = require('../connect/requestConnection');
var _requestConnection2 = _interopRequireDefault(_requestConnection);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; }
var findByIds = function findByIds(model, _ids) {
var _options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
return new Promise(function () {
var _ref = _asyncToGenerator( /*#__PURE__*/regeneratorRuntime.mark(function _callee(resolve, reject) {
var options, linkType, ids, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, id, results;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
_context.prev = 0;
options = (0, _cloneDeep2.default)(_options);
if (options.connection) {
_context.next = 6;
break;
}
_context.next = 5;
return (0, _requestConnection2.default)();
case 5:
options.connection = _context.sent;
case 6:
if (options.connection.connector) {
_context.next = 8;
break;
}
throw new Error('Connection has no connector');
case 8:
linkType = (0, _getType2.default)((0, _link2.default)());
_context.next = 11;
return Promise.all(_ids.map(function (_id) {
return linkType.convert(_id, options);
}));
case 11:
ids = _context.sent;
_iteratorNormalCompletion = true;
_didIteratorError = false;
_iteratorError = undefined;
_context.prev = 15;
for (_iterator = ids[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
id = _step.value;
linkType.validate(id, options);
}
_context.next = 23;
break;
case 19:
_context.prev = 19;
_context.t0 = _context['catch'](15);
_didIteratorError = true;
_iteratorError = _context.t0;
case 23:
_context.prev = 23;
_context.prev = 24;
if (!_iteratorNormalCompletion && _iterator.return) {
_iterator.return();
}
case 26:
_context.prev = 26;
if (!_didIteratorError) {
_context.next = 29;
break;
}
throw _iteratorError;
case 29:
return _context.finish(26);
case 30:
return _context.finish(23);
case 31:
_context.next = 33;
return options.connection.connector.actions.findByIds(ids, model);
case 33:
results = _context.sent;
resolve(results);
_context.next = 40;
break;
case 37:
_context.prev = 37;
_context.t1 = _context['catch'](0);
reject(_context.t1);
case 40:
case 'end':
return _context.stop();
}
}
}, _callee, undefined, [[0, 37], [15, 19, 23, 31], [24,, 26, 30]]);
}));
return function (_x2, _x3) {
return _ref.apply(this, arguments);
};
}());
};
exports.default = findByIds;