Types issues
Hey Kapa, for a "count", how will be correct?
GetPaginatedUsers
operations.ts
type GetPaginatedUsersOutput = {
users: (Pick<
User,
'id' | 'email' | 'username' | 'subscriptionStatus' | 'paymentProcessorUserId' | 'isAdmin' | 'createdAt' | 'hasCompletedOnboarding' | 'firstSeenData'
& {_count: { projects: number; documents: number; chats: number; }; messageCount: number; })[]; totalPages: number; }; // Calculate total message count for each user const usersWithMessageCounts = pageOfUsers.map((user) => { const messageCount = user.chats.reduce((total: number, chat: { _count: { messages: number } }) => total + chat._count.messages, 0); return { id: user.id, email: user.email, username: user.username, isAdmin: user.isAdmin, subscriptionStatus: user.subscriptionStatus, paymentProcessorUserId: user.paymentProcessorUserId, createdAt: user.createdAt, hasCompletedOnboarding: user.hasCompletedOnboarding, firstSeenData: user.firstSeenData, _count: user._count, messageCount, }; }); main.wasp query getPaginatedUsers { fn: import { getPaginatedUsers } from "@src/user/operations", entities: [User, Message, Chat, Document, Project] } [ Wasp ] src/user/operations.ts(150,31): error TS2339: Property 'chats' does not exist on type '{ id: string; createdAt: Date; email: string | null; username: string | null; name: string | null; isAdmin: boolean; jwtUserSecret: string | null; lastLogoutTime: Date | null; ... 9 more ...; hasCompletedOnboardingTour: boolean; }'. [ Wasp ] src/user/operations.ts(161,20): error TS2339: Property '_count' does not exist on type '{ id: string; createdAt: Date; email: string | null; username: string | null; name: string | null; isAdmin: boolean; jwtUserSecret: string | null; lastLogoutTime: Date | null; ... 9 more ...; hasCompletedOnboardingTour: boolean; }'.
4 Replies
Hi @NEROX. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
The TypeScript errors you’re seeing are due to a mismatch between the type you expect for each user object and what is actually returned from your database query. Specifically, properties like chats and _count are not present on the base User type—these are only available if you explicitly include them in your Prisma query using the include or select options.
How to Fix
You need to:
1. Adjust your Prisma query to include the necessary relations and counts.
2. Update your output type to match the actual structure returned by the query.
#### 1. Adjust the Prisma Query
For example, to get the counts and related chats for each user, your query should look something like:
This ensures that each user object in pageOfUsers will have the _count and chats properties you expect.
#### 2. Update the Output Type
You need to annotate your query’s return type to reflect the included relations. In Wasp, when you include relations or counts, you must extend the base type accordingly. For example:
Or, as suggested in the Wasp community, you can use an intersection type for the return value, e.g.:
This pattern is also used for similar issues with included relations in Wasp queries, as seen in the community discussions:
The way you solve this is ... put a type like this:And for more complex relations:GetQuestionnaires<void, (Questionnaire & { project: Project })[]>
. Here you are being explicit about the{ project: Project }
part.
— Discord support thread: Need help with joined tables
Materials.GetAllQuery<void, (Material & { products: (ProductMaterials & { product: Product })[] })[]>#### Summary - Make sure your Prisma query includes the relations and counts you need. - Update your output type to reflect the actual structure returned. - If you want TypeScript to infer the type, you can omit the explicit return type or use the satisfies keyword (TypeScript 4.9+), but being explicit is often clearer in Wasp projects. If you follow this pattern, the TypeScript errors about missing properties should be resolved. If you need a concrete code example, let me know! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NEROX, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?
— Discord: Object relation doesn't compile to TypeScript
@kapa.ai
model User {
id String @id @default(uuid())
documents Document[]
}
model Document {
id String @id @default(uuid())
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
}
Create a query to count all docs from a user.
Let me check the knowledge base for you again. One moment! :books:
----------------------
To create a query in Wasp that counts all documents for a specific user, you need to:
1. Declare the query in your
main.wasp
file and specify the entities involved.
2. Implement the query in your operations file, using the Prisma count API via context.entities.Document.count
.
1. main.wasp
2. src/user/operations.ts
- The query expects an argument object with userId
.
- It returns the count of documents where userId
matches.
This approach follows the Wasp pattern for queries and uses the Prisma count API as described in the docs:The objectUsing Entities in Queries If you want to call this query from your client code, you can do: Using Queries Let me know if you need a React example or further details! ---------------------- Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: ---------------------- Sorry @NEROX, I seem to be having a bit of trouble. Don't worry though, I've pinged my support and they will try to fix me up! :technologist: Perhaps try again in 15mins?context.entities.Task
exposesprisma.task
from Prisma's CRUD API.
thx kapa!