Jazon
Jazon
Explore posts from servers
TTCTheo's Typesafe Cult
Created by Jazon on 11/14/2023 in #questions
Can't pass PrismaLibSQL client-adapter to PrismaClient
I am using the T3-stack with Prisma. I just installed the required libraries for Turso. But when I try to setup the adapter into the PrismaClient. I receive a type error on the adapter passed to the client. Its like the PrismaClient class doesn't expect a value of adapter, no?
import { PrismaClient } from "@prisma/client";
import { createClient } from "@libsql/client";
import { PrismaLibSQL } from "@prisma/adapter-libsql";

import { env } from "~/env.mjs";

const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined;
};

const libSQL = createClient({
url: `${process.env.TURSO_DATABASE_URL}`,
authToken: `${process.env.TURSO_AUTH_TOKEN}`
});

const adapter = new PrismaLibSQL(libSQL);

export const db =
globalForPrisma.prisma ??
new PrismaClient({
adapter // Type 'PrismaLibSQL' is not assignable to type 'never'.,
log:
env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
});

if (env.NODE_ENV !== "production") globalForPrisma.prisma = db;
import { PrismaClient } from "@prisma/client";
import { createClient } from "@libsql/client";
import { PrismaLibSQL } from "@prisma/adapter-libsql";

import { env } from "~/env.mjs";

const globalForPrisma = globalThis as unknown as {
prisma: PrismaClient | undefined;
};

const libSQL = createClient({
url: `${process.env.TURSO_DATABASE_URL}`,
authToken: `${process.env.TURSO_AUTH_TOKEN}`
});

const adapter = new PrismaLibSQL(libSQL);

export const db =
globalForPrisma.prisma ??
new PrismaClient({
adapter // Type 'PrismaLibSQL' is not assignable to type 'never'.,
log:
env.NODE_ENV === "development" ? ["query", "error", "warn"] : ["error"],
});

if (env.NODE_ENV !== "production") globalForPrisma.prisma = db;
2 replies
TTCTheo's Typesafe Cult
Created by Jazon on 10/14/2023 in #questions
How do I query self-relations?
I guess my schema must be wrong. But I think I've followed the Prisma docs carefully. I currently have the schema beneath. I am trying to query a user by its id, and include the following and followedBy. But it aint working. I am just receiving the 500 status (from the catch block). The schema
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
following UserFollows[] @relation("follower")
followedBy UserFollows[] @relation("following")
}

model UserFollows {
follower User @relation("follower", fields: [followerId], references: [id])
followerId String
following User @relation("following", fields: [followingId], references: [id])
followingId String

@@id([followerId, followingId])
@@index([followerId])
@@index([followingId])
}
model User {
id String @id @default(cuid())
name String?
email String? @unique
emailVerified DateTime?
image String?
accounts Account[]
sessions Session[]
following UserFollows[] @relation("follower")
followedBy UserFollows[] @relation("following")
}

model UserFollows {
follower User @relation("follower", fields: [followerId], references: [id])
followerId String
following User @relation("following", fields: [followingId], references: [id])
followingId String

@@id([followerId, followingId])
@@index([followerId])
@@index([followingId])
}
The query
try {
const data = await prisma.user.findFirst({
where: {
id: id as string
},
include: {
following: true,
}
});

return new Response(JSON.stringify(data));
} catch (error) {
console.error(error);
return new Response("Internal Server Error", { status: 500 });
}
try {
const data = await prisma.user.findFirst({
where: {
id: id as string
},
include: {
following: true,
}
});

return new Response(JSON.stringify(data));
} catch (error) {
console.error(error);
return new Response("Internal Server Error", { status: 500 });
}
12 replies
TTCTheo's Typesafe Cult
Created by Jazon on 9/13/2023 in #questions
Next Auth middleware redirects me to the login page, even if there's a session
I am busting my head around this thing. I tried to setup NextJS with NextAuth, Drizzle and PlanetScale. So I am not using the T3-scaffold. The thing is that I can see that there's a user returned from getServerSession. But when I add my middleware.ts file. It just keeps redirecting me to the login page. As if there would be no session the middleware could recognize. Any ideas to why?
4 replies
TTCTheo's Typesafe Cult
Created by Jazon on 4/23/2023 in #questions
Handle many-to-many relations in Prisma with MySQL
I am struggling with creating a schema that works in Prisma with MySQL. I've read an article at prisma.io regarding the subject, but couldn't figure out how to solve my own problem. Here's the schema:
model User {
id String @id @default(cuid())
name String
shoppingList ShoppingList[]
}

model ShoppingList {
id String @id @default(cuid())
name String
items Item[]
user User @relation(fields: [userId], references: [id])
userId String
}

model Item {
id String @id @default(cuid())
name String
productName String
category String
shoppingList ShoppingList[] @relation(fields: [shoppingId], references: [id])
shoppingId String
}
model User {
id String @id @default(cuid())
name String
shoppingList ShoppingList[]
}

model ShoppingList {
id String @id @default(cuid())
name String
items Item[]
user User @relation(fields: [userId], references: [id])
userId String
}

model Item {
id String @id @default(cuid())
name String
productName String
category String
shoppingList ShoppingList[] @relation(fields: [shoppingId], references: [id])
shoppingId String
}
Basically. Every User should be able to have several ShoppingLists. And the ShoppingLists can contain several Items.
9 replies
TTCTheo's Typesafe Cult
Created by Jazon on 4/12/2023 in #questions
Is it safe to get all data based on a route query parameter?
I have a dynamic route which renders a single Project-page. The page itself is being rendered based on the id of the route-query. Like this:
const router = useRouter();

const todoQuery = api.project.getById.useQuery(router.query.id as string);
const { data: project } = todoQuery;
const router = useRouter();

const todoQuery = api.project.getById.useQuery(router.query.id as string);
const { data: project } = todoQuery;
Inside this page I am also fetching (and mutate) all the categories that belongs to this project. I am doing this by looking at what projectId the route-query has. Like this:
const { data: categories, refetch: refetchCategories } = api.category.getAll.useQuery(
{
projectId: project?.id as string
},
)
const { data: categories, refetch: refetchCategories } = api.category.getAll.useQuery(
{
projectId: project?.id as string
},
)
I feel like this is kinda unsafe though. Wouldn't another user just be able to enter this projectId into their browser, and be able to see all the categories? Should I also check that the author is the current signed in user? I am also seeing an error that projectId is undefined at first render, but the categories is being fetched anyway.
7 replies