Skip to content

Commit

Permalink
better triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
autom8ter committed Dec 22, 2020
1 parent 50796b6 commit c1474ea
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 18 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ mutation {

### Triggers

- triggers may be used to automatically mutate data before it is commited to the database
- triggers may be used to automatically mutate the attributes of documents/connections before they are commited to the database
- this is useful for automatically annotating your data without having to make additional client-side requests

#### Trigger Examples
Expand All @@ -361,10 +361,10 @@ mutation {
{
name: "createdAt"
gtype: "*"
expression: "!has(this.attributes.created_at)"
expression: "!has(this.created_at)"
trigger: "{'created_at': now}"
target_docs: true
target_connections: false
target_connections: true
},
]
})
Expand Down
49 changes: 35 additions & 14 deletions database/graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,9 +470,12 @@ func (g *Graph) CreateDocs(ctx context.Context, constructors *apipb.DocConstruct
}
g.rangeTriggers(func(a *trigger) bool {
if a.trigger.GetTargetDocs() && (doc.GetRef().GetGtype() == a.trigger.GetGtype() || a.trigger.GetGtype() == apipb.Any) {
data := doc.AsMap()
data := doc.GetAttributes().AsMap()
if data == nil {
data = map[string]interface{}{}
}
err := a.evalTrigger.Trigger(data)
if err == nil && len(data) > 0 {
if err == nil {
for k, v := range data {
val, _ := structpb.NewValue(v)
doc.GetAttributes().GetFields()[k] = val
Expand Down Expand Up @@ -587,9 +590,12 @@ func (g *Graph) CreateConnections(ctx context.Context, constructors *apipb.Conne
}
g.rangeTriggers(func(a *trigger) bool {
if a.trigger.GetTargetConnections() && (c.GetRef().GetGtype() == a.trigger.GetGtype() || a.trigger.GetGtype() == apipb.Any) {
data := c.AsMap()
data := c.GetAttributes().AsMap()
if data == nil {
data = map[string]interface{}{}
}
err := a.evalTrigger.Trigger(data)
if err == nil && len(data) > 0 {
if err == nil {
for k, v := range data {
val, _ := structpb.NewValue(v)
c.GetAttributes().GetFields()[k] = val
Expand Down Expand Up @@ -818,9 +824,12 @@ func (n *Graph) EditDoc(ctx context.Context, value *apipb.Edit) (*apipb.Doc, err
}
n.rangeTriggers(func(a *trigger) bool {
if a.trigger.GetTargetDocs() && (setDoc.GetRef().GetGtype() == a.trigger.GetGtype() || a.trigger.GetGtype() == apipb.Any) {
data := setDoc.AsMap()
data := setDoc.GetAttributes().AsMap()
if data == nil {
data = map[string]interface{}{}
}
err := a.evalTrigger.Trigger(data)
if err == nil && len(data) > 0 {
if err == nil {
for k, v := range data {
val, _ := structpb.NewValue(v)
setDoc.GetAttributes().GetFields()[k] = val
Expand Down Expand Up @@ -891,9 +900,12 @@ func (n *Graph) EditDocs(ctx context.Context, patch *apipb.EditFilter) (*apipb.D
}
n.rangeTriggers(func(a *trigger) bool {
if a.trigger.GetTargetDocs() && (setDoc.GetRef().GetGtype() == a.trigger.GetGtype() || a.trigger.GetGtype() == apipb.Any) {
data := setDoc.AsMap()
data := setDoc.GetAttributes().AsMap()
if data == nil {
data = map[string]interface{}{}
}
err := a.evalTrigger.Trigger(data)
if err == nil && len(data) > 0 {
if err == nil {
for k, v := range data {
val, _ := structpb.NewValue(v)
setDoc.GetAttributes().GetFields()[k] = val
Expand Down Expand Up @@ -1184,9 +1196,12 @@ func (n *Graph) EditConnection(ctx context.Context, value *apipb.Edit) (*apipb.C
n.rangeTriggers(func(a *trigger) bool {
if a.trigger.GetTargetConnections() && (setConnection.GetRef().GetGtype() == a.trigger.GetGtype() || a.trigger.GetGtype() == apipb.Any) {
data := setConnection.AsMap()
if data == nil {
data = map[string]interface{}{}
}
err := a.evalTrigger.Trigger(data)
if err == nil && len(data) > 0 {
for k, v := range data {
if err == nil && data["attributes"] != nil {
for k, v := range data["attributes"].(map[string]interface{}) {
val, _ := structpb.NewValue(v)
setConnection.GetAttributes().GetFields()[k] = val
}
Expand Down Expand Up @@ -1225,9 +1240,12 @@ func (n *Graph) EditConnections(ctx context.Context, patch *apipb.EditFilter) (*
}
n.rangeTriggers(func(a *trigger) bool {
if a.trigger.GetTargetConnections() && (connection.GetRef().GetGtype() == a.trigger.GetGtype() || a.trigger.GetGtype() == apipb.Any) {
data := connection.AsMap()
data := connection.GetAttributes().AsMap()
if data == nil {
data = map[string]interface{}{}
}
err := a.evalTrigger.Trigger(data)
if err == nil && len(data) > 0 {
if err == nil {
for k, v := range data {
val, _ := structpb.NewValue(v)
connection.GetAttributes().GetFields()[k] = val
Expand Down Expand Up @@ -1450,9 +1468,12 @@ func (g *Graph) SeedDocs(server apipb.DatabaseService_SeedDocsServer) error {
}
g.rangeTriggers(func(a *trigger) bool {
if a.trigger.GetTargetDocs() && (msg.GetRef().GetGtype() == a.trigger.GetGtype() || a.trigger.GetGtype() == apipb.Any) {
data := msg.AsMap()
data := msg.GetAttributes().AsMap()
if data == nil {
data = map[string]interface{}{}
}
err := a.evalTrigger.Trigger(data)
if err == nil && len(data) > 0 {
if err == nil {
for k, v := range data {
val, _ := structpb.NewValue(v)
msg.GetAttributes().GetFields()[k] = val
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ services:
- "7820:7820"
- "7821:7821"
volumes:
- default:/tmp/graphik
- default:/tmp/graphik2
networks:
default:
aliases:
Expand Down

0 comments on commit c1474ea

Please sign in to comment.