Guest User

Untitled

a guest
Oct 26th, 2023
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { relations } from "drizzle-orm";
  2. import { type AdapterAccount } from "next-auth/adapters";
  3. import {
  4.     index,
  5.     pgTableCreator,
  6.     primaryKey,
  7.     serial,
  8.     text,
  9.     timestamp,
  10.     varchar,
  11.     integer,
  12.     date, boolean, doublePrecision
  13. } from "drizzle-orm/pg-core";
  14. import { createInsertSchema, createSelectSchema } from "drizzle-zod";
  15. import { z } from "zod";
  16.  
  17. /**
  18.  * This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
  19.  * database instance for multiple projects.
  20.  *
  21.  * @see https://orm.drizzle.team/docs/goodies#multi-project-schema
  22.  */
  23. export const pgTable = pgTableCreator((name) => `shoptongout_${name}`);
  24.  
  25. export const posts = pgTable(
  26.     "post",
  27.     {
  28.         id: serial("id").primaryKey(),
  29.  
  30.     },
  31. );
  32.  
  33. export const users = pgTable("user", {
  34.     id: text("id").notNull().unique().primaryKey(),
  35.     name: varchar("name", { length: 255 }),
  36.     surname: varchar("surname", { length: 255 }),
  37.     postal_code: varchar("postal_code", { length: 6 }),
  38.     city: varchar("city", { length: 255 }),
  39.     address: varchar("address", { length: 255 }),
  40.     image: varchar("image", { length: 191 }),
  41.     email: text("email").notNull(),
  42.     emailVerified: timestamp("emailVerified", { mode: "date" }),
  43.     isStaff: boolean("isStaff").default(false).notNull(),
  44.     password: varchar("password")
  45. });
  46.  
  47.  
  48. export const userSchema = createInsertSchema(users).omit({
  49.     emailVerified: true,
  50.     password: true,
  51.     isStaff: true,
  52.     image: true,
  53.  
  54. }).extend({
  55.     type: z.string()
  56. })
  57.  
  58.  
  59.  
  60.  
  61. export const insertUserSchema = createInsertSchema(users)
  62. export const selectUserSchema = createSelectSchema(users);
  63.  
  64. //generate a schema for the user form like insertProductSchemaFromForm
  65.  
  66.  
  67.  
  68. export const orders = pgTable("order", {
  69.     id: serial("order_id").primaryKey(),
  70.     user_id: text("id").notNull(),
  71.     ordered_date: date("ordered_at").notNull().defaultNow(),
  72.     shipped_date: date("shipped_at"),
  73.     payment_date: date("paid_at"),
  74.     payment_amount: doublePrecision("payment_amount"),
  75.  
  76. })
  77. export const products = pgTable("product", {
  78.     id: serial("product_id").primaryKey(),
  79.     name: varchar("name", { length: 255 }).notNull(),
  80.     nicotine_quantity: integer("nicotine_quantity").notNull(),
  81.     liquid_quantity: integer("liquid_quantity").notNull(),
  82.     price: doublePrecision("price").notNull(),
  83.     remaining_quantity: integer("remaining_quantity").notNull()
  84. })
  85.  
  86. export const insertProductSchema = createInsertSchema(products);
  87. export const selectProductSchema = createSelectSchema(products);
  88. export const deleteProductSchema = insertProductSchema.pick({
  89.     id: true,
  90. });
  91.  
  92.  
  93.  
  94.  
  95.  
  96. export const orderItems = pgTable("orderItem", {
  97.     product_id: serial("product_id").notNull(),
  98.     order_id: serial("order_id").notNull(),
  99.     quantity: integer("quantity")
  100.  
  101. },
  102.     (orderItems) => ({
  103.         compoundKey: primaryKey(orderItems.product_id, orderItems.order_id),
  104.     }
  105.     ))
  106.  
  107. export const insertOrderItemSchema = createInsertSchema(orderItems);
  108. export const selectOrderItemSchema = createSelectSchema(orderItems);
  109. export const deleteOrderItemSchema = insertOrderItemSchema.pick({ product_id: true, order_id: true });
  110.  
  111. export const userItems = pgTable("userItem", {
  112.     product_id: serial("product_id").notNull().references(() => products.id),
  113.     user_id: text("user_id").notNull().references(() => users.id),
  114.     quantity: integer("quantity").notNull()
  115. },
  116.     (userItems) => ({
  117.         compoundKey: primaryKey(userItems.product_id, userItems.user_id),
  118.     }
  119.     )
  120. )
  121.  
  122. export const insertUserItemSchema = createInsertSchema(userItems);
  123. export const selectUserItemSchema = createSelectSchema(userItems);
  124. export const deleteUserItemSchema = insertUserItemSchema.pick({ product_id: true, user_id: true });
  125.  
  126.  
  127. export const usersRelations = relations(users, ({ many }) => ({
  128.     //accounts: many(accounts),
  129.     userItems: many(userItems),
  130.     orders: many(orders),
  131.     //sessions: many(sessions)
  132. }));
  133.  
  134. export const productsRelations = relations(products, ({ many }) => ({
  135.     userItems: many(userItems),
  136.     orderItems: many(orderItems)
  137. }))
  138.  
  139. export const userItemsRelations = relations(userItems, ({ one }) => ({
  140.     product: one(products, {
  141.         fields: [userItems.product_id],
  142.         references: [products.id]
  143.     }),
  144.  
  145.     user: one(users, {
  146.         fields: [userItems.user_id],
  147.         references: [users.id]
  148.     }),
  149. }))
  150.  
  151. export const orderItemsRelations = relations(orderItems, ({ one }) => ({
  152.     product: one(products, {
  153.         fields: [orderItems.product_id],
  154.         references: [products.id]
  155.     }),
  156.  
  157.     order: one(orders, {
  158.         fields: [orderItems.order_id],
  159.         references: [orders.id]
  160.     }),
  161. }))
  162.  
  163. export const ordersRelations = relations(orders, ({ one, many }) => ({
  164.     user: one(users, {
  165.         fields: [orders.user_id],
  166.         references: [users.id]
  167.     }),
  168.  
  169.     orderItems: many(orderItems)
  170. }))
  171.  
  172. const accounts = pgTable(
  173.     "account",
  174.     {
  175.       userId: text("userId")
  176.         .notNull()
  177.         .references(() => users.id, { onDelete: "cascade" }),
  178.       type: text("type").$type<AdapterAccount["type"]>().notNull(),
  179.       provider: text("provider").notNull(),
  180.       providerAccountId: text("providerAccountId").notNull(),
  181.       refresh_token: text("refresh_token"),
  182.       access_token: text("access_token"),
  183.       expires_at: integer("expires_at"),
  184.       token_type: text("token_type"),
  185.       scope: text("scope"),
  186.       id_token: text("id_token"),
  187.       session_state: text("session_state"),
  188.     },
  189.     (account) => ({
  190.       compoundKey: primaryKey(account.provider, account.providerAccountId),
  191.     })
  192.   )
  193.  
  194.   const sessions = pgTable("session", {
  195.     sessionToken: text("sessionToken").notNull().primaryKey(),
  196.     userId: text("userId")
  197.       .notNull()
  198.       .references(() => users.id, { onDelete: "cascade" }),
  199.     expires: timestamp("expires", { mode: "date" }).notNull(),
  200.   })
  201.  
  202.   const verificationTokens = pgTable(
  203.     "verificationToken",
  204.     {
  205.       identifier: text("identifier").notNull(),
  206.       token: text("token").notNull(),
  207.       expires: timestamp("expires", { mode: "date" }).notNull(),
  208.     },
  209.     (vt) => ({
  210.       compoundKey: primaryKey(vt.identifier, vt.token),
  211.     })
  212.   )
  213.  
  214.  
  215.  
  216.  
  217.  
  218. //export const accountsRelations = relations(accounts, ({ one }) => ({
  219. //    user: one(users, { fields: [accounts.userId], references: [users.id] }),
  220. //}));
  221.  
  222.  
  223. //export const sessionsRelations = relations(sessions, ({ one }) => ({
  224. //    user: one(users, { fields: [sessions.userId], references: [users.id] }),
  225. //}));
  226.  
  227.  
  228.  
Add Comment
Please, Sign In to add comment