V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lengjingxu
V2EX  ›  数据库

有没有工具可以直接将 SQL 转化为可供前端调用的 API?

  •  
  •   lengjingxu · 2018-06-18 15:49:59 +08:00 · 3504 次点击
    这是一个创建于 2131 天前的主题,其中的信息可能已经有所发展或是发生改变。
    24 条回复    2018-06-26 17:33:36 +08:00
    Livid
        1
    Livid  
    MOD
       2018-06-18 15:52:49 +08:00
    请不要再把这样的提问类型的主题发到 /go/create

    这个主题已经被移动。
    lhx2008
        2
    lhx2008  
       2018-06-18 16:13:26 +08:00 via Android
    有一些后端云服务,其实本质上是 ORM+接口直接下放到前端,面对复杂查询就很麻烦了。安全性,扩展性也不好
    vinsa
        3
    vinsa  
       2018-06-18 16:30:35 +08:00
    MyBatis + SpringBoot
    mdluo
        4
    mdluo  
       2018-06-18 17:21:26 +08:00 via iPhone   ❤️ 1
    1. 后端框架 ORM + 资源型 RESTful 路由

    2. Prisma 提供了对 MySQL 和 Postgres 的映射,并自动生成前端可以调用的 GraphQL 接口;

    3. 或者前端直接用商业 NoSQL 数据库的 SDK,后台控制好权限,比如 AWS 的 DynamoDB、Firebase 的 FireStore、leancloud 的数据库等
    glacer
        5
    glacer  
       2018-06-18 17:25:47 +08:00
    GraphQL
    shengyu
        6
    shengyu  
       2018-06-18 17:28:30 +08:00 via iPhone
    sandman2 生成 restful
    sammo
        7
    sammo  
       2018-06-18 17:44:16 +08:00 via iPhone
    ruby on rails 的 Active Record
    WildCat
        8
    WildCat  
       2018-06-18 17:53:46 +08:00
    ActiveRecord example:

    ```ruby

    @users = User.order_by(:id).page(params[:page])

    render json: @users

    ```
    mxtob
        9
    mxtob  
       2018-06-18 18:13:30 +08:00 via Android
    @glacer 公司用的就是 graphql,个人觉得前端接口升级好点
    nine99
        10
    nine99  
       2018-06-18 20:51:52 +08:00   ❤️ 1
    xmysql
    beginor
        11
    beginor  
       2018-06-18 22:02:04 +08:00 via Android
    去年自己撸了一个类似的,根据需要添加功能,客户用着也觉得挺爽的。 就是把 select where order by 等等 做成 http 参数 , 做好安全性检查
    zhuangzhuang1988
        12
    zhuangzhuang1988  
       2018-06-18 22:10:58 +08:00
    看下这个有没有用 https://postgrest.org/
    qinxi
        13
    qinxi  
       2018-06-18 22:30:28 +08:00 via Android   ❤️ 1
    spring data jpa rest
    HmyBmny
        14
    HmyBmny  
       2018-06-18 22:52:05 +08:00 via iPhone
    @nine99 #10 这个应该是目前回复中最简单的
    shilielin
        15
    shilielin  
       2018-06-18 22:56:08 +08:00 via iPad
    可以试一下 flask,自己写一个 API 路由
    SlipStupig
        16
    SlipStupig  
       2018-06-18 23:14:33 +08:00
    这个不就是 crate.io
    agagega
        17
    agagega  
       2018-06-19 00:45:44 +08:00   ❤️ 1
    Postgrest 了解一下
    congeec
        18
    congeec  
       2018-06-19 07:51:55 +08:00 via iPhone
    Swagger-codegen 支持好几种后端语言,前端框架覆盖更广
    顺带 swagger 还能自动生成文档
    yureng
        19
    yureng  
       2018-06-19 07:58:30 +08:00 via Android
    grewer
        20
    grewer  
       2018-06-19 09:19:45 +08:00   ❤️ 1
    ryuzaki113
        21
    ryuzaki113  
       2018-06-19 15:43:54 +08:00
    TommyLemon
        22
    TommyLemon  
       2018-06-26 17:24:11 +08:00
    @lhx2008 所以就有了 APIJSON,支持自动化的复杂查询和简单增删改查,通过自动化的 权限管理 和 预编译及白名单机制 保证安全性。还有完整的 demo、文档、调试工具和视频教程,欢迎使用^_^
    https://github.com/TommyLemon/APIJSON
    TommyLemon
        23
    TommyLemon  
       2018-06-26 17:32:37 +08:00
    推荐 APIJSON, 后端接口和文档自动化,前端(客户端) 定制返回 JSON 的数据和结构。


    ### 1.单表查询
    ```js
    {
    "User": {
    "sex": 1
    }
    }
    ```
    自动转换为
    ```sql
    SELECT * FROM User WHERE sex = '1' LIMIT 0, 1
    ```


    ### 2.数组查询
    ```js
    {
    "[]": {
    "count": 10,
    "page": 1,
    "User": {
    "sex": 1
    }
    }
    }
    ```
    自动转换为
    ```sql
    SELECT * FROM User WHERE sex = '1' LIMIT 10, 20
    ```

    ### 3.应用场景示例:


    #### 获取用户
    请求:
    <pre><code class="language-json">{
    "User":{
     }
    }
    </code></pre>

    [点击这里测试]( http://apijson.cn:8080/get/{"User":{}})

    返回:
    <pre><code class="language-json">{
    "User":{
    "id":38710,
    "sex":0,
    "name":"TommyLemon",
    "tag":"Android&Java",
    "head":"http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
    "date":1485948110000,
    "pictureList":[
    "http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
    "http://common.cnblogs.com/images/icon_weibo_24.png"
    ]
    },
    "code":200,
    "msg":"success"
    }
    </code></pre>

    <br />

    #### 获取用户列表
    请求:
    <pre><code class="language-json">{
    "[]":{
      "count":3,             //只要 3 个
      "User":{
        "@column":"id,name" //只要 id,name 这两个字段
      }
    }
    }
    </code></pre>

    [点击这里测试]( http://apijson.cn:8080/get/{"[]":{"count":3,"User":{"@column":"id,name"}}})

    返回:
    <pre><code class="language-json">{
    "[]":[
    {
    "User":{
    "id":38710,
    "name":"TommyLemon"
    }
    },
    {
    "User":{
    "id":70793,
    "name":"Strong"
    }
    },
    {
    "User":{
    "id":82001,
    "name":"Android"
    }
    }
    ],
    "code":200,
    "msg":"success"
    }
    </code></pre>

    <br />

    #### 获取动态及发布者用户
    请求:
    <pre><code class="language-json">{
    "Moment":{
    },
    "User":{
    "id@":"Moment/userId" //User.id = Moment.userId
    }
    }
    </code></pre>

    [点击这里测试]( http://apijson.cn:8080/get/{"Moment":{},"User":{"id@":"Moment%252FuserId"}})

    返回:
    <pre><code class="language-json">{
    "Moment":{
    "id":12,
    "userId":70793,
    "date":"2017-02-08 16:06:11.0",
    "content":"1111534034"
    },
    "User":{
    "id":70793,
    "sex":0,
    "name":"Strong",
    "tag":"djdj",
    "head":"http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000",
    "contactIdList":[
    38710,
    82002
    ],
    "date":"2017-02-01 19:21:50.0"
    },
    "code":200,
    "msg":"success"
    }
    </code></pre>

    <br />

    #### 获取类似微信朋友圈的动态列表
    请求:
    <pre><code class="language-json">{
    "[]":{ //请求一个数组
    "page":0,                     //数组条件
    "count":2,
    "Moment":{                     //请求一个名为 Moment 的对象
    "content$":"%a%"          //对象条件,搜索 content 中包含 a 的动态
    },
    "User":{
        "id@":"/Moment/userId",       //User.id = Moment.userId 缺省引用赋值路径,从所处容器的父容器路径开始
    "@column":"id,name,head"     //指定返回字段
    },
    "Comment[]":{ //请求一个名为 Comment 的数组,并去除 Comment 包装
    "count":2,
    "Comment":{
    "momentId@":"[]/Moment/id" //Comment.momentId = Moment.id 完整引用赋值路径
    }
    }
    }
    }
    </code></pre>

    [点击这里测试]( http://apijson.cn:8080/get/{"[]":{"page":0,"count":2,"Moment":{"content$":"%2525a%2525"},"User":{"id@":"%252FMoment%252FuserId","@column":"id,name,head"},"Comment[]":{"count":2,"Comment":{"momentId@":"[]%252FMoment%252Fid"}}}})

    返回:
    <pre><code class="language-json">{
    "[]":[
    {
    "Moment":{
    "id":15,
    "userId":70793,
    "date":1486541171000,
    "content":"APIJSON is a JSON Transmission Structure Protocol …",
    "praiseUserIdList":[
    82055,
    82002,
    82001
    ],
    "pictureList":[
    "http://static.oschina.net/uploads/user/1218/2437072_100.jpg?t=1461076033000",
    "http://common.cnblogs.com/images/icon_weibo_24.png"
    ]
    },
    "User":{
    "id":70793,
    "name":"Strong",
    "head":"http://static.oschina.net/uploads/user/585/1170143_50.jpg?t=1390226446000"
    },
    "Comment[]":[
    {
    "id":176,
    "toId":166,
    "userId":38710,
    "momentId":15,
    "date":1490444883000,
    "content":"thank you"
    },
    {
    "id":1490863469638,
    "toId":0,
    "userId":82002,
    "momentId":15,
    "date":1490863469000,
    "content":"Just do it"
    }
    ]
    },
    {
    "Moment":{
    "id":58,
    "userId":90814,
    "date":1485947671000,
    "content":"This is a Content...-435",
    "praiseUserIdList":[
    38710,
    82003,
    82005,
    93793,
    82006,
    82044,
    82001
    ],
    "pictureList":[
    "http://static.oschina.net/uploads/img/201604/22172507_aMmH.jpg"
    ]
    },
    "User":{
    "id":90814,
    "name":7,
    "head":"http://static.oschina.net/uploads/user/51/102723_50.jpg?t=1449212504000"
    },
    "Comment[]":[
    {
    "id":13,
    "toId":0,
    "userId":82005,
    "momentId":58,
    "date":1485948050000,
    "content":"This is a Content...-13"
    },
    {
    "id":77,
    "toId":13,
    "userId":93793,
    "momentId":58,
    "date":1485948050000,
    "content":"This is a Content...-77"
    }
    ]
    }
    ],
    "code":200,
    "msg":"success"
    }
    </code></pre>


    <br />

    [在线测试]( http://apijson.org)


    创作不易,您的支持是对作者的最大鼓励,右上角点 Star 支持下吧,谢谢 ^_^
    TommyLemon
        24
    TommyLemon  
       2018-06-26 17:33:36 +08:00
    看样子这回复,既不支持 markdown,也不支持富文本啊
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1000 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 19:48 · PVG 03:48 · LAX 12:48 · JFK 15:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.