diff --git a/config/dev.yml b/config/dev.yml index d279b67..497fe0a 100644 --- a/config/dev.yml +++ b/config/dev.yml @@ -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: diff --git a/qcode/config.go b/qcode/config.go index c68a3d1..c076b7f 100644 --- a/qcode/config.go +++ b/qcode/config.go @@ -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 { @@ -47,15 +52,17 @@ type trval struct { } insert struct { - fil *Exp - cols map[string]struct{} - set map[string]string + fil *Exp + cols map[string]struct{} + psmap map[string]string + pslist []string } update struct { - fil *Exp - cols map[string]struct{} - set map[string]string + fil *Exp + cols map[string]struct{} + 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 +} diff --git a/qcode/qcode.go b/qcode/qcode.go index 0451e2a..31874a1 100644 --- a/qcode/qcode.go +++ b/qcode/qcode.go @@ -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 + s.Paging.Limit = trv.query.limit - 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 { diff --git a/serv/config.go b/serv/config.go index d847304..f67e74a 100644 --- a/serv/config.go +++ b/serv/config.go @@ -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 } diff --git a/serv/serv.go b/serv/serv.go index e61795c..3609daf 100644 --- a/serv/serv.go +++ b/serv/serv.go @@ -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 { diff --git a/tmpl/dev.yml b/tmpl/dev.yml index ca68dcf..64170a1 100644 --- a/tmpl/dev.yml +++ b/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: diff --git a/tmpl/prod.yml b/tmpl/prod.yml index 8365fb6..a63d146 100644 --- a/tmpl/prod.yml +++ b/tmpl/prod.yml @@ -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