Skip to content

Add type inference to loadBeforeMutationEntities and loadAfterMutationEntities #2365

@genu

Description

@genu

Is there a reliable way to infer the proper return type of loadAfterMutationEntities and loadBeforeMutationEntities?

https://github.com/zenstackhq/zenstack-v3/blob/35a9e0d6575d7baf7d0aa3e29484d7aedb56b4d0/packages/runtime/src/client/plugin.ts#L170-L174

https://github.com/zenstackhq/zenstack-v3/blob/35a9e0d6575d7baf7d0aa3e29484d7aedb56b4d0/packages/runtime/src/client/plugin.ts#L153-L158

If the return data is typed, it becomes easier working with any follow up queries:

export const onAfterPostDelete = definePlugin<SchemaType>({
  id: "onAfterPostDelete",
  onEntityMutation: {
    async beforeEntityMutation({ model, action, loadBeforeMutationEntities, client, queryId }) {
      const deletedPost = await loadBeforeMutationEntities() // <--- typed as Record<string, unknown>[] | undefined but its really a Post[]

      if (action === "delete" && model === "Post") {
        // Do some logic
        const tags = await client.tag.findMany({
          where: {
            postId: deletedPost[0].id, // <-- This doesn't work unless we cast deletedPost to proper type like Post[]
          },
        })
      }
    },
  },
})

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions