Skip to content

Add template function getBlock #14474

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

Closed
frostime opened this issue Mar 29, 2025 · 7 comments
Closed

Add template function getBlock #14474

frostime opened this issue Mar 29, 2025 · 7 comments
Assignees
Milestone

Comments

@frostime
Copy link
Contributor

frostime commented Mar 29, 2025

添加模板函数,用于快速获取数据库列行所绑定的块

In what scenarios do you need this feature?

使用数据库模板列的时候,基本常用操作就是:

  • 获取 .ID
  • 使用 queryBlocks 查询 Blocks
  • 判断是否为空
  • 获取结果列表的第一个内容为块

Image

这个操作实在过于常见,所以我建议干脆直接封装一个模板函数简化一下操作算了

Describe the optimal solution

添加一个函数,例如 getBlockthisBlock 等。

该函数的输入参数为一个包含 id 属性的对象 { id: "..." }

出于鲁棒性的考虑,最好能让 getBlock 兼容两种输入:输入一个含有 id 属性的对象,或者直接输入 id 字符串。

函数会查询对应的块,如果不存在则返回 null

这样可以适当简化代码:

{{ $block = getBlock . }}
{{ if $block }}

{{ end }}

Describe the candidate solution

No response

Other information

No response

@TCOTC
Copy link
Contributor

TCOTC commented Mar 29, 2025

我在用这个:

.action{ if index . "custom-avs" }
判断主键是否有绑定块
.action{ end }

只要有绑定块就可以直接用块 ID 了

@frostime
Copy link
Contributor Author

我在用这个:

.action{ if index . "custom-avs" }
判断主键是否有绑定块
.action{ end }

只要有绑定块就可以直接用块 ID 了

仅仅这样是不够的。默认的 . 操作符所存储的信息量有限,有时我们仍需要通过 queryBlock 来获取块的详细内容。

我的异议是,使用 queryBlocks 有点麻烦:先进行查询(query),然后判断目标块是否存在,最后通过 first 方法获取块对象。

在数据库操作中,通常我们只需要获取一个块,因此 queryBlocks 返回的列表就显得有些多余。

即使按照你的方案,在判断块绑定之后,仍然需要通过 queryBlocks + first 来获取第一个块,与现有方案的区别并不显著。

@TCOTC
Copy link
Contributor

TCOTC commented Mar 29, 2025

至少不需要查数据库就知道 ID 是否有效(

@frostime
Copy link
Contributor Author

至少不需要查数据库就知道 ID 是否有效(

这个确实是一个明显的优点

@88250 88250 self-assigned this Mar 29, 2025
@88250 88250 added this to the 3.1.27 milestone Mar 31, 2025
@88250 88250 changed the title 添加模板函数,用于快速获取数据库列行所绑定的块 Add template function getBlock Mar 31, 2025
88250 added a commit that referenced this issue Mar 31, 2025
@88250 88250 closed this as completed Mar 31, 2025
@TCOTC
Copy link
Contributor

TCOTC commented Mar 31, 2025

这个函数的用法是怎样的?我没搞懂

Image

@88250
Copy link
Member

88250 commented Mar 31, 2025

获取块以后直接用就行了,和 queryBlocks 返回的对象结构一样。

@TCOTC
Copy link
Contributor

TCOTC commented Mar 31, 2025

哦我懂了,这两个差不多等价:

.action{ $v := getBlock "20250331174505-4uqhdq8" }
.action{ $blocks := queryBlocks "select * from blocks where id='20250331174505-4uqhdq8'" }
.action{ range $v := $blocks }
.action{ end }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants