mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
more many-to-many tests and fixes
This commit is contained in:
parent
ec13a23c0c
commit
399798268c
@ -19,25 +19,22 @@ let options = {
|
||||
|
||||
TypeORM.createMysqlConnection(options, [Post, PostDetails, PostCategory, PostMetadata, PostImage, PostInformation, PostAuthor]).then(connection => {
|
||||
|
||||
/*
|
||||
let category1 = new Category();
|
||||
category1.name = "People";
|
||||
|
||||
let category2 = new Category();
|
||||
category2.name = "Human";
|
||||
|
||||
let post = new Post();
|
||||
post.text = "Hello how are you?";
|
||||
post.title = "hello";
|
||||
post.categories = [category1, category2];
|
||||
*/
|
||||
let details1 = new PostDetails();
|
||||
details1.comment = "People";
|
||||
|
||||
// finally save it
|
||||
/*let postRepository = connection.getRepository<Post>(Post);
|
||||
let details2 = new PostDetails();
|
||||
details2.comment = "Human";
|
||||
|
||||
let post = new Post();
|
||||
post.text = "Hello how are you?";
|
||||
post.title = "hello";
|
||||
post.details = [details1, details2];
|
||||
|
||||
let postRepository = connection.getRepository<Post>(Post);
|
||||
|
||||
postRepository
|
||||
.persist(post)
|
||||
.then(post => console.log("Post has been saved"))
|
||||
.catch(error => console.log("Cannot save. Error: ", error));*/
|
||||
.catch(error => console.log("Cannot save. Error: ", error));
|
||||
|
||||
}, error => console.log("Cannot connect: ", error));
|
||||
@ -247,6 +247,7 @@ export class QueryBuilder<Entity> {
|
||||
}
|
||||
|
||||
getResults(): Promise<Entity[]> {
|
||||
console.log(this.getSql());
|
||||
return this.connection.driver
|
||||
.query<any[]>(this.getSql())
|
||||
.then(results => this.rawResultsToEntities(results));
|
||||
|
||||
@ -48,6 +48,7 @@ export class PlainObjectToDatabaseEntityTransformer<Entity> {
|
||||
private buildLoadMap(object: any, metadata: EntityMetadata): LoadMap[] {
|
||||
return metadata.relations
|
||||
.filter(relation => object.hasOwnProperty(relation.propertyName))
|
||||
.filter(relation => !(object[relation.propertyName] instanceof Array) || object[relation.propertyName].length > 0) // this is very important check that prevents building additional query for empty relations
|
||||
.map(relation => {
|
||||
let value = object[relation.propertyName];
|
||||
if (value instanceof Array)
|
||||
|
||||
@ -121,7 +121,15 @@ export class EntityPersistOperationsBuilder {
|
||||
const junctionRemoveOperations = this.findJunctionRemoveOperations(metadata, entity1, allEntities);
|
||||
const updatesByRelationsOperations = this.updateRelations(insertOperations, entity2);
|
||||
//const insertJunctionOperations = ;//this.a();
|
||||
/*console.log("---------------------------------------------------------");
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log("DB ENTITY");
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log(entity1);
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log("NEW ENTITY");
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log(entity2);
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log("DB ENTITIES");
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log(dbEntities);
|
||||
@ -153,7 +161,7 @@ export class EntityPersistOperationsBuilder {
|
||||
console.log("UPDATES BY RELATIONS");
|
||||
console.log("---------------------------------------------------------");
|
||||
console.log(updatesByRelationsOperations);
|
||||
console.log("---------------------------------------------------------");*/
|
||||
console.log("---------------------------------------------------------");
|
||||
|
||||
// now normalize inserted entities
|
||||
// no need probably, since we cant rely on deepness because of recursion: insertOperations.sort((a, b) => a.deepness + b.deepness);
|
||||
|
||||
@ -294,9 +294,8 @@ describe("many-to-many", function() {
|
||||
.leftJoinAndSelect("post.details", "details")
|
||||
.where("post.id=:id")
|
||||
.setParameter("id", updatedPost.id)
|
||||
.getSingleResult()
|
||||
.getSingleResult();
|
||||
}).then(updatedPostReloaded => {
|
||||
console.log("updatedPost: ", updatedPostReloaded);
|
||||
updatedPostReloaded.details[0].comment.should.be.equal("this is post");
|
||||
});
|
||||
}); // todo: also check that updates throw exception in strict cascades mode
|
||||
@ -334,7 +333,7 @@ describe("many-to-many", function() {
|
||||
.setParameter("id", updatedPost.id)
|
||||
.getSingleResult();
|
||||
}).then(updatedPostReloaded => {
|
||||
// todo fix updatedPostReloaded.details[0].comment.should.be.equal("this is post");
|
||||
updatedPostReloaded.details[0].comment.should.be.equal("this is post");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user