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,CAAC,YAAY,qDAAgD,CAAA;yBAClG,QAAQ,CAAC,qBAAqB,CAAC,UAAU,SAAI,QAAQ,CAAC,eAAe,CAAC,YAAY,uCAAkC,CAAA;wBACvH,wDAAsD,CAAC,CAAC;YACpE,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC;IAEL,8BAAC;AAAD,CA9MA,AA8MC,IAAA;AA9MY,0DAAuB","file":"EntityMetadataValidator.js","sourcesContent":["import {EntityMetadata} from \"../metadata/EntityMetadata\";\nimport {MissingPrimaryColumnError} from \"../error/MissingPrimaryColumnError\";\nimport {CircularRelationsError} from \"../error/CircularRelationsError\";\nimport {DepGraph} from \"../util/DepGraph\";\nimport {Driver} from \"../driver/Driver\";\nimport {DataTypeNotSupportedError} from \"../error/DataTypeNotSupportedError\";\nimport {ColumnType} from \"../driver/types/ColumnTypes\";\nimport {MongoDriver} from \"../driver/mongodb/MongoDriver\";\nimport {SqlServerDriver} from \"../driver/sqlserver/SqlServerDriver\";\nimport {MysqlDriver} from \"../driver/mysql/MysqlDriver\";\nimport {NoConnectionOptionError} from \"../error/NoConnectionOptionError\";\nimport {InitializedRelationError} from \"../error/InitializedRelationError\";\n\n/// todo: add check if there are multiple tables with the same name\n/// todo: add checks when generated column / table names are too long for the specific driver\n// todo: type in function validation, inverse side function validation\n// todo: check on build for duplicate names, since naming checking was removed from MetadataStorage\n// todo: duplicate name checking for: table, relation, column, index, naming strategy, join tables/columns?\n// todo: check if multiple tree parent metadatas in validator\n// todo: tree decorators can be used only on closure table (validation)\n// todo: throw error if parent tree metadata was not specified in a closure table\n\n// todo: MetadataArgsStorage: type in function validation, inverse side function validation\n// todo: MetadataArgsStorage: check on build for duplicate names, since naming checking was removed from MetadataStorage\n// todo: MetadataArgsStorage: duplicate name checking for: table, relation, column, index, naming strategy, join tables/columns?\n// todo: MetadataArgsStorage: check for duplicate targets too since this check has been removed too\n// todo: check if relation decorator contains primary: true and nullable: true\n// todo: check column length, precision. scale\n// todo: MySQL index can be unique or spatial or fulltext\n\n/**\n * Validates built entity metadatas.\n */\nexport class EntityMetadataValidator {\n\n // -------------------------------------------------------------------------\n // Public Methods\n // -------------------------------------------------------------------------\n\n /**\n * Validates all given entity metadatas.\n */\n validateMany(entityMetadatas: EntityMetadata[], driver: Driver) {\n entityMetadatas.forEach(entityMetadata => this.validate(entityMetadata, entityMetadatas, driver));\n this.validateDependencies(entityMetadatas);\n this.validateEagerRelations(entityMetadatas);\n }\n\n /**\n * Validates given entity metadata.\n */\n validate(entityMetadata: EntityMetadata, allEntityMetadatas: EntityMetadata[], driver: Driver) {\n\n // check if table metadata has an id\n if (!entityMetadata.primaryColumns.length && !entityMetadata.isJunction)\n throw new MissingPrimaryColumnError(entityMetadata);\n\n // validate if table is using inheritance it has a discriminator\n // also validate if discriminator values are not empty and not repeated\n if (entityMetadata.inheritancePattern === \"STI\") {\n if (!entityMetadata.discriminatorColumn)\n throw new Error(`Entity ${entityMetadata.name} using single-table inheritance, it should also have a discriminator column. Did you forget to put discriminator column options?`);\n\n if ([\"\", undefined, null].indexOf(entityMetadata.discriminatorValue) !== -1)\n throw new Error(`Entity ${entityMetadata.name} has empty discriminator value. Discriminator value should not be empty.`);\n\n const sameDiscriminatorValueEntityMetadata = allEntityMetadatas.find(metadata => {\n return metadata !== entityMetadata && metadata.discriminatorValue === entityMetadata.discriminatorValue;\n });\n if (sameDiscriminatorValueEntityMetadata)\n throw new Error(`Entities ${entityMetadata.name} and ${sameDiscriminatorValueEntityMetadata.name} as equal discriminator values. Make sure their discriminator values are not equal using @DiscriminatorValue decorator.`);\n }\n\n entityMetadata.relationCounts.forEach(relationCount => {\n if (relationCount.relation.isManyToOne || relationCount.relation.isOneToOne)\n throw new Error(`Relation count can not be implemented on ManyToOne or OneToOne relations.`);\n });\n\n if (!(driver instanceof MongoDriver)) {\n entityMetadata.columns.forEach(column => {\n const normalizedColumn = driver.normalizeType(column) as ColumnType;\n if (driver.supportedDataTypes.indexOf(normalizedColumn) === -1)\n throw new DataTypeNotSupportedError(column, normalizedColumn, driver.options.type);\n if (column.length && driver.withLengthColumnTypes.indexOf(normalizedColumn) === -1)\n throw new Error(`Column ${column.propertyName} of Entity ${entityMetadata.name} does not support length property.`);\n });\n }\n\n if (driver instanceof MysqlDriver) {\n const generatedColumns = entityMetadata.columns.filter(column => column.isGenerated && column.generationStrategy !== \"uuid\");\n if (generatedColumns.length > 1)\n throw new Error(`Error in ${entityMetadata.name} entity. There can be only one auto-increment column in MySql table.`);\n }\n\n // for mysql we are able to not define a default selected database, instead all entities can have their database\n // defined in their decorators. To make everything work either all entities must have database define and we\n // can live without database set in the connection options, either database in the connection options must be set\n if (driver instanceof MysqlDriver) {\n const metadatasWithDatabase = allEntityMetadatas.filter(metadata => metadata.database);\n if (metadatasWithDatabase.length === 0 && !driver.database)\n throw new NoConnectionOptionError(\"database\");\n }\n\n if (driver instanceof SqlServerDriver) {\n const charsetColumns = entityMetadata.columns.filter(column => column.charset);\n if (charsetColumns.length > 1)\n throw new Error(`Character set specifying is not supported in Sql Server`);\n }\n\n // check if relations are all without initialized properties\n const entityInstance = entityMetadata.create();\n entityMetadata.relations.forEach(relation => {\n if (relation.isManyToMany || relation.isOneToMany) {\n\n // we skip relations for which persistence is disabled since initialization in them cannot harm somehow\n if (relation.persistenceEnabled === false)\n return;\n\n // get entity relation value and check if its an array\n const relationInitializedValue = relation.getEntityValue(entityInstance);\n if (relationInitializedValue instanceof Array)\n throw new InitializedRelationError(relation);\n }\n });\n\n // validate relations\n entityMetadata.relations.forEach(relation => {\n\n // check join tables:\n // using JoinTable is possible only on one side of the many-to-many relation\n // todo(dima): fix\n // if (relation.joinTable) {\n // if (!relation.isManyToMany)\n // throw new UsingJoinTableIsNotAllowedError(entityMetadata, relation);\n\n // // if there is inverse side of the relation, then check if it does not have join table too\n // if (relation.hasInverseSide && relation.inverseRelation.joinTable)\n // throw new UsingJoinTableOnlyOnOneSideAllowedError(entityMetadata, relation);\n // }\n\n // check join columns:\n // using JoinColumn is possible only on one side of the relation and on one-to-one, many-to-one relation types\n // first check if relation is one-to-one or many-to-one\n // todo(dima): fix\n /*if (relation.joinColumn) {\n\n // join column can be applied only on one-to-one and many-to-one relations\n if (!relation.isOneToOne && !relation.isManyToOne)\n throw new UsingJoinColumnIsNotAllowedError(entityMetadata, relation);\n\n // if there is inverse side of the relation, then check if it does not have join table too\n if (relation.hasInverseSide && relation.inverseRelation.joinColumn && relation.isOneToOne)\n throw new UsingJoinColumnOnlyOnOneSideAllowedError(entityMetadata, relation);\n\n // check if join column really has referenced column\n if (relation.joinColumn && !relation.joinColumn.referencedColumn)\n throw new Error(`Join column does not have referenced column set`);\n\n }\n\n // if its a one-to-one relation and JoinColumn is missing on both sides of the relation\n // or its one-side relation without JoinColumn we should give an error\n if (!relation.joinColumn && relation.isOneToOne && (!relation.hasInverseSide || !relation.inverseRelation.joinColumn))\n throw new MissingJoinColumnError(entityMetadata, relation);*/\n\n // if its a many-to-many relation and JoinTable is missing on both sides of the relation\n // or its one-side relation without JoinTable we should give an error\n // todo(dima): fix it\n // if (!relation.joinTable && relation.isManyToMany && (!relation.hasInverseSide || !relation.inverseRelation.joinTable))\n // throw new MissingJoinTableError(entityMetadata, relation);\n\n\n // todo: validate if its one-to-one and side which does not have join column MUST have inverse side\n // todo: validate if its many-to-many and side which does not have join table MUST have inverse side\n // todo: if there is a relation, and inverse side is specified only on one side, shall we give error\n // todo: with message like: \"Inverse side is specified only on one side of the relationship. Specify on other side too to prevent confusion\".\n // todo: add validation if there two entities with the same target, and show error message with description of the problem (maybe file was renamed/moved but left in output directory)\n // todo: check if there are multiple columns on the same column applied.\n // todo: check column type if is missing in relational databases (throw new Error(`Column type of ${type} cannot be determined.`);)\n // todo: include driver-specific checks. for example in mongodb empty prefixes are not allowed\n // todo: if multiple columns with same name - throw exception, including cases when columns are in embeds with same prefixes or without prefix at all\n // todo: if multiple primary key used, at least one of them must be unique or @Index decorator must be set on entity\n // todo: check if entity with duplicate names, some decorators exist\n\n\n });\n\n // make sure cascade remove is not set for both sides of relationships (can be set in OneToOne decorators)\n entityMetadata.relations.forEach(relation => {\n const isCircularCascadeRemove = relation.isCascadeRemove && relation.inverseRelation && relation.inverseRelation!.isCascadeRemove;\n if (isCircularCascadeRemove)\n throw new Error(`Relation ${entityMetadata.name}#${relation.propertyName} and ${relation.inverseRelation!.entityMetadata.name}#${relation.inverseRelation!.propertyName} both has cascade remove set. ` +\n `This may lead to unexpected circular removals. Please set cascade remove only from one side of relationship.`);\n }); // todo: maybe better just deny removal from one to one relation without join column?\n\n entityMetadata.eagerRelations.forEach(relation => {\n\n });\n }\n\n /**\n * Validates dependencies of the entity metadatas.\n */\n protected validateDependencies(entityMetadatas: EntityMetadata[]) {\n\n const graph = new DepGraph();\n entityMetadatas.forEach(entityMetadata => {\n graph.addNode(entityMetadata.name);\n });\n entityMetadatas.forEach(entityMetadata => {\n entityMetadata.relationsWithJoinColumns\n .filter(relation => !relation.isNullable)\n .forEach(relation => {\n graph.addDependency(entityMetadata.name, relation.inverseEntityMetadata.name);\n });\n });\n try {\n graph.overallOrder();\n\n } catch (err) {\n throw new CircularRelationsError(err.toString().replace(\"Error: Dependency Cycle Found: \", \"\"));\n }\n }\n\n /**\n * Validates eager relations to prevent circular dependency in them.\n */\n protected validateEagerRelations(entityMetadatas: EntityMetadata[]) {\n entityMetadatas.forEach(entityMetadata => {\n entityMetadata.eagerRelations.forEach(relation => {\n if (relation.inverseRelation && relation.inverseRelation.isEager)\n throw new Error(`Circular eager relations are disallowed. ` +\n `${entityMetadata.targetName}#${relation.propertyPath} contains \"eager: true\", and its inverse side ` +\n `${relation.inverseEntityMetadata.targetName}#${relation.inverseRelation.propertyPath} contains \"eager: true\" as well.` +\n ` Remove \"eager: true\" from one side of the relation.`);\n });\n });\n }\n\n}"],"sourceRoot":".."}
|