diff --git a/internal/database/base.go b/internal/database/base.go index 32a725b..447b90f 100644 --- a/internal/database/base.go +++ b/internal/database/base.go @@ -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 } diff --git a/internal/database/dm8/adapter.go b/internal/database/dm8/adapter.go index a014259..a7b16ad 100644 --- a/internal/database/dm8/adapter.go +++ b/internal/database/dm8/adapter.go @@ -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 } diff --git a/internal/database/postgres/adapter.go b/internal/database/postgres/adapter.go index e7b9481..d4e0ad0 100644 --- a/internal/database/postgres/adapter.go +++ b/internal/database/postgres/adapter.go @@ -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 } diff --git a/internal/database/sqlite/adapter.go b/internal/database/sqlite/adapter.go index e31fb29..e9b3ec1 100644 --- a/internal/database/sqlite/adapter.go +++ b/internal/database/sqlite/adapter.go @@ -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 } diff --git a/internal/engine/crud_handler.go b/internal/engine/crud_handler.go index 6141c1a..36841f3 100644 --- a/internal/engine/crud_handler.go +++ b/internal/engine/crud_handler.go @@ -54,8 +54,8 @@ 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) (*types.UpdateResult, error) { - matched, modified, _, err := h.store.Update(collection, filter, update, false, nil) +func (h *CRUDHandler) Update(ctx context.Context, collection string, filter types.Filter, update types.Update, upsert bool, arrayFilters []types.Filter) (*types.UpdateResult, error) { + matched, modified, upsertedIDs, err := h.store.Update(collection, filter, update, upsert, arrayFilters) 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) diff --git a/internal/protocol/http/server.go b/internal/protocol/http/server.go index 2447d63..9e5b4a7 100644 --- a/internal/protocol/http/server.go +++ b/internal/protocol/http/server.go @@ -270,8 +270,8 @@ func (h *RequestHandler) HandleUpdate(w http.ResponseWriter, r *http.Request, db totalModified := 0 upserted := make([]types.UpsertID, 0) - for _, op := range req.Updates { - result, err := h.crud.Update(context.Background(), fullCollection, op.Q, op.U) + for idx, op := range req.Updates { + result, err := h.crud.Update(context.Background(), fullCollection, op.Q, op.U, op.Upsert, op.ArrayFilters) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return @@ -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 _, uid := range result.Upserted { + upserted = append(upserted, types.UpsertID{ + Index: idx + uid.Index, + ID: uid.ID, + }) + } } response := types.UpdateResult{