Add config driven presets for insert, update and upsert
This commit is contained in:
parent
8ae7210e70
commit
b87ba1fcd0
@ -169,7 +169,7 @@ roles:
|
||||
insert:
|
||||
filters: ["{ user_id: { eq: $user_id } }"]
|
||||
columns: ["id", "name", "description" ]
|
||||
set:
|
||||
presets:
|
||||
- created_at: "now"
|
||||
|
||||
update:
|
||||
@ -177,7 +177,7 @@ roles:
|
||||
columns:
|
||||
- id
|
||||
- name
|
||||
set:
|
||||
presets:
|
||||
- updated_at: "now"
|
||||
|
||||
delete:
|
||||
|
@ -1,5 +1,10 @@
|
||||
package qcode
|
||||
|
||||
import (
|
||||
"sort"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Config struct {
|
||||
Blocklist []string
|
||||
KeepArgs bool
|
||||
@ -15,13 +20,13 @@ type QueryConfig struct {
|
||||
type InsertConfig struct {
|
||||
Filters []string
|
||||
Columns []string
|
||||
Set map[string]string
|
||||
Presets map[string]string
|
||||
}
|
||||
|
||||
type UpdateConfig struct {
|
||||
Filters []string
|
||||
Columns []string
|
||||
Set map[string]string
|
||||
Presets map[string]string
|
||||
}
|
||||
|
||||
type DeleteConfig struct {
|
||||
@ -49,13 +54,15 @@ type trval struct {
|
||||
insert struct {
|
||||
fil *Exp
|
||||
cols map[string]struct{}
|
||||
set map[string]string
|
||||
psmap map[string]string
|
||||
pslist []string
|
||||
}
|
||||
|
||||
update struct {
|
||||
fil *Exp
|
||||
cols map[string]struct{}
|
||||
set map[string]string
|
||||
psmap map[string]string
|
||||
pslist []string
|
||||
}
|
||||
|
||||
delete struct {
|
||||
@ -97,3 +104,20 @@ func (trv *trval) filter(qt QType) *Exp {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func listToMap(list []string) map[string]struct{} {
|
||||
m := make(map[string]struct{}, len(list))
|
||||
for i := range list {
|
||||
m[strings.ToLower(list[i])] = struct{}{}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
func mapToList(m map[string]string) []string {
|
||||
list := []string{}
|
||||
for k, _ := range m {
|
||||
list = append(list, strings.ToLower(k))
|
||||
}
|
||||
sort.Strings(list)
|
||||
return list
|
||||
}
|
||||
|
@ -46,6 +46,8 @@ type Select struct {
|
||||
Children []int32
|
||||
Functions bool
|
||||
Allowed map[string]struct{}
|
||||
PresetMap map[string]string
|
||||
PresetList []string
|
||||
}
|
||||
|
||||
type Column struct {
|
||||
@ -182,14 +184,6 @@ func (com *Compiler) AddRole(role, table string, trc TRConfig) error {
|
||||
var err error
|
||||
trv := &trval{}
|
||||
|
||||
toMap := func(cols []string) map[string]struct{} {
|
||||
m := make(map[string]struct{}, len(cols))
|
||||
for i := range cols {
|
||||
m[strings.ToLower(cols[i])] = struct{}{}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
// query config
|
||||
trv.query.fil, err = compileFilter(trc.Query.Filters)
|
||||
if err != nil {
|
||||
@ -198,27 +192,30 @@ func (com *Compiler) AddRole(role, table string, trc TRConfig) error {
|
||||
if trc.Query.Limit > 0 {
|
||||
trv.query.limit = strconv.Itoa(trc.Query.Limit)
|
||||
}
|
||||
trv.query.cols = toMap(trc.Query.Columns)
|
||||
trv.query.cols = listToMap(trc.Query.Columns)
|
||||
trv.query.disable.funcs = trc.Query.DisableFunctions
|
||||
|
||||
// insert config
|
||||
if trv.insert.fil, err = compileFilter(trc.Insert.Filters); err != nil {
|
||||
return err
|
||||
}
|
||||
trv.insert.cols = toMap(trc.Insert.Columns)
|
||||
trv.insert.cols = listToMap(trc.Insert.Columns)
|
||||
trv.insert.psmap = trc.Insert.Presets
|
||||
trv.insert.pslist = mapToList(trv.insert.psmap)
|
||||
|
||||
// update config
|
||||
if trv.update.fil, err = compileFilter(trc.Update.Filters); err != nil {
|
||||
return err
|
||||
}
|
||||
trv.insert.cols = toMap(trc.Insert.Columns)
|
||||
trv.insert.set = trc.Insert.Set
|
||||
trv.update.cols = listToMap(trc.Update.Columns)
|
||||
trv.update.psmap = trc.Update.Presets
|
||||
trv.update.pslist = mapToList(trv.update.psmap)
|
||||
|
||||
// delete config
|
||||
if trv.delete.fil, err = compileFilter(trc.Delete.Filters); err != nil {
|
||||
return err
|
||||
}
|
||||
trv.delete.cols = toMap(trc.Delete.Columns)
|
||||
trv.delete.cols = listToMap(trc.Delete.Columns)
|
||||
|
||||
singular := flect.Singularize(table)
|
||||
plural := flect.Pluralize(table)
|
||||
@ -302,12 +299,18 @@ func (com *Compiler) compileQuery(qc *QCode, op *Operation, role string) error {
|
||||
})
|
||||
s := &selects[(len(selects) - 1)]
|
||||
|
||||
if action == QTQuery {
|
||||
switch action {
|
||||
case QTQuery:
|
||||
s.Functions = !trv.query.disable.funcs
|
||||
|
||||
if len(trv.query.limit) != 0 {
|
||||
s.Paging.Limit = trv.query.limit
|
||||
}
|
||||
|
||||
case QTInsert:
|
||||
s.PresetMap = trv.insert.psmap
|
||||
s.PresetList = trv.insert.pslist
|
||||
|
||||
case QTUpdate:
|
||||
s.PresetMap = trv.update.psmap
|
||||
s.PresetList = trv.update.pslist
|
||||
}
|
||||
|
||||
if s.ID != 0 {
|
||||
|
@ -117,14 +117,14 @@ type configRole struct {
|
||||
Insert struct {
|
||||
Filters []string
|
||||
Columns []string
|
||||
Set map[string]string
|
||||
Presets map[string]string
|
||||
Block bool
|
||||
}
|
||||
|
||||
Update struct {
|
||||
Filters []string
|
||||
Columns []string
|
||||
Set map[string]string
|
||||
Presets map[string]string
|
||||
Block bool
|
||||
}
|
||||
|
||||
|
@ -48,7 +48,7 @@ func initCompilers(c *config) (*qcode.Compiler, *psql.Compiler, error) {
|
||||
insert := qcode.InsertConfig{
|
||||
Filters: t.Insert.Filters,
|
||||
Columns: t.Insert.Columns,
|
||||
Set: t.Insert.Set,
|
||||
Presets: t.Insert.Presets,
|
||||
}
|
||||
|
||||
if t.Query.Block {
|
||||
@ -58,7 +58,7 @@ func initCompilers(c *config) (*qcode.Compiler, *psql.Compiler, error) {
|
||||
update := qcode.UpdateConfig{
|
||||
Filters: t.Insert.Filters,
|
||||
Columns: t.Insert.Columns,
|
||||
Set: t.Insert.Set,
|
||||
Presets: t.Insert.Presets,
|
||||
}
|
||||
|
||||
if t.Query.Block {
|
||||
|
10
tmpl/dev.yml
10
tmpl/dev.yml
@ -1,4 +1,4 @@
|
||||
app_name: "Super Graph Development"
|
||||
app_name: "{{app_name}} Development"
|
||||
host_port: 0.0.0.0:8080
|
||||
web_ui: true
|
||||
|
||||
@ -49,7 +49,7 @@ migrations_path: ./config/migrations
|
||||
auth:
|
||||
# Can be 'rails' or 'jwt'
|
||||
type: rails
|
||||
cookie: _app_session
|
||||
cookie: _{{app_name_slug}}_session
|
||||
|
||||
# Comment this out if you want to disable setting
|
||||
# the user_id via a header. Good for testing
|
||||
@ -84,7 +84,7 @@ database:
|
||||
type: postgres
|
||||
host: db
|
||||
port: 5432
|
||||
dbname: app_development
|
||||
dbname: {{app_name_slug}}_development
|
||||
user: postgres
|
||||
password: ''
|
||||
|
||||
@ -169,7 +169,7 @@ roles:
|
||||
insert:
|
||||
filters: ["{ user_id: { eq: $user_id } }"]
|
||||
columns: ["id", "name", "description" ]
|
||||
set:
|
||||
presets:
|
||||
- created_at: "now"
|
||||
|
||||
update:
|
||||
@ -177,7 +177,7 @@ roles:
|
||||
columns:
|
||||
- id
|
||||
- name
|
||||
set:
|
||||
presets:
|
||||
- updated_at: "now"
|
||||
|
||||
delete:
|
||||
|
@ -2,7 +2,7 @@
|
||||
# so I only need to overwrite some values
|
||||
inherit: dev
|
||||
|
||||
app_name: "Super Graph Production"
|
||||
app_name: "{{app_name}} Production"
|
||||
host_port: 0.0.0.0:8080
|
||||
web_ui: false
|
||||
|
||||
@ -49,7 +49,7 @@ enable_tracing: true
|
||||
auth:
|
||||
# Can be 'rails' or 'jwt'
|
||||
type: rails
|
||||
cookie: _app_session
|
||||
cookie: _{{app_name_slug}}_session
|
||||
|
||||
rails:
|
||||
# Rails version this is used for reading the
|
||||
|
Loading…
Reference in New Issue
Block a user