Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { relations } from "drizzle-orm";
- import { type AdapterAccount } from "next-auth/adapters";
- import {
- index,
- pgTableCreator,
- primaryKey,
- serial,
- text,
- timestamp,
- varchar,
- integer,
- date, boolean, doublePrecision
- } from "drizzle-orm/pg-core";
- import { createInsertSchema, createSelectSchema } from "drizzle-zod";
- import { z } from "zod";
- /**
- * This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
- * database instance for multiple projects.
- *
- * @see https://orm.drizzle.team/docs/goodies#multi-project-schema
- */
- export const pgTable = pgTableCreator((name) => `shoptongout_${name}`);
- export const posts = pgTable(
- "post",
- {
- id: serial("id").primaryKey(),
- },
- );
- export const users = pgTable("user", {
- id: text("id").notNull().unique().primaryKey(),
- name: varchar("name", { length: 255 }),
- surname: varchar("surname", { length: 255 }),
- postal_code: varchar("postal_code", { length: 6 }),
- city: varchar("city", { length: 255 }),
- address: varchar("address", { length: 255 }),
- image: varchar("image", { length: 191 }),
- email: text("email").notNull(),
- emailVerified: timestamp("emailVerified", { mode: "date" }),
- isStaff: boolean("isStaff").default(false).notNull(),
- password: varchar("password")
- });
- export const userSchema = createInsertSchema(users).omit({
- emailVerified: true,
- password: true,
- isStaff: true,
- image: true,
- }).extend({
- type: z.string()
- })
- export const insertUserSchema = createInsertSchema(users)
- export const selectUserSchema = createSelectSchema(users);
- //generate a schema for the user form like insertProductSchemaFromForm
- export const orders = pgTable("order", {
- id: serial("order_id").primaryKey(),
- user_id: text("id").notNull(),
- ordered_date: date("ordered_at").notNull().defaultNow(),
- shipped_date: date("shipped_at"),
- payment_date: date("paid_at"),
- payment_amount: doublePrecision("payment_amount"),
- })
- export const products = pgTable("product", {
- id: serial("product_id").primaryKey(),
- name: varchar("name", { length: 255 }).notNull(),
- nicotine_quantity: integer("nicotine_quantity").notNull(),
- liquid_quantity: integer("liquid_quantity").notNull(),
- price: doublePrecision("price").notNull(),
- remaining_quantity: integer("remaining_quantity").notNull()
- })
- export const insertProductSchema = createInsertSchema(products);
- export const selectProductSchema = createSelectSchema(products);
- export const deleteProductSchema = insertProductSchema.pick({
- id: true,
- });
- export const orderItems = pgTable("orderItem", {
- product_id: serial("product_id").notNull(),
- order_id: serial("order_id").notNull(),
- quantity: integer("quantity")
- },
- (orderItems) => ({
- compoundKey: primaryKey(orderItems.product_id, orderItems.order_id),
- }
- ))
- export const insertOrderItemSchema = createInsertSchema(orderItems);
- export const selectOrderItemSchema = createSelectSchema(orderItems);
- export const deleteOrderItemSchema = insertOrderItemSchema.pick({ product_id: true, order_id: true });
- export const userItems = pgTable("userItem", {
- product_id: serial("product_id").notNull().references(() => products.id),
- user_id: text("user_id").notNull().references(() => users.id),
- quantity: integer("quantity").notNull()
- },
- (userItems) => ({
- compoundKey: primaryKey(userItems.product_id, userItems.user_id),
- }
- )
- )
- export const insertUserItemSchema = createInsertSchema(userItems);
- export const selectUserItemSchema = createSelectSchema(userItems);
- export const deleteUserItemSchema = insertUserItemSchema.pick({ product_id: true, user_id: true });
- export const usersRelations = relations(users, ({ many }) => ({
- //accounts: many(accounts),
- userItems: many(userItems),
- orders: many(orders),
- //sessions: many(sessions)
- }));
- export const productsRelations = relations(products, ({ many }) => ({
- userItems: many(userItems),
- orderItems: many(orderItems)
- }))
- export const userItemsRelations = relations(userItems, ({ one }) => ({
- product: one(products, {
- fields: [userItems.product_id],
- references: [products.id]
- }),
- user: one(users, {
- fields: [userItems.user_id],
- references: [users.id]
- }),
- }))
- export const orderItemsRelations = relations(orderItems, ({ one }) => ({
- product: one(products, {
- fields: [orderItems.product_id],
- references: [products.id]
- }),
- order: one(orders, {
- fields: [orderItems.order_id],
- references: [orders.id]
- }),
- }))
- export const ordersRelations = relations(orders, ({ one, many }) => ({
- user: one(users, {
- fields: [orders.user_id],
- references: [users.id]
- }),
- orderItems: many(orderItems)
- }))
- const accounts = pgTable(
- "account",
- {
- userId: text("userId")
- .notNull()
- .references(() => users.id, { onDelete: "cascade" }),
- type: text("type").$type<AdapterAccount["type"]>().notNull(),
- provider: text("provider").notNull(),
- providerAccountId: text("providerAccountId").notNull(),
- refresh_token: text("refresh_token"),
- access_token: text("access_token"),
- expires_at: integer("expires_at"),
- token_type: text("token_type"),
- scope: text("scope"),
- id_token: text("id_token"),
- session_state: text("session_state"),
- },
- (account) => ({
- compoundKey: primaryKey(account.provider, account.providerAccountId),
- })
- )
- const sessions = pgTable("session", {
- sessionToken: text("sessionToken").notNull().primaryKey(),
- userId: text("userId")
- .notNull()
- .references(() => users.id, { onDelete: "cascade" }),
- expires: timestamp("expires", { mode: "date" }).notNull(),
- })
- const verificationTokens = pgTable(
- "verificationToken",
- {
- identifier: text("identifier").notNull(),
- token: text("token").notNull(),
- expires: timestamp("expires", { mode: "date" }).notNull(),
- },
- (vt) => ({
- compoundKey: primaryKey(vt.identifier, vt.token),
- })
- )
- //export const accountsRelations = relations(accounts, ({ one }) => ({
- // user: one(users, { fields: [accounts.userId], references: [users.id] }),
- //}));
- //export const sessionsRelations = relations(sessions, ({ one }) => ({
- // user: one(users, { fields: [sessions.userId], references: [users.id] }),
- //}));
Add Comment
Please, Sign In to add comment