Hi all,
I'm trying to use the REST API to call server-side XQuery scripts. As I understand it, these scripts are not part of a database, but reside in the filesystem where BaseX is "installed" (or where RESTPATH points to).
However, when I execute an XQuery I would like it to run on a particular database. Is there anyway to achieve this without passing the database as an argument to the query?
For example, http://localhost:8984/rest?run=find.xq http://localhost:8984/rest/Test?run=find.xq ... both execute the same query, and that query needs to open a database to do its searching, so currently the database to open is hard-coded. It would be nice if the query could work out what database or "collection" was its context and automatically use that.
Additionally, if I do http://localhost:8984/rest?run=Test/find.xq ... then I run the query that is in the Test folder, but this still does not have any context and so still needs to know to open the Test database.
Is there a way to achieve this without having to pass the name of the database to every XQuery? Or am I not understanding things and is there a better way to do this sort of thing?
Thanks, Luke
Hi Luke,
However, when I execute an XQuery I would like it to run on a particular database. Is there anyway to achieve this without passing the database as an argument to the query?
If you address a database in your URL, it will be bound to the context of your query, which can be obtained via a simple dot (.). For example, run the following query to get the number of documents in your 'Test' database:
http://localhost:8984/rest/Test?query=count(.)
I have added another example in our documentation [1]. The given query can also be written to a file and evaluated with 'run'.
Hope this helps, Christian
Hi Christian,
Thanks for that. That's exactly what I was hoping for (and I thought I tried that, but must have done something wrong).
Now for the trickier part of the problem:
Let's say that I want to add a file to the database, but I want to do it through a server-side XQuery, so that it can validate, etc. Can I do this using the context of the call, or do I have to pass the name of the database as an argument?
e.g.
db:add("Test", <a/>, "doc.xml") (: Works, but has hardcoded the database :)
==8<=========================
let $db := request:parameter('param1') return db:add($db, <a/>, "doc.xml") (: Works, but had to pass the database name as a parameter :)
==8<=========================
db:add(., <a/>, "doc.xml") (: Doesn't work, and I didn't expect it to, but would be ideal :)
Is there any way to achieve this? There isn't anyway of converting the current context into an expression containing the database (and path) is there?
Thanks, Luke
-----Original Message----- From: Christian Grün christian.gruen@gmail.com Sent: Tuesday, 21 January 2020 6:38 PM To: ERRINGTON Luke Luke.Errington@sydac.com Cc: basex-talk@mailman.uni-konstanz.de Subject: Re: [basex-talk] Server-side XQuery scripts understanding their "context"
Hi Luke,
However, when I execute an XQuery I would like it to run on a particular database. Is there anyway to achieve this without passing the database as an argument to the query?
If you address a database in your URL, it will be bound to the context of your query, which can be obtained via a simple dot (.). For example, run the following query to get the number of documents in your 'Test' database:
http://localhost:8984/rest/Test?query=count(.)
I have added another example in our documentation [1]. The given query can also be written to a file and evaluated with 'run'.
Hope this helps, Christian
Hi Luke,
That’s a classical recommendation for RESTXQ, which gives you a more powerful and flexible framework for writing custom REST APIs. Did you already have a look at it?
Best, Christian
On Wed, Jan 22, 2020 at 2:33 AM ERRINGTON Luke Luke.Errington@sydac.com wrote:
Hi Christian,
Thanks for that. That's exactly what I was hoping for (and I thought I tried that, but must have done something wrong).
Now for the trickier part of the problem:
Let's say that I want to add a file to the database, but I want to do it through a server-side XQuery, so that it can validate, etc. Can I do this using the context of the call, or do I have to pass the name of the database as an argument?
e.g.
db:add("Test", <a/>, "doc.xml") (: Works, but has hardcoded the database :)
==8<=========================
let $db := request:parameter('param1') return db:add($db, <a/>, "doc.xml") (: Works, but had to pass the database name as a parameter :)
==8<=========================
db:add(., <a/>, "doc.xml") (: Doesn't work, and I didn't expect it to, but would be ideal :)
Is there any way to achieve this? There isn't anyway of converting the current context into an expression containing the database (and path) is there?
Thanks, Luke
-----Original Message----- From: Christian Grün christian.gruen@gmail.com Sent: Tuesday, 21 January 2020 6:38 PM To: ERRINGTON Luke Luke.Errington@sydac.com Cc: basex-talk@mailman.uni-konstanz.de Subject: Re: [basex-talk] Server-side XQuery scripts understanding their "context"
Hi Luke,
However, when I execute an XQuery I would like it to run on a particular database. Is there anyway to achieve this without passing the database as an argument to the query?
If you address a database in your URL, it will be bound to the context of your query, which can be obtained via a simple dot (.). For example, run the following query to get the number of documents in your 'Test' database:
http://localhost:8984/rest/Test?query=count(.)
I have added another example in our documentation [1]. The given query can also be written to a file and evaluated with 'run'.
Hope this helps, Christian
basex-talk@mailman.uni-konstanz.de