Compare commits

...

3 Commits

Author SHA1 Message Date
14b14f9f26 updated create region form
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m26s
2024-06-02 08:14:30 +02:00
2c5fcbcbdb fixed conflict handling 2024-06-02 07:56:39 +02:00
7590e54392 updated schema so same region name can exist in differentcountries 2024-06-02 07:54:09 +02:00
4 changed files with 22 additions and 8 deletions

View File

@ -2,6 +2,7 @@ import { db } from "~/server/db";
export default async function AllRegions() { export default async function AllRegions() {
const allRegions = await db.query.regions.findMany(); const allRegions = await db.query.regions.findMany();
const allCountries = await db.query.countries.findMany();
return ( return (
<div className="pt-4"> <div className="pt-4">
<h1 className="text-2xl">All Regions:</h1> <h1 className="text-2xl">All Regions:</h1>
@ -9,7 +10,14 @@ export default async function AllRegions() {
<> <>
<ul> <ul>
{allRegions.map((region) => ( {allRegions.map((region) => (
<li key={region.id}>{region.name}</li> <li key={region.id}>
{region.name} -{" "}
{
allCountries.find(
(country) => country.id === region.countryId,
)?.name
}
</li>
))} ))}
</ul> </ul>
</> </>

View File

@ -11,7 +11,6 @@ import {
SelectValue, SelectValue,
} from "~/components/ui/select"; } from "~/components/ui/select";
import { useFormState } from "react-dom"; import { useFormState } from "react-dom";
import { init } from "next/dist/compiled/webpack/webpack";
interface Country { interface Country {
id: string; id: string;

View File

@ -25,7 +25,7 @@ export const addRegion = async (prevstate: any, formData: FormData) => {
const confirmedRegion = await db const confirmedRegion = await db
.insert(regions) .insert(regions)
.values(newRegion.data) .values(newRegion.data)
.onConflictDoNothing({ target: regions.name }) .onConflictDoNothing()
.returning({ name: regions.name }); .returning({ name: regions.name });
if (!confirmedRegion[0]?.name) { if (!confirmedRegion[0]?.name) {
return { return {

View File

@ -7,6 +7,7 @@ import {
text, text,
timestamp, timestamp,
uuid, uuid,
unique,
} from "drizzle-orm/pg-core"; } from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm"; import { relations } from "drizzle-orm";
@ -95,11 +96,17 @@ export const subRegionsRelations = relations(subRegions, ({ many, one }) => ({
}), }),
})); }));
export const regions = createTable("region", { export const regions = createTable(
id: uuid("id").primaryKey().notNull().defaultRandom(), "region",
name: text("name").notNull().unique(), {
countryId: uuid("countryId").notNull(), id: uuid("id").primaryKey().notNull().defaultRandom(),
}); name: text("name").notNull(),
countryId: uuid("countryId").notNull(),
},
(t) => ({
uniqueRegion: unique().on(t.name, t.countryId),
}),
);
export const regionsRelations = relations(regions, ({ many, one }) => ({ export const regionsRelations = relations(regions, ({ many, one }) => ({
wines: many(wines), wines: many(wines),