Move db calls to server file

This commit is contained in:
Gabriel Brown 2024-07-19 15:58:24 -05:00
parent bf7f6a466a
commit 76977427e0
6 changed files with 79 additions and 41 deletions

View File

@ -25,6 +25,7 @@
"next-auth": "5.0.0-beta.19", "next-auth": "5.0.0-beta.19",
"react": "^18.3.1", "react": "^18.3.1",
"react-dom": "^18.3.1", "react-dom": "^18.3.1",
"server-only": "^0.0.1",
"tailwind-merge": "^2.4.0", "tailwind-merge": "^2.4.0",
"tailwindcss-animate": "^1.0.7", "tailwindcss-animate": "^1.0.7",
"zod": "^3.23.3" "zod": "^3.23.3"

8
pnpm-lock.yaml generated
View File

@ -41,6 +41,9 @@ importers:
react-dom: react-dom:
specifier: ^18.3.1 specifier: ^18.3.1
version: 18.3.1(react@18.3.1) version: 18.3.1(react@18.3.1)
server-only:
specifier: ^0.0.1
version: 0.0.1
tailwind-merge: tailwind-merge:
specifier: ^2.4.0 specifier: ^2.4.0
version: 2.4.0 version: 2.4.0
@ -2066,6 +2069,9 @@ packages:
seq-queue@0.0.5: seq-queue@0.0.5:
resolution: {integrity: sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q==} resolution: {integrity: sha512-hr3Wtp/GZIc/6DAGPDcV4/9WoZhjrkXsi5B/07QgX8tsdc6ilr7BFM6PM6rbdAX1kFSDYeZGLipIZZKyQP0O5Q==}
server-only@0.0.1:
resolution: {integrity: sha512-qepMx2JxAa5jjfzxG79yPPq+8BuFToHd1hm7kI+Z4zAq1ftQiP7HcxMhDDItrbtwVeLg/cY2JnKnrcFkmiswNA==}
set-function-length@1.2.2: set-function-length@1.2.2:
resolution: {integrity: sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==} resolution: {integrity: sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==}
engines: {node: '>= 0.4'} engines: {node: '>= 0.4'}
@ -4326,6 +4332,8 @@ snapshots:
seq-queue@0.0.5: {} seq-queue@0.0.5: {}
server-only@0.0.1: {}
set-function-length@1.2.2: set-function-length@1.2.2:
dependencies: dependencies:
define-data-property: 1.1.4 define-data-property: 1.1.4

View File

