Skip to content

Commit

Permalink
fix: call listeners for array embeddeds in MongoDB (#4260)
Browse files Browse the repository at this point in the history
* fix call listeners for array embeddeds

* fix tests

* fix tests

Co-authored-by: AlexMesser <dmzt08@gmail.com>
  • Loading branch information
rustamwin and AlexMesser committed May 29, 2021
1 parent 9b6d7bc commit 2dc355b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/metadata/EntityListenerMetadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,11 @@ export class EntityListenerMetadata {
return;

if (propertyPaths.length === 0) {
entity[propertyPath][this.propertyName]();
if (entity[propertyPath] instanceof Array) {
entity[propertyPath].map((embedded: ObjectLiteral) => embedded[this.propertyName]());
} else {
entity[propertyPath][this.propertyName]();
}
} else {
if (entity[propertyPath])
this.callEntityEmbeddedMethod(entity[propertyPath], propertyPaths);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {Column} from "../../../../../../src/decorator/columns/Column";
import {ObjectIdColumn} from "../../../../../../src/decorator/columns/ObjectIdColumn";
import {Counters} from "./Counters";
import {ObjectID} from "../../../../../../src/driver/mongodb/typings";
import { Tags } from "./Tags";

@Entity()
export class Post {
Expand All @@ -19,4 +20,7 @@ export class Post {
@Column(type => Counters)
counters?: Counters;

}
@Column(type => Tags)
tags?: Tags[];

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Column} from "../../../../../../src/decorator/columns/Column";
import {BeforeInsert} from "../../../../../../src";

export class Tags {

@Column()
name: string;

@Column()
used?: number;

@BeforeInsert()
beforeInsert() {
this.used = 100;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {Post} from "./entity/Post";
import {Counters} from "./entity/Counters";
import {Information} from "./entity/Information";
import {expect} from "chai";
import { Tags } from "./entity/Tags";

describe("mongodb > embedded columns listeners", () => {

Expand All @@ -27,7 +28,7 @@ describe("mongodb > embedded columns listeners", () => {
post.counters.information = new Information();
await postRepository.save(post);

const loadedPost = await postRepository.findOne({title: "Post"});
const loadedPost = await postRepository.findOne({ title: "Post" });

expect(loadedPost).to.be.not.empty;
expect(loadedPost!.counters).to.be.not.empty;
Expand All @@ -39,7 +40,7 @@ describe("mongodb > embedded columns listeners", () => {
post.title = "Updated post";
await postRepository.save(post);

const loadedUpdatedPost = await postRepository.findOne({title: "Updated post"});
const loadedUpdatedPost = await postRepository.findOne({ title: "Updated post" });

expect(loadedUpdatedPost).to.be.not.empty;
expect(loadedUpdatedPost!.counters).to.be.not.empty;
Expand Down Expand Up @@ -70,4 +71,28 @@ describe("mongodb > embedded columns listeners", () => {
loadedPost.text.should.be.eql("Everything about post");

})));

it("should work listeners in entity array embeddeds correctly", () => Promise.all(connections.map(async connection => {
const postRepository = connection.getMongoRepository(Post);

// save posts without embeddeds
const post = new Post();
post.title = "Post";
post.text = "Everything about post";
const tag1 = new Tags();
tag1.name = "Tag #1";
const tag2 = new Tags();
tag2.name = "Tag #2";
post.tags = [tag1, tag2];
await postRepository.save(post);

const cursor = postRepository.createCursor();
const loadedPost = await cursor.next();

loadedPost.title.should.be.eql("Post");
loadedPost.text.should.be.eql("Everything about post");
expect(loadedPost!.tags).to.be.not.empty;
loadedPost!.tags![0].used.should.be.equal(100);
loadedPost!.tags![1].name.should.be.equal("Tag #2");
})));
});

0 comments on commit 2dc355b

Please sign in to comment.