added card to show all wines in db
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m24s

This commit is contained in:
ChrQR 2024-05-28 15:39:21 +02:00
parent 2f158c49d6
commit e70ff89833
3 changed files with 52 additions and 52 deletions

View File

@ -1,7 +1,13 @@
import { db } from "~/server/db";
export default async function AllCountries() { export default async function AllCountries() {
const countries = await db.query.countries.findMany();
return ( return (
<div className="pt-4"> <div className="pt-4">
<h1 className="text-2xl">All Countries:</h1> <h1 className="text-2xl">All Countries:</h1>
{countries.map((country) => (
<p key={country.id}>{country.name}</p>
))}
</div> </div>
); );
} }

View File

@ -2,8 +2,8 @@
import { db } from "~/server/db"; import { db } from "~/server/db";
import WineName from "./WineName"; import WineName from "./WineName";
const wines = await db.query.wines.findMany();
export default async function WineList() { export default async function WineList() {
const wines = await db.query.wines.findMany();
return ( return (
<div className="pt-4"> <div className="pt-4">
<h1 className="text-2xl">All wines:</h1> <h1 className="text-2xl">All wines:</h1>

View File

@ -6,7 +6,7 @@ import {
pgTableCreator, pgTableCreator,
text, text,
timestamp, timestamp,
uuid uuid,
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm"; import { relations } from "drizzle-orm";
@ -17,9 +17,9 @@ export const producers = createTable(
"producer", "producer",
{ {
id: uuid("id").primaryKey().defaultRandom(), id: uuid("id").primaryKey().defaultRandom(),
name: text("name").notNull(), name: text("name").notNull().unique(),
description: text("description"), description: text("description"),
imageUrl: text('imageUrl'), imageUrl: text("imageUrl"),
countryId: uuid("countryId").notNull(), countryId: uuid("countryId").notNull(),
createdAt: timestamp("createdAt").defaultNow().notNull(), createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow(), updatedAt: timestamp("updatedAt").defaultNow(),
@ -37,26 +37,29 @@ export const producersRelations = relations(producers, ({ many }) => ({
})); }));
// Wines schema // Wines schema
export const typeEnum = pgEnum('type', ['sparkling', 'white', 'red', 'sweet', 'other']); export const typeEnum = pgEnum("type", [
"sparkling",
"white",
"red",
"sweet",
"other",
]);
export const wines = createTable( export const wines = createTable("wine", {
"wine", id: uuid("id").primaryKey().defaultRandom(),
{ name: text("name").notNull(),
id: uuid("id").primaryKey().defaultRandom(), type: typeEnum("type").notNull(),
name: text("name").notNull(), description: text("description"),
type: typeEnum("type").notNull(), imageUrl: text("imageUrl"),
description: text("description"), producerId: uuid("producerId").notNull(),
imageUrl: text('imageUrl'), subRegionId: uuid("subRegionId"),
producerId: uuid("producerId").notNull(), regionId: uuid("regionId").notNull(),
subRegionId: uuid("subRegionId"), countryId: uuid("countryId").notNull(),
regionId: uuid("regionId").notNull(), price: integer("price").notNull(),
countryId: uuid("countryId").notNull(), inStock: boolean("inStock").notNull().default(false),
price: integer("price").notNull(), createdAt: timestamp("createdAt").defaultNow().notNull(),
inStock: boolean("inStock").notNull().default(false), updatedAt: timestamp("updatedAt").defaultNow(),
createdAt: timestamp("createdAt").defaultNow().notNull(), });
updatedAt: timestamp("updatedAt").defaultNow(),
},
);
// many-to-one relationship wine -> producer // many-to-one relationship wine -> producer
export const winesRelations = relations(wines, ({ one }) => ({ export const winesRelations = relations(wines, ({ one }) => ({
@ -75,53 +78,44 @@ export const winesRelations = relations(wines, ({ one }) => ({
country: one(countries, { country: one(countries, {
fields: [wines.countryId], fields: [wines.countryId],
references: [countries.id], references: [countries.id],
}) }),
})); }));
export const subRegions = createTable( export const subRegions = createTable("subRegion", {
"subRegion", id: uuid("id").primaryKey().notNull().defaultRandom(),
{ name: text("name").notNull().unique(),
id: uuid("id").primaryKey().notNull().defaultRandom(), regionId: uuid("regionId").notNull(),
name: text("name").notNull().unique(), });
regionId: uuid("regionId").notNull(),
},
);
export const subRegionsRelations = relations(subRegions, ({ many, one }) => ({ export const subRegionsRelations = relations(subRegions, ({ many, one }) => ({
wines: many(wines), wines: many(wines),
region: one(regions, { region: one(regions, {
fields: [subRegions.regionId], fields: [subRegions.regionId],
references: [regions.id] references: [regions.id],
}) }),
})); }));
export const regions = createTable( export const regions = createTable("region", {
"region", id: uuid("id").primaryKey().notNull().defaultRandom(),
{ name: text("name").notNull().unique(),
id: uuid("id").primaryKey().notNull().defaultRandom(), countryId: uuid("countryId").notNull(),
name: text("name").notNull().unique(), });
countryId: uuid("countryId").notNull(),
},
);
export const regionsRelations = relations(regions, ({ many, one }) => ({ export const regionsRelations = relations(regions, ({ many, one }) => ({
wines: many(wines), wines: many(wines),
subRegions: many(subRegions), subRegions: many(subRegions),
country: one(countries, { country: one(countries, {
fields: [regions.countryId], fields: [regions.countryId],
references: [countries.id] references: [countries.id],
}) }),
})); }));
export const countries = createTable( export const countries = createTable("country", {
"country", id: uuid("id").primaryKey().notNull().defaultRandom(),
{ name: text("name").notNull().unique(),
id: uuid("id").primaryKey().notNull().defaultRandom(), });
name: text("name").notNull(),
},
);
export const countriesRelations = relations(countries, ({ many }) => ({ export const countriesRelations = relations(countries, ({ many }) => ({
wines: many(wines), wines: many(wines),
regions: many(regions) regions: many(regions),
})); }));