backup
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, pgTableCreator } from "drizzle-orm/pg-core";
|
||||
import { index, pgEnum, 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
|
||||
@@ -12,11 +12,14 @@ import { index, pgTableCreator } from "drizzle-orm/pg-core";
|
||||
*/
|
||||
export const createTable = pgTableCreator((name) => `gregorlohaus.com_${name}`);
|
||||
|
||||
export const layoutPositionEnum = pgEnum('layout_position',['sidebar','header','col1','col2'])
|
||||
|
||||
export const cvCategory = createTable(
|
||||
"cv_category",
|
||||
(d) => ({
|
||||
id: d.uuid().primaryKey(),
|
||||
name: d.varchar({length: 50})
|
||||
name: d.varchar({length: 50}),
|
||||
layoutPosition: layoutPositionEnum()
|
||||
}),
|
||||
(t) => [index("name_idx").on(t.name)],
|
||||
)
|
||||
@@ -32,6 +35,8 @@ export const cvEntry = createTable(
|
||||
categoryId: d.uuid('category_id'),
|
||||
fromTime: d.date().notNull(),
|
||||
toTime: d.date().notNull(),
|
||||
title: d.varchar({length:50}).notNull(),
|
||||
description: d.text(),
|
||||
createdAt: d
|
||||
.timestamp({ withTimezone: true })
|
||||
.default(sql`CURRENT_TIMESTAMP`)
|
||||
@@ -46,3 +51,24 @@ export const cvEntryRelations = relations(cvEntry, ({one}) => ({
|
||||
references: [cvCategory.id]
|
||||
}),
|
||||
}));
|
||||
|
||||
export const sourceTypeEnum = pgEnum('source_type',['open','closed'])
|
||||
export const stackItemEnum = pgEnum('stack_item',['drizzle','postgres','nextjs','react','servercomponents','php','laravel','reactnative','expo','mysql','nginx','protobuf','grpc'])
|
||||
|
||||
export const project = createTable(
|
||||
"project",
|
||||
(d) => ({
|
||||
id: d.uuid().primaryKey().notNull(),
|
||||
title: d.varchar({length: 50}).notNull(),
|
||||
sourceType: sourceTypeEnum(),
|
||||
|
||||
})
|
||||
)
|
||||
|
||||
export const techStack = createTable(
|
||||
"tech_stack",
|
||||
(d) => ({
|
||||
id: d.uuid().primaryKey().notNull(),
|
||||
stackItems: stackItemEnum().array()
|
||||
})
|
||||
)
|
||||
|
||||
@@ -1,8 +1,12 @@
|
||||
import type { inferRouterOutputs } from "@trpc/server";
|
||||
import { router } from "../trpc";
|
||||
import { CvRouter } from "./cv";
|
||||
import type { inferReactQueryProcedureOptions } from "@trpc/react-query";
|
||||
|
||||
export const trpcRouter = router({
|
||||
cv: CvRouter
|
||||
})
|
||||
|
||||
export type TrpcRouter = typeof trpcRouter
|
||||
export type RouterOutputs = inferRouterOutputs<TrpcRouter>
|
||||
export type ReactQueryOptions = inferReactQueryProcedureOptions<TrpcRouter>
|
||||
|
||||
@@ -1,45 +1,74 @@
|
||||
import { db } from "~/server/db";
|
||||
import { publicProcedure, router } from "~/server/trpc";
|
||||
import { cvCategory } from "~/server/db/schema";
|
||||
import { createInsertSchema, createUpdateSchema, createSelectSchema} from 'drizzle-zod'
|
||||
import { z } from 'zod'
|
||||
import { cvCategory, cvEntry } from "~/server/db/schema";
|
||||
import { eq } from "drizzle-orm";
|
||||
import { isAdmin } from "~/app/actions";
|
||||
|
||||
const selectShema = createSelectSchema(cvCategory)
|
||||
const insertShema = createInsertSchema(cvCategory)
|
||||
const updateSchema = createUpdateSchema(cvCategory)
|
||||
import * as Schemas from "~/lib/schema/cv/category"
|
||||
import { TRPCError } 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)
|
||||
orderBy: (model, { desc }) => desc(model.name),
|
||||
with: {
|
||||
cvEntry: true
|
||||
}
|
||||
});
|
||||
return categories;
|
||||
}),
|
||||
get: publicProcedure.input(selectShema.pick({id: true})).query(async (opts) => {
|
||||
get: publicProcedure.input(Schemas.getSchema).query(async (opts) => {
|
||||
const { input } = opts
|
||||
const category = await db.query.cvCategory.findFirst({
|
||||
where: eq(cvCategory.id,input.id)
|
||||
const categories = await db.query.cvCategory.findMany({
|
||||
with: {
|
||||
cvEntry: true
|
||||
},
|
||||
where: eq(cvCategory.id, input.id),
|
||||
limit: 1
|
||||
})
|
||||
return category;
|
||||
if (categories[0] !== undefined) {
|
||||
return categories[0]
|
||||
} else {
|
||||
return null
|
||||
}
|
||||
}),
|
||||
create: publicProcedure.input(insertShema).mutation(async (opts) => {
|
||||
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(z.object({
|
||||
by: selectShema.pick({id:true}),
|
||||
update: updateSchema
|
||||
}))
|
||||
.input(Schemas.updateRouteSchema)
|
||||
.mutation(async (opts) => {
|
||||
const {input} = opts;
|
||||
const category = await db.update(cvCategory)
|
||||
.set(input.update)
|
||||
.returning()
|
||||
.where(eq(cvCategory.id,input.by.id))
|
||||
return category
|
||||
})
|
||||
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
|
||||
})
|
||||
})
|
||||
|
||||
|
||||
74
src/server/routers/cv/entry.ts
Normal file
74
src/server/routers/cv/entry.ts
Normal file
@@ -0,0 +1,74 @@
|
||||
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 } 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;
|
||||
const entry = await db.insert(cvEntry).values(input).returning().execute()
|
||||
return entry
|
||||
}),
|
||||
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
|
||||
})
|
||||
})
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { router } from "~/server/trpc"
|
||||
import { CategoryRouter } from "./category"
|
||||
import { EntryRouter } from "./entry"
|
||||
|
||||
export const CvRouter = router({
|
||||
category: CategoryRouter
|
||||
category: CategoryRouter,
|
||||
entry: EntryRouter
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user