glennliao / apijson-go

[WIP] 基于 go + goframe 实现的 apijson
MIT License
78 stars 16 forks source link

关联查询问题 #14

Open gzxy-0102 opened 1 year ago

gzxy-0102 commented 1 year ago

能不能像APIJSON官方一样 支持这种嵌套查询 image

glennliao commented 1 year ago

目前不支持嵌套 支持以下方式:

in

{
  "User": {
    "userId": "10002"
  },
  "todo[]":{
    "Todo": {
      "userId@": "User/userId"
    }
  }
}

out:

{
  "User": {
    "createdAt": "2022-10-24 17:06:09",
    "id": 4,
    "realname": "史强",
    "userId": "10002",
    "username": "shiqiang"
  },
  "todo[]": [
    {
      "Todo": {
        "createdAt": "2022-10-24 17:59:47",
        "deletedAt": null,
        "id": 24,
        "note": null,
        "partner": "10003",
        "title": "找丁仪发呆",
        "todoId": null,
        "userId": "10002"
      }
    },
    {
      "Todo": {
        "createdAt": "2022-10-24 18:00:03",
        "deletedAt": null,
        "id": 26,
        "note": null,
        "partner": "10003",
        "title": "找丁仪发呆",
        "todoId": null,
        "userId": "10002"
      }
    },
  ],
  "ok": true,
  "code": 200,
  "msg": "success",
  "span": "31.4325ms"
}
gzxy-0102 commented 1 year ago

目前不支持嵌套 支持以下方式:

in

{
  "User": {
    "userId": "10002"
  },
  "todo[]":{
    "Todo": {
      "userId@": "User/userId"
    }
  }
}

out:

{
  "User": {
    "createdAt": "2022-10-24 17:06:09",
    "id": 4,
    "realname": "史强",
    "userId": "10002",
    "username": "shiqiang"
  },
  "todo[]": [
    {
      "Todo": {
        "createdAt": "2022-10-24 17:59:47",
        "deletedAt": null,
        "id": 24,
        "note": null,
        "partner": "10003",
        "title": "找丁仪发呆",
        "todoId": null,
        "userId": "10002"
      }
    },
    {
      "Todo": {
        "createdAt": "2022-10-24 18:00:03",
        "deletedAt": null,
        "id": 26,
        "note": null,
        "partner": "10003",
        "title": "找丁仪发呆",
        "todoId": null,
        "userId": "10002"
      }
    },
  ],
  "ok": true,
  "code": 200,
  "msg": "success",
  "span": "31.4325ms"
}

这样确实好使 我尝试过 但是还是嵌套数据对前端友好点吧

glennliao commented 1 year ago

我个人前端使用上来讲,其实都一样。 先把其它完善下,再来增加对这种嵌套的支持吧

TommyLemon commented 1 year ago

能不能像APIJSON官方一样 支持这种嵌套查询 image

这种嵌套写法不支持 JOIN,还是建议把 User 和 comment[] 放在同一层级

image

https://github.com/Tencent/APIJSON/blob/master/Document.md#3.2

TommyLemon commented 1 year ago

目前不支持嵌套 支持以下方式: in

{
  "User": {
    "userId": "10002"
  },
  "todo[]":{
    "Todo": {
      "userId@": "User/userId"
    }
  }
}

out:

{
  "User": {
    "createdAt": "2022-10-24 17:06:09",
    "id": 4,
    "realname": "史强",
    "userId": "10002",
    "username": "shiqiang"
  },
  "todo[]": [
    {
      "Todo": {
        "createdAt": "2022-10-24 17:59:47",
        "deletedAt": null,
        "id": 24,
        "note": null,
        "partner": "10003",
        "title": "找丁仪发呆",
        "todoId": null,
        "userId": "10002"
      }
    },
    {
      "Todo": {
        "createdAt": "2022-10-24 18:00:03",
        "deletedAt": null,
        "id": 26,
        "note": null,
        "partner": "10003",
        "title": "找丁仪发呆",
        "todoId": null,
        "userId": "10002"
      }
    },
  ],
  "ok": true,
  "code": 200,
  "msg": "success",
  "span": "31.4325ms"
}

这样确实好使 我尝试过 但是还是嵌套数据对前端友好点吧

未必更加友好,尤其是对于客户端来说,建立实体类(JavaBean/Entity 等) 还是把 User 和 List 独立开更好,而不是

class User {
   List<Comemnt> commentList;
}

如果是这种,那你想想结构上还可能有各种嵌套组合导致不方便处理,以及对应带来的循环引用导致内存泄漏等问题

class Comemnt {
   User owner;
}
class User {
   List<Moment> momentList;
}
class Moment {
   User publisher;
   List<Comment> commentList;
}

...