added validation for relation count in EntityMetadataValidator ;

This commit is contained in:
Zotov Dmitry 2017-04-20 15:31:43 +05:00
parent 4706b69b1d
commit c2da0f3845
10 changed files with 80 additions and 65 deletions

View File

@ -54,6 +54,11 @@ export class EntityMetadataValidator {
throw new Error(`Entities ${entityMetadata.name} and ${sameDiscriminatorValueEntityMetadata.name} as equal discriminator values. Make sure their discriminator values are not equal using @DiscriminatorValue decorator.`);
}
entityMetadata.relationCounts.forEach(relationCount => {
if (relationCount.relation.isManyToOne || relationCount.relation.isOneToOne)
throw new Error(`Relation count can not be implemented on ManyToOne or OneToOne relations.`);
});
// validate relations
entityMetadata.relations.forEach(relation => {

View File

@ -24,11 +24,7 @@ export class RelationCountLoader {
const promises = this.relationCountAttributes.map(async relationCountAttr => {
// TODO make that check in EntityMetadataValidator
if (relationCountAttr.relation.isManyToOne || relationCountAttr.relation.isOneToOne) {
throw new Error(`Relation count can not be implemented on ManyToOne or OneToOne relations.`);
} else if (relationCountAttr.relation.isOneToMany) {
if (relationCountAttr.relation.isOneToMany) {
// example: Post and Category
// loadRelationCountAndMap("post.categoryCount", "post.categories")
// we expect it to load array of post ids

View File

@ -201,10 +201,7 @@ export class RawSqlResultsToEntityTransformer {
const relation = rawRelationCountResult.relationCountAttribute.relation;
let referenceColumnName: string;
if (relation.isManyToOne || relation.isOneToOne) {
throw new Error(`Relation count can not be implemented on ManyToOne or OneToOne relations.`);
} else if (relation.isOneToMany) {
if (relation.isOneToMany) {
referenceColumnName = relation.inverseRelation.joinColumn.referencedColumn.fullName;
} else {
@ -212,7 +209,6 @@ export class RawSqlResultsToEntityTransformer {
}
const referenceColumnValue = rawSqlResults[0][alias.name + "_" + referenceColumnName]; // we use zero index since its grouped data // todo: selection with alias for entity columns wont work
// console.log("referenceColumnValue: ", referenceColumnValue);
if (referenceColumnValue !== undefined && referenceColumnValue !== null) {
entity[rawRelationCountResult.relationCountAttribute.mapToPropertyPropertyName] = 0;
rawRelationCountResult.results
@ -222,7 +218,6 @@ export class RawSqlResultsToEntityTransformer {
hasData = true;
});
}
});
return hasData;

View File

@ -56,9 +56,7 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.createQueryBuilder(Post, "post")
.getMany();
expect(loadedPosts![0].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categoryCount).to.be.equal(3);
expect(loadedPosts![1].categoryCount).to.not.be.empty;
expect(loadedPosts![1].categoryCount).to.be.equal(2);
let loadedPost = await connection.entityManager
@ -66,7 +64,6 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.where("post.id = :id", { id: 1 })
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(3);
})));
@ -116,9 +113,7 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.setLimit(2)
.getMany();
expect(loadedPosts![0].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categoryCount).to.be.equal(3);
expect(loadedPosts![1].categoryCount).to.not.be.empty;
expect(loadedPosts![1].categoryCount).to.be.equal(2);
})));
@ -176,14 +171,12 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.addOrderBy("post.id, categories.id")
.getMany();
expect(loadedPosts![0].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categoryCount).to.be.equal(3);
expect(loadedPosts![0].removedCategoryCount).to.be.equal(1);
expect(loadedPosts![0].categories[0].imageCount).to.be.equal(2);
expect(loadedPosts![0].categories[0].removedImageCount).to.be.equal(1);
// expect(loadedPosts![0].categories[1].imageCount).to.be.equal(0);
// expect(loadedPosts![0].categories[2].imageCount).to.be.equal(0);
expect(loadedPosts![1].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categories[1].imageCount).to.be.equal(0);
expect(loadedPosts![0].categories[2].imageCount).to.be.equal(0);
expect(loadedPosts![1].categoryCount).to.be.equal(2);
expect(loadedPosts![1].categories[0].imageCount).to.be.equal(1);
@ -194,7 +187,6 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.addOrderBy("post.id, categories.id")
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(3);
expect(loadedPost!.removedCategoryCount).to.be.equal(1);
expect(loadedPost!.categories[0].imageCount).to.be.equal(2);
@ -231,12 +223,10 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.addOrderBy("post.id, categories.id")
.getMany();
expect(loadedPosts![0].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categoryCount).to.be.equal(3);
expect(loadedPosts![0].categories[0].postCount).to.be.equal(2);
expect(loadedPosts![0].categories[1].postCount).to.be.equal(1);
expect(loadedPosts![0].categories[2].postCount).to.be.equal(2);
expect(loadedPosts![1].categoryCount).to.not.be.empty;
expect(loadedPosts![1].categoryCount).to.be.equal(2);
expect(loadedPosts![1].categories[0].postCount).to.be.equal(2);
expect(loadedPosts![1].categories[1].postCount).to.be.equal(2);
@ -248,7 +238,6 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.addOrderBy("post.id, categories.id")
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(3);
expect(loadedPost!.categories[0].postCount).to.be.equal(2);
expect(loadedPost!.categories[1].postCount).to.be.equal(1);
@ -294,9 +283,7 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.createQueryBuilder(Category, "category")
.getMany();
expect(loadedCategories![0].postCount).to.not.be.empty;
expect(loadedCategories![0].postCount).to.be.equal(3);
expect(loadedCategories![1].postCount).to.not.be.empty;
expect(loadedCategories![1].postCount).to.be.equal(2);
let loadedCategory = await connection.entityManager
@ -304,7 +291,6 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.where("category.id = :id", { id: 1 })
.getOne();
expect(loadedCategory!.postCount).to.not.be.empty;
expect(loadedCategory!.postCount).to.be.equal(3);
})));
@ -357,9 +343,7 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.setLimit(2)
.getMany();
expect(loadedCategories![0].postCount).to.not.be.empty;
expect(loadedCategories![0].postCount).to.be.equal(3);
expect(loadedCategories![1].postCount).to.not.be.empty;
expect(loadedCategories![1].postCount).to.be.equal(2);
})));
@ -404,10 +388,8 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.createQueryBuilder(Category, "category")
.getMany();
expect(loadedCategories![0].postCount).to.not.be.empty;
expect(loadedCategories![0].postCount).to.be.equal(3);
expect(loadedCategories![0].removedPostCount).to.be.equal(2);
expect(loadedCategories![1].postCount).to.not.be.empty;
expect(loadedCategories![1].postCount).to.be.equal(2);
let loadedCategory = await connection.entityManager
@ -415,7 +397,6 @@ describe("query builder > relation-count-decorator-many-to-many > many-to-many",
.where("category.id = :id", { id: 1 })
.getOne();
expect(loadedCategory!.postCount).to.not.be.empty;
expect(loadedCategory!.postCount).to.be.equal(3);
expect(loadedCategory!.removedPostCount).to.be.equal(2);
})));

