Skip to content

Commit

Permalink
feat: modules db schema & actions
Browse files Browse the repository at this point in the history
  • Loading branch information
ixahmedxi committed Jul 26, 2024
1 parent c8dba6c commit 9fb1937
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/db/actions/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import { and, desc, eq } from 'drizzle-orm';
import { createInsertSchema } from 'drizzle-zod';
import { db } from '..';
import { modulesTable } from '../schema';
import type { z } from 'zod';

export const getModuleById = (id: string, userId: string) =>
db.query.modulesTable.findFirst({
where: and(eq(modulesTable.id, id), eq(modulesTable.user_id, userId)),
});

export const getModulesByUserId = (userId: string) =>
db
.select()
.from(modulesTable)
.where(eq(modulesTable.user_id, userId))
.orderBy(desc(modulesTable.lastVisited));

const insertModuleSchema = createInsertSchema(modulesTable).omit({
id: true,
createdAt: true,
modifiedAt: true,
lastVisited: true,
});

export const createModule = (data: z.infer<typeof insertModuleSchema>) =>
db.insert(modulesTable).values({
...data,
color: data.color ?? 'default',
icon: data.icon ?? 'default',
archived: data.archived ?? false,
credits: data.credits ?? 0,
});

export const archiveModule = (id: string, userId: string) =>
db
.update(modulesTable)
.set({ archived: true })
.where(and(eq(modulesTable.id, id), eq(modulesTable.user_id, userId)));

export const recoverModule = (id: string, userId: string) =>
db
.update(modulesTable)
.set({ archived: false })
.where(and(eq(modulesTable.id, id), eq(modulesTable.user_id, userId)));

export const updateModule = async (
id: string,
userId: string,
data: z.infer<typeof insertModuleSchema>,
) => {
const existingModule = await getModuleById(id, userId);

if (!existingModule) {
throw new Error('Module not found');
}

return db
.update(modulesTable)
.set({
...data,
color: data.color ?? existingModule.color,
icon: data.icon ?? existingModule.icon,
archived: data.archived ?? existingModule.archived,
credits: data.credits ?? existingModule.credits,
})
.where(and(eq(modulesTable.id, id), eq(modulesTable.user_id, userId)));
};

export const deleteModule = (id: string, userId: string) =>
db
.delete(modulesTable)
.where(and(eq(modulesTable.id, id), eq(modulesTable.user_id, userId)));

export const updateLastVisited = (id: string, userId: string) =>
db
.update(modulesTable)
.set({ lastVisited: new Date() })
.where(and(eq(modulesTable.id, id), eq(modulesTable.user_id, userId)));
1 change: 1 addition & 0 deletions src/db/schema/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './early-access';
export * from './modules';
23 changes: 23 additions & 0 deletions src/db/schema/modules.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
boolean,
integer,
pgTable,
text,
timestamp,
uuid,
} from 'drizzle-orm/pg-core';

export const modulesTable = pgTable('modules', {
id: uuid('id').primaryKey().unique().defaultRandom().notNull(),
user_id: text('user_id').notNull(),
name: text('name').notNull(),
description: text('description').notNull(),
code: text('code').notNull(),
icon: text('icon').default('default').notNull(),
color: text('color').default('default').notNull(),
archived: boolean('archived').default(false).notNull(),
credits: integer('credits').default(0).notNull(),
createdAt: timestamp('created_at').notNull().defaultNow(),
modifiedAt: timestamp('modified_at').notNull().defaultNow(),
lastVisited: timestamp('last_visited').notNull().defaultNow(),
});

0 comments on commit 9fb1937

Please sign in to comment.