美文网首页
postgraphile简单使用

postgraphile简单使用

作者: 桂老七 | 来源:发表于2019-02-23 11:35 被阅读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,
}));

使用filter的语法

    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"
}

相关文章

  • postgraphile简单使用

    postgraphile使用 1. 启动服务 本机安装的话: 在express服务中: 2.查询操作 语法:复数按...

  • 简单使用

    创建模型 过滤器 我们有一些字段和我们想让用户筛选的基础上 名称、价格或release_date。 我们创建一个 ...

  • gorange

    数组中简单使用 map中简单使用

  • 简单使用使用kaggle

    向我这样的条件不好的可以考虑借助云gpu来加速训练,借助kaggle可以在kaggle服务器上训练数据,kaggl...

  • 零碎的小程序笔记

    目录 template的简单使用WXS的简单使用npm的简单使用倒计时js的实现wx:for的使用一些js方法记录...

  • 命令行的简单使用

    Git命令行的简单使用,仅供自己使用 pod命令行的简单使用

  • 单元测试和OCMock

    OCMock使用一、安装及简单使用:使用Cocoapod引入:pod 'OCMock' 简单使用:新建一个单元测试...

  • Alamofire类似AFNetworking的简单使用和封装

    简单的使用。简单的使用。简单的使用。注定该文弱鸡一个,求拍砖。 一、介绍 Alamofire(Swift)的前身是...

  • Android ViewPager 使用总结

    ViewPager 简单使用 ViewPager + PagerAdapter简单的 View 可以使用这个实现,...

  • vuex简单简单使用记录

    1、Vuex有啥用(非官方解释)举例,组件a b 使用了同一个数据源count,当操作a的时候count++,同时...

网友评论

      本文标题:postgraphile简单使用

      本文链接:https://www.haomeiwen.com/subject/haiuyqtx.html