Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: implement write hooks for documents #26

Merged
merged 4 commits into from
Jan 13, 2025
Merged

Conversation

achou11
Copy link
Member

@achou11 achou11 commented Jan 9, 2025

Closes #20

Introduces the following write hooks:

  • useCreateDocument()
  • useUpdateDocument()
  • useDeleteDocument()

The idea is for the consuming application to wrap these hooks if they want the convenience of hooks scoped to a writeable data type e.g.

function useCreateObservation() {
  const projectId = useCurrentProjectId()
  return useCreateDocument({ docType: 'observation', projectId: projectId })
}

function App() {
  const createObservation = useCreateObservation()
  
  function onPress() {
    createObservation.mutate({ ... }, { onSuccess: (observationDoc) => { ... } })
  }
}

Notes:

  • Missing in this implementation is being able to specify an attachmentsToAdd opts. Since this only applies to observations, I was trying - and failing - to have the implementation elegantly handle the polymorphism from a types perspective. For now, will consider that to be an enhancement that can be implemented later if really needed. Will create a more specific issue for this after this has been merged.

  • Renames a type that's exported from the package, hence the breaking change notice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

renaming DocumentType to WriteableDocumentType is the breaking change.

@achou11 achou11 force-pushed the ac/document-write-hooks branch from 2733b31 to 0cdcf43 Compare January 13, 2025 16:21
Comment on lines +289 to +294
queryClient.invalidateQueries({
queryKey: getDocumentsQueryKey({
projectId,
docType,
}),
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if this should be more granular about what it invalidates. right now this invalidates all queries related to the document type for the relevant project, which is potentially invalidating more than necessary. it might make sense to only invalidate the queries as following:

  1. Invalidate the 'get many' query without cascading
  2. Invalidate the 'get by version id' query with cascading
  3. Invalidate the 'get by doc id' query with cascading

I think this would be an optimization, but maybe it's fine to ignore for now. How we currently have it is "safer" despite potentially doing more work than necessary.

Comment on lines +255 to +260
queryClient.invalidateQueries({
queryKey: getDocumentsQueryKey({
projectId,
docType,
}),
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar question about how granular the invalidation should be

Comment on lines +220 to +225
queryClient.invalidateQueries({
queryKey: getDocumentsQueryKey({
projectId,
docType,
}),
})
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar question about how granular the invalidation should be

@achou11 achou11 merged commit 92f5be0 into main Jan 13, 2025
5 checks passed
@achou11 achou11 deleted the ac/document-write-hooks branch January 13, 2025 16:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Figure out write hooks API
1 participant