-
Pre Intro
Q: How to add the UserId to a "create" mutationPrerequisitesI want to create an application, which allows the user to create Collections of Cards. E.g. Collection "101 German" with many cards.
Those collections can be viewed by other users and be "completed" (I have learned that question and I know the answer, so the next time I open this collection, this card will be hidden) // app/collections/components/CollectionForm.tsx
export function CollectionForm<S extends z.ZodType<any, any>>(props: FormProps<S>) {
return (
<Form<S> {...props}>
<LabeledTextField type="text" name="name" label="Name" placeholder="Name" />
<LabeledTextField type="text" name="slug" label="Slug" placeholder="Slug" />
<LabeledTextField type="text" name="category" label="Category" placeholder="Category" />
<LabeledTextField type="text" name="about" label="About" placeholder="About" />
</Form>
)
}// app/collections/mutations/createCollection.ts
import { resolver } from "blitz"
import db from "db"
import * as z from "zod"
const CreateCollection = z
.object({
name: z.string(),
slug: z.string(),
category: z.string(),
about: z.string(),
})
.nonstrict()
export default resolver.pipe(
resolver.zod(CreateCollection),
resolver.authorize(),
async (input) => {
// TODO: in multi-tenant app, you must add validation to ensure correct tenant
const collection = await db.collection.create({ data: input })
return collection
}
)BTW I get this ts error Type 'Flatten<{ name: string; about: string; slug: string; category: string; } & { [k: string]: any; }>' is not assignable to type '(Without<CollectionUncheckedCreateInput, CollectionCreateInput> & CollectionCreateInput) | (Without<...> & CollectionUncheckedCreateInput)'.
Type 'Flatten<{ name: string; about: string; slug: string; category: string; } & { [k: string]: any; }>' is not assignable to type 'Without<CollectionCreateInput, CollectionUncheckedCreateInput> & CollectionUncheckedCreateInput'.
Property 'userId' is missing in type 'Flatten<{ name: string; about: string; slug: string; category: string; } & { [k: string]: any; }>' but required in type 'CollectionUncheckedCreateInput'.// prisma.schema
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
provider = "postgres"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
// --------------------------------------
model User {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String?
email String @unique
hashedPassword String?
role String @default("USER")
tokens Token[]
sessions Session[]
Collection Collection[]
Card Card[]
}
model Session {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
expiresAt DateTime?
handle String @unique
hashedSessionToken String?
antiCSRFToken String?
publicData String?
privateData String?
user User? @relation(fields: [userId], references: [id])
userId String?
}
model Token {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
hashedToken String
type String
// See note below about TokenType enum
//type TokenType
expiresAt DateTime
sentTo String
user User @relation(fields: [userId], references: [id])
userId String
@@unique([hashedToken, type])
}
// NOTE: It's highly recommended to use an enum for the token type
// but enums only work in Postgres.
// See: https://blitzjs.com/docs/database-overview#switch-to-postgresql
enum TokenType {
RESET_PASSWORD
}
model Collection {
id String @id @default(cuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String
name String
slug String
category String
about String
Card Card[]
}
model Card {
id Int @id @default(autoincrement())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id])
userId String
collection Collection @relation(fields: [collectionId], references: [id])
collectionId String
question String
answser String
about String
completed Boolean @default(false)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
Here you go! export default resolver.pipe(
resolver.zod(CreateCollection),
resolver.authorize(),
async (input, ctx) => {
// TODO: in multi-tenant app, you must add validation to ensure correct tenant
const collection = await db.collection.create({ data: {...input, userId: ctx.session.userId} })
return collection
}
)
I think this schema will support this. Do a query like |
Beta Was this translation helpful? Give feedback.
Here you go!
I think this schema will support this. Do a query like
db.card.count({where: {userId: ###, completed: true}})