创建新行时为用户创建第二行

huangapple go评论58阅读模式
英文:

Hasura: Create Second Row When User Insert New Row

问题

我在云上使用Hasura和PostgreSQL数据库。

我有一个名为repositories的表,我想实现以下行为:
当用户使用我的insert_repositories_one变异创建新的存储库时,我希望自动创建另一个存储库(在表中插入第二行),用于缓存。

我熟悉Hasura事件,但感觉没有必要为事件Webhook创建新的API POST请求,仅仅是为了基于先前的存储库创建另一个存储库。
是否可以仅通过Hasura上的配置来完成?还是应该深入研究PostgreSQL触发器,尝试在那里完成它?

提前感谢。

英文:

I am using Hasura on cloud with Postgresql DB.

I have a table called repositories and i would like to achieve this behavior:
When the user creates new repository (with my insert_repositories_one mutation) I want to create another repository (insert second row to the table) automaticlly for caching.

I am familiar with Hasura Events but it feels unnecessary to create a new API post request for the event webhook just for creating another repository based on the previos one.
Can it be done only with configuration on Hasura?
Or should I dive into Postgresql triggers and try to accomplish it there?

Thank you in advance.

答案1

得分: 1

Hasura 单独无法自动完成此操作。您可以更新您的 mutation,以便在单个事务的一部分创建两个记录。

如果您希望在插入操作的响应中“自动”完成,而无需在 mutation 中使用命令式代码,我建议您创建一个 Postgres 触发器来处理逻辑。这样做的好处是,无论如何插入行(即使您使用 SQL 查询手动插入并绕过 GraphQL API),您始终会得到一致的数据模型。

英文:

There is no automatic way to accomplish this using Hasura alone. You could update your mutation so that both records are created as part of a single transaction.

If you want it to happen "automatically" in response to an insert without needing to have imperative code in your mutation I would recommend that you create a Postgres Trigger instead to take care of the logic. The advantage here is that no matter how the row gets inserted (even if you do it manualy with a SQL query and circumvent the GraphQL API) you will always have a consistent data model

huangapple
  • 本文由 发表于 2023年5月11日 16:11:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76225440.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定