From bff55596c6d26fe56c89bb8f99f9eb42ba356ef3 Mon Sep 17 00:00:00 2001 From: Baobeld Date: Sat, 4 Jan 2025 22:12:27 -0500 Subject: [PATCH] drop Posts table (#46) --- .../20250105023121_drop_posts/migration.sql | 11 ++ prisma/schema.prisma | 14 --- src/lib/server/pothos/schema/index.ts | 1 - src/lib/server/pothos/schema/posts.ts | 110 ------------------ src/lib/server/pothos/schema/users.ts | 1 - 5 files changed, 11 insertions(+), 126 deletions(-) create mode 100644 prisma/migrations/20250105023121_drop_posts/migration.sql delete mode 100644 src/lib/server/pothos/schema/posts.ts diff --git a/prisma/migrations/20250105023121_drop_posts/migration.sql b/prisma/migrations/20250105023121_drop_posts/migration.sql new file mode 100644 index 0000000..be37009 --- /dev/null +++ b/prisma/migrations/20250105023121_drop_posts/migration.sql @@ -0,0 +1,11 @@ +/* + Warnings: + + - You are about to drop the `Post` table. If the table is not empty, all the data it contains will be lost. + +*/ +-- DropForeignKey +ALTER TABLE "Post" DROP CONSTRAINT "Post_authorId_fkey"; + +-- DropTable +DROP TABLE "Post"; diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 090ea2e..3033568 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -20,20 +20,6 @@ model User { email String? name String - posts Post[] - - createdAt DateTime @default(now()) - updatedAt DateTime @default(now()) @updatedAt -} - -model Post { - id String @id @default(uuid()) - - title String - content String - published Boolean? @default(false) - author User @relation(references: [id], fields: [authorId]) - authorId String createdAt DateTime @default(now()) updatedAt DateTime @default(now()) @updatedAt diff --git a/src/lib/server/pothos/schema/index.ts b/src/lib/server/pothos/schema/index.ts index b0d89b6..2b2c5bf 100644 --- a/src/lib/server/pothos/schema/index.ts +++ b/src/lib/server/pothos/schema/index.ts @@ -13,7 +13,6 @@ builder.queryField('version', (t) => builder.mutationType({}); import './Scalars'; -import './posts'; import './users'; export const Schema = builder.toSchema(); diff --git a/src/lib/server/pothos/schema/posts.ts b/src/lib/server/pothos/schema/posts.ts deleted file mode 100644 index 420a760..0000000 --- a/src/lib/server/pothos/schema/posts.ts +++ /dev/null @@ -1,110 +0,0 @@ -import { prisma } from '$lib/server/prisma'; -import { builder } from '../builder'; - -export const Post = builder.prismaObject('Post', { - fields: (t) => ({ - id: t.exposeID('id'), - title: t.exposeString('title'), - content: t.exposeString('content'), - published: t.exposeBoolean('published'), - author: t.relation('author'), - createdAt: t.expose('createdAt', { - type: 'Date', - }), - updatedAt: t.expose('updatedAt', { - type: 'Date', - }), - }), -}); - -const CreatePost = builder.inputType('CreatePost', { - fields: (t) => ({ - title: t.string({ - required: true, - }), - content: t.string({ - required: true, - }), - published: t.boolean(), - authorId: t.id({ - required: true, - }), - }), -}); - -const UpdatePost = builder.inputType('UpdatePost', { - fields: (t) => ({ - id: t.id({ - required: true, - }), - title: t.string(), - content: t.string(), - published: t.boolean(), - authorId: t.id(), - }), -}); - -builder.queryFields((t) => ({ - posts: t.prismaField({ - type: [Post], - resolve: async () => { - return await prisma.post.findMany(); - }, - }), -})); - -builder.mutationFields((t) => ({ - createPost: t.field({ - type: Post, - args: { - input: t.arg({ required: true, type: CreatePost }), - }, - resolve: async (parent, args) => { - const author = await prisma.user.findUnique({ - where: { id: args.input.authorId }, - }); - if (!author) { - throw new Error('Author does not exist!'); - } - const post = await prisma.post.create({ - data: { - title: args.input.title, - content: args.input.content, - published: args.input.published, - author: { - connect: { - id: author.id, - }, - }, - }, - }); - return post; - }, - }), - updatePost: t.field({ - type: Post, - args: { - input: t.arg({ required: true, type: UpdatePost }), - }, - resolve: async (parent, args) => { - const post = await prisma.post.update({ - where: { - id: args.input.id, - }, - data: { - title: args.input.title ?? undefined, - content: args.input.content ?? undefined, - published: args.input.published, - ...(args.input.authorId && { - author: { - connect: { - id: args.input.authorId, - }, - }, - }), - }, - }); - return post; - }, - }), -})); diff --git a/src/lib/server/pothos/schema/users.ts b/src/lib/server/pothos/schema/users.ts index e62592b..24db8da 100644 --- a/src/lib/server/pothos/schema/users.ts +++ b/src/lib/server/pothos/schema/users.ts @@ -6,7 +6,6 @@ export const User = builder.prismaObject('User', { id: t.exposeID('id'), email: t.exposeString('email'), name: t.exposeString('name'), - posts: t.relation('posts'), createdAt: t.expose('createdAt', { type: 'Date', }),