嵌套的嵌入资源



我是postgREST的新手。我已经设置好了,它和我的数据库配合得很好。我正在浏览文档,我认为我可以使用资源嵌入,但我不知道如何使它以嵌套的方式工作。

我的模式有类似于以下的表:

create table ta (
a_id integer primary key,
a_desc varchar(50)
);
create table tb (
b_id integer primary key,
a_id integer not null,
b_desc varchar(50),
constraint tb_fk1 foreign key (a_id) references ta(a_id)
);
create table tc (
c_id integer primary key,
b_id integer not null,
c_desc varchar(50),
constraint tc_fk1 foreign key (b_id) references tb(b_id)
);
insert into ta values (1, 'a1');
insert into tb values (1, 1, 'b1');
insert into tb values (2, 1, 'b2');
insert into tc values (1, 1, 'c1');
insert into tc values (2, 1, 'c2');
insert into tc values (3, 2, 'c3');
insert into tc values (4, 2, 'c4');

当我选择ta an tb:时,资源嵌入有效

localhost:3000/ta?select=*,tb(*)
[
{
"a_id": 1,
"a_desc": "a1",
"tb": [
{
"b_id": 1,
"a_id": 1,
"b_desc": "b1"
},
{
"b_id": 2,
"a_id": 1,
"b_desc": "b2"
}
]
}
]

它也适用于tb和tc:

localhost:3000/tb?select=*,tc(*)
[
{
"b_id": 1,
"a_id": 1,
"b_desc": "b1",
"tc": [
{
"c_id": 1,
"b_id": 1,
"c_desc": "c1"
},
{
"c_id": 2,
"b_id": 1,
"c_desc": "c2"
}
]
},
{
"b_id": 2,
"a_id": 1,
"b_desc": "b2",
"tc": [
{
"c_id": 3,
"b_id": 2,
"c_desc": "c3"
},
{
"c_id": 4,
"b_id": 2,
"c_desc": "c4"
}
]
}
]

但我不知道如何使它从ta到tc工作,有点像是将两个查询组合在一起。

有人知道我是怎么做到的吗?最好使用查询字符串,但使用视图或存储过程也可以。

提前感谢您在这方面的帮助。

PS:使用Potstgres 12和postgREST 7

对于嵌套资源嵌入,您可以执行:

GET localhost:3000/ta?select=*,tb(*,tc(*))

最新更新