Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: allow to pass parameters when executing query #170

Merged
merged 5 commits into from Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
53 changes: 52 additions & 1 deletion README.md
Expand Up @@ -363,7 +363,16 @@ The filename extension that will be used for XQuery result files emitted by
Type: `string`
Default: `'xml'`

#### Example
##### queryParams

Query params passed to the eXist-db XMLRPC API
(https://exist-db.org/exist/apps/doc/devguide_xmlrpc). Can be used to pass
query variables (see Example 2).

Type: `Object`
Default: `{}`

#### Example 1

Upload a collection index configuration file and re-index the collection

Expand Down Expand Up @@ -411,6 +420,48 @@ function reindex () {
exports.default = series(deployCollectionXConf, reindex)
```

#### Example 2

Pass a variable to the XQuery script.

*scripts/var.xq*

```xquery
<result>{ $someVariable }</result>
```

*gulpfile.js*

```js
const { src, dest } = require('gulp')
const { createClient } = require('@existdb/gulp-exist')

// override some default connection options
const exist = createClient({
basic_auth: {
user: "admin",
pass: ""
}
})

// set `variables` query parameter
const queryConfig = {
queryParams: {
variables: {
someVariable: "some value"
}
}
}

function runQueryWithVarible () {
olvidalo marked this conversation as resolved.
Show resolved Hide resolved
return src('scripts/var.xq', {cwd: '.'})
.pipe(exist.query(queryConfig))
.pipe(dest('logs'))
}

exports.default = runQueryWithVarible
```

## Define Custom Mime Types

Override the mime type used to store files in exist based on their extension.
Expand Down
6 changes: 4 additions & 2 deletions index.js
Expand Up @@ -70,14 +70,16 @@ const defaultUploadOptions = {
* @typedef {Object} GulpExistQueryOptions
* @prop {boolean} [printXqlResults] default: true
* @prop {"xml"|"json"|string} xqlOutputExt the file extension the results are written to
* @prop {Object} queryParams query parameters passed to eXist-db
*/

/**
* @type {GulpExistQueryOptions}
*/
const defaultQueryOptions = {
printXqlResults: true,
xqlOutputExt: 'xml'
xqlOutputExt: 'xml',
queryParams: {}
}

const isWin = os.platform() === 'win32'
Expand Down Expand Up @@ -223,7 +225,7 @@ function query (client, options) {

log('Running XQuery on server: ' + file.relative)

client.queries.readAll(file.contents, {})
client.queries.readAll(file.contents, conf.queryParams)
.then(function (result) {
const resultBuffer = Buffer.concat(result.pages)
if (conf.printXqlResults) {
Expand Down
6 changes: 6 additions & 0 deletions spec/files/test-variables.xql
@@ -0,0 +1,6 @@
xquery version "3.0";

declare option exist:serialize "method=json media-type=text/javascript";

(: return value of $variable that should have been set in the query parameters:)
olvidalo marked this conversation as resolved.
Show resolved Hide resolved
<result>{$variable}</result>
olvidalo marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 22 additions & 0 deletions spec/query.js
Expand Up @@ -47,3 +47,25 @@ test('run query, expect json', function (t) {
.on('error', e => t.fail(e))
.on('finish', _ => t.end())
})

test('run query with variables', function (t) {
const testClient = createClient(connectionOptions)
return src('test-variables.xql', srcOptions)
.pipe(testClient.query({
target: targetCollection,
xqlOutputExt: 'json',
queryParams: {
variables: {
variable: 'test'
}
}
}))
.on('data', function (d) {
const parsedContents = JSON.parse(d.contents)
// inspect the results
// result should be the string set by the variables object in the query params
t.equal(parsedContents, 'test', 'variable has been set')
})
.on('error', e => t.fail(e))
.on('finish', _ => t.end())
})