目前在用 gin+gorm 写一个小玩意,遇到这样的情况:
为了代码复用及写起来方便,基本都是用的 orm select *
有时候面向前台的接口不方便展示数据集中的某个字段,但是面向后端的又需要
这应该是前后端分离都会遇到的吧,请教各位大佬,比较好的解决方案是什么?
目前在用 gin+gorm 写一个小玩意,遇到这样的情况:
为了代码复用及写起来方便,基本都是用的 orm select *
有时候面向前台的接口不方便展示数据集中的某个字段,但是面向后端的又需要
这应该是前后端分离都会遇到的吧,请教各位大佬,比较好的解决方案是什么?
大家误会了,这是我业余写的小玩意
还在写的过程中,没有将查询到的所有数据直接返回给前端
提问是为了找一个更好的解决方案。
|
hqMay 17, 2020 via Android
1
Graphql,https://graphql.org/code/#go
|
|
heimeilMay 17, 2020
2
type User struct {
Name string `json:"name"` Password string `json:"-"` } |
|
kasper4649May 17, 2020 via Android
5
DTO ?
|
|
6
var u User =.....
//举例:隐藏 User 里的 password var out = struct{ User Hide1 bool `json:"password,omitempty"` }{ User: u, } json.Marshal(out) //原理:利用 omitempty 属性的空值同名字段覆盖 |
|
CEBBCATMay 17, 2020 via Android
7
这个 Google 一下就有了吧。可以重新定义结构体的 JSON 相关方法
|
|
qiayueMay 17, 2020
8
后端处理后再返回,每一个字段都要处理,确保返回的数字是数字,字符是字符。
另外很多时候还会返回对象,举例帖子详情接口,返回的 author 就需要是一个对象,方便展示昵称和头像 { "id": 123, "title": "帖子标题", "author":{ "id": 456, "name": "帖子作者昵称", "avatar": "帖子作者头像 URL" } } |
|
JackyCDKMay 17, 2020
11
json: - , binding: password,required
|
|
renmuMay 17, 2020 via Android
12
都是 select * 会被骂的吧😂😂性能有问题
|
|
securityCodingMay 17, 2020
13
@renmu 表设计好,索引用好并不会有什么问题...
|
|
GMMay 17, 2020
14
很简单,换 PHP 就好了!(* ̄ rǒ ̄)
|
|
winrarMay 18, 2020
15
这个 ORM 都有的吧,比如 django 的 values(fields, *expressions)
|
|
ppphpMay 18, 2020
17
我的实践上还是返回的 model 和数据表的 model 会转一下,方便点,改表也没有心理压力
|
|
baobao1270May 18, 2020
18
.NET 有 DTO,其他语言应该也有类似的,思路就是用中间类转换一下
|
|
k9982874May 18, 2020 via iPhone
19
先不说 select *的问题,你是把数据直接怼进 map 里?
|
|
CounterMay 18, 2020 via Android
20
创建一个专门用于输出数据的类,不要直接输出实体
|
|
wfhtqpMay 18, 2020
21
使用 tag+reflect,根据请求 fields 返回需要的字段
|
|
22
@renmu 很多种情况下都会出现操作的数据对象是一样的,就是需要的字段不同,不 select * 每个地方都要写一次,代码复用超级低。其实 OOP 思想也会有类似的问题,看怎么取舍了。
|
|
24
@k9982874 https://gorm.io/docs/query.html 不需要用 map 啊,创建好表 struct 就行了
|
|
usebenMay 18, 2020
25
dto vo po
|
|
reusMay 18, 2020
26
如果出什么 bug,密码泄露到前台,你就知错了。
好的设计,就算出现 bug,也不至于出现敏感数据泄露。 安全距离是很重要的,你这种做法,不安全。 |
|
dandycheungMay 18, 2020 via iPhone
29
一楼正解。
|
|
siteshenMay 18, 2020
30
// models/user.go
struct User { username string password stirng age int ... } // responses/user.go struct UserResponse { username string age int ... } func NewUserResponse(user *User) { ... } // handlers/user.go funct ReadUserHandler(req, res) { user = models.ReadUserById(req.UserId) return response.NewUserResponse(user) } |
|
siteshenMay 18, 2020
31
@siteshen 简单的说,就是你们少了一层抽象:API response 。加上之后 API 返回字段就只和 API response 层相关,不需要和 model 、logic 层耦合(甚至 model 用 ORM 实现、甚至存储换成 redis/mongodb 也不会影响前端)。
|
|
wfhtqpMay 18, 2020
32
|
|
33
@siteshen 是不是可以理解为 response 就是定义数据返回格式的,有几处不同的返回格式就定义几个 response,NewRespons 没有实际输出,只是进行数据重组?
|
|
odirusMay 18, 2020
34
举个例子,假设需要分别返回“课程信息”给 PC 端和 APP 端,他们需要返回的字段详细程度是不一样的,可以这样做
查询课程 ID -> 查询出该课程的详细信息 -> 缓存到某个地方(例如 ES ) -> 通过 Graphql 分别取各个端需要的字段信息 -> 返回给端 更新课程 ID -> 更新 DB 中的课程信息 -> 更新 ES 中的课程信息 |
|
PythondrMay 18, 2020
35
不建议将数据库的 model 直接返回, 一般会定义一个 controller 层的 model, 这样的数据字段更具有灵活性,也更安全
|
|
ViggoSiteMay 18, 2020
37
指定 Select 字段,它不香麽?
|
|
loopljMay 18, 2020
38
VO,DTO,各种结构职责分离
|
|
misaka19000May 18, 2020
39
添加 exclude 或者 include 选项
|
|
penaMay 18, 2020
40
https://github.com/pengwenwu/gin-items
不介意的话可以看下我自己写的小项目,基于 gin 的 restfull 风格的商品服务。 不定字段可以通过 scan 转到 map 里,不过性能会有影响 |
|
siteshenMay 18, 2020
41
@Wenco NewUserResponse 是其他语言中的构造函数,这样对生成的 UserResponse 可控(甚至 UserResponse 可以设计为 private 的 userResponse )。
UserResponse 也是可以复用的,下面这个例子就会复用 BasicUser 。 用户列表可能就只需要 BasicUser (用户名、头像), 查看用户详情时可能需要 UserDetail (BasicUser + 粉丝数、文章数), 查看自己的详情时可能需要 Me ( UserDetail + 收入 + 草稿箱中的文章数)。 struct BasicUser { username string avatar url } // API readUser struct UserDetail { UserResponse bio string location string n_follower int n_following int n_posts int } // 用户查看自己信息的时 struct Me { UserDetail n_fav_posts int total_cost double total_income double n_draft_posts int } // API listUser struct UserList { users BasicUser[] } |
|
42
https://colobu.com/2017/06/21/json-tricks-in-Go/
刚好前段时间忘了在哪收藏的一篇文章,有楼主需要的 |
|
43
@asAnotherJack 感谢,很有用!
|