postgraphile简单使用
2019-02-23 本文已影响0人
桂老七
postgraphile使用
1. 启动服务
本机安装的话:
postgraphile -c postgres://postgres:123456@localhost:5432/postgres
postgraphile -c postgres://用户名:密码@主机名:端口/数据库名
在express服务中:
const express = require("express");
const { postgraphile } = require("postgraphile");
const app = express();
app.use(postgraphile(process.env.DATABASE_URL || "postgres:///"));
app.listen(process.env.PORT || 3000);
2.查询操作
语法:复数按英文单词来的;不支持_分隔符,需要转义为大写;
如:iot_role需要写成iotRole的形式;
用 表名+By+字段名 查找某一项数据
query{
iotRoleByRoleId(roleId:"a7e3b1bc-26c0-4760-ac5f-1f2288ed5f5b"){
roleId
roleName
roleStatus
}
}
all+表名(复数) 配合filter进行筛选查询
需要引入插件:
const PostGraphileConnectionFilterPlugin = require("postgraphile-plugin-connection-filter");
app.use(postgraphile(pgConfig, 'public', {
watchPg: true,
pgDefaultRole: 'postgres',
dynamicJson: true,
setofFunctionsContainNulls: true,
graphiql: false,
appendPlugins: [PostGraphileConnectionFilterPlugin],
enableCors: true,
}));
query {
allIotRoles(
first:1
offset:1
filter: {
roleName: {
includesInsensitive: "${filter.roleName ? filter.roleName : ''}"
}
}
) {
totalCount
nodes {
roleId
roleName
roleStatus
}
}
}
一些参数:
offset:起始位置
first:数据长度
totalCount:符合条件的数据总条数
filter里面用的:
equalTo //等于
includesInsensitive //模糊查找
greaterThanOrEqualTo //大于等于
in //存在于
如:
allPosts(filter: {
authorId: { in: [1, 2] }
})
3.通过外键多表联查
(子查父[外查主<唯一>]和父查子[主查外<多个>])
子查父,直接嵌套写
query{
allIotRepairs{
nodes{
id
reportPerson
iotUserByReportPerson{
username
}
}
}
}
reportPerson外鍵到iotUser表,從表中取username
父查子,用filter(没用到关联性)或者嵌套写法(用到了关联)
如:查某条产线上的所有设备,先在产线表查产线信息(父),再嵌套的查设备表中所有产线外键指向该产线id
的所有设备(子)
query{
iotProductionLineById(id:"55eb6984-8bdc-44a6-bcdb-663c5a661667"){
name
iotDevicesByProductionLineId{
nodes{
id
name
}
}
}
}
4. 对同一表一次进行两轮查询(父查子),重命名错误
如:对字典表同时查状态Status和类型Type的子类型有哪些,会报结果重名错误,需要手动替换返回名
下面这样会报重名错误
query {
iotDictionaryById(id: "534e8935-9ea9-40fc-9929-0926b4bdbe5b") {
iotDictionariesByParentId {
nodes {
id: code
name
}
}
}
iotDictionaryById(id: "245555cc-158d-4334-a6fb-37a136b08a6c") {
iotDictionariesByParentId {
nodes {
id: code
name
}
}
}
}
需要替换名字:
query {
status: iotDictionaryById(id: "534e8935-9ea9-40fc-9929-0926b4bdbe5b") {
iotDictionariesByParentId {
nodes {
id: code
name
}
}
}
type: iotDictionaryById(id: "245555cc-158d-4334-a6fb-37a136b08a6c") {
iotDictionariesByParentId {
nodes {
id: code
name
}
}
}
}
5. 增加
mutation{
createIotRepair(input:{
iotRepair:{
reportPerson:"${reportPerson}",
reportRemarks:"${reportRemarks}",
reportPhoto:${JSON.stringify(reportPhoto)},
repairPerson:${JSON.stringify(repairPersonArray)},
deviceId:"${deviceId}",
status:1
}
}){
iotRepair{
id,
reportRemarks
}
}
}
6. 删除
mutation{
deleteIotPlanById(input:{
id:"${data}"
}){
iotPlan{
id
}
}
}
7.修改
mutation{
updateIotMaintenanceById(
input:{
id:"${id}"
iotMaintenancePatch:{
items: "${JSON.stringify(items).replace(/\"/g,'\\"')}"
status:${status}
}
}
){
clientMutationId
}
}
5. 变量的使用(存json数据的时候)
//需要在()里面线声明变量的类型
query ($items: UUID!){
iotUserById(id:$items){
username
}
}
//json形式
{
"items":"966ab989-dce5-4669-b636-7bebf61fd8dc"
}