more query action
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 2m23s

This commit is contained in:
ChrQR 2024-05-25 00:46:33 +02:00
parent f4cf2f3adc
commit ffbf48bc8a
7 changed files with 60 additions and 40 deletions

View File

@ -7,7 +7,7 @@
"build": "next build", "build": "next build",
"db:push": "drizzle-kit push", "db:push": "drizzle-kit push",
"db:studio": "drizzle-kit studio", "db:studio": "drizzle-kit studio",
"dev": "next dev --turbo", "dev": "next dev",
"lint": "next lint", "lint": "next lint",
"start": "next start" "start": "next start"
}, },

View File

@ -1,4 +1,4 @@
import { getWines } from "../../server/actions/allWines"; import { getWines } from "~/server/actions/allWines";
import WineName from "./WineName"; import WineName from "./WineName";
import WineProducer from "./WineProducer"; import WineProducer from "./WineProducer";
@ -6,10 +6,9 @@ const allWines = await getWines();
export default async function WineCards() { export default async function WineCards() {
return ( return (
<> <>
<p>{JSON.stringify(allWines)}</p>
{allWines.map((wine, i) => { {allWines.map((wine, i) => {
return ( return (
<div key={i}> <div key={i} className="flex">
<WineName name={wine.name} /> - <WineProducer id={wine.producer} /> <WineName name={wine.name} /> - <WineProducer id={wine.producer} />
</div> </div>
); );

View File

@ -1,8 +1,25 @@
import { UUID } from "crypto";
import { PgUUID } from "drizzle-orm/pg-core";
import getProducer from "~/server/actions/getProducer"; import getProducer from "~/server/actions/getProducer";
export default async function (props: { id: string }) { export default async function WineProducer(props: { id: string }) {
const result = await getProducer(props.id); const { id } = props;
return <p>{result?.name}</p>;
// Validate the id before making the database call
if (!id) {
return <p>Invalid producer ID</p>;
}
try {
const result = await getProducer(id);
return !result ? <p>Unable to retrieve producer</p> : <p>{result?.name}</p>;
} catch (error) {
console.error("Error fetching producer:", error);
return <p>Error fetching producer</p>;
}
}
// Utility function to validate UUID
function isValidUUID(uuid: string) {
const uuidRegex =
/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
return uuidRegex.test(uuid);
} }

View File

@ -1,3 +1,4 @@
import { Suspense } from "react";
import CreateWine from "./_components/CreateWine"; import CreateWine from "./_components/CreateWine";
import WineCards from "./_components/WineCards"; import WineCards from "./_components/WineCards";
@ -5,11 +6,13 @@ export const dynamic = "force-dynamic";
export default async function HomePage() { export default async function HomePage() {
return ( return (
<main className=""> <main>
<div className="container "> <div className="container ">
<h1>Yes hello</h1> <h1>Yes hello</h1>
<Suspense fallback={<p>Loading....</p>}>
<CreateWine /> <CreateWine />
<WineCards /> <WineCards />
</Suspense>
</div> </div>
</main> </main>
); );

View File

@ -1,11 +1,10 @@
"use server" "use server";
import { producers } from "../db/schema"; import { producers } from "../db/schema";
import { db } from "../db"; import { db } from "../db";
import { eq } from "drizzle-orm"; import { eq } from "drizzle-orm";
export default async function getProducer(id: string) { export default async function getProducer(id: string) {
return db.query.producers.findFirst({ return db.query.producers.findFirst({
where: eq(producers.id, id) where: eq(producers.id, id),
}); });
} }

View File

@ -1,10 +1,13 @@
"use server" "use server";
import { sql } from "drizzle-orm"; import { sql } from "drizzle-orm";
import { db } from "../db"; import { db } from "../db";
import { wines, producers } from "../db/schema"; import { wines, producers } from "../db/schema";
import { UUID } from "crypto"; import { UUID } from "crypto";
export async function getWineDetails(id: UUID) { export async function getWineDetails(id: string) {
const results = await db.select().from(producers).where(sql `${producers.id} = ${id}`) const results = await db
.select()
.from(producers)
.where(sql`${producers.id} = ${id}`);
return results; return results;
} }

View File

@ -5,23 +5,23 @@ import {
uniqueIndex, uniqueIndex,
uuid, uuid,
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
import { relations } from 'drizzle-orm'; import { relations } from "drizzle-orm";
export const createTable = pgTableCreator((name) => `wine-shop_${name}`); export const createTable = pgTableCreator((name) => `wine-shop_${name}`);
export const producers = createTable( export const producers = createTable(
"producer", "producer",
{ {
id: uuid('id').primaryKey().defaultRandom(), id: uuid("id").primaryKey().defaultRandom(),
name: text('name').notNull(), name: text("name").notNull(),
createdAt: timestamp('createdAt').defaultNow().notNull(), createdAt: timestamp("createdAt").defaultNow().notNull(),
country: text('country').notNull(), country: text("country").notNull(),
region: text('region').notNull(), region: text("region").notNull(),
email: text('email').notNull() email: text("email").notNull(),
}, },
(producers) => { (producers) => {
return { return {
uniqueIdx: uniqueIndex('email').on(producers.email), uniqueIdx: uniqueIndex("email").on(producers.email),
}; };
}, },
); );
@ -33,15 +33,15 @@ export const producersRelations = relations(producers, ({ many }) => ({
export const wines = createTable( export const wines = createTable(
"wine", "wine",
{ {
id: uuid('id').primaryKey().defaultRandom(), id: uuid("id").primaryKey().defaultRandom(),
name: text('name').notNull(), name: text("name").notNull(),
createdAt: timestamp('createdAt').defaultNow().notNull(), createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp('updatedAt').defaultNow(), updatedAt: timestamp("updatedAt").defaultNow(),
producer: uuid('producer').notNull(), producer: uuid("producer").notNull(),
}, },
(wines) => { (wines) => {
return { return {
uniqueIdx: uniqueIndex('unique_idx').on(wines.id), uniqueIdx: uniqueIndex("unique_idx").on(wines.id),
}; };
}, },
); );
@ -55,6 +55,5 @@ export const winesRelations = relations(wines, ({ one }) => ({
export type SelectWine = typeof wines.$inferSelect; export type SelectWine = typeof wines.$inferSelect;
export type SelectProducer = typeof producers.$inferSelect; export type SelectProducer = typeof producers.$inferSelect;
export type InsertProducer = typeof producers.$inferInsert; export type InsertProducer = typeof producers.$inferInsert;