Skip to content

Commit 137d5df

Browse files
authored
feat: repository api (#7)
1 parent 40f227f commit 137d5df

File tree

15 files changed

+416
-280
lines changed

15 files changed

+416
-280
lines changed

.changeset/chilly-pumpkins-reply.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"graphql-example": major
3+
"mikro-orm-find-dataloader": major
4+
---
5+
6+
Switch to Repository API

examples/graphql/src/entities/Author.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { Collection, Entity, ManyToMany, OneToMany, PrimaryKey, Property } from "@mikro-orm/core";
1+
import { Collection, Entity, EntityRepositoryType, ManyToMany, OneToMany, PrimaryKey, Property } from "@mikro-orm/core";
22
import { Book } from "./Book";
33
import { Chat } from "./Chat";
4+
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
5+
import { type findDataloaderDefault } from "../mikro-orm-config";
46

57
@Entity()
68
export class Author {
@@ -30,6 +32,8 @@ export class Author {
3032
@OneToMany(() => Chat, (chat) => chat.owner)
3133
ownedChats: Collection<Chat> = new Collection<Chat>(this);
3234

35+
[EntityRepositoryType]?: IFindDataloaderEntityRepository<Author, typeof findDataloaderDefault>;
36+
3337
constructor({ id, name, email }: { id?: number; name: string; email: string }) {
3438
if (id != null) {
3539
this.id = id;

examples/graphql/src/entities/Book.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
import { Entity, ManyToOne, PrimaryKey, Property, Ref, ref } from "@mikro-orm/core";
1+
import { Entity, EntityRepositoryType, ManyToOne, PrimaryKey, Property, Ref, ref } from "@mikro-orm/core";
22
import { Author } from "./Author";
33
import { Publisher } from "./Publisher";
4+
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
5+
import { type findDataloaderDefault } from "../mikro-orm-config";
46

57
@Entity()
68
export class Book {
@@ -16,6 +18,8 @@ export class Book {
1618
@ManyToOne(() => Publisher, { ref: true, nullable: true })
1719
publisher!: Ref<Publisher> | null;
1820

21+
[EntityRepositoryType]?: IFindDataloaderEntityRepository<Book, typeof findDataloaderDefault>;
22+
1923
constructor({ id, title, author }: { id?: number; title: string; author: Author | Ref<Author> }) {
2024
if (id != null) {
2125
this.id = id;

examples/graphql/src/entities/Chat.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,17 @@
1-
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKeyProp, Ref, ref } from "@mikro-orm/core";
1+
import {
2+
Collection,
3+
Entity,
4+
EntityRepositoryType,
5+
ManyToOne,
6+
OneToMany,
7+
PrimaryKeyProp,
8+
Ref,
9+
ref,
10+
} from "@mikro-orm/core";
211
import { Author } from "./Author";
312
import { Message } from "./Message";
13+
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
14+
import { type findDataloaderDefault } from "../mikro-orm-config";
415

516
@Entity()
617
export class Chat {
@@ -15,6 +26,8 @@ export class Chat {
1526
@OneToMany(() => Message, (message) => message.chat)
1627
messages: Collection<Message> = new Collection<Message>(this);
1728

29+
[EntityRepositoryType]?: IFindDataloaderEntityRepository<Chat, typeof findDataloaderDefault>;
30+
1831
constructor({ owner, recipient }: { owner: Author | Ref<Author>; recipient: Author | Ref<Author> }) {
1932
this.owner = ref(owner);
2033
this.recipient = ref(recipient);

examples/graphql/src/entities/Message.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { Entity, ManyToOne, PrimaryKey, Property, Ref, ref } from "@mikro-orm/core";
1+
import { Entity, EntityRepositoryType, ManyToOne, PrimaryKey, Property, Ref, ref } from "@mikro-orm/core";
22
import { Chat } from "./Chat";
3+
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
4+
import { type findDataloaderDefault } from "../mikro-orm-config";
35

46
@Entity()
57
export class Message {
@@ -12,6 +14,8 @@ export class Message {
1214
@Property()
1315
content: string;
1416

17+
[EntityRepositoryType]?: IFindDataloaderEntityRepository<Message, typeof findDataloaderDefault>;
18+
1519
constructor({ id, chat, content }: { id?: number; chat?: Chat | Ref<Chat>; content: string }) {
1620
if (id != null) {
1721
this.id = id;

examples/graphql/src/entities/Publisher.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
import { Collection, Entity, Enum, OneToMany, PrimaryKey, Property } from "@mikro-orm/core";
1+
import { Collection, Entity, EntityRepositoryType, Enum, OneToMany, PrimaryKey, Property } from "@mikro-orm/core";
22
import { Book } from "./Book";
3+
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
4+
import { type findDataloaderDefault } from "../mikro-orm-config";
35

46
export enum PublisherType {
57
LOCAL = "local",
@@ -20,6 +22,8 @@ export class Publisher {
2022
@Enum(() => PublisherType)
2123
type = PublisherType.LOCAL;
2224

25+
[EntityRepositoryType]?: IFindDataloaderEntityRepository<Publisher, typeof findDataloaderDefault>;
26+
2327
constructor({ id, name = "asd", type = PublisherType.LOCAL }: { id?: number; name?: string; type?: PublisherType }) {
2428
if (id != null) {
2529
this.id = id;

examples/graphql/src/index.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { assertSingleValue, executeOperation } from "./utils/yoga";
77
import gql from "graphql-tag";
88
import { Book } from "./entities/Book";
99
import { Author } from "./entities/Author";
10-
import { EntityDataLoader } from "mikro-orm-find-dataloader";
10+
// import { EntityDataLoader } from "mikro-orm-find-dataloader";
1111
import { type EntityManager } from "@mikro-orm/core";
1212

1313
const getAuthorsQuery = gql`
@@ -41,7 +41,7 @@ void (async () => {
4141
await populateDatabase(em);
4242
em = orm.em.fork();
4343

44-
const entityDataLoader = new EntityDataLoader(em);
44+
// const entityDataLoader = new EntityDataLoader(em);
4545

4646
const schema = createSchema({
4747
typeDefs: gql`
@@ -68,12 +68,22 @@ void (async () => {
6868
// return await author.books.load();
6969
// return await author.books.load({ dataloader: true });
7070
// return await em.find(Book, { author: author.id });
71-
return await entityDataLoader.find(Book, { author: author.id });
71+
// return await entityDataLoader.find(Book, { author: author.id });
72+
return await em.getRepository(Book).find({ author: author.id }, { dataloader: true });
7273
},
7374
},
7475
},
7576
});
7677

78+
/*
79+
await em.getRepository(Book).find({}, { populate: ["*"], limit: 2 });
80+
await em.getRepository(Book).find({}, { populate: ["*"] });
81+
await em.getRepository(Book).find({}, { populate: ["*"], limit: 2, dataloader: false });
82+
await em.getRepository(Book).find({}, { populate: ["*"], dataloader: false });
83+
await em.getRepository(Book).find({}, { populate: ["*"], limit: 2, dataloader: true });
84+
await em.getRepository(Book).find({}, { populate: ["*"], dataloader: true });
85+
*/
86+
7787
const yoga = createYoga({ schema });
7888
const res = await executeOperation(yoga, getAuthorsQuery);
7989
assertSingleValue(res);
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
1+
import { type Options } from "@mikro-orm/sqlite";
12
import { Author } from "./entities/Author";
23
import { Book } from "./entities/Book";
34
import { Chat } from "./entities/Chat";
45
import { Message } from "./entities/Message";
56
import { Publisher } from "./entities/Publisher";
7+
import { getFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
8+
9+
export const findDataloaderDefault = false;
610

711
export default {
12+
entityRepository: getFindDataloaderEntityRepository(findDataloaderDefault),
813
entities: [Author, Book, Chat, Message, Publisher],
914
dbName: ":memory:",
1015
debug: true,
11-
};
16+
} satisfies Options;

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"@changesets/cli": "^2.27.1",
2727
"@types/eslint": "^8.44.8",
2828
"@types/jest": "^29.5.10",
29-
"@types/node": "^20.10.1",
29+
"@types/node": "^20.10.2",
3030
"@typescript-eslint/eslint-plugin": "^6.13.1",
3131
"@typescript-eslint/parser": "^6.13.1",
3232
"eslint": "^8.54.0",
@@ -40,7 +40,7 @@
4040
"husky": "^8.0.3",
4141
"jest": "^29.7.0",
4242
"lint-staged": "^15.1.0",
43-
"nodemon": "^3.0.1",
43+
"nodemon": "^3.0.2",
4444
"prettier": "^3.1.0",
4545
"rimraf": "^5.0.5",
4646
"ts-jest": "^29.1.1",

0 commit comments

Comments
 (0)