View File

@ -66,13 +66,11 @@ describe("decorators > relation-count-decorator > one-to-many", () => {
.addOrderBy("post.id, categories.id")
.getMany();
expect(loadedPosts![0].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categoryCount).to.be.equal(2);
expect(loadedPosts![0].removedCategoryCount).to.be.equal(1);
expect(loadedPosts![0].categories[0].imageCount).to.be.equal(2);
expect(loadedPosts![0].categories[0].removedImageCount).to.be.equal(1);
// expect(loadedPosts![0].categories[1].imageCount).to.be.equal(0);
expect(loadedPosts![1].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categories[1].imageCount).to.be.equal(0);
expect(loadedPosts![1].categoryCount).to.be.equal(1);
expect(loadedPosts![1].categories[0].imageCount).to.be.equal(1);
@ -83,11 +81,10 @@ describe("decorators > relation-count-decorator > one-to-many", () => {
.addOrderBy("post.id, categories.id")
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(2);
expect(loadedPost!.categories[0].imageCount).to.be.equal(2);
expect(loadedPost!.removedCategoryCount).to.be.equal(1);
// expect(loadedPosts![0].categories[1].imageCount).to.be.equal(0);
expect(loadedPosts![0].categories[1].imageCount).to.be.equal(0);
expect(loadedPost!.categories[0].removedImageCount).to.be.equal(1);
})));

View File

