Compare commits

..

No commits in common. "cf33077aa09becf7acc2fe5f09911db15a42bb31" and "0df891fc3b31e36af5d3aaef0249f4bac7139ce7" have entirely different histories.

View File

@ -1,123 +1,59 @@
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(),
id: uuid("id").primaryKey().defaultRandom(),
name: text("name").notNull(),
description: text("description").notNull(),
imageUrl: text('imageUrl'),
country: text("country").notNull(),
createdAt: timestamp("createdAt").defaultNow().notNull(),
updatedAt: timestamp("updatedAt").defaultNow(),
country: text("country").notNull(),
region: text("region").notNull(),
email: text("email").notNull(),
},
(producers) => {
return {
uniqueIdx: uniqueIndex("producers").on(producers.id),
uniqueIdx: uniqueIndex("email").on(producers.email),
};
},
);
// one to man realtion producer -> wines
export const producersRelations = relations(producers, ({ many, one }) => ({
export const producersRelations = relations(producers, ({ many }) => ({
wines: many(wines),
countries: one(countries, {
fields: [producers.country],
references: [countries.id]
})
}));
// Wines schema
export const wines = createTable(
"wine",
{
id: uuid("id").primaryKey(),
id: uuid("id").primaryKey().defaultRandom(),
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("wines").on(wines.id),
uniqueIdx: uniqueIndex("unique_idx").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 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 SelectWine = typeof wines.$inferSelect;
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)
})
);
export type SelectProducer = typeof producers.$inferSelect;
export type InsertProducer = typeof producers.$inferInsert;