UNPKG

slim-ef

Version:

An implementation of basic entity framework functionnalities in typescript

86 lines 3.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getMetaData = void 0; const metadata_proxy_1 = require("./metadata-proxy"); const map = new WeakMap(); function getMetaData(con, type, includePaths) { // if (map.has(type)) return map.get(type); // const meta = _getMetaData<T>(con, type, [], '', includePaths); // map.set(type, meta); // TODO: Find a way to cache this with the include paths return _getMetaData(con, type, [], '', includePaths); } exports.getMetaData = getMetaData; function _getMetaData(con, type, constructorChain = [], basePropName = '', includePaths = []) { const meta = con.getMetadata(type); // TODO: find a way to better cache the constructor chain. return _getTypeMetaData(meta, type, con, constructorChain, basePropName, includePaths); } function _getTypeMetaData(meta, type, con, constructorChain, basePropName = '', includePaths = []) { const index = constructorChain.find(o => o.constructor === type); if (index >= 0) { const proxy = constructorChain[index]; return proxy; } let instance = _getInstanceWithColumnsMetadata(meta, type, basePropName); instance = _getInstanceWithRelationsMetadata(con, meta, instance, type, constructorChain, basePropName, includePaths); return instance; } function _getInstanceWithRelationsMetadata(con, metaData, previousInstance, type, constructorChain, basePropName = '', includePaths = []) { const instance = new type(); Object.assign(instance, previousInstance); if (constructorChain.findIndex(o => o.constructor === type) < 0) constructorChain.push(instance); let toAssign; for (const c of metaData.relations) { toAssign = { $$propertyName: basePropName ? `${basePropName}.${c.propertyName}` : c.propertyName }; const subType = new c.type(); let meta; const i = constructorChain.findIndex(o => o.constructor === c.type); if (includePaths.includes(toAssign.$$propertyName)) { if (i >= 0) { meta = constructorChain[i]; } else { meta = _getMetaData(con, c.type, constructorChain, toAssign.$$propertyName, includePaths); } } if (meta) { if (c.isOneToMany) { // handling one to many as array since the type in target is Array const arrProxy = new metadata_proxy_1.SelectArrayProxy(toAssign.$$propertyName); instance[c.propertyName] = arrProxy; Object.assign(subType, toAssign, meta); instance[c.propertyName].push(subType); } else { Object.assign(subType, toAssign, meta); instance[c.propertyName] = subType; } } } return instance; } function _getInstanceWithColumnsMetadata(metaData, type, basePropName = '') { const instance = new type(); for (const c of metaData.columns) { const realProp = basePropName ? `${basePropName}.${c.propertyName}` : c.propertyName; if (c.type === Number) { instance[c.propertyName] = new metadata_proxy_1.SelectNumberProxy(realProp); } else if (c.type === Boolean) { instance[c.propertyName] = new metadata_proxy_1.SelectBooleanProxy(realProp); } else { instance[c.propertyName] = new metadata_proxy_1.SelectStringProxy(realProp); } } return instance; } //# sourceMappingURL=metadata.js.map