rykuno
rykuno
Explore posts from servers
TTCTheo's Typesafe Cult
Created by rykuno on 5/7/2023 in #questions
Next-Auth Session in Server Function
Trying out the new app directory with an example project and ran into issues with accessing the user session within a server function via getServerSession
"use server";

import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import prisma from "@/db";
import { EventPrivacy } from "@prisma/client";
import { getServerSession } from "next-auth";
import { redirect } from "next/navigation";

export async function createEvent(formData: FormData) {
const session = await getServerSession(authOptions);
if (!session) return;

const event = await prisma.event.create({
data: {
image: formData.get("imageUrl") as string,
name: formData.get("name") as string,
description: formData.get("description") as string,
startDate: new Date(formData.get("startDate") as string),
endDate: new Date(formData.get("endDate") as string),
privacy: formData.get("privacy") as EventPrivacy,
createdById: session.user.id
}
});
redirect(`/event/${event.id}`);
}
"use server";

import { authOptions } from "@/app/api/auth/[...nextauth]/route";
import prisma from "@/db";
import { EventPrivacy } from "@prisma/client";
import { getServerSession } from "next-auth";
import { redirect } from "next/navigation";

export async function createEvent(formData: FormData) {
const session = await getServerSession(authOptions);
if (!session) return;

const event = await prisma.event.create({
data: {
image: formData.get("imageUrl") as string,
name: formData.get("name") as string,
description: formData.get("description") as string,
startDate: new Date(formData.get("startDate") as string),
endDate: new Date(formData.get("endDate") as string),
privacy: formData.get("privacy") as EventPrivacy,
createdById: session.user.id
}
});
redirect(`/event/${event.id}`);
}
Calling this code from an action handler within a form yields the following error Method expects to have requestAsyncStorage, none available. Is there anyone who has successfully gotten this to work?
29 replies
TTCTheo's Typesafe Cult
Created by rykuno on 1/25/2023 in #questions
Prisma: Asyncronous Extended Fields
Anyone familiar with any better way to accomplish the equivalent of field resolvers with prisma? The Ask I need an async field returned with each user The Scenario Say I want to findMany on users but I want a likeCount and followCount fields returned with each result. The expand api looks to be on the right track but it looks like its unable to perform asynchronous computes for additional fields. Such as if I wanted to add an async field called likeCount to user which would be returned on each user result.
const xprisma = prisma
.$extends({
result: {
user: {
likeCount: {
needs: { id: true },
compute({id}) {
return prisma.likes.count({where: userId: id})
},
},
},
},
})
const xprisma = prisma
.$extends({
result: {
user: {
likeCount: {
needs: { id: true },
compute({id}) {
return prisma.likes.count({where: userId: id})
},
},
},
},
})
I know I can just _count likes on user for this example. This is simplified for an example
1 replies
TTCTheo's Typesafe Cult
Created by rykuno on 1/17/2023 in #questions
Prisma `clientExtensions` Preview Feature Breaks Types
Enabling the clientExtensions preview feature in prisma.schema causes trpc routers to break all typesatefy. Anyone mess with this yet?
4 replies