diff --git a/drizzle/relations.ts b/drizzle/relations.ts new file mode 100644 index 0000000..4784d00 --- /dev/null +++ b/drizzle/relations.ts @@ -0,0 +1,80 @@ +import { relations } from "drizzle-orm/relations"; +import { blog, blogTag, blogTagRelationship, mentorMenteeRelationship, personalInfo, professionalInfo, saseInfo, session, user } from "./schema"; + +export const blogTagRelationshipRelations = relations(blogTagRelationship, ({ one }) => ({ + blogTag: one(blogTag, { + fields: [blogTagRelationship.tagId], + references: [blogTag.id], + }), + blog: one(blog, { + fields: [blogTagRelationship.blogId], + references: [blog.id], + }), +})); + +export const blogTagRelations = relations(blogTag, ({ many }) => ({ + blogTagRelationships: many(blogTagRelationship), +})); + +export const blogRelations = relations(blog, ({ many, one }) => ({ + blogTagRelationships: many(blogTagRelationship), + user: one(user, { + fields: [blog.author_id], + references: [user.id], + }), +})); + +export const userRelations = relations(user, ({ many }) => ({ + blogs: many(blog), + mentorMenteeRelationships_menteeId: many(mentorMenteeRelationship, { + relationName: "mentorMenteeRelationship_menteeId_user_id", + }), + mentorMenteeRelationships_mentorId: many(mentorMenteeRelationship, { + relationName: "mentorMenteeRelationship_mentorId_user_id", + }), + sessions: many(session), + personalInfos: many(personalInfo), + professionalInfos: many(professionalInfo), + saseInfos: many(saseInfo), +})); + +export const mentorMenteeRelationshipRelations = relations(mentorMenteeRelationship, ({ one }) => ({ + user_menteeId: one(user, { + fields: [mentorMenteeRelationship.menteeId], + references: [user.id], + relationName: "mentorMenteeRelationship_menteeId_user_id", + }), + user_mentorId: one(user, { + fields: [mentorMenteeRelationship.mentorId], + references: [user.id], + relationName: "mentorMenteeRelationship_mentorId_user_id", + }), +})); + +export const sessionRelations = relations(session, ({ one }) => ({ + user: one(user, { + fields: [session.userId], + references: [user.id], + }), +})); + +export const personalInfoRelations = relations(personalInfo, ({ one }) => ({ + user: one(user, { + fields: [personalInfo.userId], + references: [user.id], + }), +})); + +export const professionalInfoRelations = relations(professionalInfo, ({ one }) => ({ + user: one(user, { + fields: [professionalInfo.userId], + references: [user.id], + }), +})); + +export const saseInfoRelations = relations(saseInfo, ({ one }) => ({ + user: one(user, { + fields: [saseInfo.userId], + references: [user.id], + }), +})); diff --git a/drizzle/schema.ts b/drizzle/schema.ts new file mode 100644 index 0000000..17727b0 --- /dev/null +++ b/drizzle/schema.ts @@ -0,0 +1,150 @@ +// import { sql } from "drizzle-orm"; +import { blob, integer, sqliteTable, text, uniqueIndex } from "drizzle-orm/sqlite-core"; + +export const blogTagRelationship = sqliteTable("blog_tag_relationship", { + id: text().primaryKey().notNull(), + blogId: text("blog_id").references(() => blog.id), + tagId: text("tag_id").references(() => blogTag.id), +}); + +export const blogTag = sqliteTable( + "blog_tag", + { + id: text().primaryKey().notNull(), + name: text().notNull(), + }, + (table) => { + return { + nameUnique: uniqueIndex("blog_tag_name_unique").on(table.name), + }; + }, +); + +export const blog = sqliteTable( + "blog", + { + id: text().primaryKey().notNull(), + title: text().notNull(), + content: text().notNull(), + author_id: text("author_id").references(() => user.id), + published_date: integer("published_date").notNull(), + last_update_date: text("last_update_date"), + time_updated: integer("time_updated").notNull(), + }, + (table) => { + return { + titleUnique: uniqueIndex("blog_title_unique").on(table.title), + }; + }, +); + +export const event = sqliteTable( + "event", + { + id: text().primaryKey().notNull(), + name: text().notNull(), + description: text(), + location: text().notNull(), + startTime: integer("start_time").notNull(), + endTime: integer("end_time").notNull(), + involvedGroups: text("involved_groups"), + timeAdded: integer("time_added").notNull(), + time_updated: integer("time_updated").notNull(), + slidesUrl: text("slides_url"), + }, + (table) => { + return { + nameUnique: uniqueIndex("event_name_unique").on(table.name), + }; + }, +); + +export const mentorMenteeRelationship = sqliteTable("mentor_mentee_relationship", { + id: text().primaryKey().notNull(), + mentorId: text("mentor_id").references(() => user.id), + menteeId: text("mentee_id").references(() => user.id), +}); + +export const session = sqliteTable("session", { + id: text().primaryKey().notNull(), + userId: text("user_id") + .notNull() + .references(() => user.id), + expiresAt: integer("expires_at").notNull(), +}); + +export const user = sqliteTable( + "user", + { + id: text().primaryKey().notNull(), + username: text().notNull(), + points: integer(), + roles: text(), + timeAdded: integer("time_added").notNull(), + time_updated: integer("time_updated").notNull(), + password: text().notNull(), + }, + (table) => { + return { + usernameUnique: uniqueIndex("user_username_unique").on(table.username), + }; + }, +); + +export const emailSubscriber = sqliteTable( + "email_subscriber", + { + id: text().primaryKey().notNull(), + email: text().notNull(), + name: text(), + subscribedAt: integer("subscribed_at").notNull(), + }, + (table) => { + return { + emailUnique: uniqueIndex("email_subscriber_email_unique").on(table.email), + }; + }, +); + +export const personalInfo = sqliteTable( + "personal_info", + { + userId: text("user_id") + .primaryKey() + .notNull() + .references(() => user.id), + firstName: text("first_name").notNull(), + lastName: text("last_name").notNull(), + email: text().notNull(), + phone: blob(), + areaCode: integer("area_code"), + }, + (table) => { + return { + phoneUnique: uniqueIndex("personal_info_phone_unique").on(table.phone), + emailUnique: uniqueIndex("personal_info_email_unique").on(table.email), + }; + }, +); + +export const professionalInfo = sqliteTable("professional_info", { + userId: text("user_id") + .primaryKey() + .notNull() + .references(() => user.id), + resumePath: text("resume_path"), + linkedin: text(), + portfolio: text(), + majors: text(), + minors: text(), + graduationSemester: text("graduation_semester"), +}); + +export const saseInfo = sqliteTable("sase_info", { + userId: text("user_id") + .primaryKey() + .notNull() + .references(() => user.id), + eventsAttended: text("events_attended"), + groups: text(), +}); diff --git a/package.json b/package.json index e3ffe78..d263899 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "type": "module", "scripts": { "dev": "vinxi dev", - "build": "concurrently \"bun run db:push\" \"bunx dotenv -- vinxi build\"", + "build": "bun run db:push && bunx dotenv -- vinxi build", "start": "vinxi start", "fix": "concurrently \"bun run lint:js:fix\" \"bun run lint:fmt:fix\"", "generate": "bun run src/client/assets/generateImageMaps.ts", diff --git a/src/server/api/blogs.ts b/src/server/api/blogs.ts index 33e38ce..f42609e 100644 --- a/src/server/api/blogs.ts +++ b/src/server/api/blogs.ts @@ -77,7 +77,6 @@ blogRoutes.post("/blogs/update", async (c) => { .set({ ...update, time_updated: new Date(), - last_update_date: new Date(), }) .where(eq(Schema.blogs.id, id)) .returning(); diff --git a/src/server/db/tables.ts b/src/server/db/tables.ts index c42161a..9482720 100644 --- a/src/server/db/tables.ts +++ b/src/server/db/tables.ts @@ -102,7 +102,6 @@ export const blogs = sqliteTable("blog", { time_updated: integer("time_updated", { mode: "timestamp" }) .notNull() .$onUpdateFn(() => new Date()), - last_update_date: text("last_update_date"), }); // Blog Tags table diff --git a/src/shared/schema/blogSchema.ts b/src/shared/schema/blogSchema.ts index 25f04db..5c86e87 100644 --- a/src/shared/schema/blogSchema.ts +++ b/src/shared/schema/blogSchema.ts @@ -9,7 +9,6 @@ export const blogSchema = z.object({ author_id: z.string().min(1, "Author ID is required."), published_date: z.string().min(0, "Published date must be a valid timestamp."), time_updated: z.string().min(0, "Update time must be a valid timestamp."), - last_update_date: z.string().min(1, "Last update date must be a valid date.").optional(), }); export const blogTitleSchema = z.object({ @@ -22,7 +21,6 @@ export const createBlogSchema = blogSchema id: true, time_updated: true, published_date: true, - last_update_date: true, }) .extend({ tags: z.array(z.string()).optional(),