added wine types and subregions. removed indexes for now
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m29s

This commit is contained in:
christian 2024-05-27 21:26:48 +02:00
parent cf33077aa0
commit 4914e314c9

View File

@ -1,8 +1,9 @@
import { import {
boolean, boolean,
index,
integer, integer,
pgEnum,
pgTableCreator, pgTableCreator,
serial,
text, text,
timestamp, timestamp,
uniqueIndex, uniqueIndex,
@ -13,7 +14,6 @@ import { relations } from "drizzle-orm";
export const createTable = pgTableCreator((name) => `wine-shop_${name}`); export const createTable = pgTableCreator((name) => `wine-shop_${name}`);
// Producer Schema // Producer Schema
export const producers = createTable( export const producers = createTable(
"producer", "producer",
{ {
@ -21,84 +21,95 @@ export const producers = createTable(
name: text("name").notNull(), name: text("name").notNull(),
description: text("description").notNull(), description: text("description").notNull(),
imageUrl: text('imageUrl'), imageUrl: text('imageUrl'),
country: text("country").notNull(), countryId: uuid("countryId").notNull(),
createdAt: timestamp("createdAt").defaultNow().notNull(), createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow(), updatedAt: timestamp("updatedAt").defaultNow(),
}, },
(producers) => { (producers) => {
return { return {
uniqueIdx: uniqueIndex("producers").on(producers.id), nameIdx: index("producer_name").on(producers.name),
}; };
}, },
); );
// one to man realtion producer -> wines // one-to-many relation producer -> wines
export const producersRelations = relations(producers, ({ many, one }) => ({ export const producersRelations = relations(producers, ({ many }) => ({
wines: many(wines), wines: many(wines),
countries: one(countries, {
fields: [producers.country],
references: [countries.id]
})
})); }));
// Wines schema // Wines schema
export const typeEnum = pgEnum('type', ['sparkling', 'white', 'red', 'sweet', 'other']);
export const wines = createTable( export const wines = createTable(
"wine", "wine",
{ {
id: uuid("id").primaryKey(), id: uuid("id").primaryKey(),
name: text("name").notNull(), name: text("name").notNull(),
type: typeEnum("type").notNull(),
description: text("description"), description: text("description"),
imageUrl: text('imageUrl'), imageUrl: text('imageUrl'),
producer: text("producer").notNull(), producerId: uuid("producerId").notNull(),
region: text("region").notNull(), subRegionId: uuid("subRegionId"),
country: text("country").notNull(), regionId: uuid("regionId").notNull(),
countryId: uuid("countryId").notNull(),
price: integer("price").notNull(), price: integer("price").notNull(),
inStock: boolean("inStock").notNull().default(false), inStock: boolean("inStock").notNull().default(false),
createdAt: timestamp("createdAt").defaultNow().notNull(), createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow(), 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 subRegionsRelations = relations(subRegions, ({ many, one }) => ({
export const winesRelations = relations(wines, ({ one }) => ({ wines: many(wines),
producer: one(producers, { region: one(regions, {
fields: [wines.producer], fields: [subRegions.regionId],
references: [producers.id],
}),
regions: one(regions, {
fields: [wines.region],
references: [regions.id] references: [regions.id]
}),
countries: one(countries, {
fields: [wines.country],
references: [countries.id]
}) })
})); }));
export const regions = createTable( export const regions = createTable(
"region", "region",
{ {
id: serial("id").primaryKey().notNull(), id: uuid("id").primaryKey().notNull(),
name: text("name").notNull().unique(), name: text("name").notNull().unique(),
country: text("country").notNull(), countryId: uuid("countryId").notNull(),
},
(regions) => {
return {
uniqueIdx: uniqueIndex("regions").on(regions.id),
};
}, },
); );
export const regionsRelations = relations(regions, ({ many, one }) => ({ export const regionsRelations = relations(regions, ({ many, one }) => ({
wines: many(wines), wines: many(wines),
countries: one(countries, { subRegions: many(subRegions),
fields: [regions.country], country: one(countries, {
fields: [regions.countryId],
references: [countries.id] references: [countries.id]
}) })
})); }));
@ -106,18 +117,12 @@ export const regionsRelations = relations(regions, ({ many, one }) => ({
export const countries = createTable( export const countries = createTable(
"country", "country",
{ {
id: serial("id").primaryKey().notNull(), id: uuid("id").primaryKey().notNull(),
name: text("name").notNull(), name: text("name").notNull(),
}, },
(countries) => {
return {
uniqueIdx: uniqueIndex("countries").on(countries.id),
};
},
); );
export const countriesRelations = relations(countries, ({ many }) => ({ export const countriesRelations = relations(countries, ({ many }) => ({
wines: many(wines), wines: many(wines),
regions: many(regions) regions: many(regions)
}) }));
);