@ -4,14 +4,13 @@ import TT_Header from "~/components/ui/TT_Header";
import Techs_Table from "~/components/ui/Techs_Table"; import Techs_Table from "~/components/ui/Techs_Table";
export default async function HomePage() { export default async function HomePage() {
const user = await auth(); const session = await auth();
if (!user) { if (!session) {
return ( return <No_Session />
<No_Session />
);
} else { } else {
return ( return (
<main className="min-h-screen bg-gradient-to-b from-[#111111] to-[#111325]"> <main className="min-h-screen bg-gradient-to-b
from-[#111111] to-[#111325]">
<TT_Header /> <TT_Header />
<Techs_Table /> <Techs_Table />
</main> </main>

View File

@ -1,10 +1,11 @@
import { db } from '~/server/db'; //import { auth } from "~/auth";
import { getEmployees } from "~/server/functions";
export const dynamic = "force-dynamic";
export default async function Techs_Table() { export default async function Techs_Table() {
const employees = await db.query.users.findMany({ const employees = await getEmployees();
orderBy: (model, {desc}) => desc(model.id),
});
const formatTime = (timestamp: Date) => { const formatTime = (timestamp: Date) => {
const date = new Date(timestamp); const date = new Date(timestamp);
@ -14,35 +15,53 @@ export default async function Techs_Table() {
const month = date.toLocaleString("default", { month: "long" }); const month = date.toLocaleString("default", { month: "long" });
return `${time} - ${month} ${day}`; return `${time} - ${month} ${day}`;
} }
//const session = await auth();
//const users_name = session?.user?.name;
return ( return (
<table className="w-5/6 m-auto text-center border-collapse text-[42px]"> <div>
<thead className="bg-gradient-to-br from-[#212121] to-[#333333]"> <table className="w-5/6 m-auto text-center border-collapse text-[42px]">
<tr> <thead className="bg-gradient-to-br from-[#212121] to-[#333333]">
<th className="p-5 border border-[#3e4446] text-[48px]"/> <tr>
<th className="p-2 border border-[#3e4446] text-[48px]"> <th className="p-5 border border-[#3e4446] text-[48px]"/>
Name <th className="p-2 border border-[#3e4446] text-[48px]">
</th> Name
<th className="p-2 border border-[#3e4446] text-[48px]"> </th>
Status <th className="p-2 border border-[#3e4446] text-[48px]">
</th> Status
<th className="p-2 border border-[#3e4446] text-[48px]"> </th>
Updated At <th className="p-2 border border-[#3e4446] text-[48px]">
</th> Updated At
</tr> </th>
</thead> </tr>
<tbody> </thead>
{employees.map((employee) => ( <tbody>
<tr className="even:bg-gradient-to-bl from-[#222222] to-[#232323]" key={employee.id}> {employees.map((employee) => (
<td className="p-1 border border-[#3e4446]"> <tr className="even:bg-gradient-to-bl from-[#222222] to-[#232323]" key={employee.id}>
<input type="checkbox"/> <td className="p-1 border border-[#3e4446]">
</td> <input type="checkbox"
<td className="p-1 border border-[#3e4446]">{employee.name}</td> className="m-0 cursor-pointer transform scale-150"
<td className="p-1 border border-[#3e4446]">{employee.status}</td> //checked={}
<td className="p-1 border border-[#3e4446]">{formatTime(employee.updatedAt)}</td> />
</tr> </td>
))} <td className="p-1 border border-[#3e4446]">{employee.name}</td>
</tbody> <td className="p-1 border border-[#3e4446]">{employee.status}</td>
</table> <td className="p-1 border border-[#3e4446]">{formatTime(employee.updatedAt)}</td>
</tr>
))}
</tbody>
</table>
<div className="m-auto flex flex-row items-center justify-center">
<input type="text" placeholder="New Status"
className="w-1/5 p-2 border-none rounded-md"
//value={}
/>
<button type="submit"
className="m-2 px-2 py-5 border-none rounded-md text-center bg-gradient-to-br from-[#484848] to-[#333333]"
>
Update
</button>
</div>
</div>
); );
}; };

View File

@ -13,8 +13,8 @@ export const users = createTable(
"users", "users",
{ {
id: bigint("id", {mode: "number"}).primaryKey().autoincrement(), id: bigint("id", {mode: "number"}).primaryKey().autoincrement(),
name: varchar("name", { length: 256 }), name: varchar("name", { length: 256 }).notNull(),
status: varchar("status", { length: 256 }), status: varchar("status", { length: 256 }).notNull(),
updatedAt: timestamp("updated_at") updatedAt: timestamp("updated_at")
.default(sql`CURRENT_TIMESTAMP`) .default(sql`CURRENT_TIMESTAMP`)
.notNull(), .notNull(),
@ -26,7 +26,7 @@ export const history = createTable(
{ {
id: bigint("id", {mode: "number"}).primaryKey().autoincrement(), id: bigint("id", {mode: "number"}).primaryKey().autoincrement(),
user_id: bigint("user_id", {mode: "number"}).references(() => users.id), user_id: bigint("user_id", {mode: "number"}).references(() => users.id),
status: varchar("status", { length: 256 }), status: varchar("status", { length: 256 }).notNull(),
updatedAt: timestamp("updated_at").notNull(), updatedAt: timestamp("updated_at").notNull(),
}, },
); );

11
src/server/functions.ts Normal file
View File

@ -0,0 +1,11 @@
import "server-only";
import { db } from "~/server/db";
//import * as schema from "~/server/db/schema";
export const getEmployees = async () => {
return await db.query.users.findMany({
orderBy: (model, { desc }) => desc(model.id),
});
};