UNPKG

mongo-ts-struct

Version:

Mongoose wrapper for Typescript supports

155 lines 5.06 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); class MetadataAgent { static set(classObject, [path, value]) { const original = classObject; const proto = this.getPrototype(original); const metadata = proto['$metadata'] || {}; setValueUsingNestedPath(metadata, path, value); proto['$metadata'] = metadata; } static has(classObject, path) { const original = classObject; const proto = this.getPrototype(original); const metadata = proto['$metadata'] || {}; return hasValueUsingNestedPath(metadata, path); } static assign(classObject, [path, value]) { const original = classObject; const proto = this.getPrototype(original); const metadata = proto['$metadata'] || {}; assignValueUsingNestedPath(metadata, path, value); proto['$metadata'] = metadata; } static getMeta(classObject) { const original = classObject; const proto = this.getPrototype(original); const metadata = proto['$metadata']; return metadata; } static getPrototype(classObject) { if (classObject.prototype == undefined) { return classObject; } const proto = classObject.prototype; return proto; } } exports.MetadataAgent = MetadataAgent; function hasValueUsingNestedPath(obj, path) { const splitPath = path.split('.'); let target = obj; if (!obj || splitPath.length == 0) { return false; } for (let i = 0; i < splitPath.length; i++) { const slice = splitPath[i]; try { target = target[slice]; if (target == undefined || target == null) { return false; } } catch (error) { return false; } } return true; } function setValueUsingNestedPath(obj, path, value) { const splitPath = path.split('.'); if (splitPath.length == 1) { obj[splitPath[0]] = value; } else if (splitPath.length > 1) { /* let valueParentObj = obj; for (let i = 0; i < splitPath.length-1; i++) { const slice = splitPath[i]; try { let currentLevel = valueParentObj[slice]; if(currentLevel == undefined || currentLevel == null) { valueParentObj[slice] = {}; } else if(typeof currentLevel != 'object') { return false; } valueParentObj = valueParentObj[slice]; } catch (error) { return false; } } */ const valueParentObj = digAndBuildPath(obj, splitPath); if (!valueParentObj) { return false; } valueParentObj[splitPath[splitPath.length - 1]] = value; return true; } } function assignValueUsingNestedPath(obj, path, value) { const splitPath = path.split('.'); if (splitPath.length == 1) { obj[splitPath[0]] = value; } else if (splitPath.length > 1) { /* let valueParentObj = obj; for (let i = 0; i < splitPath.length-1; i++) { const slice = splitPath[i]; try { let currentLevel = valueParentObj[slice]; if(currentLevel == undefined || currentLevel == null) { valueParentObj[slice] = {}; } else if(typeof currentLevel != 'object') { return false; } valueParentObj = valueParentObj[slice]; } catch (error) { return false; } } */ const valueParentObj = digAndBuildPath(obj, splitPath); if (!valueParentObj) { return false; } if (typeof value == 'object') { const existingValue = valueParentObj[splitPath[splitPath.length - 1]] || {}; valueParentObj[splitPath[splitPath.length - 1]] = Object.assign({}, existingValue, value); } else { valueParentObj[splitPath[splitPath.length - 1]] = value; } return true; } } function digAndBuildPath(obj, splitPath) { let valueParentObj = obj; for (let i = 0; i < splitPath.length - 1; i++) { const slice = splitPath[i]; try { let currentLevel = valueParentObj[slice]; if (currentLevel == undefined || currentLevel == null) { valueParentObj[slice] = {}; } else if (typeof currentLevel != 'object') { return; } valueParentObj = valueParentObj[slice]; } catch (error) { return; } } return valueParentObj; } function isTypedSchemaClass(typeCtor) { const name = typeCtor.name; if (!name) { return false; } return MetadataAgent.has(typeCtor, `isProcessed:${name}`); } exports.isTypedSchemaClass = isTypedSchemaClass; //# sourceMappingURL=index.js.map