forked from nbredikhin/fivem-mongodb
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.js
37 lines (32 loc) · 893 Bytes
/
utils.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
const mongodb = require("mongodb");
function exportDocument(document) {
if (!document) return;
if (document._id && typeof document._id !== "string") {
document._id = document._id.toString();
}
return document;
};
function exportDocuments(documents) {
if (!Array.isArray(documents)) return;
return documents.map((document => exportDocument(document)));
}
function safeObjectArgument(object) {
if (!object) return {};
if (Array.isArray(object)) {
if (object.length === 0) return {};
return object;
}
if (typeof object !== "object") return {};
if (object._id) object._id = mongodb.ObjectId(object._id);
return object;
}
function safeCallback(cb, ...args) {
if(typeof cb !== "function") return;
cb(...args);
}
module.exports = {
exportDocument,
exportDocuments,
safeObjectArgument,
safeCallback
}