我的測試環境使用docker 進行的安裝
基本安裝
- pg 數據庫
我使用的是timesacledb 復制集版本,參考https://github.com/timescale/streaming-replication-docker
./start_containers.sh
- postgraphile(postgraphql)
npm install -g postgraphile
基本使用
- 創建數據庫
psql -U postgres -h localhost
create database appdemo
- 啟動postgraphile
使用watch 參數可以自動更新api
postgraphile -c "postgres://postgres:postgres@localhost:5432/appdemo" --watch
參考結果
- 使用graphql playground訪問
- 創建表
create table person (
id serial primary key,
name varchar not null,
about text,
email varchar not null unique,
created_at timestamp default current_timestamp
);
自動更新結果
- 數據查詢操作
query {
allPeople{
nodes {
id
name
}
}
}
- 添加數據
mutation {
createPerson(input:{
clientMutationId:"demoappdemo",
person:{
name:"dalong",
about:"demoappdemo",
email:"1141591465@qq.com",
createdAt:"2018-07-10"
}
}){
person {
name
about
email
}
}
}
與prisma比較
主要是擴展行行,prisma是多數據庫支持的,基於nodejs 開發模式,同時生態不錯,有好多相關的組件,使用上主要
以模型優先的模式,postgraphile 相對就簡單了,只需要安裝基本上不用寫代碼(當然角色,權限的問題,依然需要處理,
而且在ID的處理上postgraphile有點模糊,還有待仔細閱讀。
詳細的比較后邊會有介紹
參考資料
https://www.graphile.org/postgraphile/introduction/
https://www.prisma.io/