Merge pull request 'Final schema \u2705' (#4) from full-schema into main
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m18s
All checks were successful
Vercel Production Deployment / Deploy-Production (push) Successful in 1m18s
Reviewed-on: #4
This commit is contained in:
commit
cf33077aa0
@ -1,59 +1,123 @@
|
|||||||
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().defaultRandom(),
|
id: uuid("id").primaryKey(),
|
||||||
name: text("name").notNull(),
|
name: text("name").notNull(),
|
||||||
createdAt: timestamp("createdAt").defaultNow().notNull(),
|
description: text("description").notNull(),
|
||||||
|
imageUrl: text('imageUrl'),
|
||||||
country: text("country").notNull(),
|
country: text("country").notNull(),
|
||||||
region: text("region").notNull(),
|
createdAt: timestamp("createdAt").defaultNow().notNull(),
|
||||||
email: text("email").notNull(),
|
updatedAt: timestamp("updatedAt").defaultNow(),
|
||||||
|
|
||||||
},
|
},
|
||||||
(producers) => {
|
(producers) => {
|
||||||
return {
|
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),
|
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().defaultRandom(),
|
id: uuid("id").primaryKey(),
|
||||||
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("unique_idx").on(wines.id),
|
uniqueIdx: uniqueIndex("wines").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 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 const regionsRelations = relations(regions, ({ many, one }) => ({
|
||||||
export type InsertProducer = typeof producers.$inferInsert;
|
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)
|
||||||
|
})
|
||||||
|
);
|
Loading…
Reference in New Issue
Block a user