Final schema \u2705 #4

Merged
christian merged 1 commits from full-schema into main 2024-05-26 20:32:47 +00:00

View File

@ -1,59 +1,123 @@
import {
boolean,
integer,
pgTableCreator,
serial,
text,
timestamp,
uniqueIndex,
uuid,
uuid
} from "drizzle-orm/pg-core";
import { relations } from "drizzle-orm";
export const createTable = pgTableCreator((name) => `wine-shop_${name}`);
// Producer Schema
export const producers = createTable(
"producer",
{
id: uuid("id").primaryKey().defaultRandom(),
id: uuid("id").primaryKey(),
name: text("name").notNull(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
description: text("description").notNull(),
imageUrl: text('imageUrl'),
country: text("country").notNull(),
region: text("region").notNull(),
email: text("email").notNull(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow(),
},
(producers) => {
return {
uniqueIdx: uniqueIndex("email").on(producers.email),
uniqueIdx: uniqueIndex("producers").on(producers.id),
};
},
);
export const producersRelations = relations(producers, ({ many }) => ({
// one to man realtion producer -> wines
export const producersRelations = relations(producers, ({ many, one }) => ({
wines: many(wines),
countries: one(countries, {
fields: [producers.country],
references: [countries.id]
})
}));
// Wines schema
export const wines = createTable(
"wine",
{
id: uuid("id").primaryKey().defaultRandom(),
id: uuid("id").primaryKey(),
name: text("name").notNull(),
description: text("description"),
imageUrl: text('imageUrl'),
producer: text("producer").notNull(),
region: text("region").notNull(),
country: text("country").notNull(),
price: integer("price").notNull(),
inStock: boolean("inStock").notNull().default(false),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow(),
producer: uuid("producer").notNull(),
},
(wines) => {
return {
uniqueIdx: uniqueIndex("unique_idx").on(wines.id),
uniqueIdx: uniqueIndex("wines").on(wines.id),
};
},
);
//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],
references: [regions.id]
}),
countries: one(countries, {
fields: [wines.country],
references: [countries.id]
})
}));
export type SelectWine = typeof wines.$inferSelect;
export const regions = createTable(
"region",
{
id: serial("id").primaryKey().notNull(),
name: text("name").notNull().unique(),
country: text("country").notNull(),
},
(regions) => {
return {
uniqueIdx: uniqueIndex("regions").on(regions.id),
};
},
);
export type SelectProducer = typeof producers.$inferSelect;
export type InsertProducer = typeof producers.$inferInsert;
export const regionsRelations = relations(regions, ({ many, one }) => ({
wines: many(wines),
countries: one(countries, {
fields: [regions.country],
references: [countries.id]
})
}));
export const countries = createTable(
"country",
{
id: serial("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)
})
);