questions webfrontend icw BaseX-queryserver bachend
Wednesday, 23 November 2016 · 4 messages · last reply 25 Nov 2016
RS
Rob Stapper23 Nov 2016 17:35 #
Hi,
I'm working on this POC of a XML-database application with a web based frontend in Basex.
I have a few questions:
- what is the difference between the rest- and the restxq- prefix? Do they refer to different namespaces?
- should I call database-functions in the backend- server through a client-server connection? If I call the database functions directly from my restxq-functions it just works. But is this a correct configuration in a multi-user environment?
- in case of adding or changing data in the database, is it possible to retrieve the saved data form the dababase in the same session and return it to the webpage? The saved data is enriched with f.e. an unique identification that I want to return to the webpage.
TIA for yout reply.
Rob Stapper
CG
Christian Grün24 Nov 2016 09:46 #
Hi Rob,
Quoted text · Rob Stapper · 2 lines
> - what is the difference between the rest- and the restxq- prefix? Do they > refer to different namespaces?
Yes they do. 'rest' is the one we recommend.
Quoted text · Rob Stapper · 4 lines
> - should I call database-functions in the backend- server through a > client-server connection? If I call the database functions directly from my > restxq-functions it just works. But is this a correct configuration in a > multi-user environment?
That’s an interestig point. If you follow the traditional
3-tier-architecture, it makes sense to have two BaseX instances: One for the application logic (restxq), the other one for data. The decision mostly depends on your use case, its complexity, number of users, etc: It is always easier to only have one layer, but if it turns out that you need to maximize parallel access, updates in a single sayer may block other operations. If all data is stored in a data layer, all queries of your application layer will be non-updating and, thus, better parallelizable.
Quoted text · Rob Stapper · 4 lines
> - in case of adding or changing data in the database, is it possible to > retrieve the saved data form the dababase in the same session and return it > to the webpage? The saved data is enriched with f.e. an unique > identification that I want to return to the webpage.
Do you mean that you want to save data and return it to the client? Yes, that’s possible; db:output [1] should do the job.
Hope this helps,
Christian
[1] http://docs.basex.org/wiki/Database_Module#db:output
CG
Christian Grün24 Nov 2016 20:34 #
Hi Rob (cc to the list),
Quoted text · Christian Grün · 5 lines
> Thanx fort the quick reply. It sure helps but the db:output suggestion is not doing what I have in mind. > > - rest versus restxq. In that case I'll use the rest namespace. > - so it is a matter of choice. "Separation of concerns" would be my guideline in this. > - Yep, I want to return the save data to the client as a confirmation of the data that has been stored. This data is also enriched with f.e. an unique code. My problem with "db:output" is that the argument of the function is evaluated before the pending updates have been processed and I would like this evaluation take place on the updated data, like some kind of "reread"-functionality. I could not make this work with db:output. Am I overlooking something here?
You are completely right, it is not possible to access the result of an update within the same query (because the pending updates will always be executed after query evaluation). This is what can be done instead:
• You can use db:output(
web:redirect(...)) to call another RESTXQ function that will generate the user feedback. This pattern is a good one for web applications anyway, because the result URL will be different from the updating URL.
• An alternative is to create the result in main-memory and then a) write it to the database and b) pass it on to the client. Here is an arbitrary example: declare %updating
%rest:POST
%rest:path('/city/$city/$population')
function local:update($city, $population) {
let $city := db:open('factbook')//city[name = $city]
let $updated-city := $city update {
replace value of node population with $population
}
return (
replace node $city with $updated-city,
db:output($updated-city)
)
};Does this help?
Christian
RS
Rob Stapper25 Nov 2016 10:50 #
( cc to the list ;-))
Thanx Christian, this helps so much.
Rob
-----Oorspronkelijk bericht-----
Quoted text · Christian Grün · 29 lines
Van: Christian Grün [mailto:christian.gruen at gmail.com] Verzonden: donderdag 24 november 2016 20:34 Aan: Rob Stapper; BaseX Onderwerp: Re: [basex-talk] questions webfrontend icw BaseX-queryserver bachend Hi Rob (cc to the list), > Thanx fort the quick reply. It sure helps but the db:output suggestion is not doing what I have in mind. > > - rest versus restxq. In that case I'll use the rest namespace. > - so it is a matter of choice. "Separation of concerns" would be my guideline in this. > - Yep, I want to return the save data to the client as a confirmation of the data that has been stored. This data is also enriched with f.e. an unique code. My problem with "db:output" is that the argument of the function is evaluated before the pending updates have been processed and I would like this evaluation take place on the updated data, like some kind of "reread"-functionality. I could not make this work with db:output. Am I overlooking something here? You are completely right, it is not possible to access the result of an update within the same query (because the pending updates will always be executed after query evaluation). This is what can be done instead: • You can use db:output(web:redirect(...)) to call another RESTXQ function that will generate the user feedback. This pattern is a good one for web applications anyway, because the result URL will be different from the updating URL. • An alternative is to create the result in main-memory and then a) write it to the database and b) pass it on to the client. Here is an arbitrary example: declare %updating %rest:POST %rest:path('/city/$city/$population') function local:update($city, $population) { let $city := db:open('factbook')//city[name = $city] let $updated-city := $city update { replace value of node population with $population } return ( replace node $city with $updated-city, db:output($updated-city) ) }; Does this help? Christian
Written for BaseX 8.5. BaseX may have changed since: please check the current documentation.
⚡Generated with XQuery