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",
"react": "^18.3.1",
"react-dom": "^18.3.1",
"server-only": "^0.0.1",
"tailwind-merge": "^2.4.0",
"tailwindcss-animate": "^1.0.7",
"zod": "^3.23.3"

View File

@ -41,6 +41,9 @@ importers:
react-dom:
specifier: ^18.3.1
version: 18.3.1(react@18.3.1)
server-only:
specifier: ^0.0.1
version: 0.0.1
tailwind-merge:
specifier: ^2.4.0
version: 2.4.0
@ -2066,6 +2069,9 @@ packages:
seq-queue@0.0.5:
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:
resolution: {integrity: sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==}
engines: {node: '>= 0.4'}
@ -4326,6 +4332,8 @@ snapshots:
seq-queue@0.0.5: {}
server-only@0.0.1: {}
set-function-length@1.2.2:
dependencies:
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";
export default async function HomePage() {
const user = await auth();
if (!user) {
return (
<No_Session />
);
const session = await auth();
if (!session) {
return <No_Session />
} else {
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 />
<Techs_Table />
</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() {
const employees = await db.query.users.findMany({
orderBy: (model, {desc}) => desc(model.id),
});
const employees = await getEmployees();
const formatTime = (timestamp: Date) => {
const date = new Date(timestamp);
@ -14,35 +15,53 @@ export default async function Techs_Table() {
const month = date.toLocaleString("default", { month: "long" });
return `${time} - ${month} ${day}`;
}
//const session = await auth();
//const users_name = session?.user?.name;
return (
<table className="w-5/6 m-auto text-center border-collapse text-[42px]">
<thead className="bg-gradient-to-br from-[#212121] to-[#333333]">
<tr>
<th className="p-5 border border-[#3e4446] text-[48px]"/>
<th className="p-2 border border-[#3e4446] text-[48px]">
Name
</th>
<th className="p-2 border border-[#3e4446] text-[48px]">
Status
</th>
<th className="p-2 border border-[#3e4446] text-[48px]">
Updated At
</th>
</tr>
</thead>
<tbody>
{employees.map((employee) => (
<tr className="even:bg-gradient-to-bl from-[#222222] to-[#232323]" key={employee.id}>
<td className="p-1 border border-[#3e4446]">
<input type="checkbox"/>
</td>
<td className="p-1 border border-[#3e4446]">{employee.name}</td>
<td className="p-1 border border-[#3e4446]">{employee.status}</td>
<td className="p-1 border border-[#3e4446]">{formatTime(employee.updatedAt)}</td>
</tr>
))}
</tbody>
</table>
<div>
<table className="w-5/6 m-auto text-center border-collapse text-[42px]">
<thead className="bg-gradient-to-br from-[#212121] to-[#333333]">
<tr>
<th className="p-5 border border-[#3e4446] text-[48px]"/>
<th className="p-2 border border-[#3e4446] text-[48px]">
Name
</th>
<th className="p-2 border border-[#3e4446] text-[48px]">
Status
</th>
<th className="p-2 border border-[#3e4446] text-[48px]">
Updated At
</th>
</tr>
</thead>
<tbody>
{employees.map((employee) => (
<tr className="even:bg-gradient-to-bl from-[#222222] to-[#232323]" key={employee.id}>
<td className="p-1 border border-[#3e4446]">
<input type="checkbox"
className="m-0 cursor-pointer transform scale-150"
//checked={}
/>
</td>
<td className="p-1 border border-[#3e4446]">{employee.name}</td>
<td className="p-1 border border-[#3e4446]">{employee.status}</td>
<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",
{
id: bigint("id", {mode: "number"}).primaryKey().autoincrement(),
name: varchar("name", { length: 256 }),
status: varchar("status", { length: 256 }),
name: varchar("name", { length: 256 }).notNull(),
status: varchar("status", { length: 256 }).notNull(),
updatedAt: timestamp("updated_at")
.default(sql`CURRENT_TIMESTAMP`)
.notNull(),
@ -26,7 +26,7 @@ export const history = createTable(
{
id: bigint("id", {mode: "number"}).primaryKey().autoincrement(),
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(),
},
);

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),
});
};