|
| 1 | +/* eslint-disable @typescript-eslint/no-non-null-assertion */ |
| 2 | +import { |
| 3 | + type SqlEntityManager, |
| 4 | + MikroORM, |
| 5 | + Entity, |
| 6 | + PrimaryKey, |
| 7 | + Property, |
| 8 | + Collection, |
| 9 | + OneToMany, |
| 10 | + Ref, |
| 11 | + ref, |
| 12 | + ManyToOne, |
| 13 | +} from "@mikro-orm/sqlite"; |
| 14 | +import { EntityDataLoader } from "./EntityDataLoader"; |
| 15 | + |
| 16 | +@Entity() |
| 17 | +class Author { |
| 18 | + @PrimaryKey() |
| 19 | + id!: number; |
| 20 | + |
| 21 | + @Property() |
| 22 | + name: string; |
| 23 | + |
| 24 | + @OneToMany(() => Book, (book) => book.author) |
| 25 | + books = new Collection<Book>(this); |
| 26 | + |
| 27 | + constructor({ id, name }: { id?: number; name: string }) { |
| 28 | + if (id != null) { |
| 29 | + this.id = id; |
| 30 | + } |
| 31 | + this.name = name; |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +@Entity() |
| 36 | +class Book { |
| 37 | + @PrimaryKey() |
| 38 | + id!: number; |
| 39 | + |
| 40 | + @Property() |
| 41 | + title: string; |
| 42 | + |
| 43 | + @ManyToOne(() => Author, { ref: true }) |
| 44 | + author: Ref<Author>; |
| 45 | + |
| 46 | + constructor({ id, title, author }: { id?: number; title: string; author: Author | Ref<Author> }) { |
| 47 | + if (id != null) { |
| 48 | + this.id = id; |
| 49 | + } |
| 50 | + this.title = title; |
| 51 | + this.author = ref(author); |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +async function populateDatabase(em: MikroORM["em"]): Promise<void> { |
| 56 | + const authors = [ |
| 57 | + new Author({ id: 1, name: "a" }), |
| 58 | + new Author({ id: 2, name: "b" }), |
| 59 | + new Author({ id: 3, name: "c" }), |
| 60 | + new Author({ id: 4, name: "d" }), |
| 61 | + new Author({ id: 5, name: "e" }), |
| 62 | + ]; |
| 63 | + em.persist(authors); |
| 64 | + |
| 65 | + const books = [ |
| 66 | + new Book({ id: 1, title: "One", author: authors[0]! }), |
| 67 | + new Book({ id: 2, title: "Two", author: authors[0]! }), |
| 68 | + new Book({ id: 3, title: "Three", author: authors[1]! }), |
| 69 | + new Book({ id: 4, title: "Four", author: authors[2]! }), |
| 70 | + new Book({ id: 5, title: "Five", author: authors[2]! }), |
| 71 | + new Book({ id: 6, title: "Six", author: authors[2]! }), |
| 72 | + ]; |
| 73 | + em.persist(books); |
| 74 | + await em.flush(); |
| 75 | +} |
| 76 | + |
| 77 | +describe("find", () => { |
| 78 | + let orm: MikroORM; |
| 79 | + let emFork: SqlEntityManager; |
| 80 | + let dataloader: EntityDataLoader; |
| 81 | + |
| 82 | + beforeAll(async () => { |
| 83 | + orm = await MikroORM.init({ |
| 84 | + dbName: ":memory:", |
| 85 | + entities: [Author, Book], |
| 86 | + }); |
| 87 | + try { |
| 88 | + await orm.schema.clearDatabase(); |
| 89 | + } catch {} |
| 90 | + try { |
| 91 | + const generator = orm.getSchemaGenerator(); |
| 92 | + await generator.createSchema({ wrap: true }); |
| 93 | + } catch {} |
| 94 | + await populateDatabase(orm.em.fork()); |
| 95 | + }); |
| 96 | + |
| 97 | + beforeEach(async () => { |
| 98 | + emFork = orm.em.fork(); |
| 99 | + dataloader = new EntityDataLoader(orm.em.fork()); |
| 100 | + }); |
| 101 | + |
| 102 | + it("should fetch books with the find dataloader", async () => { |
| 103 | + const authors = await emFork.find(Author, {}); |
| 104 | + const authorBooks = await Promise.all(authors.map(async ({ id }) => await dataloader.find(Book, { author: id }))); |
| 105 | + expect(authorBooks).toBeDefined(); |
| 106 | + expect(authorBooks).toMatchSnapshot(); |
| 107 | + }); |
| 108 | + |
| 109 | + it("should return the same books as find", async () => { |
| 110 | + const authors = await emFork.find(Author, {}); |
| 111 | + const dataloaderBooks = await Promise.all( |
| 112 | + authors.map(async ({ id }) => await dataloader.find(Book, { author: id })), |
| 113 | + ); |
| 114 | + const findBooks = await Promise.all(authors.map(async ({ id }) => await emFork.find(Book, { author: id }))); |
| 115 | + expect(dataloaderBooks.map((res) => res.map(({ id }) => id))).toEqual( |
| 116 | + findBooks.map((res) => res.map(({ id }) => id)), |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + afterEach(async () => {}); |
| 121 | + |
| 122 | + afterAll(async () => { |
| 123 | + await orm.close(true); |
| 124 | + }); |
| 125 | +}); |
0 commit comments