UNPKG

ph-dev-tools

Version:
71 lines 3.06 kB
"use strict"; const QBuilder_1 = require("./../QBuilder"); /** * Created by Papa on 4/25/2016. */ const MANY_TO_ONE_DECORATOR = 'ManyToOne'; const ONE_TO_MANY_DECORATOR = 'OneToMany'; class QRelationBuilder { constructor(parentBuilder, entityProperty) { this.parentBuilder = parentBuilder; this.entityProperty = entityProperty; } build() { let type = this.entityProperty.entity.type; let qType = 'Q' + type; this.parentBuilder.addImport(qType); let definition; let classQType = `Q${this.parentBuilder.entity.type}`; let inverseRelationFieldName; let name = this.entityProperty.name; if (!this.entityProperty.isArray) { this.ensureDecorator(MANY_TO_ONE_DECORATOR, 'ManyToOne'); inverseRelationFieldName = name; } else { let inverseRelation = this.getDecoratorValue(ONE_TO_MANY_DECORATOR, 'OneToMany'); inverseRelationFieldName = inverseRelation.mappedBy; } let generisizedType = `QRelation<${qType}, ${type}, ${classQType}>`; let relationType = QBuilder_1.getRelationFieldType(this.entityProperty); let qClazz = `Q${this.parentBuilder.entity.docEntry.name}`; definition = `${name} = new ${generisizedType}(this, ${qClazz}, RelationType.${relationType}, '${name}', ${type}, ${qType});`; return definition; } buildInterfaceDefinition() { let type = this.entityProperty.entity.type; let iqType = 'IQ' + type; let classIQType = `IQ${this.parentBuilder.entity.type}`; let generisizedType = `IQRelation<${iqType}, ${type}, ${classIQType}>`; let iType = 'I' + type; let definition = `${this.entityProperty.name}?: ${generisizedType} | ${iType}`; return definition; } ensureDecorator(decoratorName, relationTypeDescription) { let decorators = this.entityProperty.decorators; if (!decorators.length) { throw `Expecting a @${decoratorName} decorator for ${relationTypeDescription} relation`; } let expectedDecorator; decorators.some((decorator) => { if (decorator.name === decoratorName) { expectedDecorator = decorator; return true; } }); if (!expectedDecorator) { throw `Expecting a @${decoratorName} decorator for ${relationTypeDescription} relation`; } return expectedDecorator; } getDecoratorValue(decoratorName, relationTypeDescription) { let expectedDecorator = this.ensureDecorator(decoratorName, relationTypeDescription); if (expectedDecorator.values.length != 1) { throw `Expecting a single parameter on @${decoratorName} decorator`; } let parameter = expectedDecorator.values[0]; return parameter; } } exports.QRelationBuilder = QRelationBuilder; //# sourceMappingURL=QRelationBuilder.js.map