Skip to content

[Feature Request] Enable optimistic updates for related models #1840

Closed
@priyanka-sadhwani

Description

@priyanka-sadhwani

Currently, optimistic updates only seem to work for hooks within the same model. We have a use case that would benefit from optimistic updates that is between 2 models.

For example, we have the following two modes:

`model Class extends BaseWithCuid {
name String
shortDescription String? @db.VarChar(100)
longDescription String?
module Module @relation(fields: [moduleId], references: [id], onDelete: Restrict)
moduleId String
userClassProgress UserClassProgress[]
order Int
published Boolean
metadata Json? // TODO link to a class video model?

documents DocumentClassJoin[] // Keeps track of documents associated with this class, with any required metadata

category String? // For UI - can separate classes by their category
// tags

@@unique([order, category, moduleId])

// Access policy
@@Allow('all', true)
}`

`model UserClassProgress extends Base {
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
class Class @relation(fields: [classId], references: [id], onDelete: Cascade)
classId String
watched Boolean
progressState ProgressState

@@id(name: "userClassProgressId", [userId, classId])

@@Allow('all', true)
}`

We are using the useFindManyClass to retrieve some Classes along with the related UserClassProgress rows (query below). We are then using the useUpsertUserClassProgress hook to update some of the UserClassProgress rows. Optimistic update doesn't work in this case even if I include the id's of the UserClassProgress in the useFindManyClass hook.

Proposed solution
Ideally we would like for optimistic update to work with hooks across models.

Additional context
For reference, this is the query I am using in the useFindManyClass:

const { data, isLoading, error } = useFindManyClass({ where: { moduleId }, select: { id: true, name: true, longDescription: true, metadata: true, order: true, userClassProgress: { where: { userId, }, select: { userId: true, classId: true, progressState: true, watched: true, }, }, documents: { select: { id: true, order: true, document: { select: { displayName: true, storageRef: true, type: true, size: true, }, }, }, }, }, })

And this is the query used in the upsert hook:

mutate({ where: { userClassProgressId: { userId: "4bb0822c-9642-466d-81f0-50dada230c2f", classId: "cm2vtha9o000pfy55olraqnlq" } }, create: { watched: true, progressState: ProgressState.OKAY, user: { connect: { id: "4bb0822c-9642-466d-81f0-50dada230c2f" } }, class: { connect: { id: "cm2vtha9o000pfy55olraqnlq" } }, }, update: { progressState: ProgressState.OKAY }, })

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions