Compare commits

..

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

View File

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