@ -0,0 +1,25 @@
import "reflect-metadata";
import * as chai from "chai";
import {expect} from "chai";
import {setupTestingConnections} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {Post} from "./entity/Post";
import {EntityMetadataValidator} from "../../../src/metadata-builder/EntityMetadataValidator";
import {getConnectionManager} from "../../../src/index";
const should = chai.should();
describe("entity-metadata-validator", () => {
let connections: Connection[];
before(() => connections = setupTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchemaOnConnection: true,
}).map(options => getConnectionManager().create(options)));
it("should throw error if relation count decorator used with ManyToOne or OneToOne relations", () => Promise.all(connections.map(async connection => {
expect(() => new EntityMetadataValidator().validate(connection.getMetadata(Post), connection.entityMetadatas)).to.throw(Error);
})));
});

View File

@ -0,0 +1,14 @@
import {Entity} from "../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../src/decorator/columns/Column";
@Entity()
export class Category {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
}

View File

@ -0,0 +1,30 @@
import {OneToOne} from "../../../../src/decorator/relations/OneToOne";
import {Entity} from "../../../../src/decorator/entity/Entity";
import {PrimaryGeneratedColumn} from "../../../../src/decorator/columns/PrimaryGeneratedColumn";
import {Column} from "../../../../src/decorator/columns/Column";
import {RelationCount} from "../../../../src/decorator/relations/RelationCount";
import {ManyToMany} from "../../../../src/decorator/relations/ManyToMany";
import {Category} from "./Category";
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@OneToOne(type => Category)
category: Category;
@ManyToMany(type => Category)
category2: Category;
@RelationCount((post: Post) => post.category)
categoryCount: number;
@RelationCount((post: Post) => post.category2)
categoryCount2: number;
}

View File

@ -57,9 +57,7 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.loadRelationCountAndMap("post.categoryCount", "post.categories")
.getMany();
expect(loadedPosts![0].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categoryCount).to.be.equal(3);
expect(loadedPosts![1].categoryCount).to.not.be.empty;
expect(loadedPosts![1].categoryCount).to.be.equal(2);
let loadedPost = await connection.entityManager
@ -68,7 +66,6 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.where("post.id = :id", { id: 1 })
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(3);
})));
@ -119,9 +116,7 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.setLimit(2)
.getMany();
expect(loadedPosts![0].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categoryCount).to.be.equal(3);
expect(loadedPosts![1].categoryCount).to.not.be.empty;
expect(loadedPosts![1].categoryCount).to.be.equal(2);
})));
@ -183,14 +178,12 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.addOrderBy("post.id, categories.id")
.getMany();
expect(loadedPosts![0].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categoryCount).to.be.equal(3);
expect(loadedPosts![0].removedCategoryCount).to.be.equal(1);
expect(loadedPosts![0].categories[0].imageCount).to.be.equal(2);
expect(loadedPosts![0].categories[0].removedImageCount).to.be.equal(1);
expect(loadedPosts![0].categories[1].imageCount).to.be.equal(0);
expect(loadedPosts![0].categories[2].imageCount).to.be.equal(0);
expect(loadedPosts![1].categoryCount).to.not.be.empty;
expect(loadedPosts![1].categoryCount).to.be.equal(2);
expect(loadedPosts![1].categories[0].imageCount).to.be.equal(1);
@ -205,7 +198,6 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.addOrderBy("post.id, categories.id")
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(3);
expect(loadedPost!.removedCategoryCount).to.be.equal(1);
expect(loadedPost!.categories[0].imageCount).to.be.equal(2);
@ -244,12 +236,10 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.addOrderBy("post.id, categories.id")
.getMany();
expect(loadedPosts![0].categoryCount).to.not.be.empty;
expect(loadedPosts![0].categoryCount).to.be.equal(3);
expect(loadedPosts![0].categories[0].postCount).to.be.equal(2);
expect(loadedPosts![0].categories[1].postCount).to.be.equal(1);
expect(loadedPosts![0].categories[2].postCount).to.be.equal(2);
expect(loadedPosts![1].categoryCount).to.not.be.empty;
expect(loadedPosts![1].categoryCount).to.be.equal(2);
expect(loadedPosts![1].categories[0].postCount).to.be.equal(2);
expect(loadedPosts![1].categories[1].postCount).to.be.equal(2);
@ -263,7 +253,6 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.addOrderBy("post.id, categories.id")
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(3);
expect(loadedPost!.categories[0].postCount).to.be.equal(2);
expect(loadedPost!.categories[1].postCount).to.be.equal(1);
@ -310,9 +299,7 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.loadRelationCountAndMap("category.postCount", "category.posts")
.getMany();
expect(loadedCategories![0].postCount).to.not.be.empty;
expect(loadedCategories![0].postCount).to.be.equal(3);
expect(loadedCategories![1].postCount).to.not.be.empty;
expect(loadedCategories![1].postCount).to.be.equal(2);
let loadedCategory = await connection.entityManager
@ -321,7 +308,6 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.where("category.id = :id", { id: 1 })
.getOne();
expect(loadedCategory!.postCount).to.not.be.empty;
expect(loadedCategory!.postCount).to.be.equal(3);
})));
@ -375,9 +361,7 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.setLimit(2)
.getMany();
expect(loadedCategories![0].postCount).to.not.be.empty;
expect(loadedCategories![0].postCount).to.be.equal(3);
expect(loadedCategories![1].postCount).to.not.be.empty;
expect(loadedCategories![1].postCount).to.be.equal(2);
})));
@ -424,10 +408,8 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.loadRelationCountAndMap("category.removedPostCount", "category.posts", "removedPosts", qb => qb.andWhere("removedPosts.isRemoved = :isRemoved", { isRemoved: true }))
.getMany();
expect(loadedCategories![0].postCount).to.not.be.empty;
expect(loadedCategories![0].postCount).to.be.equal(3);
expect(loadedCategories![0].removedPostCount).to.be.equal(2);
expect(loadedCategories![1].postCount).to.not.be.empty;
expect(loadedCategories![1].postCount).to.be.equal(2);
let loadedCategory = await connection.entityManager
@ -437,7 +419,6 @@ describe("query builder > load-relation-count-and-map > many-to-many", () => {
.where("category.id = :id", { id: 1 })
.getOne();
expect(loadedCategory!.postCount).to.not.be.empty;
expect(loadedCategory!.postCount).to.be.equal(3);
expect(loadedCategory!.removedPostCount).to.be.equal(2);
})));

