before remove routers
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
// https://orm.drizzle.team/docs/sql-schema-declaration
|
||||
|
||||
import { relations, sql } from "drizzle-orm";
|
||||
import { index, pgEnum, pgTableCreator } from "drizzle-orm/pg-core";
|
||||
import { index, pgEnum, pgSchema, pgTableCreator } from "drizzle-orm/pg-core";
|
||||
|
||||
/**
|
||||
* This is an example of how to use the multi-project schema feature of Drizzle ORM. Use the same
|
||||
@@ -63,7 +63,9 @@ export const project = createTable(
|
||||
id: d.uuid().primaryKey().notNull(),
|
||||
title: d.varchar({length: 50}).notNull(),
|
||||
sourceType: sourceTypeEnum(),
|
||||
sourceLink: d.varchar({length: 200}),
|
||||
releaseStatus: releaseStatus(),
|
||||
releaseLink: d.varchar({length: 200}),
|
||||
stackId: d.uuid(),
|
||||
})
|
||||
)
|
||||
|
||||
136
src/server/lib.ts
Normal file
136
src/server/lib.ts
Normal file
@@ -0,0 +1,136 @@
|
||||
import type { Entries } from "type-fest";
|
||||
import { publicProcedure, router } from "~/server/trpc";
|
||||
import { db } from "~/server/db";
|
||||
import * as schema from "~/server/db/schema";
|
||||
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";
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
function isKeyOf<T extends object>(k: any, obj: T): k is keyof T {
|
||||
return k in obj
|
||||
}
|
||||
|
||||
export function trpcCrudRouterFromDrizzleEntity<T extends SchemaKeys<Schema>>(table: T) {
|
||||
const schemas = entitySchemas<T>(table)
|
||||
if (!isKeyOf(table, schema)) {
|
||||
throw new Error('table not found')
|
||||
}
|
||||
let r = router({
|
||||
select: publicProcedure.input(schemas.update).query(async (opts) => {
|
||||
const { input } = opts;
|
||||
if (input === undefined) {
|
||||
return await db.select().from(schema[table]).execute();
|
||||
}
|
||||
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.select().from(schema[table]).where(and(...conds)).execute();
|
||||
}
|
||||
return await db.select().from(schema[table]).execute();
|
||||
}),
|
||||
update: publicProcedure.input(schemas.update).mutation(async (opts) => {
|
||||
let admin = await isAdmin();
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
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();
|
||||
}
|
||||
throw new TRPCError({ message: "trying to update all entities", code: "BAD_REQUEST" })
|
||||
}),
|
||||
insert: publicProcedure.input(schemas.insert).mutation(async (opts) => {
|
||||
let admin = await isAdmin();
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
return await db.insert(schema[table]).values(input).returning().execute();
|
||||
}),
|
||||
delete: publicProcedure.input(schemas.update).mutation(async (opts) => {
|
||||
let admin = await isAdmin();
|
||||
if (!admin) {
|
||||
throw new TRPCError(
|
||||
{ message: "Access denied", code: "FORBIDDEN", }
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
const inputEntrie = Object.entries(input) as Entries<typeof input>;
|
||||
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]).where(and(...conds)).returning().execute();
|
||||
}
|
||||
throw new TRPCError({ message: "trying to delete all entities", code: "BAD_REQUEST" })
|
||||
}),
|
||||
// list: publicProcedure.input(z.optional(z.object({
|
||||
// orderBy: selectSchema.keyof(),
|
||||
// direction: z.enum(["asc","desc"])
|
||||
// }))).query(async (opts) => {
|
||||
// const { input } = opts;
|
||||
// const query = db.select().from(schema[table])
|
||||
// if (input !== undefined) {
|
||||
// switch (input.direction) {
|
||||
// case "asc" :
|
||||
// query.orderBy(asc(schema[table]['_']['columns'][input.orderBy]));
|
||||
// case "desc" :
|
||||
// query.orderBy(desc(schema[table]['_']['columns'][input.orderBy]));
|
||||
// }
|
||||
// }
|
||||
// return await query.execute()
|
||||
// }),
|
||||
// get: publicProcedure.input(z.optional(insertSchema)).query(async (opts) => {
|
||||
// const { input } = opts
|
||||
// return await db.select().from(schema[table]).where(eq(schema[table]['_']['columns']['id'], input)).limit(1)
|
||||
// }),
|
||||
// delete: publicProcedure.input(z.string()).query(async (opts) => {
|
||||
// const { input } = opts
|
||||
// let admin = await isAdmin();
|
||||
// if (!admin) {
|
||||
// throw new TRPCError(
|
||||
// { message: "Access denied", code: "FORBIDDEN", }
|
||||
// )
|
||||
// }
|
||||
// return await db.delete(schema[table]).where(eq(schema[table]['_']['columns']['id'], input)).returning()
|
||||
// }),
|
||||
// create: publicProcedure.input(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(schema[table]).values(input).returning().execute();
|
||||
// })
|
||||
});
|
||||
return { router: r };
|
||||
}
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
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: CategoryRouter,
|
||||
entry: EntryRouter
|
||||
category: categoryv2,
|
||||
entry:entryv2
|
||||
})
|
||||
|
||||
@@ -7,6 +7,9 @@ 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({
|
||||
@@ -61,15 +64,10 @@ export const ProjectRouter = router({
|
||||
)
|
||||
}
|
||||
const { input } = opts;
|
||||
const updateProj = await db.update(project)
|
||||
return await db.update(project)
|
||||
.set(input.update)
|
||||
.returning()
|
||||
.where(eq(project.id,input.by.id));
|
||||
if (updateProj[0] !== undefined) {
|
||||
return updateProj[0]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
stack: StackRouter,
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user