Compare commits

...

3 Commits

Author SHA1 Message Date
kingecg 5f85e67047 ```
feat(engine): 修复更新操作中upsertedIDs变量未定义的问题

在CRUDHandler的Update方法中,修正了upsertedIDs变量的赋值,
使其与store.Update方法的返回值保持一致。
```
2026-03-20 20:19:26 +08:00
kingecg 7428dc45f6 Merge branch 'main' of ssh://git.kingecg.top:2222/kingecg/gomog 2026-03-20 20:19:15 +08:00
kingecg 3e206c42cf fix 2026-03-20 20:15:03 +08:00
6 changed files with 60 additions and 8 deletions

View File

@ -95,7 +95,14 @@ func (a *BaseAdapter) InsertMany(ctx context.Context, collection string, docs []
defer stmt.Close()
for _, doc := range docs {
jsonData, err := json.Marshal(doc.Data)
// 创建包含 _id 的新 Data 字段
dataWithID := make(map[string]interface{})
for k, v := range doc.Data {
dataWithID[k] = v
}
dataWithID["_id"] = doc.ID
jsonData, err := json.Marshal(dataWithID)
if err != nil {
return err
}

View File

@ -99,7 +99,14 @@ func (a *DM8Adapter) InsertMany(ctx context.Context, collection string, docs []t
defer tx.Rollback()
for _, doc := range docs {
jsonData, err := json.Marshal(doc.Data)
// 创建包含 _id 的新 Data 字段
dataWithID := make(map[string]interface{})
for k, v := range doc.Data {
dataWithID[k] = v
}
dataWithID["_id"] = doc.ID
jsonData, err := json.Marshal(dataWithID)
if err != nil {
return err
}

View File

@ -98,7 +98,14 @@ func (a *PostgresAdapter) InsertMany(ctx context.Context, collection string, doc
defer tx.Rollback()
for _, doc := range docs {
jsonData, err := json.Marshal(doc.Data)
// 创建包含 _id 的新 Data 字段
dataWithID := make(map[string]interface{})
for k, v := range doc.Data {
dataWithID[k] = v
}
dataWithID["_id"] = doc.ID
jsonData, err := json.Marshal(dataWithID)
if err != nil {
return err
}

View File

@ -200,7 +200,14 @@ func (a *SQLiteAdapter) InsertMany(ctx context.Context, collection string, docs
defer tx.Rollback()
for _, doc := range docs {
jsonData, err := json.Marshal(doc.Data)
// 创建包含 _id 的新 Data 字段
dataWithID := make(map[string]interface{})
for k, v := range doc.Data {
dataWithID[k] = v
}
dataWithID["_id"] = doc.ID
jsonData, err := json.Marshal(dataWithID)
if err != nil {
return err
}

View File

@ -55,7 +55,7 @@ func (h *CRUDHandler) Insert(ctx context.Context, collection string, docs []map[
// Update 更新文档
func (h *CRUDHandler) Update(ctx context.Context, collection string, filter types.Filter, update types.Update, upsert bool) (*types.UpdateResult, error) {
matched, modified, _, err := h.store.Update(collection, filter, update, upsert, nil)
matched, modified, upsertedIDs, err := h.store.Update(collection, filter, update, upsert, nil)
if err != nil {
return nil, err
}
@ -63,11 +63,24 @@ func (h *CRUDHandler) Update(ctx context.Context, collection string, filter type
// 异步持久化到数据库
go h.persistToDB(ctx, collection)
return &types.UpdateResult{
result := &types.UpdateResult{
OK: 1,
N: matched,
NModified: modified,
}, nil
}
// 转换 upserted IDs
if len(upsertedIDs) > 0 {
for i, id := range upsertedIDs {
result.Upserted = append(result.Upserted, types.UpsertID{
Index: i,
ID: id,
})
}
result.UpsertedN = len(upsertedIDs)
}
return result, nil
}
// Delete 删除文档
@ -89,6 +102,11 @@ func (h *CRUDHandler) Delete(ctx context.Context, collection string, filter type
// persistToDB 持久化集合到数据库
func (h *CRUDHandler) persistToDB(ctx context.Context, collection string) {
// 如果没有配置数据库适配器,跳过持久化
if h.adapter == nil {
return
}
log.Printf("[DEBUG] Starting persist for collection: %s", collection)
if err := h.store.SyncToDB(ctx, collection); err != nil {
log.Printf("[ERROR] Failed to persist collection %s: %v", collection, err)

View File

@ -280,7 +280,13 @@ func (h *RequestHandler) HandleUpdate(w http.ResponseWriter, r *http.Request, db
totalMatched += result.N
totalModified += result.NModified
// TODO: 处理 upserted IDs
// 合并 upserted IDs
for i, uid := range result.Upserted {
upserted = append(upserted, types.UpsertID{
Index: len(upserted) + i,
ID: uid.ID,
})
}
}
response := types.UpdateResult{