Replies: 2 comments
-
|
I'm fairly new to SurrealQL but resolved to create it like this... DEFINE EVENT relate_autor_collection ON collection WHEN $event = "CREATE" THEN {
LET $autor = $auth.id;
LET $collection = $value.id;
RELATE $autor->owns->$collection;
};Basically create symbols for the origin and target records because the There is another example here @ #1820. For more details view the full comment. DEFINE EVENT register_order ON TABLE user
WHEN ( $before.place_order != $after.place_order AND $after.place_order != NULL ) THEN {
LET $from = place_order.from;
LET $to = place_order.to;
// Enforce that the user only creates orders for they businesses
IF ($from.owner.id = $auth.id) THEN
RELATE $from->order->$to
END;
// Reset the endpoint to null again
UPDATE type::thing(id) SET place_order = NULL;
};Edit: If anyone has a better way to do it I'll be glad to hear from it. |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Here's a generic event that I've been using for this, but I do it in Golang when I create a table. It works by passing a sort of schema into the template placeholders tmpl := template.Must(template.New("event").Parse(`
DEFINE EVENT IF NOT EXISTS {{.Name}}_relation ON TABLE {{.Trigger.Table}}
WHEN $event = 'CREATE'
AND $after.{{.Trigger.InField}} != NONE
THEN {
LET $in = IF type::is::record($after.{{.Trigger.InField}}) {
$after.{{.Trigger.InField}}
} ELSE {
type::thing('{{.InTable}}', $after.{{.Trigger.InField}})
};
LET $out = IF type::is::record($after.{{.Trigger.OutField}}) {
$after.{{.Trigger.OutField}}
} ELSE {
type::thing('{{.OutTable}}', $after.{{.Trigger.OutField}})
};
RELATE $in->{{.Name}}->$out;
};`)) |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hey, I'm pretty new to SurrealDB and was playing with it. Is it possible to create an event that triggers a
RELATEstatement?I'm struggling with it and the error messages are not very helpful.
This yields
Adding an event to trigger a
CREATEevent works fine, but if I try to use aRELATEevent it doesn't.Beta Was this translation helpful? Give feedback.
All reactions