zenstack icon indicating copy to clipboard operation
zenstack copied to clipboard

[Feature Request] Generating model-specific client for client hooks

Open ymc9 opened this issue 2 years ago • 0 comments

Requested by no-fox (discord).

Feature request for tanstack query:

Generate a model-specific client (like the prisma orm) instead of flat hooks.

<script lang="ts">
  import postsClient from "$generated/hooks/post";

  const myPost = postsClient.useFindfirst({ id: "1" });
</script>

This would allow you to succinctly abstract over the model e.g.:

<!-- DeleteButton.svelte -->
<script lang="ts">
  type T = $$Generic;

  export let model: ModelClient<T>
  export let id: string;

  const deleteModel = modelClient.useDelete();

  function handleDelete() {
    $deleteModel.mutate({ where: { id: { $in: [id] } } });
  }
</script>

<button on:click={handleDelete}>Delete</button>

And then:

<!-- EditPost.svelte -->
<script lang="ts">
import posts from "$generated/hooks/post"
// ...
</script>

<DeleteButton model={posts} id={postId} />

ymc9 avatar Jun 07 '23 13:06 ymc9