added tests and fix another bug

This commit is contained in:
Yannis Güdel 2016-10-03 22:12:23 +02:00
parent 54274b1966
commit 8db90339fa
4 changed files with 107 additions and 5 deletions

View File

@ -111,7 +111,7 @@ export class EntityPersister<Entity extends ObjectLiteral> {
protected async findNotLoadedIds(persistedEntities: OperateEntity[], dbEntities?: OperateEntity[]): Promise<OperateEntity[]> {
const newDbEntities: OperateEntity[] = dbEntities ? dbEntities.map(dbEntity => dbEntity) : [];
const missingDbEntitiesLoad = persistedEntities.map(async entityWithId => {
console.log(entityWithId);
if (entityWithId.id === null || // todo: not sure if this condition will work
entityWithId.id === undefined || // todo: not sure if this condition will work
newDbEntities.find(dbEntity => dbEntity.entityTarget === entityWithId.entityTarget && dbEntity.compareId(entityWithId.id!)))
@ -121,19 +121,20 @@ export class EntityPersister<Entity extends ObjectLiteral> {
const parameters: ObjectLiteral = {};
let condition = "";
if (this.metadata.hasParentIdColumn) {
condition = this.metadata.parentIdColumns.map(parentIdColumn => {
const metadata = this.connection.entityMetadatas.findByTarget(entityWithId.entityTarget);
if (metadata.hasParentIdColumn) {
condition = metadata.parentIdColumns.map(parentIdColumn => {
parameters[parentIdColumn.propertyName] = entityWithId.id![parentIdColumn.propertyName];
return alias + "." + parentIdColumn.propertyName + "=:" + parentIdColumn.propertyName;
}).join(" AND ");
} else {
condition = this.metadata.primaryColumns.map(primaryColumn => {
condition = metadata.primaryColumns.map(primaryColumn => {
parameters[primaryColumn.propertyName] = entityWithId.id![primaryColumn.propertyName];
return alias + "." + primaryColumn.propertyName + "=:" + primaryColumn.propertyName;
}).join(" AND ");
}
const metadata = this.connection.entityMetadatas.findByTarget(entityWithId.entityTarget);
const loadedEntity = await new QueryBuilder(this.connection, this.queryRunner)
.select(alias)
.from(entityWithId.entityTarget, alias)

View File

@ -0,0 +1,21 @@
import {Table} from "../../../../../src/decorator/tables/Table";
import {PrimaryColumn} from "../../../../../src/decorator/columns/PrimaryColumn";
import {Column} from "../../../../../src/decorator/columns/Column";
import {Post} from "./Post";
import {OneToMany} from "../../../../../src/decorator/relations/OneToMany";
import {JoinColumn} from "../../../../../src/decorator/relations/JoinColumn";
@Table()
export class Category {
@PrimaryColumn("int", {generated: true})
categoryId: number;
@Column()
name: string;
@OneToMany(type => Post, post => post.category)
posts: Post[];
}

View File

@ -0,0 +1,23 @@
import {Table} from "../../../../../src/decorator/tables/Table";
import {PrimaryColumn} from "../../../../../src/decorator/columns/PrimaryColumn";
import {Column} from "../../../../../src/decorator/columns/Column";
import {ManyToOne} from "../../../../../src/decorator/relations/ManyToOne";
import {JoinColumn} from "../../../../../src/decorator/relations/JoinColumn";
import {Category} from "./Category";
@Table()
export class Post {
@PrimaryColumn("int")
firstId: number;
@PrimaryColumn("int")
secondId: number;
@Column()
title: string;
@ManyToOne(type => Category, category => category.posts)
category: Category;
}

View File

@ -0,0 +1,57 @@
import "reflect-metadata";
import {setupTestingConnections, closeConnections, reloadDatabases} from "../../../utils/test-utils";
import {Connection} from "../../../../src/connection/Connection";
import {Post} from "./entity/Post";
import {Category} from "./entity/Category";
describe("persistence > mutli primary keys", () => {
let connections: Connection[];
before(async () => connections = await setupTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchemaOnConnection: true
}));
beforeEach(() => reloadDatabases(connections));
after(() => closeConnections(connections));
describe("insertt", function () {
it("should insert entity when when there are mutli column primary keys", () => Promise.all(connections.map(async connection => {
const post1 = new Post();
post1.title = "Hello Post #1";
post1.firstId = 1;
post1.secondId = 2;
await connection.entityManager.persist(post1);
// create first category and post and save them
const category1 = new Category();
category1.name = "Category saved by cascades #1";
category1.posts = [post1];
await connection.entityManager.persist(category1);
// now check
const posts = await connection.entityManager.find(Post, {
alias: "post",
innerJoinAndSelect: {
category: "post.category"
},
orderBy: {
"post.firstId": "ASC"
}
});
posts.should.be.eql([{
firstId: 1,
secondId: 2,
title: "Hello Post #1",
category: {
categoryId: 1,
name: "Category saved by cascades #1"
}
}]);
})));
});
});