From 4914e314c9e5f4475babc7cbdc1ee4ac2a58e7bc Mon Sep 17 00:00:00 2001 From: christian Date: Mon, 27 May 2024 21:26:48 +0200 Subject: [PATCH] added wine types and subregions. removed indexes for now --- src/server/db/schema.ts | 99 ++++++++++++++++++++++------------------- 1 file changed, 52 insertions(+), 47 deletions(-) diff --git a/src/server/db/schema.ts b/src/server/db/schema.ts index 30a74a9..9ad74a8 100644 --- a/src/server/db/schema.ts +++ b/src/server/db/schema.ts @@ -1,8 +1,9 @@ import { boolean, + index, integer, + pgEnum, pgTableCreator, - serial, text, timestamp, uniqueIndex, @@ -13,7 +14,6 @@ import { relations } from "drizzle-orm"; export const createTable = pgTableCreator((name) => `wine-shop_${name}`); // Producer Schema - export const producers = createTable( "producer", { @@ -21,84 +21,95 @@ export const producers = createTable( name: text("name").notNull(), description: text("description").notNull(), imageUrl: text('imageUrl'), - country: text("country").notNull(), + countryId: uuid("countryId").notNull(), createdAt: timestamp("createdAt").defaultNow().notNull(), updatedAt: timestamp("updatedAt").defaultNow(), - }, (producers) => { return { - uniqueIdx: uniqueIndex("producers").on(producers.id), + nameIdx: index("producer_name").on(producers.name), }; }, ); -// one to man realtion producer -> wines -export const producersRelations = relations(producers, ({ many, one }) => ({ +// one-to-many relation producer -> wines +export const producersRelations = relations(producers, ({ many }) => ({ wines: many(wines), - countries: one(countries, { - fields: [producers.country], - references: [countries.id] - }) })); // Wines schema +export const typeEnum = pgEnum('type', ['sparkling', 'white', 'red', 'sweet', 'other']); + export const wines = createTable( "wine", { id: uuid("id").primaryKey(), name: text("name").notNull(), + type: typeEnum("type").notNull(), description: text("description"), imageUrl: text('imageUrl'), - producer: text("producer").notNull(), - region: text("region").notNull(), - country: text("country").notNull(), + producerId: uuid("producerId").notNull(), + subRegionId: uuid("subRegionId"), + regionId: uuid("regionId").notNull(), + countryId: uuid("countryId").notNull(), price: integer("price").notNull(), inStock: boolean("inStock").notNull().default(false), createdAt: timestamp("createdAt").defaultNow().notNull(), updatedAt: timestamp("updatedAt").defaultNow(), }, - (wines) => { - return { - uniqueIdx: uniqueIndex("wines").on(wines.id), - }; +); + +// many-to-one relationship wine -> producer +export const winesRelations = relations(wines, ({ one }) => ({ + producer: one(producers, { + fields: [wines.producerId], + references: [producers.id], + }), + subregion: one(subRegions, { + fields: [wines.subRegionId], + references: [subRegions.id], + }), + region: one(regions, { + fields: [wines.regionId], + references: [regions.id], + }), + country: one(countries, { + fields: [wines.countryId], + references: [countries.id], + }) +})); + +export const subRegions = createTable( + "subRegion", + { + id: uuid("id").primaryKey().notNull(), + name: text("name").notNull().unique(), + regionId: uuid("regionId").notNull(), }, ); -//many to one relationship wine -> producer -export const winesRelations = relations(wines, ({ one }) => ({ - producer: one(producers, { - fields: [wines.producer], - references: [producers.id], - }), - regions: one(regions, { - fields: [wines.region], +export const subRegionsRelations = relations(subRegions, ({ many, one }) => ({ + wines: many(wines), + region: one(regions, { + fields: [subRegions.regionId], references: [regions.id] - }), - countries: one(countries, { - fields: [wines.country], - references: [countries.id] }) })); export const regions = createTable( "region", { - id: serial("id").primaryKey().notNull(), + id: uuid("id").primaryKey().notNull(), name: text("name").notNull().unique(), - country: text("country").notNull(), - }, - (regions) => { - return { - uniqueIdx: uniqueIndex("regions").on(regions.id), - }; + countryId: uuid("countryId").notNull(), }, ); export const regionsRelations = relations(regions, ({ many, one }) => ({ wines: many(wines), - countries: one(countries, { - fields: [regions.country], + subRegions: many(subRegions), + country: one(countries, { + fields: [regions.countryId], references: [countries.id] }) })); @@ -106,18 +117,12 @@ export const regionsRelations = relations(regions, ({ many, one }) => ({ export const countries = createTable( "country", { - id: serial("id").primaryKey().notNull(), + id: uuid("id").primaryKey().notNull(), name: text("name").notNull(), }, - (countries) => { - return { - uniqueIdx: uniqueIndex("countries").on(countries.id), - }; - }, ); export const countriesRelations = relations(countries, ({ many }) => ({ wines: many(wines), regions: many(regions) - }) -); \ No newline at end of file +}));