View File

@ -49,9 +49,7 @@ describe("query builder > load-relation-count-and-map > one-to-many", () => {
.loadRelationCountAndMap("post.categoryCount", "post.categories")
.getMany();
expect(loadedPosts[0]!.categoryCount).to.not.be.empty;
expect(loadedPosts[0]!.categoryCount).to.be.equal(2);
expect(loadedPosts[1]!.categoryCount).to.not.be.empty;
expect(loadedPosts[1]!.categoryCount).to.be.equal(1);
const loadedPost = await connection.entityManager
@ -60,7 +58,6 @@ describe("query builder > load-relation-count-and-map > one-to-many", () => {
.where("post.id = :id", { id: 1 })
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(2);
})));
@ -110,11 +107,9 @@ describe("query builder > load-relation-count-and-map > one-to-many", () => {
.addOrderBy("post.id, categories.id")
.getMany();
expect(loadedPosts[0]!.categoryCount).to.not.be.empty;
expect(loadedPosts[0]!.categoryCount).to.be.equal(2);
expect(loadedPosts[0]!.categories[0].imageCount).to.be.equal(2);
expect(loadedPosts[0]!.categories[1].imageCount).to.be.equal(0);
expect(loadedPosts[1]!.categoryCount).to.not.be.empty;
expect(loadedPosts[1]!.categoryCount).to.be.equal(1);
expect(loadedPosts[1]!.categories[0].imageCount).to.be.equal(1);
@ -127,7 +122,6 @@ describe("query builder > load-relation-count-and-map > one-to-many", () => {
.addOrderBy("post.id, categories.id")
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(2);
expect(loadedPost!.categories[0].imageCount).to.be.equal(2);
expect(loadedPost!.categories[1].imageCount).to.be.equal(0);
@ -164,10 +158,8 @@ describe("query builder > load-relation-count-and-map > one-to-many", () => {
.loadRelationCountAndMap("post.removedCategoryCount", "post.categories", "removedCategories", qb => qb.andWhere("removedCategories.isRemoved = :isRemoved", { isRemoved: true }))
.getMany();
expect(loadedPosts[0]!.categoryCount).to.not.be.empty;
expect(loadedPosts[0]!.categoryCount).to.be.equal(2);
expect(loadedPosts[0]!.removedCategoryCount).to.be.equal(1);
expect(loadedPosts[1]!.categoryCount).to.not.be.empty;
expect(loadedPosts[1]!.categoryCount).to.be.equal(1);
const loadedPost = await connection.entityManager
@ -177,7 +169,6 @@ describe("query builder > load-relation-count-and-map > one-to-many", () => {
.where("post.id = :id", { id: 1 })
.getOne();
expect(loadedPost!.categoryCount).to.not.be.empty;
expect(loadedPost!.categoryCount).to.be.equal(2);
expect(loadedPost!.removedCategoryCount).to.be.equal(1);
})));