Skip to content

Commit

Permalink
feat: repository api (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkbasic authored Dec 1, 2023
1 parent 40f227f commit 137d5df
Show file tree
Hide file tree
Showing 15 changed files with 416 additions and 280 deletions.
6 changes: 6 additions & 0 deletions .changeset/chilly-pumpkins-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"graphql-example": major
"mikro-orm-find-dataloader": major
---

Switch to Repository API
6 changes: 5 additions & 1 deletion examples/graphql/src/entities/Author.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Collection, Entity, ManyToMany, OneToMany, PrimaryKey, Property } from "@mikro-orm/core";
import { Collection, Entity, EntityRepositoryType, ManyToMany, OneToMany, PrimaryKey, Property } from "@mikro-orm/core";
import { Book } from "./Book";
import { Chat } from "./Chat";
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
import { type findDataloaderDefault } from "../mikro-orm-config";

@Entity()
export class Author {
Expand Down Expand Up @@ -30,6 +32,8 @@ export class Author {
@OneToMany(() => Chat, (chat) => chat.owner)
ownedChats: Collection<Chat> = new Collection<Chat>(this);

[EntityRepositoryType]?: IFindDataloaderEntityRepository<Author, typeof findDataloaderDefault>;

constructor({ id, name, email }: { id?: number; name: string; email: string }) {
if (id != null) {
this.id = id;
Expand Down
6 changes: 5 additions & 1 deletion examples/graphql/src/entities/Book.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { Entity, ManyToOne, PrimaryKey, Property, Ref, ref } from "@mikro-orm/core";
import { Entity, EntityRepositoryType, ManyToOne, PrimaryKey, Property, Ref, ref } from "@mikro-orm/core";
import { Author } from "./Author";
import { Publisher } from "./Publisher";
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
import { type findDataloaderDefault } from "../mikro-orm-config";

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

[EntityRepositoryType]?: IFindDataloaderEntityRepository<Book, typeof findDataloaderDefault>;

constructor({ id, title, author }: { id?: number; title: string; author: Author | Ref<Author> }) {
if (id != null) {
this.id = id;
Expand Down
15 changes: 14 additions & 1 deletion examples/graphql/src/entities/Chat.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,17 @@
import { Collection, Entity, ManyToOne, OneToMany, PrimaryKeyProp, Ref, ref } from "@mikro-orm/core";
import {
Collection,
Entity,
EntityRepositoryType,
ManyToOne,
OneToMany,
PrimaryKeyProp,
Ref,
ref,
} from "@mikro-orm/core";
import { Author } from "./Author";
import { Message } from "./Message";
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
import { type findDataloaderDefault } from "../mikro-orm-config";

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

[EntityRepositoryType]?: IFindDataloaderEntityRepository<Chat, typeof findDataloaderDefault>;

constructor({ owner, recipient }: { owner: Author | Ref<Author>; recipient: Author | Ref<Author> }) {
this.owner = ref(owner);
this.recipient = ref(recipient);
Expand Down
6 changes: 5 additions & 1 deletion examples/graphql/src/entities/Message.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Entity, ManyToOne, PrimaryKey, Property, Ref, ref } from "@mikro-orm/core";
import { Entity, EntityRepositoryType, ManyToOne, PrimaryKey, Property, Ref, ref } from "@mikro-orm/core";
import { Chat } from "./Chat";
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
import { type findDataloaderDefault } from "../mikro-orm-config";

@Entity()
export class Message {
Expand All @@ -12,6 +14,8 @@ export class Message {
@Property()
content: string;

[EntityRepositoryType]?: IFindDataloaderEntityRepository<Message, typeof findDataloaderDefault>;

constructor({ id, chat, content }: { id?: number; chat?: Chat | Ref<Chat>; content: string }) {
if (id != null) {
this.id = id;
Expand Down
6 changes: 5 additions & 1 deletion examples/graphql/src/entities/Publisher.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Collection, Entity, Enum, OneToMany, PrimaryKey, Property } from "@mikro-orm/core";
import { Collection, Entity, EntityRepositoryType, Enum, OneToMany, PrimaryKey, Property } from "@mikro-orm/core";
import { Book } from "./Book";
import { type IFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";
import { type findDataloaderDefault } from "../mikro-orm-config";

export enum PublisherType {
LOCAL = "local",
Expand All @@ -20,6 +22,8 @@ export class Publisher {
@Enum(() => PublisherType)
type = PublisherType.LOCAL;

[EntityRepositoryType]?: IFindDataloaderEntityRepository<Publisher, typeof findDataloaderDefault>;

constructor({ id, name = "asd", type = PublisherType.LOCAL }: { id?: number; name?: string; type?: PublisherType }) {
if (id != null) {
this.id = id;
Expand Down
16 changes: 13 additions & 3 deletions examples/graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { assertSingleValue, executeOperation } from "./utils/yoga";
import gql from "graphql-tag";
import { Book } from "./entities/Book";
import { Author } from "./entities/Author";
import { EntityDataLoader } from "mikro-orm-find-dataloader";
// import { EntityDataLoader } from "mikro-orm-find-dataloader";
import { type EntityManager } from "@mikro-orm/core";

const getAuthorsQuery = gql`
Expand Down Expand Up @@ -41,7 +41,7 @@ void (async () => {
await populateDatabase(em);
em = orm.em.fork();

const entityDataLoader = new EntityDataLoader(em);
// const entityDataLoader = new EntityDataLoader(em);

const schema = createSchema({
typeDefs: gql`
Expand All @@ -68,12 +68,22 @@ void (async () => {
// return await author.books.load();
// return await author.books.load({ dataloader: true });
// return await em.find(Book, { author: author.id });
return await entityDataLoader.find(Book, { author: author.id });
// return await entityDataLoader.find(Book, { author: author.id });
return await em.getRepository(Book).find({ author: author.id }, { dataloader: true });
},
},
},
});

/*
await em.getRepository(Book).find({}, { populate: ["*"], limit: 2 });
await em.getRepository(Book).find({}, { populate: ["*"] });
await em.getRepository(Book).find({}, { populate: ["*"], limit: 2, dataloader: false });
await em.getRepository(Book).find({}, { populate: ["*"], dataloader: false });
await em.getRepository(Book).find({}, { populate: ["*"], limit: 2, dataloader: true });
await em.getRepository(Book).find({}, { populate: ["*"], dataloader: true });
*/

const yoga = createYoga({ schema });
const res = await executeOperation(yoga, getAuthorsQuery);
assertSingleValue(res);
Expand Down
7 changes: 6 additions & 1 deletion examples/graphql/src/mikro-orm-config.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { type Options } from "@mikro-orm/sqlite";
import { Author } from "./entities/Author";
import { Book } from "./entities/Book";
import { Chat } from "./entities/Chat";
import { Message } from "./entities/Message";
import { Publisher } from "./entities/Publisher";
import { getFindDataloaderEntityRepository } from "mikro-orm-find-dataloader";

export const findDataloaderDefault = false;

export default {
entityRepository: getFindDataloaderEntityRepository(findDataloaderDefault),
entities: [Author, Book, Chat, Message, Publisher],
dbName: ":memory:",
debug: true,
};
} satisfies Options;
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@changesets/cli": "^2.27.1",
"@types/eslint": "^8.44.8",
"@types/jest": "^29.5.10",
"@types/node": "^20.10.1",
"@types/node": "^20.10.2",
"@typescript-eslint/eslint-plugin": "^6.13.1",
"@typescript-eslint/parser": "^6.13.1",
"eslint": "^8.54.0",
Expand All @@ -40,7 +40,7 @@
"husky": "^8.0.3",
"jest": "^29.7.0",
"lint-staged": "^15.1.0",
"nodemon": "^3.0.1",
"nodemon": "^3.0.2",
"prettier": "^3.1.0",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.1",
Expand Down
Loading

0 comments on commit 137d5df

Please sign in to comment.