{"action":"created","issue":{"url":"https://api.github.com/repos/BaseXdb/basex/issues/1082","labels_url":"https://api.github.com/repos/BaseXdb/basex/issues/1082/labels{/name}","comments_url":"https://api.github.com/repos/BaseXdb/basex/issues/1082/comments","events_url":"https://api.github.com/repos/BaseXdb/basex/issues/1082/events","html_url":"https://github.com/BaseXdb/basex/issues/1082","id":58254425,"number":1082,"title":"Database Triggers","user":{"login":"ChristianGruen","id":196589,"avatar_url":"https://avatars.githubusercontent.com/u/196589?v=3","gravatar_id":"","url":"https://api.github.com/users/ChristianGruen","html_url":"https://github.com/ChristianGruen","followers_url":"https://api.github.com/users/ChristianGruen/followers","following_url":"https://api.github.com/users/ChristianGruen/following{/other_user}","gists_url":"https://api.github.com/users/ChristianGruen/gists{/gist_id}","starred_url":"https://api.github.com/users/ChristianGruen/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ChristianGruen/subscriptions","organizations_url":"https://api.github.com/users/ChristianGruen/orgs","repos_url":"https://api.github.com/users/ChristianGruen/repos","events_url":"https://api.github.com/users/ChristianGruen/events{/privacy}","received_events_url":"https://api.github.com/users/ChristianGruen/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":4,"created_at":"2015-02-19T19:15:50Z","updated_at":"2015-02-20T13:45:27Z","closed_at":null,"body":"Allow definition of database triggers to react on changes in a database (creations, updates, deletions).\r\n\r\nOpen questions:\r\n\r\n1. What is the smallest observable unit? A Resource? Arbitrary XML nodes?\r\n2. Is it sufficient to work with affected resource paths, or do we also want to cache and return updated nodes?\r\n3. How are triggers defined? As independent XQuery expressions?\r\n4. How are errors treated that occur during trigger execution?\r\n  * Will the original transaction return an error?\r\n  * Will another transaction be triggered?\r\n  * Will it only be remembered in the database log files?\r\n5. If we remove the current database event architecture, do we still want to inform clients on changes, or do we use something else like web sockets?\r\n\r\n**Database Commands:**\r\n\r\n```\r\nOPEN db\r\nTRIGGER INSTALL trigger.xq delete\r\nTRIGGER DELETE trigger.xq\r\nTRIGGER LIST\r\n```\r\n\r\n**XQuery:**\r\n\r\n```xquery\r\nlet $db := 'db'\r\nlet $code := 'push-it.xq'\r\nlet $operation := 'delete'\r\nreturn trigger:install($db, $code, $operation)\r\n```\r\n\r\n**Trigger Query (trigger.xq):**\r\n\r\n```xquery\r\ndeclare variable $db as xs:string external;\r\ndeclare variable $resources as xs:string* external;\r\ndeclare variable $operation as xs:string external;\r\nadmin:write-log(\r\n  \"Operation: \" || $operation ||\r\n  \", Database: \" || $db\r\n  \", Resources: \" || count($resources)\r\n)\r\n```\r\n\r\nFeedback is welcome!"},"comment":{"url":"https://api.github.com/repos/BaseXdb/basex/issues/comments/75239689","html_url":"https://github.com/BaseXdb/basex/issues/1082#issuecomment-75239689","issue_url":"https://api.github.com/repos/BaseXdb/basex/issues/1082","id":75239689,"user":{"login":"dirkk","id":327759,"avatar_url":"https://avatars.githubusercontent.com/u/327759?v=3","gravatar_id":"","url":"https://api.github.com/users/dirkk","html_url":"https://github.com/dirkk","followers_url":"https://api.github.com/users/dirkk/followers","following_url":"https://api.github.com/users/dirkk/following{/other_user}","gists_url":"https://api.github.com/users/dirkk/gists{/gist_id}","starred_url":"https://api.github.com/users/dirkk/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dirkk/subscriptions","organizations_url":"https://api.github.com/users/dirkk/orgs","repos_url":"https://api.github.com/users/dirkk/repos","events_url":"https://api.github.com/users/dirkk/events{/privacy}","received_events_url":"https://api.github.com/users/dirkk/received_events","type":"User","site_admin":false},"created_at":"2015-02-20T13:45:27Z","updated_at":"2015-02-20T13:45:27Z","body":"I see the problem with using function items now, thanks for the explanation.\r\n\r\nI prefer the second approach using annotations, simply because I think it is more flexible and also a nicer syntax. If I understood you correctly, you want to move all functionality into the functions itself, so that they are self-contained. So wouldn't it make sense to move _all_ functionality there? So e.g. we could have our file `trigger.xq` within the `.trigger` directory:\r\n\r\n    declare \r\n      %trigger:delete\r\n      %trigger:on(\"resource.xml\")\r\n    function f($db, $res) { ... };\r\n\r\nThis way, we could have a simple `TRIGGER INSTALL` command, which parses these files when called. Of course, we still could have convenience operations for installing and updating triggers in the way already proposed here.\r\n\r\nRegarding the trigger execution I would definitely go for synchronous execution. Three reasons:\r\n\r\n - Asynchronous execution is very surprising and might be useless if your updated nodes have been changed by another query in the meantime. It is hard to figure this out from an application perspective.\r\n - Using synchronous execution, you can once again call some message broker or some other mechanism to have an asynchronous execution. But if we implement it in an asynchronous way, the user can't make it synchronous.\r\n - It's what all relational databases I am aware of are doing. It might be wise not to ignore 40+ years of research"}}