-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(api): ⚡ improve database queries for the main page
Related to #1
- Loading branch information
1 parent
990e95e
commit e037e7e
Showing
10 changed files
with
158 additions
and
100 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,6 +1,7 @@ | ||
{ | ||
"conventionalCommits.scopes": [ | ||
"repo", | ||
"header" | ||
"header", | ||
"api" | ||
] | ||
} |
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,15 @@ | ||
import { PrismaClient } from '@prisma/client' | ||
|
||
declare global { | ||
// allow global `var` declarations | ||
// eslint-disable-next-line no-var | ||
var prisma: PrismaClient | undefined | ||
} | ||
|
||
export const prisma = | ||
global.prisma || | ||
new PrismaClient({ | ||
log: ['query'], | ||
}) | ||
|
||
if (process.env.NODE_ENV !== 'production') global.prisma = prisma |
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,91 @@ | ||
// Next.js API route support: https://nextjs.org/docs/api-routes/introduction | ||
import type { NextApiRequest, NextApiResponse } from 'next' | ||
import { prisma } from './../../db' | ||
|
||
type Data = { | ||
name: string | ||
} | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse<Data> | ||
) { | ||
const params = req.query | ||
|
||
if(params.get === "standard"){ | ||
const tools = await prisma.tools.findMany({ | ||
select: { | ||
id: true, | ||
tool_name: true, | ||
submitted_by: true, | ||
tool_link: true, | ||
logo: true, | ||
github_repo: true, | ||
twitter_link: true, | ||
tool_categories: { | ||
select: { | ||
categories: { | ||
select: { | ||
id: true, | ||
category_name: true, | ||
category_icon: true | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
take: 10, | ||
}); | ||
|
||
return res.status(200).json(tools) | ||
} | ||
|
||
if (params.get === "categories") { | ||
const categories = await prisma.categories.findMany({ | ||
select: { | ||
id: true, | ||
category_name: true, | ||
category_icon: true, | ||
category_description: true, | ||
} | ||
}); | ||
|
||
return res.status(200).json(categories) | ||
} | ||
|
||
if (params.get === "recentlyAdded") { | ||
const recentlyAdded = await prisma.tools.findMany({ | ||
select: { | ||
id: true, | ||
tool_name: true, | ||
submitted_by: true, | ||
tool_link: true, | ||
logo: true, | ||
github_repo: true, | ||
twitter_link: true, | ||
tool_categories: { | ||
select: { | ||
categories: { | ||
select: { | ||
id: true, | ||
category_name: true, | ||
category_icon: true | ||
} | ||
} | ||
} | ||
}, | ||
}, | ||
take: 10, | ||
orderBy: { | ||
created_at: "desc" | ||
} | ||
}); | ||
|
||
return res.status(200).json(recentlyAdded) | ||
} | ||
|
||
return res.status(200).json({ | ||
"message": "No data found" | ||
}) | ||
|
||
} |
This file was deleted.
Oops, something went wrong.
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
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
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