dumb refactor but sunk cost is to great keep pushing
This commit is contained in:
@@ -33,16 +33,16 @@ export const cvEntry = createTable(
|
||||
(d) => ({
|
||||
id: d.uuid().primaryKey().notNull(),
|
||||
categoryId: d.uuid('category_id'),
|
||||
fromTime: d.date().notNull(),
|
||||
toTime: d.date().notNull(),
|
||||
fromTime: d.timestamp().notNull().$type<Date>(),
|
||||
toTime: d.timestamp().notNull().$type<Date>(),
|
||||
title: d.varchar({length:50}).notNull(),
|
||||
description: d.text(),
|
||||
hideDates: d.boolean(),
|
||||
createdAt: d
|
||||
.timestamp({ withTimezone: true })
|
||||
.default(sql`CURRENT_TIMESTAMP`)
|
||||
.notNull(),
|
||||
updatedAt: d.timestamp({ withTimezone: true }).$onUpdate(() => new Date()),
|
||||
.notNull().$type<Date>(),
|
||||
updatedAt: d.timestamp({ withTimezone: true }).$onUpdate(() => new Date()).$type<Date>(),
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
@@ -6,16 +6,13 @@ import { isAdmin } from "~/app/actions";
|
||||
import { TRPCError } from "@trpc/server";
|
||||
import { entitySchemas, type Schema, type SchemaKeys } from "~/lib/utils";
|
||||
import { eq, and } from "drizzle-orm";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
import z from "zod";
|
||||
|
||||
function isKeyOf<T extends object>(k: any, obj: T): k is keyof T {
|
||||
return k in obj
|
||||
}
|
||||
|
||||
// this sucks should have never refactored to it
|
||||
export function trpcCrudRouterFromDrizzleEntity<T extends SchemaKeys<Schema>>(table: T) {
|
||||
const schemas = entitySchemas<T>(table)
|
||||
if (!isKeyOf(table, schema)) {
|
||||
@@ -46,19 +43,14 @@ export function trpcCrudRouterFromDrizzleEntity<T extends SchemaKeys<Schema>>(ta
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
console.log(input)
|
||||
if (input === undefined) {
|
||||
throw new TRPCError({ message: "no update input", code: "BAD_REQUEST" })
|
||||
}
|
||||
const conds = (Object.entries(input) as Entries<typeof input>).map(([k, v]) => {
|
||||
if (!isKeyOf(k, schema[table])) {
|
||||
throw new TRPCError({ message: "Invalid key for column", code: "BAD_REQUEST" });
|
||||
}
|
||||
return eq(schema[table][k], v)
|
||||
})
|
||||
if (conds.length > 0) {
|
||||
return await db.update(schema[table]).set(input).where(and(...conds)).returning().execute();
|
||||
if (!isKeyOf('id',input) || !isKeyOf('id',schema[table])) {
|
||||
throw new TRPCError({ message: "no id provided", code: "BAD_REQUEST" })
|
||||
}
|
||||
throw new TRPCError({ message: "trying to update all entities", code: "BAD_REQUEST" })
|
||||
return await db.update(schema[table]).set(input).where(eq(schema[table]['id'],input['id'])).returning().execute();
|
||||
}),
|
||||
insert: publicProcedure.input(schemas.insert).mutation(async (opts) => {
|
||||
let admin = await isAdmin();
|
||||
@@ -86,7 +78,7 @@ export function trpcCrudRouterFromDrizzleEntity<T extends SchemaKeys<Schema>>(ta
|
||||
return eq(schema[table][k], v)
|
||||
})
|
||||
if (conds.length > 0) {
|
||||
return await db.update(schema[table]).where(and(...conds)).returning().execute();
|
||||
return await db.delete(schema[table]).where(and(...conds)).returning().execute();
|
||||
}
|
||||
throw new TRPCError({ message: "trying to delete all entities", code: "BAD_REQUEST" })
|
||||
}),
|
||||
|
||||
@@ -1,12 +1,19 @@
|
||||
import type { inferRouterOutputs } from "@trpc/server";
|
||||
import { router } from "../trpc";
|
||||
import { CvRouter } from "./cv";
|
||||
import type { inferReactQueryProcedureOptions } from "@trpc/react-query";
|
||||
import { ProjectRouter } from "./project";
|
||||
import { trpcCrudRouterFromDrizzleEntity } from "../lib";
|
||||
import type { inferRouterMeta } from "@trpc/server/unstable-core-do-not-import";
|
||||
|
||||
const { router : project } = trpcCrudRouterFromDrizzleEntity('project')
|
||||
const { router : techStack } = trpcCrudRouterFromDrizzleEntity('techStack')
|
||||
const { router : category } = trpcCrudRouterFromDrizzleEntity('cvCategory')
|
||||
const { router : entry } = trpcCrudRouterFromDrizzleEntity('cvEntry')
|
||||
const root = {}
|
||||
export const trpcRouter = router({
|
||||
cv: CvRouter,
|
||||
project: ProjectRouter
|
||||
project: project,
|
||||
techStack: techStack,
|
||||
category: category,
|
||||
entry: entry
|
||||
})
|
||||
|
||||
export type TrpcRouter = typeof trpcRouter
|
||||
|
||||
@@ -1,75 +0,0 @@
|
||||
import { db } from "~/server/db";
|
||||
import { publicProcedure, router } from "~/server/trpc";
|
||||
import { cvCategory, cvEntry } from "~/server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { isAdmin } from "~/app/actions";
|
||||
|
||||
import * as Schemas from "~/lib/schema/cv/category"
|
||||
import { TRPCError, type inferRouterOutputs } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
|
||||
export const CategoryRouter = router({
|
||||
list: publicProcedure.query(async () => {
|
||||
const categories = await db.query.cvCategory.findMany({
|
||||
orderBy: (model, { desc }) => desc(model.name),
|
||||
with: {
|
||||
cvEntry: true
|
||||
}
|
||||
});
|
||||
return categories;
|
||||
}),
|
||||
get: publicProcedure.input(Schemas.getSchema).query(async (opts) => {
|
||||
const { input } = opts
|
||||
const categories = await db.query.cvCategory.findMany({
|
||||
with: {
|
||||
cvEntry: true
|
||||
},
|
||||
where: eq(cvCategory.id, input.id),
|
||||
limit: 1
|
||||
})
|
||||
if (categories[0] !== undefined) {
|
||||
return categories[0]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
delete: publicProcedure.input(z.string()).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
db.delete(cvCategory).where(eq(cvCategory.id,input)).execute()
|
||||
}),
|
||||
create: publicProcedure.input(Schemas.insertSchema).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
const category = await db.insert(cvCategory).values(input).returning().execute()
|
||||
return category
|
||||
}),
|
||||
update: publicProcedure
|
||||
.input(Schemas.updateRouteSchema)
|
||||
.mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
const category = await db.update(cvCategory)
|
||||
.set(input.update)
|
||||
.returning()
|
||||
.where(eq(cvCategory.id, input.by.id))
|
||||
return category
|
||||
})
|
||||
})
|
||||
|
||||
export type CategoryRouterOutputs = inferRouterOutputs<typeof CategoryRouter>
|
||||
@@ -1,74 +0,0 @@
|
||||
import { db } from "~/server/db";
|
||||
import { publicProcedure, router } from "~/server/trpc";
|
||||
import { cvEntry } from "~/server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { isAdmin } from "~/app/actions";
|
||||
|
||||
import * as Schemas from "~/lib/schema/cv/entry"
|
||||
import { TRPCError, type inferRouterOutputs } from "@trpc/server";
|
||||
import { z } from "zod";
|
||||
|
||||
export const EntryRouter = router({
|
||||
list: publicProcedure.query(async () => {
|
||||
const entries = await db.query.cvEntry.findMany({
|
||||
orderBy: (model, { desc }) => desc(model.toTime),
|
||||
with: {
|
||||
category: true
|
||||
}
|
||||
});
|
||||
return entries;
|
||||
}),
|
||||
get: publicProcedure.input(Schemas.getSchema).query(async (opts) => {
|
||||
const { input } = opts
|
||||
const entries = await db.query.cvEntry.findMany({
|
||||
with: {
|
||||
category: true
|
||||
},
|
||||
where: eq(cvEntry.id, input.id),
|
||||
limit: 1
|
||||
})
|
||||
if (entries[0] !== undefined) {
|
||||
return entries[0]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
delete: publicProcedure.input(z.string()).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
db.delete(cvEntry).where(eq(cvEntry.id,input)).execute()
|
||||
}),
|
||||
create: publicProcedure.input(Schemas.insertSchema).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.insert(cvEntry).values(input).returning().execute()
|
||||
}),
|
||||
update: publicProcedure
|
||||
.input(Schemas.updateRouteSchema)
|
||||
.mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
const entry = await db.update(cvEntry)
|
||||
.set(input.update)
|
||||
.returning()
|
||||
.where(eq(cvEntry.id, input.by.id))
|
||||
return entry
|
||||
})
|
||||
})
|
||||
|
||||
export type EntryRouterOutputs = inferRouterOutputs<typeof EntryRouter>
|
||||
@@ -1,12 +0,0 @@
|
||||
import { router } from "~/server/trpc"
|
||||
import { CategoryRouter } from "./category"
|
||||
import { EntryRouter } from "./entry"
|
||||
import { trpcCrudRouterFromDrizzleEntity } from "~/server/lib"
|
||||
import * as schema from '~/server/db/schema'
|
||||
|
||||
const { router : categoryv2 } = trpcCrudRouterFromDrizzleEntity('cvCategory')
|
||||
const { router : entryv2 } = trpcCrudRouterFromDrizzleEntity('cvEntry')
|
||||
export const CvRouter = router({
|
||||
category: categoryv2,
|
||||
entry:entryv2
|
||||
})
|
||||
@@ -1,75 +0,0 @@
|
||||
import { publicProcedure, router } from "~/server/trpc"
|
||||
import { db } from "~/server/db";
|
||||
import * as Schemas from '~/lib/schema/project/project'
|
||||
import { eq } from "drizzle-orm";
|
||||
import { project } from "~/server/db/schema";
|
||||
import { isAdmin } from "~/app/actions";
|
||||
import z from "zod";
|
||||
import { StackRouter } from "./techStack";
|
||||
import { TRPCError, type inferRouterOutputs} from "@trpc/server";
|
||||
import { trpcCrudRouterFromDrizzleEntity } from "~/server/lib";
|
||||
const { router : project } = trpcCrudRouterFromDrizzleEntity('project')
|
||||
const { router : techStack } = trpcCrudRouterFromDrizzleEntity('techStack')
|
||||
export const ProjectRouter = router({
|
||||
list: publicProcedure.query(async () => {
|
||||
return await db.query.project.findMany({
|
||||
orderBy: (model, {desc}) => desc(model.title),
|
||||
with: {
|
||||
techStack: true
|
||||
}
|
||||
});
|
||||
}),
|
||||
get: publicProcedure.input(Schemas.getSchema).query(async (opts) => {
|
||||
const {input} = opts;
|
||||
const projects = await db.query.project.findMany({
|
||||
with: {
|
||||
techStack: true
|
||||
},
|
||||
where: eq(project.id,input.id),
|
||||
limit: 1
|
||||
});
|
||||
if (projects[0] !== undefined) {
|
||||
return projects[0]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
delete: publicProcedure.input(z.string()).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
db.delete(project).where(eq(project.id,input)).execute()
|
||||
}),
|
||||
create: publicProcedure.input(Schemas.insertSchema).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.insert(project).values(input).returning().execute();
|
||||
}),
|
||||
update: publicProcedure
|
||||
.input(Schemas.updateRouteSchema)
|
||||
.mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.update(project)
|
||||
.set(input.update)
|
||||
.returning()
|
||||
.where(eq(project.id,input.by.id));
|
||||
}),
|
||||
stack: StackRouter,
|
||||
})
|
||||
|
||||
export type ProjectRouterOutputs = inferRouterOutputs<typeof ProjectRouter>
|
||||
@@ -1,62 +0,0 @@
|
||||
import { publicProcedure, router } from "~/server/trpc"
|
||||
import { db } from "~/server/db";
|
||||
import * as Schemas from '~/lib/schema/project/techStack'
|
||||
import { eq } from "drizzle-orm";
|
||||
import { techStack } from "~/server/db/schema";
|
||||
import { isAdmin } from "~/app/actions";
|
||||
import { TRPCError, type inferRouterOutputs} from "@trpc/server";
|
||||
import z from "zod";
|
||||
export const StackRouter = router({
|
||||
list: publicProcedure.query(async () => {
|
||||
return await db.query.techStack.findMany();
|
||||
}),
|
||||
get: publicProcedure.input(Schemas.getSchema).query(async (opts) => {
|
||||
const {input} = opts;
|
||||
const techStacks = await db.query.techStack.findMany({
|
||||
where: eq(techStack.id,input.id),
|
||||
limit: 1
|
||||
});
|
||||
if (techStacks[0] !== undefined) {
|
||||
return techStacks[0]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
delete: publicProcedure.input(z.string()).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
db.delete(techStack).where(eq(techStack.id,input)).execute()
|
||||
}),
|
||||
create: publicProcedure.input(Schemas.insertSchema).mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.insert(techStack).values(input).returning().execute();
|
||||
}),
|
||||
update: publicProcedure
|
||||
.input(Schemas.updateRouteSchema)
|
||||
.mutation(async (opts) => {
|
||||
let admin = await isAdmin()
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.update(techStack)
|
||||
.set(input.update)
|
||||
.returning()
|
||||
.where(eq(techStack.id,input.by.id))
|
||||
})
|
||||
})
|
||||
|
||||
export type StackRouterOutputs = inferRouterOutputs<typeof StackRouter>
|
||||
Reference in New Issue
Block a user