@driveflux/search 5.0.53 → 5.0.54
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 -275
- package/dist/search.js +48 -331
- package/dist/types.js +13 -40
- package/package.json +3 -3
package/dist/index.js
CHANGED
package/dist/search-mongodb.js
CHANGED
|
@@ -1,286 +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);
|
|
53
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
54
|
-
return this;
|
|
55
|
-
}), g;
|
|
56
|
-
function verb(n) {
|
|
57
|
-
return function(v) {
|
|
58
|
-
return step([
|
|
59
|
-
n,
|
|
60
|
-
v
|
|
61
|
-
]);
|
|
62
|
-
};
|
|
63
|
-
}
|
|
64
|
-
function step(op) {
|
|
65
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
66
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
67
|
-
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;
|
|
68
|
-
if (y = 0, t) op = [
|
|
69
|
-
op[0] & 2,
|
|
70
|
-
t.value
|
|
71
|
-
];
|
|
72
|
-
switch(op[0]){
|
|
73
|
-
case 0:
|
|
74
|
-
case 1:
|
|
75
|
-
t = op;
|
|
76
|
-
break;
|
|
77
|
-
case 4:
|
|
78
|
-
_.label++;
|
|
79
|
-
return {
|
|
80
|
-
value: op[1],
|
|
81
|
-
done: false
|
|
82
|
-
};
|
|
83
|
-
case 5:
|
|
84
|
-
_.label++;
|
|
85
|
-
y = op[1];
|
|
86
|
-
op = [
|
|
87
|
-
0
|
|
88
|
-
];
|
|
89
|
-
continue;
|
|
90
|
-
case 7:
|
|
91
|
-
op = _.ops.pop();
|
|
92
|
-
_.trys.pop();
|
|
93
|
-
continue;
|
|
94
|
-
default:
|
|
95
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
96
|
-
_ = 0;
|
|
97
|
-
continue;
|
|
98
|
-
}
|
|
99
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
100
|
-
_.label = op[1];
|
|
101
|
-
break;
|
|
102
|
-
}
|
|
103
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
104
|
-
_.label = t[1];
|
|
105
|
-
t = op;
|
|
106
|
-
break;
|
|
107
|
-
}
|
|
108
|
-
if (t && _.label < t[2]) {
|
|
109
|
-
_.label = t[2];
|
|
110
|
-
_.ops.push(op);
|
|
111
|
-
break;
|
|
112
|
-
}
|
|
113
|
-
if (t[2]) _.ops.pop();
|
|
114
|
-
_.trys.pop();
|
|
115
|
-
continue;
|
|
116
|
-
}
|
|
117
|
-
op = body.call(thisArg, _);
|
|
118
|
-
} catch (e) {
|
|
119
|
-
op = [
|
|
120
|
-
6,
|
|
121
|
-
e
|
|
122
|
-
];
|
|
123
|
-
y = 0;
|
|
124
|
-
} finally{
|
|
125
|
-
f = t = 0;
|
|
126
|
-
}
|
|
127
|
-
if (op[0] & 5) throw op[1];
|
|
128
|
-
return {
|
|
129
|
-
value: op[0] ? op[1] : void 0,
|
|
130
|
-
done: true
|
|
131
|
-
};
|
|
132
|
-
}
|
|
133
|
-
}
|
|
134
1
|
import { prisma } from '@driveflux/db';
|
|
135
2
|
import { instantiateSingleton, singleton } from '@driveflux/singleton';
|
|
136
3
|
import { MongoClient } from 'mongodb';
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
return new MongoClient(process.env.DATABASE_URL);
|
|
145
|
-
});
|
|
146
|
-
isConnected = singleton('search__MongoClientConnected', false);
|
|
147
|
-
if (!!isConnected) return [
|
|
148
|
-
3,
|
|
149
|
-
2
|
|
150
|
-
];
|
|
151
|
-
client.on('connectionReady', function() {
|
|
152
|
-
// update the singleton value to true (second true is to force the value to be true)
|
|
153
|
-
singleton('search__MongoClientConnected', true, true);
|
|
154
|
-
});
|
|
155
|
-
client.on('close', function() {
|
|
156
|
-
singleton('search__MongoClientConnected', false, true);
|
|
157
|
-
});
|
|
158
|
-
return [
|
|
159
|
-
4,
|
|
160
|
-
client.connect()
|
|
161
|
-
];
|
|
162
|
-
case 1:
|
|
163
|
-
_state.sent();
|
|
164
|
-
_state.label = 2;
|
|
165
|
-
case 2:
|
|
166
|
-
return [
|
|
167
|
-
2,
|
|
168
|
-
client
|
|
169
|
-
];
|
|
170
|
-
}
|
|
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);
|
|
171
11
|
});
|
|
172
|
-
|
|
12
|
+
client.on('close', () => {
|
|
13
|
+
singleton('search__MongoClientConnected', false, true);
|
|
14
|
+
});
|
|
15
|
+
await client.connect();
|
|
16
|
+
}
|
|
17
|
+
return client;
|
|
173
18
|
};
|
|
174
|
-
|
|
175
|
-
var _prisma__runtimeDataModel_models_model;
|
|
19
|
+
const getCollectionName = (model) => {
|
|
176
20
|
// TODO
|
|
177
21
|
// @ts-expect-error - This is a private property
|
|
178
|
-
return
|
|
22
|
+
return prisma._runtimeDataModel.models[model]?.dbName;
|
|
179
23
|
};
|
|
180
|
-
export
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
return _ts_generator(this, function(_state) {
|
|
184
|
-
switch(_state.label){
|
|
185
|
-
case 0:
|
|
186
|
-
return [
|
|
187
|
-
4,
|
|
188
|
-
searchMongoDbFull(model, search, [
|
|
189
|
-
'id'
|
|
190
|
-
], undefined)
|
|
191
|
-
];
|
|
192
|
-
case 1:
|
|
193
|
-
data = _state.sent();
|
|
194
|
-
return [
|
|
195
|
-
2,
|
|
196
|
-
data.map(function(o) {
|
|
197
|
-
return o._id;
|
|
198
|
-
})
|
|
199
|
-
];
|
|
200
|
-
}
|
|
201
|
-
});
|
|
202
|
-
})();
|
|
24
|
+
export const searchMongodb = async (model, search) => {
|
|
25
|
+
const data = await searchMongoDbFull(model, search, ['id'], undefined);
|
|
26
|
+
return data.map((o) => o._id);
|
|
203
27
|
};
|
|
204
|
-
export
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
return
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
client.db().collection(collectionName).find({
|
|
233
|
-
$text: {
|
|
234
|
-
$search: decodeURIComponent(search),
|
|
235
|
-
$caseSensitive: false
|
|
236
|
-
}
|
|
237
|
-
}, {
|
|
238
|
-
projection: projectionFields
|
|
239
|
-
}).sort({
|
|
240
|
-
score: {
|
|
241
|
-
$meta: 'textScore'
|
|
242
|
-
}
|
|
243
|
-
}).skip(skip).limit(limit).toArray()
|
|
244
|
-
];
|
|
245
|
-
case 2:
|
|
246
|
-
result = _state.sent();
|
|
247
|
-
return [
|
|
248
|
-
2,
|
|
249
|
-
result
|
|
250
|
-
];
|
|
251
|
-
}
|
|
252
|
-
});
|
|
253
|
-
})();
|
|
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;
|
|
254
56
|
};
|
|
255
|
-
export
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
return [
|
|
269
|
-
4,
|
|
270
|
-
client.db().collection(collectionName).countDocuments({
|
|
271
|
-
$text: {
|
|
272
|
-
$search: decodeURIComponent(search),
|
|
273
|
-
$caseSensitive: false
|
|
274
|
-
}
|
|
275
|
-
})
|
|
276
|
-
];
|
|
277
|
-
case 2:
|
|
278
|
-
count = _state.sent();
|
|
279
|
-
return [
|
|
280
|
-
2,
|
|
281
|
-
count
|
|
282
|
-
];
|
|
283
|
-
}
|
|
284
|
-
});
|
|
285
|
-
})();
|
|
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;
|
|
286
70
|
};
|
|
71
|
+
//# sourceMappingURL=search-mongodb.js.map
|
package/dist/search.js
CHANGED
|
@@ -1,340 +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 = _object_without_properties_loose(source, excluded);
|
|
120
|
-
var key, i;
|
|
121
|
-
if (Object.getOwnPropertySymbols) {
|
|
122
|
-
var sourceSymbolKeys = Object.getOwnPropertySymbols(source);
|
|
123
|
-
for(i = 0; i < sourceSymbolKeys.length; i++){
|
|
124
|
-
key = sourceSymbolKeys[i];
|
|
125
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
126
|
-
if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue;
|
|
127
|
-
target[key] = source[key];
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
return target;
|
|
131
|
-
}
|
|
132
|
-
function _object_without_properties_loose(source, excluded) {
|
|
133
|
-
if (source == null) return {};
|
|
134
|
-
var target = {};
|
|
135
|
-
var sourceKeys = Object.keys(source);
|
|
136
|
-
var key, i;
|
|
137
|
-
for(i = 0; i < sourceKeys.length; i++){
|
|
138
|
-
key = sourceKeys[i];
|
|
139
|
-
if (excluded.indexOf(key) >= 0) continue;
|
|
140
|
-
target[key] = source[key];
|
|
141
|
-
}
|
|
142
|
-
return target;
|
|
143
|
-
}
|
|
144
|
-
function _sliced_to_array(arr, i) {
|
|
145
|
-
return _array_with_holes(arr) || _iterable_to_array_limit(arr, i) || _unsupported_iterable_to_array(arr, i) || _non_iterable_rest();
|
|
146
|
-
}
|
|
147
|
-
function _unsupported_iterable_to_array(o, minLen) {
|
|
148
|
-
if (!o) return;
|
|
149
|
-
if (typeof o === "string") return _array_like_to_array(o, minLen);
|
|
150
|
-
var n = Object.prototype.toString.call(o).slice(8, -1);
|
|
151
|
-
if (n === "Object" && o.constructor) n = o.constructor.name;
|
|
152
|
-
if (n === "Map" || n === "Set") return Array.from(n);
|
|
153
|
-
if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _array_like_to_array(o, minLen);
|
|
154
|
-
}
|
|
155
|
-
function _ts_generator(thisArg, body) {
|
|
156
|
-
var f, y, t, _ = {
|
|
157
|
-
label: 0,
|
|
158
|
-
sent: function() {
|
|
159
|
-
if (t[0] & 1) throw t[1];
|
|
160
|
-
return t[1];
|
|
161
|
-
},
|
|
162
|
-
trys: [],
|
|
163
|
-
ops: []
|
|
164
|
-
}, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
|
|
165
|
-
return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() {
|
|
166
|
-
return this;
|
|
167
|
-
}), g;
|
|
168
|
-
function verb(n) {
|
|
169
|
-
return function(v) {
|
|
170
|
-
return step([
|
|
171
|
-
n,
|
|
172
|
-
v
|
|
173
|
-
]);
|
|
174
|
-
};
|
|
175
|
-
}
|
|
176
|
-
function step(op) {
|
|
177
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
178
|
-
while(g && (g = 0, op[0] && (_ = 0)), _)try {
|
|
179
|
-
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;
|
|
180
|
-
if (y = 0, t) op = [
|
|
181
|
-
op[0] & 2,
|
|
182
|
-
t.value
|
|
183
|
-
];
|
|
184
|
-
switch(op[0]){
|
|
185
|
-
case 0:
|
|
186
|
-
case 1:
|
|
187
|
-
t = op;
|
|
188
|
-
break;
|
|
189
|
-
case 4:
|
|
190
|
-
_.label++;
|
|
191
|
-
return {
|
|
192
|
-
value: op[1],
|
|
193
|
-
done: false
|
|
194
|
-
};
|
|
195
|
-
case 5:
|
|
196
|
-
_.label++;
|
|
197
|
-
y = op[1];
|
|
198
|
-
op = [
|
|
199
|
-
0
|
|
200
|
-
];
|
|
201
|
-
continue;
|
|
202
|
-
case 7:
|
|
203
|
-
op = _.ops.pop();
|
|
204
|
-
_.trys.pop();
|
|
205
|
-
continue;
|
|
206
|
-
default:
|
|
207
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
|
|
208
|
-
_ = 0;
|
|
209
|
-
continue;
|
|
210
|
-
}
|
|
211
|
-
if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
|
|
212
|
-
_.label = op[1];
|
|
213
|
-
break;
|
|
214
|
-
}
|
|
215
|
-
if (op[0] === 6 && _.label < t[1]) {
|
|
216
|
-
_.label = t[1];
|
|
217
|
-
t = op;
|
|
218
|
-
break;
|
|
219
|
-
}
|
|
220
|
-
if (t && _.label < t[2]) {
|
|
221
|
-
_.label = t[2];
|
|
222
|
-
_.ops.push(op);
|
|
223
|
-
break;
|
|
224
|
-
}
|
|
225
|
-
if (t[2]) _.ops.pop();
|
|
226
|
-
_.trys.pop();
|
|
227
|
-
continue;
|
|
228
|
-
}
|
|
229
|
-
op = body.call(thisArg, _);
|
|
230
|
-
} catch (e) {
|
|
231
|
-
op = [
|
|
232
|
-
6,
|
|
233
|
-
e
|
|
234
|
-
];
|
|
235
|
-
y = 0;
|
|
236
|
-
} finally{
|
|
237
|
-
f = t = 0;
|
|
238
|
-
}
|
|
239
|
-
if (op[0] & 5) throw op[1];
|
|
240
|
-
return {
|
|
241
|
-
value: op[0] ? op[1] : void 0,
|
|
242
|
-
done: true
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
}
|
|
246
1
|
import { prisma } from '@driveflux/db';
|
|
247
2
|
import { getIdRegex, ID_PREFIX_MAP } from '@driveflux/db/id';
|
|
248
3
|
import { Ok } from '@driveflux/result';
|
|
249
4
|
import { camelCase } from 'change-case';
|
|
250
5
|
import { z } from 'zod';
|
|
251
6
|
import { searchMongoDbFull } from './search-mongodb.js';
|
|
252
|
-
import { SEARCH_RESULT_FIELDS_MAP } from './types.js';
|
|
253
|
-
|
|
254
|
-
|
|
7
|
+
import { SEARCH_RESULT_FIELDS_MAP, } from './types.js';
|
|
8
|
+
const SEARCHABLE_MODELS = Object.keys(SEARCH_RESULT_FIELDS_MAP);
|
|
9
|
+
const Query = z.object({
|
|
255
10
|
q: z.string(),
|
|
256
|
-
take: z.number().default(5)
|
|
11
|
+
take: z.number().default(5),
|
|
257
12
|
});
|
|
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
|
-
result = _state.sent();
|
|
302
|
-
return [
|
|
303
|
-
2,
|
|
304
|
-
new Ok(_define_property({}, targetModel, result ? [
|
|
305
|
-
result
|
|
306
|
-
] : []))
|
|
307
|
-
];
|
|
308
|
-
case 2:
|
|
309
|
-
return [
|
|
310
|
-
4,
|
|
311
|
-
Promise.allSettled(SEARCHABLE_MODELS.map(function(m) {
|
|
312
|
-
return searchMongoDbFull(m, q, SEARCH_RESULT_FIELDS_MAP[m], take);
|
|
313
|
-
}))
|
|
314
|
-
];
|
|
315
|
-
case 3:
|
|
316
|
-
results = _state.sent();
|
|
317
|
-
return [
|
|
318
|
-
2,
|
|
319
|
-
new Ok(Object.fromEntries(SEARCHABLE_MODELS.map(function(m, i) {
|
|
320
|
-
var result = results[i];
|
|
321
|
-
return [
|
|
322
|
-
m,
|
|
323
|
-
result.status === 'fulfilled' ? result.value.map(function(o) {
|
|
324
|
-
var _id = o._id, rest = _object_without_properties(o, [
|
|
325
|
-
"_id"
|
|
326
|
-
]);
|
|
327
|
-
return _id ? _object_spread_props(_object_spread({}, rest), {
|
|
328
|
-
id: "".concat(_id)
|
|
329
|
-
}) : rest;
|
|
330
|
-
}) : []
|
|
331
|
-
];
|
|
332
|
-
}).filter(function(param) {
|
|
333
|
-
var _param = _sliced_to_array(param, 2), v = _param[1];
|
|
334
|
-
return v.length > 0;
|
|
335
|
-
})))
|
|
336
|
-
];
|
|
337
|
-
}
|
|
338
|
-
});
|
|
339
|
-
})();
|
|
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)));
|
|
340
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.54",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": {
|
|
@@ -12,8 +12,8 @@
|
|
|
12
12
|
"dist"
|
|
13
13
|
],
|
|
14
14
|
"dependencies": {
|
|
15
|
-
"@driveflux/db": "4.0.
|
|
16
|
-
"@driveflux/result": "6.0.
|
|
15
|
+
"@driveflux/db": "4.0.34",
|
|
16
|
+
"@driveflux/result": "6.0.1",
|
|
17
17
|
"change-case": "^5.4.4",
|
|
18
18
|
"mongodb": "^6.20.0",
|
|
19
19
|
"zod": "^4.1.12"
|