MCPcopy
hub / github.com/Pythagora-io/pythagora / jsonObjToMongo

Function jsonObjToMongo

src/helpers/mongodb.js:51–77  ·  view source on GitHub ↗
(originalObj)

Source from the content-addressed store, hash-verified

49}
50
51function jsonObjToMongo(originalObj) {
52 let obj = _.clone(originalObj);
53 if (!obj) return obj;
54 if (Array.isArray(obj)) return obj.map(d => jsonObjToMongo(d));
55 else if (typeof obj === 'string' && objectIdAsStringRegex.test(obj)) return stringToMongoObjectId(obj);
56 else if (typeof obj === 'string' && mongoIdRegex.test(obj)) return stringToMongoObjectId(`ObjectId("${obj}")`);
57 else if (typeof obj === 'string' && dateAsStringRegex.test(obj)) return stringToDate(obj);
58 else if (typeof obj === 'string' && regExpRegex.test(obj)) return stringToRegExp(obj);
59 else if (isJSONObject(obj)) {
60 obj = convertToRegularObject(obj);
61 for (let key in obj) {
62 if (!obj[key]) continue;
63 else if (typeof obj[key] === 'string') {
64 // TODO label a key as ObjectId better (not through a string)
65 if (objectIdAsStringRegex.test(obj[key])) obj[key] = stringToMongoObjectId(obj[key]);
66 else if (mongoIdRegex.test(obj[key])) obj[key] = stringToMongoObjectId(`ObjectId("${obj[key]}")`);
67 else if (dateAsStringRegex.test(obj[key])) obj[key] = stringToDate(obj[key]);
68 else if (regExpRegex.test(obj[key])) obj[key] = stringToRegExp(obj[key]);
69 } else if (obj[key]._bsontype === "ObjectID") {
70 continue;
71 } else if (isJSONObject(obj[key]) || Array.isArray(obj[key])) {
72 obj[key] = jsonObjToMongo(obj[key]);
73 }
74 }
75 }
76 return obj;
77}
78
79function stringToMongoObjectId(str) {
80 let idValue = str.match(objectIdAsStringRegex);

Callers 5

prepareDBFunction · 0.85
getMongoDocsFunction · 0.85
prepareDBFunction · 0.85
setUpDbFunction · 0.85

Calls 5

stringToMongoObjectIdFunction · 0.85
stringToDateFunction · 0.85
stringToRegExpFunction · 0.85
isJSONObjectFunction · 0.85
convertToRegularObjectFunction · 0.85

Tested by

no test coverage detected