Skip to content

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

:art: Improve database date fields to automatically fill in creation time https://github.com/siyuan-note/siyuan/issues/15828

Daniel6cc6ef66f9ea
diff --git a/kernel/av/av.go b/kernel/av/av.go
index 21874e1ac3..c8ccbf4168 100644
--- a/kernel/av/av.go
+++ b/kernel/av/av.go
@@ -221,6 +221,10 @@ type View struct {
GroupSort int `json:"groupSort"` // 分组排序值,用于手动排序
}
+func (view *View) IsGroupView() bool {
+ return nil != view.Group && "" != view.Group.Field
+}
+
// GetGroupValue 获取分组视图的分组值。
func (view *View) GetGroupValue() string {
if nil == view.GroupVal {
@@ -270,7 +274,7 @@ func (view *View) RemoveGroupByID(groupID string) {
// GetGroupKey 获取分组视图的分组字段。
func (view *View) GetGroupKey(attrView *AttributeView) (ret *Key) {
- if nil == view.Group || "" == view.Group.Field {
+ if !view.IsGroupView() {
return
}
@@ -295,6 +299,7 @@ type LayoutType string
const (
LayoutTypeTable LayoutType = "table" // 属性视图类型 - 表格
LayoutTypeGallery LayoutType = "gallery" // 属性视图类型 - 卡片
+ LayoutTypeKanban LayoutType = "kanban" // 属性视图类型 - 看板
)
const (
@@ -530,6 +535,15 @@ func SaveAttributeView(av *AttributeView) (err error) {
}
}
+ // 清理渲染回填值
+ for _, kv := range av.KeyValues {
+ for i := len(kv.Values) - 1; i >= 0; i-- {
+ if kv.Values[i].IsRenderAutoFill {
+ kv.Values = append(kv.Values[:i], kv.Values[i+1:]...)
+ }
+ }
+ }
+
var data []byte
if util.UseSingleLineSave {
data, err = gulu.JSON.MarshalJSON(av)
diff --git a/kernel/av/value.go b/kernel/av/value.go
index 033990d282..fc34414761 100644
--- a/kernel/av/value.go
+++ b/kernel/av/value.go
@@ -57,6 +57,8 @@ type Value struct {
Checkbox *ValueCheckbox `json:"checkbox,omitempty"`
Relation *ValueRelation `json:"relation,omitempty"`
Rollup *ValueRollup `json:"rollup,omitempty"`
+
+ IsRenderAutoFill bool `json:"-"` // 标识是否是渲染阶段自动填充的值,保存数据的时候要删掉
}
func (value *Value) SetUpdatedAt(mills int64) {
diff --git a/kernel/model/attribute_view.go b/kernel/model/attribute_view.go
index c6d6ec5fdb..e5a02ba72d 100644
--- a/kernel/model/attribute_view.go
+++ b/kernel/model/attribute_view.go
@@ -101,7 +101,7 @@ func GetAttrViewAddingBlockDefaultValues(avID, viewID, groupID, previousBlockID,
return
}
- if 1 > len(view.Filters) && nil == view.Group {
+ if 1 > len(view.Filters) && !view.IsGroupView() {
// 没有过滤条件也没有分组条件时忽略
return
}
@@ -128,7 +128,7 @@ func GetAttrViewAddingBlockDefaultValues(avID, viewID, groupID, previousBlockID,
func getAttrViewAddingBlockDefaultValues(attrView *av.AttributeView, view, groupView *av.View, previousItemID, addingItemID string) (ret map[string]*av.Value) {
ret = map[string]*av.Value{}
- if 1 > len(view.Filters) && nil == view.Group {
+ if 1 > len(view.Filters) && !view.IsGroupView() {
// 没有过滤条件也没有分组条件时忽略
return
}
@@ -533,7 +533,7 @@ func foldAttrViewGroup(avID, blockID, groupID string, folded bool) (err error) {
return err
}
- if nil == view.Group {
+ if !view.IsGroupView() {
return
}
@@ -1032,7 +1032,7 @@ func AppendAttributeViewDetachedBlocksWithValues(avID string, blocksValues [][]*
v.IsDetached = true
v.CreatedAt = now
v.UpdatedAt = now
-
+ v.IsRenderAutoFill = false
keyValues.Values = append(keyValues.Values, v)
if av.KeyTypeSelect == v.Type || av.KeyTypeMSelect == v.Type {
@@ -1725,7 +1725,7 @@ func GetBlockAttributeViewKeys(nodeID string) (ret []*BlockAttributeViewKeys) {
}
func genAttrViewGroups(view *av.View, attrView *av.AttributeView) {
- if nil == view.Group {
+ if !view.IsGroupView() {
return
}
@@ -1969,7 +1969,7 @@ type GroupState struct {
func getAttrViewGroupStates(view *av.View) (groupStates map[string]*GroupState) {
groupStates = map[string]*GroupState{}
- if nil == view.Group {
+ if !view.IsGroupView() {
return
}
@@ -2329,6 +2329,7 @@ func updateAttributeViewColRelation(operation *Operation) (err error) {
destVal.Relation = &av.ValueRelation{}
}
destVal.UpdatedAt = now
+ destVal.IsRenderAutoFill = false
}
destVal.Relation.BlockIDs = append(destVal.Relation.BlockIDs, srcVal.BlockID)
destVal.Relation.BlockIDs = gulu.Str.RemoveDuplicatedElem(destVal.Relation.BlockIDs)
@@ -3145,12 +3146,19 @@ func addAttributeViewBlock(now int64, avID, dbBlockID, viewID, groupID, previous
// The database date field supports filling the current time by default https://github.com/siyuan-note/siyuan/issues/10823
for _, keyValues := range attrView.KeyValues {
if av.KeyTypeDate == keyValues.Key.Type && nil != keyValues.Key.Date && keyValues.Key.Date.AutoFillNow {
- if nil == keyValues.GetValue(addingItemID) { // 避免覆盖已有值(可能前面已经通过过滤或者分组条件填充了值)
+ val := keyValues.GetValue(addingItemID)
+ if nil == val { // 避免覆盖已有值(可能前面已经通过过滤或者分组条件填充了值)
dateVal := &av.Value{
ID: ast.NewNodeID(), KeyID: keyValues.Key.ID, BlockID: addingItemID, Type: av.KeyTypeDate, IsDetached: isDetached, CreatedAt: now, UpdatedAt: now + 1000,
Date: &av.ValueDate{Content: now, IsNotEmpty: true},
}
keyValues.Values = append(keyValues.Values, dateVal)
+ } else {
+ if val.IsRenderAutoFill {
+ val.CreatedAt, val.UpdatedAt = now, now+1000
+ val.Date.Content, val.Date.IsNotEmpty = now, true
+ val.IsRenderAutoFill = false
+ }
}
}
}
@@ -3230,11 +3238,13 @@ func fillDefaultValue(attrView *av.AttributeView, view, groupView *av.View, prev
existingVal := keyValues.GetValue(addingItemID)
if nil == existingVal {
+ newValue.IsRenderAutoFill = false
keyValues.Values = append(keyValues.Values, newValue)
} else {
newValueRaw := newValue.GetValByType(keyValues.Key.Type)
if av.KeyTypeBlock != existingVal.Type || (av.KeyTypeBlock == existingVal.Type && existingVal.IsDetached) {
// 非主键的值直接覆盖,主键的值只覆盖非绑定块
+ existingVal.IsRenderAutoFill = false
existingVal.SetValByType(keyValues.Key.Type, newValueRaw)
}
}
diff --git a/kernel/model/attribute_view_render.go b/kernel/model/attribute_view_render.go
index 992a39c4bd..534159c866 100644
--- a/kernel/model/attribute_view_render.go
+++ b/kernel/model/attribute_view_render.go
@@ -177,7 +177,7 @@ func renderAttributeViewGroups(viewable av.Viewable, attrView *av.AttributeView,
}
func hideEmptyGroupViews(view *av.View, viewable av.Viewable) {
- if nil == view.Group {
+ if !view.IsGroupView() {
return
}
@@ -343,14 +343,14 @@ func sortGroupsBySelectOption(view *av.View, groupKey *av.Key) {
}
func isGroupByDate(view *av.View) bool {
- if nil == view.Group {
+ if !view.IsGroupView() {
return false
}
return av.GroupMethodDateDay == view.Group.Method || av.GroupMethodDateWeek == view.Group.Method || av.GroupMethodDateMonth == view.Group.Method || av.GroupMethodDateYear == view.Group.Method || av.GroupMethodDateRelative == view.Group.Method
}
func isGroupByTemplate(attrView *av.AttributeView, view *av.View) bool {
- if nil == view.Group {
+ if !view.IsGroupView() {
return false
}
diff --git a/kernel/sql/av.go b/kernel/sql/av.go
index 7d8e7b873c..7dff8e64a6 100644
--- a/kernel/sql/av.go
+++ b/kernel/sql/av.go
@@ -624,6 +624,7 @@ func fillAttributeViewKeyValues(attrView *av.AttributeView, collection av.Collec
}
}
if !exist {
+ val.IsRenderAutoFill = true
keyValues.Values = append(keyValues.Values, val)
}
}