fix
This commit is contained in:
parent
2841e31d84
commit
3e206c42cf
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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{
|
||||
|
|
|
|||
Loading…
Reference in New Issue