drop Posts table

This commit is contained in:
Benjamin Palko 2025-01-04 21:06:52 -05:00
parent ed2e18310e
commit 01d8b8df24
5 changed files with 11 additions and 126 deletions

View file

@ -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";

View file

@ -20,20 +20,6 @@ model User {
email String? email String?
name 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()) createdAt DateTime @default(now())
updatedAt DateTime @default(now()) @updatedAt updatedAt DateTime @default(now()) @updatedAt

View file

@ -13,7 +13,6 @@ builder.queryField('version', (t) =>
builder.mutationType({}); builder.mutationType({});
import './Scalars'; import './Scalars';
import './posts';
import './users'; import './users';
export const Schema = builder.toSchema(); export const Schema = builder.toSchema();

View file

@ -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;
},
}),
}));

View file

@ -6,7 +6,6 @@ export const User = builder.prismaObject('User', {
id: t.exposeID('id'), id: t.exposeID('id'),
email: t.exposeString('email'), email: t.exposeString('email'),
name: t.exposeString('name'), name: t.exposeString('name'),
posts: t.relation('posts'),
createdAt: t.expose('createdAt', { createdAt: t.expose('createdAt', {
type: 'Date', type: 'Date',
}), }),