Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Database table view cell value adds `createdAt` and `updatedAt` fields https://github.com/siyuan-note/siyuan/issues/10492

Daniel01744ae69ef9
diff --git a/kernel/av/av.go b/kernel/av/av.go
index bd11c165bb..d40c14307d 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -269,7 +269,7 @@ func SaveAttributeView(av *AttributeView) (err error) {
}
}
if 0 == v.Block.Updated {
- v.Block.Updated = now
+ v.Block.Updated = v.Block.Created
}
}
case KeyTypeNumber:
@@ -302,6 +302,19 @@ func SaveAttributeView(av *AttributeView) (err error) {
}
}
}
+
+ // 补全值的创建时间和更新时间
+ createdStr := v.ID[:len("20060102150405")]
+ created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
+ if nil == parseErr {
+ v.CreatedAt = created.UnixMilli()
+ } else {
+ v.CreatedAt = now
+ }
+
+ if 0 == v.UpdatedAt {
+ v.UpdatedAt = v.CreatedAt
+ }
}
}
diff --git a/kernel/av/table.go b/kernel/av/table.go
index d3922dcc3f..61738cf338 100644
--- a/kernel/av/table.go
+++ b/kernel/av/table.go
@@ -84,12 +84,20 @@ const (
func (value *Value) Compare(other *Value) int {
if nil == value {
- return -1
+ return 1
}
if nil == other {
+ return -1
+ }
+
+ if !value.IsEdited() {
return 1
}
+ if !other.IsEdited() {
+ return -1
+ }
+
switch value.Type {
case KeyTypeBlock:
if nil != value.Block && nil != other.Block {
diff --git a/kernel/av/value.go b/kernel/av/value.go
index e3f2601354..2a674e2a7d 100644
--- a/kernel/av/value.go
+++ b/kernel/av/value.go
@@ -37,6 +37,9 @@ type Value struct {
Type KeyType `json:"type,omitempty"`
IsDetached bool `json:"isDetached,omitempty"`
+ CreatedAt int64 `json:"createdAt,omitempty"`
+ UpdatedAt int64 `json:"updatedAt,omitempty"`
+
Block *ValueBlock `json:"block,omitempty"`
Text *ValueText `json:"text,omitempty"`
Number *ValueNumber `json:"number,omitempty"`
@@ -180,6 +183,14 @@ func (value *Value) Clone() (ret *Value) {
return
}
+func (value *Value) IsEdited() bool {
+ if value.CreatedAt == value.UpdatedAt {
+ // 说明是刚刚创建的块
+ return false
+ }
+ return true
+}
+
type ValueBlock struct {
ID string `json:"id"`
Content string `json:"content"`
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index 57c0f6a7ae..68e9bc8d78 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -605,10 +605,25 @@ func renderAttributeView(attrView *av.AttributeView, viewID string, page, pageSi
}
}
if 0 == v.Block.Updated {
- v.Block.Updated = currentTimeMillis
+ v.Block.Updated = v.Block.Created
}
}
}
+
+ // 补全值的创建时间和更新时间
+ for _, v := range kv.Values {
+ createdStr := v.ID[:len("20060102150405")]
+ created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
+ if nil == parseErr {
+ v.CreatedAt = created.UnixMilli()
+ } else {
+ v.CreatedAt = currentTimeMillis
+ }
+
+ if 0 == v.UpdatedAt {
+ v.UpdatedAt = v.CreatedAt
+ }
+ }
}
switch view.LayoutType {
@@ -1698,82 +1713,35 @@ func addAttributeViewBlock(avID, previousBlockID, blockID string, isDetached boo
content = getNodeRefText(node)
}
now := time.Now().UnixMilli()
- blockValue := &av.Value{ID: ast.NewNodeID(), KeyID: blockValues.Key.ID, BlockID: blockID, Type: av.KeyTypeBlock, IsDetached: isDetached, Block: &av.ValueBlock{ID: blockID, Content: content, Created: now, Updated: now}}
+ blockValue := &av.Value{
+ ID: ast.NewNodeID(),
+ KeyID: blockValues.Key.ID,
+ BlockID: blockID,
+ Type: av.KeyTypeBlock,
+ IsDetached: isDetached,
+ CreatedAt: now,
+ UpdatedAt: now,
+ Block: &av.ValueBlock{ID: blockID, Content: content, Created: now, Updated: now}}
blockValues.Values = append(blockValues.Values, blockValue)
- // 如果存在排序和过滤条件,则将排序和过滤条件应用到新添加的块上
+ // 如果存在过滤条件,则将过滤条件应用到新添加的块上
view, _ := attrView.GetCurrentView()
if nil != view && (0 < len(view.Table.Filters) || 0 < len(view.Table.Sorts)) {
viewable, _ := renderAttributeViewTable(attrView, view)
viewable.FilterRows(attrView)
- viewable.SortRows()
-
- affectKeyIDs := map[string]bool{}
- for _, f := range view.Table.Filters {
- affectKeyIDs[f.Column] = true
- }
- for _, s := range view.Table.Sorts {
- affectKeyIDs[s.Column] = true
- }
-
- addedValues := map[string]bool{}
- if 0 < len(viewable.Rows) {
- row := GetLastSortRow(viewable.Rows)
- if nil != row {
- for affectKeyID := range affectKeyIDs {
- for _, cell := range row.Cells {
- if nil != cell.Value && cell.Value.KeyID == affectKeyID {
- if av.KeyTypeBlock == cell.ValueType {
- blockValue.Block.Content = cell.Value.Block.Content
- continue
- }
-
- if av.KeyTypeRollup == cell.ValueType || av.KeyTypeRelation == cell.ValueType || av.KeyTypeCreated == cell.ValueType || av.KeyTypeUpdated == cell.ValueType || av.KeyTypeTemplate == cell.ValueType {
- continue
- }
-
- newValue := cell.Value.Clone()
- newValue.ID = ast.NewNodeID()
- newValue.BlockID = blockID
- newValue.IsDetached = isDetached
- values, _ := attrView.GetKeyValues(affectKeyID)
- values.Values = append(values.Values, newValue)
- addedValues[affectKeyID] = true
- break
- }
- }
- }
- }
- }
-
- notAddedValues := map[string]bool{}
- for affectKeyID := range affectKeyIDs {
- if !addedValues[affectKeyID] {
- notAddedValues[affectKeyID] = true
- break
- }
- }
- if 0 < len(notAddedValues) {
- for _, filter := range view.Table.Filters {
- if !notAddedValues[filter.Column] {
- continue
- }
-
- for _, keyValues := range attrView.KeyValues {
- if keyValues.Key.ID == filter.Column {
- newValue := filter.GetAffectValue(keyValues.Key)
- newValue.ID = ast.NewNodeID()
- newValue.KeyID = keyValues.Key.ID
- newValue.BlockID = blockID
- newValue.IsDetached = isDetached
- keyValues.Values = append(keyValues.Values, newValue)
- break
- }
+ for _, filter := range view.Table.Filters {
+ for _, keyValues := range attrView.KeyValues {
+ if keyValues.Key.ID == filter.Column {
+ newValue := filter.GetAffectValue(keyValues.Key)
+ newValue.ID = ast.NewNodeID()
+ newValue.KeyID = keyValues.Key.ID
+ newValue.BlockID = blockID
+ newValue.IsDetached = isDetached
+ keyValues.Values = append(keyValues.Values, newValue)
+ break
}
}
-
- // 仅使用上面的过滤条件计算受影响的值并插入兜底,受影响的排序条件不进行计算值插入
}
}
@@ -1830,8 +1798,7 @@ func GetLastSortRow(rows []*av.TableRow) *av.TableRow {
row := rows[i]
blockVal := row.GetBlockValue()
if nil != blockVal {
- if nil != blockVal.Block && blockVal.Block.Created == blockVal.Block.Updated {
- // 说明是刚刚创建的块,跳过
+ if !blockVal.IsEdited() {
continue
}
return row