json-server的关系图谱

json-server是非常好用的一款模拟REST API的工具,文档也很详细和全面.
详情:json-server
而其中的关系图谱是它非常强大的一个功能,可以非常方便实现多个路由之间关联数据的获取。

示例数据

官网上对于关系图谱的案例非常好,我这里在它示例的基础上稍以改进,进行说明,首先我这里编写了一个原始数据,db.json:

{
"posts": [
{ "id": 1, "title": "post的第一个title", "author": "typicode" },
{ "id": 2, "title": "post的第二个title", "author": "tangcaiye" }
],
"comments": [
{ "id": 1, "body": "some comment1111", "postId": 2 },
{ "id": 2, "body": "some comment2222", "postId": 1 }
],
"profile": { "name": "typicode" }
}

这里对这个db.json数据内容解释一下:
这个json文件中postscomments是有关联的,他们的关系通过的就是commentspostId属性,postId对应的就是postsid
比如commentspostId:2的对象关联的就是posts下的{ "id": 2, "title": "post的第二个title", "author": "tangcaiye" }

_embed

json-server中的_embed就是用来获取包含下级资源的数据.
比如我json-server服务器的端口号是8081,然后我的请求路径是http://localhost:8081/posts/2?_embed=comments
这个路径获取的就是posts下的id为2的数据和它关联的comments的数据:{ "id": 1, "body": "some comment1111", "postId": 2 }
输出结果为:

{
"id": 2,
"title": "post的第二个title",
"author": "tangcaiye",
"comments": [
{
"id": 1,
"body": "some comment1111",
"postId": 2
}
]
}

_expand

如果理解了_embed那么_expand它也就很轻松了,_expand获取的是包含上级资源的数据:
路径:http://localhost:8081/comments/2?_expand=post
上面这个路径获取的就是commentsid为2的数据和它关联的上级资源post,也就是posts下的:
{ "id": 1, "title": "post的第一个title", "author": "typicode" }
输出结果:

{
"id": 2,
"body": "some comment2222",
"postId": 1,
"post": {
"id": 1,
"title": "post的第一个title",
"author": "typicode"
}
}

只获取下级资源

有时候我们可能想只获取下级资源,可以通过:
路径:http://localhost:8081/posts/2/comments
上面这个路径就是获取postsid:2所关联的comments数据:
返回结果:

 
[
{
"id": 1,
"body": "some comment1111",
"postId": 2
}
]

json-server的关系图谱详解(Relationships)的更多相关文章

  1. CentOS6.5下VNC Server远程桌面配置详解

    参考文献: (总结)CentOS Linux下VNC Server远程桌面配置详解 远程桌面连接工具VNC——license Key 我的下载地址为 太平洋下载 VNC连接黑屏的问题 centos 6 ...

  2. SQL Server 执行计划操作符详解(3)——计算标量(Compute Scalar)

    接上文:SQL Server 执行计划操作符详解(2)--串联(Concatenation ) 前言: 前面两篇文章介绍了关于串联(Concatenation)和断言(Assert)操作符,本文介绍第 ...

  3. SQL Server 执行计划操作符详解(2)——串联(Concatenation )

    本文接上文:SQL Server 执行计划操作符详解(1)--断言(Assert) 前言: 根据计划,本文开始讲述另外一个操作符串联(Concatenation),读者可以根据这个词(中英文均可)先幻 ...

  4. JWT(Json web token)认证详解

    JWT(Json web token)认证详解 什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该to ...

  5. 引用 Windows Server 2003 FTP服务器配置详解

    引用 昆神之星 的 Windows Server 2003 FTP服务器配置详解 1.FTP文件传输协议,主要用于计算机之间文件传输,是互联网上仅次于www的第二大服务.本文主要演示如何在Window ...

  6. SQL Server中的锁 详解 nolock,rowlock,tablock,xlock,paglock

    摘自: http://www.myexception.cn/sql-server/385562.html 高手进 锁 nolock,rowlock,tablock,xlock,paglock 锁 no ...

  7. SQL Server DBA工作内容详解

    在Microsoft SQL Server 2008系统中,数据库管理员(Database Administration,简称为DBA)是最重要的角色.DBA的工作目标就是确保Microsoft SQ ...

  8. python 序列化及其相关模块(json,pickle,shelve,xml)详解

    什么是序列化对象? 我们把对象(变量)从内存中编程可存储或传输的过程称之为序列化,在python中称为pickle,其他语言称之为serialization ,marshalling ,flatter ...

  9. 创建Oracle数据库、数据库名与实例名与SID之间的关系(图文详解)

    分类: Oracle(9) 版权声明:转载请注明出处 JmilkFan_范桂飓:http://blog.csdn.net/jmilk 目录(?)[+] 目录 目录 软件环境 前言 安装Oracle监听 ...

随机推荐

  1. iphone6,iphone6 plus适配,旧项目出现黑线问题

    问题:可能开始适配iPhone6和iPhone6 plus的朋友很快就发现,模块器头部和底部会出线一条黑线.但是在其他模拟器完全没有问题.程序也能正常跑.如下图 很清楚的看到头部有一条黑线. 解决办法 ...

  2. jquery 时间戳转日期

    搜了一下发现这个时间戳转时间的代码很好用,附上实践的代码 结果如下 / * * 时间戳转日期 * @param timestamp * @returns {*} */ function timesta ...

  3. [gulp]Cannot find module 'orchestrator'

    从github 将项目 clone到本地后,运行gulp 启动项目时,出现这个问题的原因是: 1.clone 项目连同 nodemodules目录也一起下载到本地. 解决方式: 1.从本地删除node ...

  4. 【2018百度之星资格赛】 A 问卷调查 - 位运算&动规

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=6344 参考博客:在此感谢http://www.cnblogs.com/LQLlulu/p/941923 ...

  5. 洛谷 P1280 尼克的任务 (线性DP)

    题意概括 线性资源分配的问题,因为空闲的时间大小看后面的时间(反正感觉这个就是个套路)所以从后往前DP. 转移方程 如果当前时刻没有工作 f[i]=f[i+1]+1 如果当前时刻有工作 f[i]=ma ...

  6. HDU - 6446 Tree and Permutation

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6446 本题是一个树上的问题——DFS. 一棵N个结点的树,其结点为1~N.树具有N-1条边,每一条边具有 ...

  7. juruo的刷题&博文祭

    Nothing--- 祭我bzoj过66题,博文240篇(.弱.) 自娱(愚)自乐下-

  8. How to put username &password in MongoDB(Security&Authentication)?(配置用户认证在MongoDB)

    Default do not need username and password authenticate when access mongoDB ,I want to set up the use ...

  9. JS中showModalDialog 详细使用方法

    基本介绍: showModalDialog() (IE 4+ 支持) showModelessDialog() (IE 5+ 支持) window.showModalDialog() 方法用来创建一个 ...

  10. Contemplation! Algebra 矩阵快速幂

    Given the value of a+b and ab you will have to find the value of a n + b n Input The input file cont ...