tongyuelong

如果只想要数据库表中指定列的数据,请问这样的 rest api 应该如何设计?

  •  
  •   tongyuelong · Feb 7, 2017 · 2077 views
    This topic created in 3511 days ago, the information mentioned may be changed or developed.

    比如说我可以用 /api/users 来访问后台所有用户的数据, sql 表达为:

    select * from users ; 
    

    但如果此时我有一个需求,我只需要用户表中用户名这一列数据,如:

    select name from users ; 
    

    这样的 api 应该如何设计呢?请指教。

    1 replies    2017-02-07 22:33:03 +08:00
    shoaly
        1
    shoaly  
       Feb 7, 2017
    /get/field/name => select name_of_users from users ;

    这里 name 我觉得可以直接写死, 原则上 取某几个字段的接口应该是固定的, 并不要让前端去获得真实数据库的字段名字, 更不要用变量来给予前端调取任意字段的能力. 这样耦合太多.
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   931 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:01 · PVG 03:01 · LAX 12:01 · JFK 15:01
    ♥ Do have faith in what you're doing.