@driveflux/search 5.0.77 → 5.0.78
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +1 -0
- package/dist/search-mongodb.js +60 -283
- package/dist/search.js +48 -348
- package/dist/types.js +13 -40
- package/package.json +2 -2
package/dist/index.js
CHANGED
package/dist/search-mongodb.js
CHANGED
|
@@ -1,294 +1,71 @@
|
|
|
1
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
2
|
-
try {
|
|
3
|
-
var info = gen[key](arg);
|
|
4
|
-
var value = info.value;
|
|
5
|
-
} catch (error) {
|
|
6
|
-
reject(error);
|
|
7
|
-
return;
|
|
8
|
-
}
|
|
9
|
-
if (info.done) {
|
|
10
|
-
resolve(value);
|
|
11
|
-
} else {
|
|
12
|
-
Promise.resolve(value).then(_next, _throw);
|
|
13
|
-
}
|
|
14
|
-
}
|
|
15
|
-
function _async_to_generator(fn) {
|
|
16
|
-
return function() {
|
|
17
|
-
var self = this, args = arguments;
|
|
18
|
-
return new Promise(function(resolve, reject) {
|
|
19
|
-
var gen = fn.apply(self, args);
|
|
20
|
-
function _next(value) {
|
|
21
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
22
|
-
}
|
|
23
|
-
function _throw(err) {
|
|
24
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
25
|
-
}
|
|
26
|
-
_next(undefined);
|
|
27
|
-
});
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
function _define_property(obj, key, value) {
|
|
31
|
-
if (key in obj) {
|
|
32
|
-
Object.defineProperty(obj, key, {
|
|
33
|
-
value: value,
|
|
34
|
-
enumerable: true,
|
|
35
|
-
configurable: true,
|
|
36
|
-
writable: true
|
|
37
|
-
});
|
|
38
|
-
} else {
|
|
39
|
-
obj[key] = value;
|
|
40
|
-
}
|
|
41
|
-
return obj;
|
|
42
|
-
}
|
|
43
|
-
function _ts_generator(thisArg, body) {
|
|
44
|
-
var f, y, t, _ = {
|
|
45
|
-
label: 0,
|
|
46
|
-
sent: function() {
|
|
47
|
-
if (t[0] & 1) throw t[1];
|
|
48
|
-
return t[1];
|
|
49
|
-
},
|
|
50
|
-
trys: [],
|
|
51
|
-
ops: []
|
|
52
|
-
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
|
|
53
|
-
return d(g, "next", {
|
|
54
|
-
value: verb(0)
|
|
55
|
-
}), d(g, "throw", {
|
|
56
|
-
value: verb(1)
|
|
57
|
-
}), d(g, "return", {
|
|
58
|
-
value: verb(2)
|
|
59
|
-
}), typeof Symbol === "function" && d(g, Symbol.iterator, {
|
|
60
|
-
value: function() {
|
|
61
|
-
return this;
|
|
62
|
-
}
|
|
63
|
-
}), g;
|
|
64
|
-
function verb(n) {
|
|
65
|
-
return function(v) {
|
|
66
|
-
return step([
|
|
67
|
-
n,
|
|
68
|
-
v
|
|
69
|
-
]);
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
function step(op) {
|
|
73
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
74
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
75
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
76
|
-
if (y = 0, t) op = [
|
|
77
|
-
op[0] & 2,
|
|
78
|
-
t.value
|
|
79
|
-
];
|
|
80
|
-
switch(op[0]){
|
|
81
|
-
case 0:
|
|
82
|
-
case 1:
|
|
83
|
-
t = op;
|
|
84
|
-
break;
|
|
85
|
-
case 4:
|
|
86
|
-
_.label++;
|
|
87
|
-
return {
|
|
88
|
-
value: op[1],
|
|
89
|
-
done: false
|
|
90
|
-
};
|
|
91
|
-
case 5:
|
|
92
|
-
_.label++;
|
|
93
|
-
y = op[1];
|
|
94
|
-
op = [
|
|
95
|
-
0
|
|
96
|
-
];
|
|
97
|
-
continue;
|
|
98
|
-
case 7:
|
|
99
|
-
op = _.ops.pop();
|
|
100
|
-
_.trys.pop();
|
|
101
|
-
continue;
|
|
102
|
-
default:
|
|
103
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
104
|
-
_ = 0;
|
|
105
|
-
continue;
|
|
106
|
-
}
|
|
107
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
108
|
-
_.label = op[1];
|
|
109
|
-
break;
|
|
110
|
-
}
|
|
111
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
112
|
-
_.label = t[1];
|
|
113
|
-
t = op;
|
|
114
|
-
break;
|
|
115
|
-
}
|
|
116
|
-
if (t && _.label < t[2]) {
|
|
117
|
-
_.label = t[2];
|
|
118
|
-
_.ops.push(op);
|
|
119
|
-
break;
|
|
120
|
-
}
|
|
121
|
-
if (t[2]) _.ops.pop();
|
|
122
|
-
_.trys.pop();
|
|
123
|
-
continue;
|
|
124
|
-
}
|
|
125
|
-
op = body.call(thisArg, _);
|
|
126
|
-
} catch (e) {
|
|
127
|
-
op = [
|
|
128
|
-
6,
|
|
129
|
-
e
|
|
130
|
-
];
|
|
131
|
-
y = 0;
|
|
132
|
-
} finally{
|
|
133
|
-
f = t = 0;
|
|
134
|
-
}
|
|
135
|
-
if (op[0] & 5) throw op[1];
|
|
136
|
-
return {
|
|
137
|
-
value: op[0] ? op[1] : void 0,
|
|
138
|
-
done: true
|
|
139
|
-
};
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
1
|
import { prisma } from '@driveflux/db';
|
|
143
2
|
import { instantiateSingleton, singleton } from '@driveflux/singleton';
|
|
144
3
|
import { MongoClient } from 'mongodb';
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
return new MongoClient(process.env.DATABASE_URL);
|
|
153
|
-
});
|
|
154
|
-
isConnected = singleton('search__MongoClientConnected', false);
|
|
155
|
-
if (!!isConnected) return [
|
|
156
|
-
3,
|
|
157
|
-
2
|
|
158
|
-
];
|
|
159
|
-
client.on('connectionReady', function() {
|
|
160
|
-
// update the singleton value to true (second true is to force the value to be true)
|
|
161
|
-
singleton('search__MongoClientConnected', true, true);
|
|
162
|
-
});
|
|
163
|
-
client.on('close', function() {
|
|
164
|
-
singleton('search__MongoClientConnected', false, true);
|
|
165
|
-
});
|
|
166
|
-
return [
|
|
167
|
-
4,
|
|
168
|
-
client.connect()
|
|
169
|
-
];
|
|
170
|
-
case 1:
|
|
171
|
-
_state.sent();
|
|
172
|
-
_state.label = 2;
|
|
173
|
-
case 2:
|
|
174
|
-
return [
|
|
175
|
-
2,
|
|
176
|
-
client
|
|
177
|
-
];
|
|
178
|
-
}
|
|
4
|
+
const getClient = async () => {
|
|
5
|
+
const client = instantiateSingleton('search__MongoClient', () => new MongoClient(process.env.DATABASE_URL));
|
|
6
|
+
const isConnected = singleton('search__MongoClientConnected', false);
|
|
7
|
+
if (!isConnected) {
|
|
8
|
+
client.on('connectionReady', () => {
|
|
9
|
+
// update the singleton value to true (second true is to force the value to be true)
|
|
10
|
+
singleton('search__MongoClientConnected', true, true);
|
|
179
11
|
});
|
|
180
|
-
|
|
12
|
+
client.on('close', () => {
|
|
13
|
+
singleton('search__MongoClientConnected', false, true);
|
|
14
|
+
});
|
|
15
|
+
await client.connect();
|
|
16
|
+
}
|
|
17
|
+
return client;
|
|
181
18
|
};
|
|
182
|
-
|
|
183
|
-
var _prisma__runtimeDataModel_models_model;
|
|
19
|
+
const getCollectionName = (model) => {
|
|
184
20
|
// TODO
|
|
185
21
|
// @ts-expect-error - This is a private property
|
|
186
|
-
return
|
|
22
|
+
return prisma._runtimeDataModel.models[model]?.dbName;
|
|
187
23
|
};
|
|
188
|
-
export
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
return _ts_generator(this, function(_state) {
|
|
192
|
-
switch(_state.label){
|
|
193
|
-
case 0:
|
|
194
|
-
return [
|
|
195
|
-
4,
|
|
196
|
-
searchMongoDbFull(model, search, [
|
|
197
|
-
'id'
|
|
198
|
-
], undefined)
|
|
199
|
-
];
|
|
200
|
-
case 1:
|
|
201
|
-
data = _state.sent();
|
|
202
|
-
return [
|
|
203
|
-
2,
|
|
204
|
-
data.map(function(o) {
|
|
205
|
-
return o._id;
|
|
206
|
-
})
|
|
207
|
-
];
|
|
208
|
-
}
|
|
209
|
-
});
|
|
210
|
-
})();
|
|
24
|
+
export const searchMongodb = async (model, search) => {
|
|
25
|
+
const data = await searchMongoDbFull(model, search, ['id'], undefined);
|
|
26
|
+
return data.map((o) => o._id);
|
|
211
27
|
};
|
|
212
|
-
export
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
return
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
client.db().collection(collectionName).find({
|
|
241
|
-
$text: {
|
|
242
|
-
$search: decodeURIComponent(search),
|
|
243
|
-
$caseSensitive: false
|
|
244
|
-
}
|
|
245
|
-
}, {
|
|
246
|
-
projection: projectionFields
|
|
247
|
-
}).sort({
|
|
248
|
-
score: {
|
|
249
|
-
$meta: 'textScore'
|
|
250
|
-
}
|
|
251
|
-
}).skip(skip).limit(limit).toArray()
|
|
252
|
-
];
|
|
253
|
-
case 2:
|
|
254
|
-
result = _state.sent();
|
|
255
|
-
return [
|
|
256
|
-
2,
|
|
257
|
-
result
|
|
258
|
-
];
|
|
259
|
-
}
|
|
260
|
-
});
|
|
261
|
-
})();
|
|
28
|
+
export const searchMongoDbFull = async (model, search, fields, skip = 0, limit = 10) => {
|
|
29
|
+
const client = await getClient();
|
|
30
|
+
const collectionName = getCollectionName(model);
|
|
31
|
+
if (!collectionName) {
|
|
32
|
+
return [];
|
|
33
|
+
}
|
|
34
|
+
const projectionFields = fields
|
|
35
|
+
.map((f) => (f === 'id' ? '_id' : f))
|
|
36
|
+
.reduce((acc, f) => {
|
|
37
|
+
Object.assign(acc, { [f]: 1 });
|
|
38
|
+
return acc;
|
|
39
|
+
}, {});
|
|
40
|
+
const result = await client
|
|
41
|
+
.db()
|
|
42
|
+
.collection(collectionName)
|
|
43
|
+
.find({
|
|
44
|
+
$text: {
|
|
45
|
+
$search: decodeURIComponent(search),
|
|
46
|
+
$caseSensitive: false,
|
|
47
|
+
},
|
|
48
|
+
}, {
|
|
49
|
+
projection: projectionFields,
|
|
50
|
+
})
|
|
51
|
+
.sort({ score: { $meta: 'textScore' } })
|
|
52
|
+
.skip(skip)
|
|
53
|
+
.limit(limit)
|
|
54
|
+
.toArray();
|
|
55
|
+
return result;
|
|
262
56
|
};
|
|
263
|
-
export
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
return [
|
|
277
|
-
4,
|
|
278
|
-
client.db().collection(collectionName).countDocuments({
|
|
279
|
-
$text: {
|
|
280
|
-
$search: decodeURIComponent(search),
|
|
281
|
-
$caseSensitive: false
|
|
282
|
-
}
|
|
283
|
-
})
|
|
284
|
-
];
|
|
285
|
-
case 2:
|
|
286
|
-
count = _state.sent();
|
|
287
|
-
return [
|
|
288
|
-
2,
|
|
289
|
-
count
|
|
290
|
-
];
|
|
291
|
-
}
|
|
292
|
-
});
|
|
293
|
-
})();
|
|
57
|
+
export const getMongoDbSearchResultCount = async (model, search) => {
|
|
58
|
+
const client = await getClient();
|
|
59
|
+
const collectionName = getCollectionName(model);
|
|
60
|
+
const count = await client
|
|
61
|
+
.db()
|
|
62
|
+
.collection(collectionName)
|
|
63
|
+
.countDocuments({
|
|
64
|
+
$text: {
|
|
65
|
+
$search: decodeURIComponent(search),
|
|
66
|
+
$caseSensitive: false,
|
|
67
|
+
},
|
|
68
|
+
});
|
|
69
|
+
return count;
|
|
294
70
|
};
|
|
71
|
+
//# sourceMappingURL=search-mongodb.js.map
|
package/dist/search.js
CHANGED
|
@@ -1,357 +1,57 @@
|
|
|
1
|
-
function _array_like_to_array(arr, len) {
|
|
2
|
-
if (len == null || len > arr.length) len = arr.length;
|
|
3
|
-
for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i];
|
|
4
|
-
return arr2;
|
|
5
|
-
}
|
|
6
|
-
function _array_with_holes(arr) {
|
|
7
|
-
if (Array.isArray(arr)) return arr;
|
|
8
|
-
}
|
|
9
|
-
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) {
|
|
10
|
-
try {
|
|
11
|
-
var info = gen[key](arg);
|
|
12
|
-
var value = info.value;
|
|
13
|
-
} catch (error) {
|
|
14
|
-
reject(error);
|
|
15
|
-
return;
|
|
16
|
-
}
|
|
17
|
-
if (info.done) {
|
|
18
|
-
resolve(value);
|
|
19
|
-
} else {
|
|
20
|
-
Promise.resolve(value).then(_next, _throw);
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
function _async_to_generator(fn) {
|
|
24
|
-
return function() {
|
|
25
|
-
var self = this, args = arguments;
|
|
26
|
-
return new Promise(function(resolve, reject) {
|
|
27
|
-
var gen = fn.apply(self, args);
|
|
28
|
-
function _next(value) {
|
|
29
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value);
|
|
30
|
-
}
|
|
31
|
-
function _throw(err) {
|
|
32
|
-
asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err);
|
|
33
|
-
}
|
|
34
|
-
_next(undefined);
|
|
35
|
-
});
|
|
36
|
-
};
|
|
37
|
-
}
|
|
38
|
-
function _define_property(obj, key, value) {
|
|
39
|
-
if (key in obj) {
|
|
40
|
-
Object.defineProperty(obj, key, {
|
|
41
|
-
value: value,
|
|
42
|
-
enumerable: true,
|
|
43
|
-
configurable: true,
|
|
44
|
-
writable: true
|
|
45
|
-
});
|
|
46
|
-
} else {
|
|
47
|
-
obj[key] = value;
|
|
48
|
-
}
|
|
49
|
-
return obj;
|
|
50
|
-
}
|
|
51
|
-
function _iterable_to_array_limit(arr, i) {
|
|
52
|
-
var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"];
|
|
53
|
-
if (_i == null) return;
|
|
54
|
-
var _arr = [];
|
|
55
|
-
var _n = true;
|
|
56
|
-
var _d = false;
|
|
57
|
-
var _s, _e;
|
|
58
|
-
try {
|
|
59
|
-
for(_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true){
|
|
60
|
-
_arr.push(_s.value);
|
|
61
|
-
if (i && _arr.length === i) break;
|
|
62
|
-
}
|
|
63
|
-
} catch (err) {
|
|
64
|
-
_d = true;
|
|
65
|
-
_e = err;
|
|
66
|
-
} finally{
|
|
67
|
-
try {
|
|
68
|
-
if (!_n && _i["return"] != null) _i["return"]();
|
|
69
|
-
} finally{
|
|
70
|
-
if (_d) throw _e;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
return _arr;
|
|
74
|
-
}
|
|
75
|
-
function _non_iterable_rest() {
|
|
76
|
-
throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
|
|
77
|
-
}
|
|
78
|
-
function _object_spread(target) {
|
|
79
|
-
for(var i = 1; i < arguments.length; i++){
|
|
80
|
-
var source = arguments[i] != null ? arguments[i] : {};
|
|
81
|
-
var ownKeys = Object.keys(source);
|
|
82
|
-
if (typeof Object.getOwnPropertySymbols === "function") {
|
|
83
|
-
ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function(sym) {
|
|
84
|
-
return Object.getOwnPropertyDescriptor(source, sym).enumerable;
|
|
85
|
-
}));
|
|
86
|
-
}
|
|
87
|
-
ownKeys.forEach(function(key) {
|
|
88
|
-
_define_property(target, key, source[key]);
|
|
89
|
-
});
|
|
90
|
-
}
|
|
91
|
-
return target;
|
|
92
|
-
}
|
|
93
|
-
function ownKeys(object, enumerableOnly) {
|
|
94
|
-
var keys = Object.keys(object);
|
|
95
|
-
if (Object.getOwnPropertySymbols) {
|
|
96
|
-
var symbols = Object.getOwnPropertySymbols(object);
|
|
97
|
-
if (enumerableOnly) {
|
|
98
|
-
symbols = symbols.filter(function(sym) {
|
|
99
|
-
return Object.getOwnPropertyDescriptor(object, sym).enumerable;
|
|
100
|
-
});
|
|
101
|
-
}
|
|
102
|
-
keys.push.apply(keys, symbols);
|
|
103
|
-
}
|
|
104
|
-
return keys;
|
|
105
|
-
}
|
|
106
|
-
function _object_spread_props(target, source) {
|
|
107
|
-
source = source != null ? source : {};
|
|
108
|
-
if (Object.getOwnPropertyDescriptors) {
|
|
109
|
-
Object.defineProperties(target, Object.getOwnPropertyDescriptors(source));
|
|
110
|
-
} else {
|
|
111
|
-
ownKeys(Object(source)).forEach(function(key) {
|
|
112
|
-
Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key));
|
|
113
|
-
});
|
|
114
|
-
}
|
|
115
|
-
return target;
|
|
116
|
-
}
|
|
117
|
-
function _object_without_properties(source, excluded) {
|
|
118
|
-
if (source == null) return {};
|
|
119
|
-
var target = {}, sourceKeys, key, i;
|
|
120
|
-
if (typeof Reflect !== "undefined" && Reflect.ownKeys) {
|
|
121
|
-
sourceKeys = Reflect.ownKeys(Object(source));
|
|
122
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
123
|
-
key = sourceKeys[i];
|
|
124
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
125
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
126
|
-
target[key] = source[key];
|
|
127
|
-
}
|
|
128
|
-
return target;
|
|
129
|
-
}
|
|
130
|
-
target = _object_without_properties_loose(source, excluded);
|
|
131
|
-
if (Object.getOwnPropertySymbols) {
|
|
132
|
-
sourceKeys = Object.getOwnPropertySymbols(source);
|
|
133
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
134
|
-
key = sourceKeys[i];
|
|
135
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
136
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
137
|
-
target[key] = source[key];
|
|
138
|
-
}
|
|
139
|
-
}
|
|
140
|
-
return target;
|
|
141
|
-
}
|
|
142
|
-
function _object_without_properties_loose(source, excluded) {
|
|
143
|
-
if (source == null) return {};
|
|
144
|
-
var target = {}, sourceKeys = Object.getOwnPropertyNames(source), key, i;
|
|
145
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
146
|
-
key = sourceKeys[i];
|
|
147
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
148
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
149
|
-
target[key] = source[key];
|
|
150
|
-
}
|
|
151
|
-
return target;
|
|
152
|
-
}
|
|
153
|
-
function _sliced_to_array(arr, i) {
|
|
154
|
-
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
155
|
-
}
|
|
156
|
-
function _unsupported_iterable_to_array(o, minLen) {
|
|
157
|
-
if (!o) return;
|
|
158
|
-
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
159
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
160
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
161
|
-
if (n === "Map" || n === "Set") return Array.from(n);
|
|
162
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
163
|
-
}
|
|
164
|
-
function _ts_generator(thisArg, body) {
|
|
165
|
-
var f, y, t, _ = {
|
|
166
|
-
label: 0,
|
|
167
|
-
sent: function() {
|
|
168
|
-
if (t[0] & 1) throw t[1];
|
|
169
|
-
return t[1];
|
|
170
|
-
},
|
|
171
|
-
trys: [],
|
|
172
|
-
ops: []
|
|
173
|
-
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype), d = Object.defineProperty;
|
|
174
|
-
return d(g, "next", {
|
|
175
|
-
value: verb(0)
|
|
176
|
-
}), d(g, "throw", {
|
|
177
|
-
value: verb(1)
|
|
178
|
-
}), d(g, "return", {
|
|
179
|
-
value: verb(2)
|
|
180
|
-
}), typeof Symbol === "function" && d(g, Symbol.iterator, {
|
|
181
|
-
value: function() {
|
|
182
|
-
return this;
|
|
183
|
-
}
|
|
184
|
-
}), g;
|
|
185
|
-
function verb(n) {
|
|
186
|
-
return function(v) {
|
|
187
|
-
return step([
|
|
188
|
-
n,
|
|
189
|
-
v
|
|
190
|
-
]);
|
|
191
|
-
};
|
|
192
|
-
}
|
|
193
|
-
function step(op) {
|
|
194
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
195
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
196
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
197
|
-
if (y = 0, t) op = [
|
|
198
|
-
op[0] & 2,
|
|
199
|
-
t.value
|
|
200
|
-
];
|
|
201
|
-
switch(op[0]){
|
|
202
|
-
case 0:
|
|
203
|
-
case 1:
|
|
204
|
-
t = op;
|
|
205
|
-
break;
|
|
206
|
-
case 4:
|
|
207
|
-
_.label++;
|
|
208
|
-
return {
|
|
209
|
-
value: op[1],
|
|
210
|
-
done: false
|
|
211
|
-
};
|
|
212
|
-
case 5:
|
|
213
|
-
_.label++;
|
|
214
|
-
y = op[1];
|
|
215
|
-
op = [
|
|
216
|
-
0
|
|
217
|
-
];
|
|
218
|
-
continue;
|
|
219
|
-
case 7:
|
|
220
|
-
op = _.ops.pop();
|
|
221
|
-
_.trys.pop();
|
|
222
|
-
continue;
|
|
223
|
-
default:
|
|
224
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
225
|
-
_ = 0;
|
|
226
|
-
continue;
|
|
227
|
-
}
|
|
228
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
229
|
-
_.label = op[1];
|
|
230
|
-
break;
|
|
231
|
-
}
|
|
232
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
233
|
-
_.label = t[1];
|
|
234
|
-
t = op;
|
|
235
|
-
break;
|
|
236
|
-
}
|
|
237
|
-
if (t && _.label < t[2]) {
|
|
238
|
-
_.label = t[2];
|
|
239
|
-
_.ops.push(op);
|
|
240
|
-
break;
|
|
241
|
-
}
|
|
242
|
-
if (t[2]) _.ops.pop();
|
|
243
|
-
_.trys.pop();
|
|
244
|
-
continue;
|
|
245
|
-
}
|
|
246
|
-
op = body.call(thisArg, _);
|
|
247
|
-
} catch (e) {
|
|
248
|
-
op = [
|
|
249
|
-
6,
|
|
250
|
-
e
|
|
251
|
-
];
|
|
252
|
-
y = 0;
|
|
253
|
-
} finally{
|
|
254
|
-
f = t = 0;
|
|
255
|
-
}
|
|
256
|
-
if (op[0] & 5) throw op[1];
|
|
257
|
-
return {
|
|
258
|
-
value: op[0] ? op[1] : void 0,
|
|
259
|
-
done: true
|
|
260
|
-
};
|
|
261
|
-
}
|
|
262
|
-
}
|
|
263
1
|
import { prisma } from '@driveflux/db';
|
|
264
2
|
import { getIdRegex, ID_PREFIX_MAP } from '@driveflux/db/id';
|
|
265
3
|
import { Ok } from '@driveflux/result';
|
|
266
4
|
import { camelCase } from 'change-case';
|
|
267
5
|
import { z } from 'zod';
|
|
268
6
|
import { searchMongoDbFull } from './search-mongodb.js';
|
|
269
|
-
import { SEARCH_RESULT_FIELDS_MAP } from './types.js';
|
|
270
|
-
|
|
271
|
-
|
|
7
|
+
import { SEARCH_RESULT_FIELDS_MAP, } from './types.js';
|
|
8
|
+
const SEARCHABLE_MODELS = Object.keys(SEARCH_RESULT_FIELDS_MAP);
|
|
9
|
+
const Query = z.object({
|
|
272
10
|
q: z.string(),
|
|
273
|
-
take: z.number().default(5)
|
|
274
|
-
});
|
|
275
|
-
var ID_PREFIXES = Object.entries(ID_PREFIX_MAP).filter(function(param) {
|
|
276
|
-
var _param = _sliced_to_array(param, 1), model = _param[0];
|
|
277
|
-
return SEARCHABLE_MODELS.includes(model);
|
|
278
|
-
}).map(function(param) {
|
|
279
|
-
var _param = _sliced_to_array(param, 2), _ = _param[0], prefix = _param[1];
|
|
280
|
-
return prefix;
|
|
11
|
+
take: z.number().default(5),
|
|
281
12
|
});
|
|
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
|
-
case 2:
|
|
326
|
-
return [
|
|
327
|
-
4,
|
|
328
|
-
Promise.allSettled(SEARCHABLE_MODELS.map(function(m) {
|
|
329
|
-
return searchMongoDbFull(m, q, SEARCH_RESULT_FIELDS_MAP[m], take);
|
|
330
|
-
}))
|
|
331
|
-
];
|
|
332
|
-
case 3:
|
|
333
|
-
results = _state.sent();
|
|
334
|
-
return [
|
|
335
|
-
2,
|
|
336
|
-
new Ok(Object.fromEntries(SEARCHABLE_MODELS.map(function(m, i) {
|
|
337
|
-
var result = results[i];
|
|
338
|
-
return [
|
|
339
|
-
m,
|
|
340
|
-
result.status === 'fulfilled' ? result.value.map(function(o) {
|
|
341
|
-
var _id = o._id, rest = _object_without_properties(o, [
|
|
342
|
-
"_id"
|
|
343
|
-
]);
|
|
344
|
-
return _id ? _object_spread_props(_object_spread({}, rest), {
|
|
345
|
-
id: "".concat(_id)
|
|
346
|
-
}) : rest;
|
|
347
|
-
}) : []
|
|
348
|
-
];
|
|
349
|
-
}).filter(function(param) {
|
|
350
|
-
var _param = _sliced_to_array(param, 2), v = _param[1];
|
|
351
|
-
return v.length > 0;
|
|
352
|
-
})))
|
|
353
|
-
];
|
|
354
|
-
}
|
|
355
|
-
});
|
|
356
|
-
})();
|
|
13
|
+
const ID_PREFIXES = Object.entries(ID_PREFIX_MAP)
|
|
14
|
+
.filter(([model]) => SEARCHABLE_MODELS.includes(model))
|
|
15
|
+
.map(([_, prefix]) => prefix);
|
|
16
|
+
export const search = async (req) => {
|
|
17
|
+
const { q, take } = Query.parse(req.query);
|
|
18
|
+
const matches = q.match(getIdRegex(ID_PREFIXES));
|
|
19
|
+
let targetModel;
|
|
20
|
+
if (matches) {
|
|
21
|
+
const id = matches[0];
|
|
22
|
+
const prefix = matches[1];
|
|
23
|
+
targetModel = Object.keys(SEARCH_RESULT_FIELDS_MAP).find((key) => ID_PREFIX_MAP[key] === prefix);
|
|
24
|
+
if (targetModel) {
|
|
25
|
+
// TODO
|
|
26
|
+
// @ts-expect-error prisma can't union models
|
|
27
|
+
const result = await prisma[camelCase(targetModel)].findUnique({
|
|
28
|
+
where: {
|
|
29
|
+
id,
|
|
30
|
+
},
|
|
31
|
+
select: Object.fromEntries(SEARCH_RESULT_FIELDS_MAP[targetModel].map((f) => [f, true])),
|
|
32
|
+
});
|
|
33
|
+
return new Ok({
|
|
34
|
+
[targetModel]: result ? [result] : [],
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
const results = await Promise.allSettled(SEARCHABLE_MODELS.map((m) => searchMongoDbFull(m, q, SEARCH_RESULT_FIELDS_MAP[m], take)));
|
|
39
|
+
return new Ok(Object.fromEntries(SEARCHABLE_MODELS.map((m, i) => {
|
|
40
|
+
const result = results[i];
|
|
41
|
+
return [
|
|
42
|
+
m,
|
|
43
|
+
result.status === 'fulfilled'
|
|
44
|
+
? result.value.map((o) => {
|
|
45
|
+
const { _id, ...rest } = o;
|
|
46
|
+
return _id
|
|
47
|
+
? {
|
|
48
|
+
...rest,
|
|
49
|
+
id: `${_id}`,
|
|
50
|
+
}
|
|
51
|
+
: rest;
|
|
52
|
+
})
|
|
53
|
+
: [],
|
|
54
|
+
];
|
|
55
|
+
}).filter(([, v]) => v.length > 0)));
|
|
357
56
|
};
|
|
57
|
+
//# sourceMappingURL=search.js.map
|
package/dist/types.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const SEARCH_RESULT_FIELDS_MAP = {
|
|
2
2
|
Subscription: [
|
|
3
3
|
'id',
|
|
4
4
|
'userName',
|
|
5
5
|
'vehicleName',
|
|
6
6
|
'businessName',
|
|
7
7
|
'fluxStatus',
|
|
8
|
-
'payerType'
|
|
8
|
+
'payerType',
|
|
9
9
|
],
|
|
10
10
|
Vehicle: [
|
|
11
11
|
'id',
|
|
@@ -16,7 +16,7 @@ export var SEARCH_RESULT_FIELDS_MAP = {
|
|
|
16
16
|
'niceName',
|
|
17
17
|
'registrationNumber',
|
|
18
18
|
'status',
|
|
19
|
-
'hostId'
|
|
19
|
+
'hostId',
|
|
20
20
|
],
|
|
21
21
|
User: [
|
|
22
22
|
'id',
|
|
@@ -25,25 +25,11 @@ export var SEARCH_RESULT_FIELDS_MAP = {
|
|
|
25
25
|
'email',
|
|
26
26
|
'department',
|
|
27
27
|
'groups',
|
|
28
|
-
'status'
|
|
29
|
-
],
|
|
30
|
-
Business: [
|
|
31
|
-
'id',
|
|
32
|
-
'name',
|
|
33
|
-
'registrationNumber'
|
|
34
|
-
],
|
|
35
|
-
Host: [
|
|
36
|
-
'id',
|
|
37
|
-
'name',
|
|
38
|
-
'email',
|
|
39
|
-
'accountManagerId'
|
|
40
|
-
],
|
|
41
|
-
Coupon: [
|
|
42
|
-
'id',
|
|
43
|
-
'code',
|
|
44
|
-
'name',
|
|
45
|
-
'status'
|
|
28
|
+
'status',
|
|
46
29
|
],
|
|
30
|
+
Business: ['id', 'name', 'registrationNumber'],
|
|
31
|
+
Host: ['id', 'name', 'email', 'accountManagerId'],
|
|
32
|
+
Coupon: ['id', 'code', 'name', 'status'],
|
|
47
33
|
Invoice: [
|
|
48
34
|
'id',
|
|
49
35
|
'notes',
|
|
@@ -51,7 +37,7 @@ export var SEARCH_RESULT_FIELDS_MAP = {
|
|
|
51
37
|
'paid',
|
|
52
38
|
'status',
|
|
53
39
|
'total',
|
|
54
|
-
'payerName'
|
|
40
|
+
'payerName',
|
|
55
41
|
],
|
|
56
42
|
Payout: [
|
|
57
43
|
'id',
|
|
@@ -59,23 +45,10 @@ export var SEARCH_RESULT_FIELDS_MAP = {
|
|
|
59
45
|
'paidOut',
|
|
60
46
|
'hostId',
|
|
61
47
|
'reversalTotal',
|
|
62
|
-
'toPayAt'
|
|
63
|
-
],
|
|
64
|
-
ServiceCenter: [
|
|
65
|
-
'id',
|
|
66
|
-
'name',
|
|
67
|
-
'address'
|
|
48
|
+
'toPayAt',
|
|
68
49
|
],
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
'subscriptionId',
|
|
73
|
-
'status'
|
|
74
|
-
],
|
|
75
|
-
Log: [
|
|
76
|
-
'id',
|
|
77
|
-
'userId',
|
|
78
|
-
'resourceId',
|
|
79
|
-
'resourceType'
|
|
80
|
-
]
|
|
50
|
+
ServiceCenter: ['id', 'name', 'address'],
|
|
51
|
+
SubscriptionReservation: ['id', 'vehicleId', 'subscriptionId', 'status'],
|
|
52
|
+
Log: ['id', 'userId', 'resourceId', 'resourceType'],
|
|
81
53
|
};
|
|
54
|
+
//# sourceMappingURL=types.js.map
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@driveflux/search",
|
|
3
|
-
"version": "5.0.
|
|
3
|
+
"version": "5.0.78",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -12,7 +12,7 @@
|
|
|
12
12
|
"dist"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@driveflux/db": "4.1.
|
|
15
|
+
"@driveflux/db": "4.1.20",
|
|
16
16
|
"@driveflux/result": "6.0.2",
|
|
17
17
|
"change-case": "^5.4.4",
|
|
18
18
|
"mongodb": "^7.2.0",
|