Skip to content

Commit

Permalink
got rid of prisma completely
Browse files Browse the repository at this point in the history
  • Loading branch information
danihengeveld committed Oct 15, 2023
1 parent 2c0571b commit efa04d4
Show file tree
Hide file tree
Showing 14 changed files with 172 additions and 230 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
"dependencies": {
"@clerk/nextjs": "^4.25.4",
"@planetscale/database": "^1.7.0",
"@prisma/client": "^5.4.2",
"@radix-ui/react-avatar": "^1.0.2",
"@radix-ui/react-dropdown-menu": "^2.0.5",
"@radix-ui/react-label": "^2.0.2",
Expand All @@ -36,7 +35,6 @@
"date-fns": "^2.30.0",
"drizzle-orm": "^0.28.6",
"formik": "^2.4.2",
"kysely": "^0.25.0",
"kysely-planetscale": "^1.3.0",
"lucide-react": "^0.244.0",
"next": "^13.5.4",
Expand Down Expand Up @@ -66,7 +64,6 @@
"postcss": "^8.4.31",
"prettier": "^2.8.8",
"prettier-plugin-tailwindcss": "^0.3.0",
"prisma": "^5.4.2",
"tailwindcss": "^3.3.0",
"typescript": "^5.2.2"
},
Expand Down
39 changes: 0 additions & 39 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 0 additions & 25 deletions prisma/schema.prisma

This file was deleted.

6 changes: 4 additions & 2 deletions src/db/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ export const shurtles = mysqlTable(

creatorId: varchar("creatorId", { length: 255 }).notNull(),

createdAt: datetime("createdAt", { mode: 'string', fsp: 3 }).default(sql`CURRENT_TIMESTAMP(3)`).notNull(),
lastHitAt: datetime("lastHitAt", { mode: 'string', fsp: 3 }),
createdAt: datetime("createdAt", { mode: "date", fsp: 3 })
.default(sql`CURRENT_TIMESTAMP(3)`)
.notNull(),
lastHitAt: datetime("lastHitAt", { mode: "date", fsp: 3 }),
},
(table) => {
return {
Expand Down
12 changes: 0 additions & 12 deletions src/lib/shurtle-kysely-types.ts

This file was deleted.

9 changes: 6 additions & 3 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { authMiddleware, redirectToSignIn } from "@clerk/nextjs";
import { Ratelimit } from "@upstash/ratelimit";
import { Redis } from "@upstash/redis";
import { eq, sql } from "drizzle-orm";
import { NextResponse } from "next/server";
import { authMiddleware, redirectToSignIn } from "@clerk/nextjs";
import { db } from "./db";
import { eq, sql } from "drizzle-orm";
import { shurtles } from "./db/schema";

// Set the paths that do not result to redirection
Expand Down Expand Up @@ -56,7 +56,10 @@ export default authMiddleware({

await db
.update(shurtles)
.set({ hits: sql`${shurtles.hits} + 1` })
.set({
hits: sql`${shurtles.hits} + 1`,
lastHitAt: sql`current_timestamp(3)`,
})
.where(eq(shurtles.slug, shurtle.slug));

return addRatelimitHeaders(
Expand Down
2 changes: 1 addition & 1 deletion src/pages/dashboard/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { api } from "~/utils/api";
const DashboardPage: NextPage = () => {
const { toast } = useToast();

const shurtles = api.shurtle.getAllForUser.useQuery(
const shurtles = api.shurtle.get.allForUser.useQuery(
{
orderBy: {
createdAt: "desc",
Expand Down
123 changes: 0 additions & 123 deletions src/server/api/routers/shurtle.ts

This file was deleted.

41 changes: 41 additions & 0 deletions src/server/api/routers/shurtle/create.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { TRPCError } from "@trpc/server";
import { z } from "zod";
import { shurtles } from "~/db/schema";
import { limitOrThrow, rateLimits } from "~/utils/ratelimiter";
import { protectedProcedure } from "../../trpc";

export const createShurtleRouter = protectedProcedure
.input(
z.object({
url: z.string().url({ message: "Must be a valid URL!" }),
slug: z
.string({ required_error: "Cannot be empty!" })
.regex(/^[a-z0-9](-?[a-z0-9])*$/, {
message: "Invalid slug!",
}),
})
)
.mutation(async ({ input, ctx }) => {
const userId = ctx.auth.userId;

await limitOrThrow(rateLimits.create, userId);

try {
await ctx.db.insert(shurtles).values({
slug: input.slug,
url: input.url,
creatorId: ctx.auth.userId,
});
} catch {
throw new TRPCError({
code: "CONFLICT",
message: `Shurtle for slug ${input.slug} already exists.`,
});
}

const shurtle = await ctx.db.query.shurtles.findFirst({
where: (shurtles, { eq }) => eq(shurtles.slug, input.slug),
});

return shurtle;
});
37 changes: 37 additions & 0 deletions src/server/api/routers/shurtle/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { TRPCError } from "@trpc/server";
import { and, eq } from "drizzle-orm";
import { z } from "zod";
import { shurtles } from "~/db/schema";
import { limitOrThrow, rateLimits } from "~/utils/ratelimiter";
import { createTRPCRouter, protectedProcedure } from "../../trpc";

export const deleteShurtleRouter = createTRPCRouter({
bySlug: protectedProcedure
.input(
z.object({
slug: z
.string({ required_error: "Cannot be empty!" })
.regex(/^[a-z0-9](-?[a-z0-9])*$/, {
message: "Invalid slug!",
}),
})
)
.mutation(async ({ input, ctx }) => {
const userId = ctx.auth.userId;

await limitOrThrow(rateLimits.private, userId);

const result = await ctx.db
.delete(shurtles)
.where(
and(eq(shurtles.slug, input.slug), eq(shurtles.creatorId, userId))
);

if (result.rowsAffected < 1) {
throw new TRPCError({
code: "NOT_FOUND",
message: `Shurtle for slug ${input.slug} and creator id ${userId} does not exist.`,
});
}
}),
});
Loading

0 comments on commit efa04d4

Please sign in to comment.