You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在json-server中向worker的dayPunchings数组属性追加新工作日记录

问题背景

正在开发工时管理前端项目,使用json-server模拟接口数据,现有API结构如下:

{
    "workers": [
        {
            "id": 1,
            "name": "Raphael Alvarenga do Carmo",
            "dayPunchings": [
                {
                    "day": "7/25/2021",
                    "timePunchings": [
                        {
                            "id": 1,
                            "idWorker": 1,
                            "type": "Arrive",
                            "time": "7/25/2021, 8:05:00 AM"
                        },
                        {
                            "id": 2,
                            "idWorker": 1,
                            "type": "Lunch",
                            "time": "7/25/2021, 12:00:00 PM"
                        },
                        {
                            "id": 3,
                            "idWorker": 1,
                            "type": "lunchReturn",
                            "time": "7/25/2021, 1:00:00 PM"
                        },
                        {
                            "id": 4,
                            "idWorker": 1,
                            "type": "Exit",
                            "time": "7/25/2021, 5:00:00 PM"
                        }
                    ]
                }
            ]
        }
    ]
}
需求说明

需要向指定worker的dayPunchings属性中追加一条新的工作日打卡记录,已尝试直接拼接层级URLhttp://localhost:3000/workers/1/dayPunchings/发起POST/PUT/PATCH请求均未生效,全量更新整个worker对象效率过低,需要更优的实现方案。

解决方案

方案1:使用PATCH请求局部更新指定字段

json-server的PATCH请求支持仅覆盖传入的字段,不需要传递完整的worker对象,操作方式如下:

  • 请求URL:http://localhost:3000/workers/1
  • 请求方法:PATCH
  • 请求头:Content-Type: application/json
  • 请求体:仅传入更新后的dayPunchings数组即可,其余字段不会被修改
{
  "dayPunchings": [
    // 原有所有dayPunchings记录
    {
      "day": "7/25/2021",
      "timePunchings": [/* 原有打卡项 */]
    },
    // 新增的日打卡记录
    {
      "day": "7/26/2021",
      "timePunchings": []
    }
  ]
}

该方案不需要调整原有JSON结构,仅修改目标字段,比全量更新效率高很多。

方案2:拆解嵌套结构为独立资源(推荐)

json-server对根级的资源数组原生支持CRUD操作,将嵌套的关联数据拆解为独立根级资源后,操作会更符合REST规范,效率也更高。
调整后的JSON结构如下:

{
  "workers": [
    {
      "id": 1,
      "name": "Raphael Alvarenga do Carmo"
    }
  ],
  "dayPunchings": [
    {
      "id": 1,
      "workerId": 1,
      "day": "7/25/2021"
    }
  ],
  "timePunchings": [
    {
      "id": 1,
      "dayPunchingId": 1,
      "idWorker": 1,
      "type": "Arrive",
      "time": "7/25/2021, 8:05:00 AM"
    },
    {
      "id": 2,
      "dayPunchingId": 1,
      "idWorker": 1,
      "type": "Lunch",
      "time": "7/25/2021, 12:00:00 PM"
    },
    {
      "id": 3,
      "dayPunchingId": 1,
      "idWorker": 1,
      "type": "lunchReturn",
      "time": "7/25/2021, 1:00:00 PM"
    },
    {
      "id": 4,
      "dayPunchingId": 1,
      "idWorker": 1,
      "type": "Exit",
      "time": "7/25/2021, 5:00:00 PM"
    }
  ]
}

调整后追加新的日打卡记录操作如下:

  • 请求URL:http://localhost:3000/dayPunchings
  • 请求方法:POST
  • 请求头:Content-Type: application/json
  • 请求体:
{
  "workerId": 1,
  "day": "7/26/2021"
}

后续查询指定工人的打卡记录可以直接用http://localhost:3000/dayPunchings?workerId=1,还支持通过_expand、_embed参数关联查询工人信息和当日打卡明细,适配更多业务场景。


内容的提问来源于stack exchange,提问作者Raphael Alvarenga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.10 05:54:01