-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(@kampus/gql-utils): add createPrismaCountLoader (#583)
# Description Create createPrismaCountLoader to use for post and comment upvote counts
- Loading branch information
Showing
5 changed files
with
66 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import { beforeEach } from "vitest"; | ||
import { mockDeep, mockReset } from "vitest-mock-extended"; | ||
|
||
import { type PrismaClient, type User } from "@kampus/prisma"; | ||
import { type PrismaClient, type Upvote, type User } from "@kampus/prisma"; | ||
|
||
beforeEach(() => { | ||
mockReset(mockedPrisma); | ||
|
@@ -23,3 +23,13 @@ export const mockUser = (overrides: Partial<User>): User => ({ | |
email: "[email protected]", | ||
...overrides, | ||
}); | ||
|
||
export const mockUpvote = (overrides: Partial<Upvote>): Upvote => ({ | ||
id: "1", | ||
postID: "1", | ||
userID: "1", | ||
createdAt: mockDate(), | ||
updatedAt: mockDate(), | ||
deletedAt: null, | ||
...overrides, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { beforeEach, describe, expect, it } from "vitest"; | ||
|
||
import { mockedPrisma } from "./__mocks__/prisma"; | ||
import { ConnectionKey } from "./connection-key"; | ||
import { createPrismaCountLoader } from "./create-count-loader"; | ||
|
||
describe(createPrismaCountLoader, () => { | ||
beforeEach(() => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
mockedPrisma.upvote.count.mockImplementation((args: any): any => { | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access | ||
return Promise.resolve(args?.where?.postID === "1" ? 1 : 0); | ||
}); | ||
}); | ||
|
||
it("works", async () => { | ||
const byID = createPrismaCountLoader(mockedPrisma.upvote, "postID"); | ||
|
||
const result = await byID.load(new ConnectionKey("1")); | ||
expect(result).toBe(1); | ||
}); | ||
|
||
it("returns zero when trying to load a model that doesn't exist", async () => { | ||
const byID = createPrismaCountLoader(mockedPrisma.upvote, "postID"); | ||
|
||
const result = await byID.load(new ConnectionKey("2")); | ||
expect(result).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import DataLoader from "dataloader"; | ||
|
||
import { type ConnectionKey } from "./connection-key"; | ||
import { type PrismaModel } from "./types"; | ||
|
||
export function createPrismaCountLoader<TPrisma extends { id: string }>( | ||
table: PrismaModel<TPrisma>, | ||
identifier: string | ||
) { | ||
return new DataLoader(async (keys: readonly ConnectionKey[]) => { | ||
const counts = await Promise.all( | ||
keys.map((key) => { | ||
const where = { [identifier]: key.parentID, deletedAt: null }; | ||
|
||
return table.count({ where }); | ||
}) | ||
); | ||
|
||
return counts; | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters