Skip to content

Commit 9e8b531

Browse files
authored
Merge pull request #133 from CodeForStartup/feat/refactor-prisma
feat: refactor prisma
2 parents 59a24be + 5644249 commit 9e8b531

File tree

10 files changed

+46
-74
lines changed

10 files changed

+46
-74
lines changed

apps/web/@/actions/protect/postAction.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { revalidatePath } from "next/cache"
44
import { redirect } from "next/navigation"
55

6-
import {
6+
import prisma, {
77
createPost,
88
PostOnUserType,
99
PostStatus,

apps/web/@/molecules/post-form/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import Image from "next/image"
66
import Link from "next/link"
77

88
import { zodResolver } from "@hookform/resolvers/zod"
9-
import { Image as ImageType, Prisma, TPostItem } from "database"
9+
import { Prisma, TImage, TPostItem } from "database"
1010
import { Upload as UploadIcon, X } from "lucide-react"
1111
import { useSession } from "next-auth/react"
1212
import { useTranslations } from "next-intl"
@@ -28,7 +28,7 @@ const PostForm = ({ post: postData }: { post?: TPostItem }) => {
2828

2929
const t = useTranslations()
3030
const session = useSession()
31-
const [image, setImage] = useState<ImageType | null>(postData?.Image)
31+
const [image, setImage] = useState<TImage | null>(postData?.Image)
3232

3333
const userId = session?.data?.user?.id
3434

packages/database/src/images/queries.ts

Lines changed: 16 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,12 @@
1-
import { Image } from "@prisma/client"
1+
import { Image, Prisma } from "@prisma/client"
22

33
import prisma from "../prisma"
4-
import { DEFAULT_LIMIT, DEFAULT_PAGE, IActionReturn, IGetListResponse } from "../shared/type"
5-
import { imageSelect } from "./selects"
4+
import { DEFAULT_LIMIT, DEFAULT_PAGE, IActionReturn } from "../shared/type"
5+
import { imageSelect, TImage } from "./selects"
66
import { IImageFilter, IListImageResponse } from "./type"
77

88
export const getImages = async (options: IImageFilter): Promise<IListImageResponse> => {
9-
const {
10-
page = DEFAULT_PAGE,
11-
limit = DEFAULT_LIMIT,
12-
userId,
13-
orderBy: orderByKey,
14-
order,
15-
search,
16-
} = options
17-
18-
console.log(">>>options", options)
9+
const { page = DEFAULT_PAGE, limit = DEFAULT_LIMIT, userId, order } = options
1910

2011
try {
2112
let where = {}
@@ -25,33 +16,10 @@ export const getImages = async (options: IImageFilter): Promise<IListImageRespon
2516
userId: userId,
2617
}
2718
}
28-
29-
if (search) {
30-
where = {
31-
...where,
32-
name: {
33-
contains: search,
34-
mode: "insensitive",
35-
},
36-
}
37-
}
38-
39-
let orderBy = {
40-
createdAt: "desc",
41-
}
42-
43-
if (orderByKey && order) {
44-
orderBy = {
45-
...orderBy,
46-
[orderByKey]: order,
47-
}
48-
}
49-
5019
const [total, data] = await Promise.all([
5120
prisma.image.count({ where }),
5221
prisma.image.findMany({
5322
where,
54-
orderBy,
5523
take: limit,
5624
skip: (page - 1) * limit,
5725
select: imageSelect,
@@ -88,6 +56,12 @@ export const getImage = async (id: string): Promise<IActionReturn<Image>> => {
8856
select: imageSelect,
8957
})
9058

59+
if (!image) {
60+
return {
61+
error: "NOT_FOUND",
62+
}
63+
}
64+
9165
return {
9266
data: image,
9367
}
@@ -99,8 +73,8 @@ export const getImage = async (id: string): Promise<IActionReturn<Image>> => {
9973
}
10074

10175
export const createImage = async (
102-
data: Omit<Image, "id" | "createdAt" | "updatedAt">
103-
): Promise<IActionReturn<Image>> => {
76+
data: Prisma.ImageCreateInput
77+
): Promise<IActionReturn<TImage>> => {
10478
try {
10579
const image = await prisma.image.create({
10680
data,
@@ -118,8 +92,8 @@ export const createImage = async (
11892

11993
export const updateImage = async (
12094
id: string,
121-
data: Partial<Omit<Image, "id" | "createdAt" | "updatedAt">>
122-
): Promise<IActionReturn<Image>> => {
95+
data: Prisma.ImageUpdateInput
96+
): Promise<IActionReturn<TImage>> => {
12397
try {
12498
const image = await prisma.image.update({
12599
where: { id },
@@ -136,16 +110,15 @@ export const updateImage = async (
136110
}
137111
}
138112

139-
export const deleteImage = async (id: string, userId: string): Promise<IActionReturn<"">> => {
113+
export const deleteImage = async (id: string, userId: string): Promise<IActionReturn<Image>> => {
140114
try {
141-
// only owner can delete
142115
const deleteImage = await prisma.image.delete({
143116
where: { id, userId },
144117
})
145118

146119
if (!deleteImage) {
147120
return {
148-
error: "Unauthorized",
121+
error: "UNAUTHORIZED",
149122
}
150123
}
151124

packages/database/src/images/selects.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ export const imageSelect = {
1818
updatedAt: true,
1919
} satisfies Prisma.ImageSelect
2020

21-
const getImage = Prisma.validator<Prisma.ImageDefaultArgs>()({
22-
select: imageSelect,
23-
})
24-
25-
export type TImageItem = Prisma.ImageGetPayload<typeof getImage>
21+
export type TImage = Prisma.ImageGetPayload<{
22+
select: typeof imageSelect
23+
}>
Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import { Image } from "@prisma/client"
2-
import { IActionReturn, IGetListResponse, OrderBy } from "src/shared/type"
1+
import { Image, Prisma } from "@prisma/client"
2+
import { IActionReturn, IGetListResponse } from "src/shared/type"
33

44
export type ImageOrderBys = "createdAt" | "name"
55

66
export interface IImageFilter {
77
page?: number
88
limit?: number
99
userId?: string
10-
orderBy?: ImageOrderBys
11-
order?: OrderBy
12-
search?: string
10+
where?: Prisma.ImageWhereInput
11+
order?: Prisma.ImageOrderByRelevanceInput
1312
}
1413

1514
export interface IListImageResponse extends IActionReturn<IGetListResponse<Image>> {}

packages/database/src/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { createImage, deleteImage, getImage, getImages, updateImage } from "./images/queries"
2+
import { TImage } from "./images/selects"
23
import { IImageFilter, IListImageResponse, ImageOrderBys } from "./images/type"
34
import { createPost, getPost, getPosts, updatePost, updatePostStatus } from "./posts/queries"
45
import { TCreatePostInput, TPostActionType, TPostItem } from "./posts/selects"
@@ -9,7 +10,6 @@ import {
910
DEFAULT_PAGE,
1011
IActionReturn,
1112
IGetListResponse,
12-
OrderBy,
1313
PeriodValues,
1414
} from "./shared/type"
1515
import { createTag, getTag, getTags, getTopTags } from "./tags/queries"
@@ -56,10 +56,10 @@ export type {
5656
IImageFilter,
5757
IListImageResponse,
5858
ImageOrderBys,
59+
TImage,
5960

6061
// Shared
6162
IActionReturn,
62-
OrderBy,
6363
IGetListResponse,
6464
PeriodValues,
6565
DEFAULT_LIMIT,

packages/database/src/posts/queries.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,15 @@ export const getPosts = async ({ searchParams }: TGetPostsRequest): Promise<TGet
157157
total: total,
158158
page: Number(page),
159159
limit: Number(limit),
160+
totalPages: Math.ceil(total / Number(limit)),
160161
},
161162
}
162163
} catch (error) {
163164
return {
164165
data: {
165166
data: [],
166167
total: 0,
168+
totalPages: 0,
167169
page: Number(page),
168170
limit: Number(limit),
169171
},

packages/database/src/posts/selects.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,9 @@ export const postSelect = {
5050
},
5151
} satisfies Prisma.PostSelect
5252

53-
const getPostItem = Prisma.validator<Prisma.PostDefaultArgs>()({
54-
select: postSelect,
55-
})
56-
57-
export type TPostItem = Prisma.PostGetPayload<typeof getPostItem>
53+
export type TPostItem = Prisma.PostGetPayload<{
54+
select: typeof postSelect
55+
}>
5856

5957
export type TCreatePostInput = Prisma.PostCreateInput & {
6058
tags?: {

packages/database/src/prisma.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,19 @@
11
import { PrismaClient } from "@prisma/client"
22

3-
declare global {
4-
const prisma: PrismaClient | undefined
5-
}
3+
// declare global {
4+
// const prisma: PrismaClient | undefined
5+
// }
66

7-
const prisma =
8-
global.prisma ||
9-
new PrismaClient({
10-
log: ["info", "warn", "error", "query"],
11-
})
7+
// const prisma =
8+
// global.prisma ||
9+
// new PrismaClient({
10+
// log: ["info", "warn", "error", "query"],
11+
// })
1212

13-
if (process.env.NODE_ENV === "development") global.prisma = prisma
13+
// if (process.env.NODE_ENV === "development") global.prisma = prisma
14+
15+
const prisma = new PrismaClient({
16+
log: ["info", "warn", "error", "query"],
17+
})
1418

1519
export default prisma

packages/database/src/shared/type.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ export interface IActionReturn<T> {
1919
error?: any
2020
}
2121

22-
export type OrderBy = "asc" | "desc"
23-
2422
export interface IGetListResponse<T> {
2523
data: T[]
2624
total: number

0 commit comments

Comments
 (0)